{"record":{"id":"e5beacfaf740654c","repo":"lionsoul2014/ip2region","slug":"invalid-ip-address","errorCode":null,"errorMessage":"invalid ip address `{}`","messagePattern":"invalid ip address `(.+?)`","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"binding/python/ip2region/searcher.py","lineNumber":45,"sourceCode":"            self.__handle = io.open(db_path, \"rb\")\n            self.vector_index = vector_index\n            self.c_buffer = None\n\n    def get_ip_version(self):\n        return self.version\n\n    def get_io_count(self):\n        return self.__io_count\n\n    def search(self, ip: Union[bytes, str]):\n        # check and parse the string ip\n        ip_bytes = None\n        if isinstance(ip, str):\n            ip_bytes = util.parse_ip(ip)\n        elif isinstance(ip, bytes):\n            ip_bytes = ip\n        else:\n            raise ValueError(\"invalid ip address `{}`\".format(ip))\n\n        # ip version check\n        if len(ip_bytes) != self.version.byte_num:\n            raise ValueError(\"invalid ip address `{}` ({} expected)\".format(\n                util.ip_to_string(ip_bytes), self.version.name))\n\n        # reset the global io_count\n        self.__io_count = 0\n\n        # located the segment index block based on the vector index\n        s_ptr, e_ptr, i0, i1 = 0, 0, ip_bytes[0], ip_bytes[1]\n        idx = i0 * util.VectorIndexCols * util.VectorIndexSize + i1 * util.VectorIndexSize\n        if self.vector_index != None:\n            s_ptr = util.le_get_uint32(self.vector_index, idx)\n            e_ptr = util.le_get_uint32(self.vector_index, idx + 4)\n        elif self.c_buffer != None:\n            offset = util.HeaderInfoLength + idx\n            s_ptr = util.le_get_uint32(self.c_buffer, offset)","sourceCodeStart":27,"sourceCodeEnd":63,"githubUrl":"https://github.com/lionsoul2014/ip2region/blob/c1a1fc7d5941760db3f8431dc05c48cf7f0e30a1/binding/python/ip2region/searcher.py#L27-L63","documentation":"Python searcher type guard: the ip argument is neither str nor bytes (e.g. an int or None), so it cannot be parsed; the ValueError echoes the unusable value via {} formatting.","triggerScenarios":"Calling search() with an int, None, list, or other non-str/non-bytes object — e.g. passing the integer form of an IP or a boto of None from a failed lookup.","commonSituations":"Passing an int from a database/log field; passing a list of octets; forgetting that IPv4-mapped strings must be strings like '1.2.3.4'.","solutions":["Pass the IP as a str ('1.2.3.4' / '2001:db8::1') or as 4/16 raw bytes","Coerce or validate the input type (isinstance check) before calling search","Reject non-string inputs at your API boundary"],"exampleFix":"# before\nsearcher.search(16909060)\n# after\nimport ipaddress\nsearcher.search(str(ipaddress.ip_address(16909060)))","handlingStrategy":"type-guard","validationCode":"if not isinstance(ip, (str, bytes)):\n    raise ValueError(f\"ip must be str or bytes, got {type(ip).__name__}\")","typeGuard":"def is_ip_input(ip) -> bool:\n    return isinstance(ip, (str, bytes))","tryCatchPattern":"try:\n    region = searcher.search(ip)\nexcept ValueError as e:\n    if 'invalid ip address' in str(e):\n        region = None  # unparseable input, skip/log\n    else:\n        raise","preventionTips":["Cast int IPs to string form before searching","Validate user-supplied IPs at the edge of your system","Keep one canonical type (str) for IPs in your codebase"],"tags":["python","ip-address","type-error","input-validation"],"backgroundTag":"invalid-ip-address","analyzedSha":"c1a1fc7d5941760db3f8431dc05c48cf7f0e30a1","analyzedAt":"2026-09-02T16:58:39.988Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T21:17:11.164Z"}