{"record":{"id":"d5867e1dfd6c6469","repo":"lionsoul2014/ip2region","slug":"invalid-bytes-ip","errorCode":null,"errorMessage":"invalid bytes ip `{}`","messagePattern":"invalid bytes ip `(.+?)`","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"binding/python/ip2region/util.py","lineNumber":75,"sourceCode":"        self.ipVersion, \n        self.runtimePtrBytes\n    )\n\n\n# ---\n# ip parse and convert functions\n\ndef parse_ip(ip_string: str):\n    try:\n        return ipaddress.ip_address(ip_string).packed\n    except:\n        raise ValueError(\"invalid ip address `{}`\".format(ip_string))\n\ndef ip_to_string(ip_bytes: bytes):\n    if isinstance(ip_bytes, bytes):\n        return str(ipaddress.ip_address(ip_bytes))\n    else:\n        raise ValueError(\"invalid bytes ip `{}`\".format(ip_bytes))\n\ndef ip_compare(ip1: bytes, ip2: bytes):\n    if ip1 > ip2:\n        return 1\n    elif ip1 < ip2:\n        return -1\n    else:\n        return 0\n\ndef ip_sub_compare(ip1: bytes, buff: bytes, offset: int):\n    ip2 = buff[offset:offset+len(ip1)]\n    if ip1 > ip2:\n        return 1\n    elif ip1 < ip2:\n        return -1\n    else:\n        return 0\n","sourceCodeStart":57,"sourceCodeEnd":93,"githubUrl":"https://github.com/lionsoul2014/ip2region/blob/c1a1fc7d5941760db3f8431dc05c48cf7f0e30a1/binding/python/ip2region/util.py#L57-L93","documentation":"util.ip_to_string() formats packed IP bytes as a human-readable string, but only if the input is actually a bytes object; anything else raises ValueError('invalid bytes ip ...'). It is a strict input-type guard used in error messages and utilities.","triggerScenarios":"Calling ip_to_string with a str, bytearray, memoryview, int, or None instead of bytes/byte-like input.","commonSituations":"Passing the original string IP back into ip_to_string by mistake; passing a bytearray from decompression; logic that mixes str and bytes forms of the address.","solutions":["Convert to bytes first: bytes(value) if it's bytearray/memoryview, or parse strings with util.parse_ip instead","Check isinstance(ip_bytes, bytes) before calling","For string IPs just print them directly instead of round-tripping through ip_to_string"],"exampleFix":"# before\nutil.ip_to_string('1.2.3.4')  # str, not bytes\n# after\nutil.ip_to_string(util.parse_ip('1.2.3.4'))  # -> '1.2.3.4'","handlingStrategy":"type-guard","validationCode":"if not isinstance(ip, bytes):\n    ip = util.parse_ip(ip) if isinstance(ip, str) else bytes(ip)","typeGuard":"def is_packed_ip(v) -> bool:\n    return isinstance(v, bytes) and len(v) in (4, 16)","tryCatchPattern":"try:\n    text = util.ip_to_string(b)\nexcept ValueError:\n    text = str(b)  # fallback representation","preventionTips":["Keep a single internal representation (bytes) for IPs","Convert bytearray/memoryview with bytes() before calling","Avoid round-tripping: print the original string if you already have it"],"tags":["python","ip-address","type-error","bytes"],"backgroundTag":"invalid-ip-address","analyzedSha":"c1a1fc7d5941760db3f8431dc05c48cf7f0e30a1","analyzedAt":"2026-09-02T16:58:39.988Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T21:17:11.164Z"}