Repository navigation
Python 2.x 3.x compatibility notes
Alex Willmer edited this page Apr 20, 2026
·
12 revisions
Python <= 3.2
- BaseException
- Exception
- StandardError
- EnvironmentError
- IOError
- socket.error
- OSError
- IOError
- EnvironmentError
- select.error
- StandardError
- Exception
Python >= 3.3
- BaseException
- Exception
- OSError
- BlockingIOError
- EnvironmentError
- IOError
- select.error
- socket.error
- OSError
- Exception
Refs
- https://docs.python.org/2/library/exceptions.html#exception-hierarchy
- https://docs.python.org/3/library/exceptions.html#exception-hierarchy
- https://docs.python.org/3/whatsnew/3.3.html#pep-3151
Python 3.5+ automatically retries system calls that return EINTR.
- https://docs.python.org/3/library/exceptions.html#InterruptedError
- https://docs.python.org/3/whatsnew/3.5.html#pep-475-retry-system-calls-failing-with-eintr
-
open(),os.fdopen(), etc return-
fileinstances in Python 2.x -
io.*instances in Python 3.x
-
-
iomodule requires Python >= 2.6 -
BlockingIOErrorrequires Python >= 3.3 (first doc mention Python 3.4)
In non-blocking mode, if n > 0 is specified, and no bytes are available
-
io.RawIOBase.read(n)returnsNone -
io.BufferedIOBase.read(n)is allowed to returnNoneor raiseBlockingIOError.iocode returnsNone -
io.BufferedReader.read(n)is the same asio.BufferedIOBase.read(n)
-
cStringIO.StringIO(Python >= 1.2)- accepts mix of byte-strings and ascii encodable unicode.
- Always outputs a byte-string.
-
cStringIO.StringIO(some_str)returns a read-only type, without awrite()method. -
cStringIO.StringIO()returns a read-write type.
-
StringIO.StringIO(Python >= 1.5)- accepts mix of byte-strings and unicode.
- If any input is uncode, then output is unicode, otherwise output is byte-string.
- If any input byte-string is not ascii encodable, then
getvalue()raisesUnicodeError.
-
io.BytesIO(Python >= 2.6)- accepts and returns byte-strings
- Always same type, always readable and writable
-
io.StringIO(Python >= 2.6; native implementation Python >= 2.7)- accepts and returns unicode strings
- Always same type, always readable and writable
See also https://github.com/mitogen-hq/mitogen/issues/1391, https://gist.github.com/moreati/6e0239e9057e8fda147ce12321011e5d
| 2.x returns | 3.x returns | |
|---|---|---|
__bytes__ |
-- | bytes |
__repr__ |
bytes | unicode string |
__str__ |
bytes | unicode string |
__unicode__ |
unicode string | -- |
- Python >= 2.4
- Python >= 2.6: Popen.send_signal(s), Popen.terminate(), Popen.kill()
- Python >= 3.3: Popen.communicate(..., timeout)