{"record":{"id":"e777b8b73c2aab64","repo":"python/cpython","slug":"pos-r-is-not-an-integer","errorCode":null,"errorMessage":"{pos!r} is not an integer","messagePattern":"(.+?) is not an integer","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"Lib/_pyio.py","lineNumber":981,"sourceCode":"            if n == 0:\n                return 0\n\n            with self._lock:\n                pos = self._pos\n                if pos > len(self._buffer):\n                    # Pad buffer to pos with null bytes.\n                    self._buffer.resize(pos)\n                self._buffer[pos:pos + n] = view\n                self._pos += n\n            return n\n\n    def seek(self, pos, whence=0):\n        if self.closed:\n            raise ValueError(\"seek on closed file\")\n        try:\n            pos_index = pos.__index__\n        except AttributeError:\n            raise TypeError(f\"{pos!r} is not an integer\")\n        else:\n            pos = pos_index()\n        if whence == 0:\n            if pos < 0:\n                raise ValueError(\"negative seek position %r\" % (pos,))\n            self._pos = pos\n        elif whence == 1:\n            with self._lock:\n                self._pos = max(0, self._pos + pos)\n        elif whence == 2:\n            with self._lock:\n                self._pos = max(0, len(self._buffer) + pos)\n        else:\n            raise ValueError(\"unsupported whence value\")\n        return self._pos\n\n    def tell(self):\n        if self.closed:","sourceCodeStart":963,"sourceCodeEnd":999,"githubUrl":"https://github.com/python/cpython/blob/bc6749cc3b5ae4a5e88a6cc2d5b3bebbe354eae6/Lib/_pyio.py#L963-L999","documentation":"BytesIO.seek (Lib/_pyio.py:981) converts its pos argument via pos.__index__ and raises TypeError('{pos!r} is not an integer') when the attribute is absent. Only true integers or index-implementing types (numpy ints, IntEnum) are accepted; floats and strings are not.","triggerScenarios":"buf.seek(offset / 2) with true division; buf.seek('0') from a parsed string header; passing a float default (0.0) or Decimal position.","commonSituations":"Offset arithmetic using '/'; offsets decoded from text protocols; config values loaded from YAML/JSON as floats (e.g. 0.0) and passed to seek.","solutions":["Coerce to int: `buf.seek(int(pos))`.","Use `//` for offset arithmetic.","Normalize numeric config/protocol fields to int at parse time."],"exampleFix":"# before\noffset = len(prefix) / 2\nbuf.seek(offset)  # TypeError: 5.0 is not an integer\n\n# after\noffset = len(prefix) // 2\nbuf.seek(offset)","handlingStrategy":"validation","validationCode":"from operator import index\npos = index(pos)  # or int(pos)\nbuf.seek(pos)","typeGuard":"def is_indexable(v) -> bool:\n    return hasattr(v, '__index__')","tryCatchPattern":"try:\n    buf.seek(pos)\nexcept TypeError:\n    buf.seek(int(pos))  # coerce and retry","preventionTips":["Use `//` for offset arithmetic.","Normalize YAML/JSON numeric fields to int before using them as offsets.","Prefer operator.index() when accepting numpy ints or IntEnum transparently."],"tags":["io","bytesio","seek","typeerror","argument-validation"],"backgroundTag":null,"analyzedSha":"bc6749cc3b5ae4a5e88a6cc2d5b3bebbe354eae6","analyzedAt":"2026-08-14T22:01:13.976Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}