2010-07-21 13:18:39 -04:00
|
|
|
PEP: 3151
|
|
|
|
Title: Reworking the OS and IO exception hierarchy
|
|
|
|
Version: $Revision$
|
|
|
|
Last-Modified: $Date$
|
|
|
|
Author: Antoine Pitrou <solipsis@pitrou.net>
|
2012-05-06 02:35:13 -04:00
|
|
|
BDFL-Delegate: Barry Warsaw
|
2011-10-12 20:13:24 -04:00
|
|
|
Status: Final
|
2010-07-21 13:18:39 -04:00
|
|
|
Type: Standards Track
|
|
|
|
Content-Type: text/x-rst
|
2021-02-09 11:54:26 -05:00
|
|
|
Created: 21-Jul-2010
|
2011-07-13 18:27:22 -04:00
|
|
|
Python-Version: 3.3
|
2010-07-21 13:18:39 -04:00
|
|
|
Post-History:
|
2017-06-11 15:02:39 -04:00
|
|
|
Resolution: https://mail.python.org/pipermail/python-dev/2011-October/114033.html
|
2010-07-21 13:18:39 -04:00
|
|
|
|
|
|
|
Abstract
|
|
|
|
========
|
|
|
|
|
|
|
|
The standard exception hierarchy is an important part of the Python
|
|
|
|
language. It has two defining qualities: it is both generic and
|
|
|
|
selective. Generic in that the same exception type can be raised
|
|
|
|
- and handled - regardless of the context (for example, whether you are
|
|
|
|
trying to add something to an integer, to call a string method, or to write
|
|
|
|
an object on a socket, a TypeError will be raised for bad argument types).
|
|
|
|
Selective in that it allows the user to easily handle (silence, examine,
|
|
|
|
process, store or encapsulate...) specific kinds of error conditions
|
|
|
|
while letting other errors bubble up to higher calling contexts. For
|
|
|
|
example, you can choose to catch ZeroDivisionErrors without affecting
|
|
|
|
the default handling of other ArithmeticErrors (such as OverflowErrors).
|
|
|
|
|
|
|
|
This PEP proposes changes to a part of the exception hierarchy in
|
|
|
|
order to better embody the qualities mentioned above: the errors
|
2010-07-22 09:13:03 -04:00
|
|
|
related to operating system calls (OSError, IOError, mmap.error,
|
|
|
|
select.error, and all their subclasses).
|
2010-07-21 13:18:39 -04:00
|
|
|
|
|
|
|
|
|
|
|
Rationale
|
|
|
|
=========
|
|
|
|
|
|
|
|
Confusing set of OS-related exceptions
|
|
|
|
--------------------------------------
|
|
|
|
|
|
|
|
OS-related (or system call-related) exceptions are currently a diversity
|
2010-07-26 18:20:52 -04:00
|
|
|
of classes, arranged in the following sub-hierarchies::
|
2010-07-21 13:18:39 -04:00
|
|
|
|
|
|
|
+-- EnvironmentError
|
|
|
|
+-- IOError
|
|
|
|
+-- io.BlockingIOError
|
|
|
|
+-- io.UnsupportedOperation (also inherits from ValueError)
|
|
|
|
+-- socket.error
|
2010-11-10 05:01:57 -05:00
|
|
|
+-- socket.gaierror
|
2010-11-10 12:38:46 -05:00
|
|
|
+-- socket.herror
|
2010-11-10 05:01:57 -05:00
|
|
|
+-- socket.timeout
|
2010-07-21 13:18:39 -04:00
|
|
|
+-- OSError
|
2010-07-30 19:43:45 -04:00
|
|
|
+-- VMSError
|
2010-07-21 13:18:39 -04:00
|
|
|
+-- WindowsError
|
2010-07-21 14:49:25 -04:00
|
|
|
+-- mmap.error
|
2010-07-21 13:18:39 -04:00
|
|
|
+-- select.error
|
|
|
|
|
|
|
|
While some of these distinctions can be explained by implementation
|
|
|
|
considerations, they are often not very logical at a higher level. The
|
|
|
|
line separating OSError and IOError, for example, is often blurry. Consider
|
|
|
|
the following::
|
|
|
|
|
|
|
|
>>> os.remove("fff")
|
|
|
|
Traceback (most recent call last):
|
|
|
|
File "<stdin>", line 1, in <module>
|
|
|
|
OSError: [Errno 2] No such file or directory: 'fff'
|
|
|
|
>>> open("fff")
|
|
|
|
Traceback (most recent call last):
|
|
|
|
File "<stdin>", line 1, in <module>
|
|
|
|
IOError: [Errno 2] No such file or directory: 'fff'
|
|
|
|
|
|
|
|
The same error condition (a non-existing file) gets cast as two different
|
|
|
|
exceptions depending on which library function was called. The reason
|
2010-07-21 15:16:49 -04:00
|
|
|
for this is that the ``os`` module exclusively raises OSError (or its
|
|
|
|
subclass WindowsError) while the ``io`` module mostly raises IOError.
|
2010-07-21 13:18:39 -04:00
|
|
|
However, the user is interested in the nature of the error, not in which
|
|
|
|
part of the interpreter it comes from (since the latter is obvious from
|
|
|
|
reading the traceback message or application source code).
|
|
|
|
|
|
|
|
In fact, it is hard to think of any situation where OSError should be
|
|
|
|
caught but not IOError, or the reverse.
|
|
|
|
|
|
|
|
A further proof of the ambiguity of this segmentation is that the standard
|
|
|
|
library itself sometimes has problems deciding. For example, in the
|
2011-04-04 09:33:34 -04:00
|
|
|
``select`` module, similar failures will raise ``select.error``, ``OSError``
|
|
|
|
or ``IOError`` depending on whether you are using select(), a poll object,
|
|
|
|
a kqueue object, or an epoll object. This makes user code uselessly
|
|
|
|
complicated since it has to be prepared to catch various exception types,
|
|
|
|
depending on which exact implementation of a single primitive it chooses
|
|
|
|
to use at runtime.
|
2010-07-21 13:18:39 -04:00
|
|
|
|
|
|
|
As for WindowsError, it seems to be a pointless distinction. First, it
|
|
|
|
only exists on Windows systems, which requires tedious compatibility code
|
2010-07-21 13:44:30 -04:00
|
|
|
in cross-platform applications (such code can be found in ``Lib/shutil.py``).
|
|
|
|
Second, it inherits from OSError and is raised for similar errors as OSError
|
|
|
|
is raised for on other systems. Third, the user wanting access to low-level
|
|
|
|
exception specifics has to examine the ``errno`` or ``winerror`` attribute
|
|
|
|
anyway.
|
2010-07-21 13:18:39 -04:00
|
|
|
|
2010-07-21 15:41:06 -04:00
|
|
|
.. note::
|
2023-07-31 12:50:14 -04:00
|
|
|
`Appendix B <PEP 3151 Appendix B_>`_ surveys the use of the
|
|
|
|
various exception types across the interpreter and the standard library.
|
2010-07-21 15:41:06 -04:00
|
|
|
|
2010-07-21 13:18:39 -04:00
|
|
|
|
|
|
|
Lack of fine-grained exceptions
|
|
|
|
-------------------------------
|
|
|
|
|
|
|
|
The current variety of OS-related exceptions doesn't allow the user to filter
|
|
|
|
easily for the desired kinds of failures. As an example, consider the task
|
|
|
|
of deleting a file if it exists. The Look Before You Leap (LBYL) idiom
|
|
|
|
suffers from an obvious race condition::
|
|
|
|
|
|
|
|
if os.path.exists(filename):
|
|
|
|
os.remove(filename)
|
|
|
|
|
2010-07-21 13:44:30 -04:00
|
|
|
If a file named as ``filename`` is created by another thread or process
|
|
|
|
between the calls to ``os.path.exists`` and ``os.remove``, it won't be
|
|
|
|
deleted. This can produce bugs in the application, or even security issues.
|
2010-07-21 13:18:39 -04:00
|
|
|
|
|
|
|
Therefore, the solution is to try to remove the file, and ignore the error
|
|
|
|
if the file doesn't exist (an idiom known as Easier to Ask Forgiveness
|
|
|
|
than to get Permission, or EAFP). Careful code will read like the following
|
|
|
|
(which works under both POSIX and Windows systems)::
|
|
|
|
|
|
|
|
try:
|
|
|
|
os.remove(filename)
|
|
|
|
except OSError as e:
|
|
|
|
if e.errno != errno.ENOENT:
|
|
|
|
raise
|
|
|
|
|
|
|
|
or even::
|
|
|
|
|
|
|
|
try:
|
|
|
|
os.remove(filename)
|
|
|
|
except EnvironmentError as e:
|
|
|
|
if e.errno != errno.ENOENT:
|
|
|
|
raise
|
|
|
|
|
|
|
|
This is a lot more to type, and also forces the user to remember the various
|
2010-07-21 13:44:30 -04:00
|
|
|
cryptic mnemonics from the ``errno`` module. It imposes an additional
|
|
|
|
cognitive burden and gets tiresome rather quickly. Consequently, many
|
|
|
|
programmers will instead write the following code, which silences exceptions
|
|
|
|
too broadly::
|
2010-07-21 13:18:39 -04:00
|
|
|
|
|
|
|
try:
|
|
|
|
os.remove(filename)
|
|
|
|
except OSError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
``os.remove`` can raise an OSError not only when the file doesn't exist,
|
|
|
|
but in other possible situations (for example, the filename points to a
|
|
|
|
directory, or the current process doesn't have permission to remove
|
|
|
|
the file), which all indicate bugs in the application logic and therefore
|
|
|
|
shouldn't be silenced. What the programmer would like to write instead is
|
|
|
|
something such as::
|
|
|
|
|
|
|
|
try:
|
|
|
|
os.remove(filename)
|
2010-09-03 13:07:56 -04:00
|
|
|
except FileNotFoundError:
|
2010-07-21 13:18:39 -04:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
2010-07-21 13:44:30 -04:00
|
|
|
Compatibility strategy
|
2010-07-21 13:18:39 -04:00
|
|
|
======================
|
|
|
|
|
|
|
|
Reworking the exception hierarchy will obviously change the exact semantics
|
|
|
|
of at least some existing code. While it is not possible to improve on the
|
|
|
|
current situation without changing exact semantics, it is possible to define
|
2010-11-16 12:33:07 -05:00
|
|
|
a narrower type of compatibility, which we will call *useful compatibility*.
|
|
|
|
|
|
|
|
For this we first must explain what we will call *careful* and *careless*
|
|
|
|
exception handling. *Careless* (or "naïve") code is defined as code which
|
2011-04-04 09:33:34 -04:00
|
|
|
blindly catches any of ``OSError``, ``IOError``, ``socket.error``,
|
|
|
|
``mmap.error``, ``WindowsError``, ``select.error`` without checking the ``errno``
|
2010-11-16 12:33:07 -05:00
|
|
|
attribute. This is because such exception types are much too broad to signify
|
2011-04-04 09:33:34 -04:00
|
|
|
anything. Any of them can be raised for error conditions as diverse as: a
|
2010-11-16 12:33:07 -05:00
|
|
|
bad file descriptor (which will usually indicate a programming error), an
|
|
|
|
unconnected socket (ditto), a socket timeout, a file type mismatch, an invalid
|
|
|
|
argument, a transmission failure, insufficient permissions, a non-existent
|
|
|
|
directory, a full filesystem, etc.
|
|
|
|
|
2023-07-31 12:50:14 -04:00
|
|
|
(moreover, the use of certain of these exceptions is irregular; `Appendix B
|
|
|
|
<PEP 3151 Appendix B_>`_ exposes the case of the `select`_ module,
|
|
|
|
which raises different exceptions depending on the implementation)
|
2010-11-16 12:33:07 -05:00
|
|
|
|
|
|
|
*Careful* code is defined as code which, when catching any of the above
|
|
|
|
exceptions, examines the ``errno`` attribute to determine the actual error
|
|
|
|
condition and takes action depending on it.
|
|
|
|
|
2010-11-16 12:45:20 -05:00
|
|
|
Then we can define *useful compatibility* as follows:
|
2010-11-16 12:33:07 -05:00
|
|
|
|
|
|
|
* useful compatibility doesn't make exception catching any narrower, but
|
|
|
|
it can be broader for *careless* exception-catching code. Given the following
|
2010-07-21 13:18:39 -04:00
|
|
|
kind of snippet, all exceptions caught before this PEP will also be
|
2011-10-11 17:50:49 -04:00
|
|
|
caught after this PEP, but the reverse may be false (because the coalescing
|
|
|
|
of ``OSError``, ``IOError`` and others means the ``except`` clause throws
|
|
|
|
a slightly broader net)::
|
2017-03-24 17:11:33 -04:00
|
|
|
|
2010-07-21 13:18:39 -04:00
|
|
|
try:
|
2011-10-11 17:50:49 -04:00
|
|
|
...
|
2010-07-21 13:18:39 -04:00
|
|
|
os.remove(filename)
|
2011-10-11 17:50:49 -04:00
|
|
|
...
|
2010-07-21 13:18:39 -04:00
|
|
|
except OSError:
|
|
|
|
pass
|
|
|
|
|
2010-11-16 12:33:07 -05:00
|
|
|
* useful compatibility doesn't alter the behaviour of *careful*
|
2010-07-21 13:18:39 -04:00
|
|
|
exception-catching code. Given the following kind of snippet, the same
|
2010-07-26 18:20:52 -04:00
|
|
|
errors should be silenced or re-raised, regardless of whether this PEP
|
2010-07-21 13:18:39 -04:00
|
|
|
has been implemented or not::
|
|
|
|
|
|
|
|
try:
|
|
|
|
os.remove(filename)
|
|
|
|
except OSError as e:
|
|
|
|
if e.errno != errno.ENOENT:
|
|
|
|
raise
|
|
|
|
|
2010-11-16 12:33:07 -05:00
|
|
|
The rationale for this compromise is that careless code can't really be
|
|
|
|
helped, but at least code which "works" won't suddenly raise errors and
|
|
|
|
crash. This is important since such code is likely to be present in
|
|
|
|
scripts used as cron tasks or automated system administration programs.
|
2010-07-21 13:18:39 -04:00
|
|
|
|
2010-11-16 12:33:07 -05:00
|
|
|
Careful code, on the other hand, should not be penalized. Actually, one
|
|
|
|
purpose of this PEP is to ease writing careful code.
|
2010-07-21 13:18:39 -04:00
|
|
|
|
|
|
|
|
2010-07-22 07:41:49 -04:00
|
|
|
.. _Step 1:
|
|
|
|
|
2010-07-21 13:18:39 -04:00
|
|
|
Step 1: coalesce exception types
|
|
|
|
================================
|
|
|
|
|
|
|
|
The first step of the resolution is to coalesce existing exception types.
|
2010-07-22 07:41:49 -04:00
|
|
|
The following changes are proposed:
|
2010-07-21 13:18:39 -04:00
|
|
|
|
2011-08-29 12:36:36 -04:00
|
|
|
* alias both socket.error and select.error to OSError
|
2010-07-21 14:49:25 -04:00
|
|
|
* alias mmap.error to OSError
|
2010-07-30 19:43:45 -04:00
|
|
|
* alias both WindowsError and VMSError to OSError
|
2011-08-29 12:36:36 -04:00
|
|
|
* alias IOError to OSError
|
|
|
|
* coalesce EnvironmentError into OSError
|
2010-07-21 13:18:39 -04:00
|
|
|
|
|
|
|
Each of these changes doesn't preserve exact compatibility, but it does
|
|
|
|
preserve *useful compatibility* (see "compatibility" section above).
|
|
|
|
|
2010-07-26 18:20:52 -04:00
|
|
|
Each of these changes can be accepted or refused individually, but of course
|
2010-07-22 07:41:49 -04:00
|
|
|
it is considered that the greatest impact can be achieved if this first step
|
2010-07-26 18:20:52 -04:00
|
|
|
is accepted in full. In this case, the IO exception sub-hierarchy would
|
2010-07-22 07:41:49 -04:00
|
|
|
become::
|
|
|
|
|
2011-08-29 12:36:36 -04:00
|
|
|
+-- OSError (replacing IOError, WindowsError, EnvironmentError, etc.)
|
2010-07-22 07:41:49 -04:00
|
|
|
+-- io.BlockingIOError
|
|
|
|
+-- io.UnsupportedOperation (also inherits from ValueError)
|
2010-11-10 05:01:57 -05:00
|
|
|
+-- socket.gaierror
|
2010-11-10 12:38:46 -05:00
|
|
|
+-- socket.herror
|
2010-11-10 05:01:57 -05:00
|
|
|
+-- socket.timeout
|
2010-07-22 07:41:49 -04:00
|
|
|
|
|
|
|
Justification
|
|
|
|
-------------
|
|
|
|
|
|
|
|
Not only does this first step present the user a simpler landscape as
|
|
|
|
explained in the rationale_ section, but it also allows for a better
|
|
|
|
and more complete resolution of `Step 2`_ (see Prerequisite_).
|
|
|
|
|
2011-08-29 12:36:36 -04:00
|
|
|
The rationale for keeping ``OSError`` as the official name for generic
|
|
|
|
OS-related exceptions is that it, precisely, is more generic than ``IOError``.
|
|
|
|
``EnvironmentError`` is more tedious to type and also much lesser-known.
|
|
|
|
|
2023-07-31 12:50:14 -04:00
|
|
|
The survey in `Appendix B <PEP 3151 Appendix B_>`_ shows that IOError is the
|
|
|
|
dominant error today in the standard library. As for third-party Python code,
|
2011-08-29 12:36:36 -04:00
|
|
|
Google Code Search shows IOError being ten times more popular than
|
|
|
|
EnvironmentError in user code, and three times more popular than OSError
|
|
|
|
[3]_. However, with no intention to deprecate IOError in the middle
|
|
|
|
term, the lesser popularity of OSError is not a problem.
|
2010-07-22 07:41:49 -04:00
|
|
|
|
|
|
|
Exception attributes
|
|
|
|
--------------------
|
|
|
|
|
2011-08-29 12:38:34 -04:00
|
|
|
Since WindowsError is coalesced into OSError, the latter gains a ``winerror``
|
|
|
|
attribute under Windows. It is set to None under situations where it is not
|
|
|
|
meaningful, as is already the case with the ``errno``, ``filename`` and
|
|
|
|
``strerror`` attributes (for example when OSError is raised directly by
|
|
|
|
Python code).
|
2010-07-21 13:18:39 -04:00
|
|
|
|
|
|
|
Deprecation of names
|
|
|
|
--------------------
|
|
|
|
|
2011-07-26 18:40:58 -04:00
|
|
|
The following paragraphs outline a possible deprecation strategy for
|
|
|
|
old exception names. However, it has been decided to keep them as aliases
|
|
|
|
for the time being. This decision could be revised in time for Python 4.0.
|
2010-07-31 13:44:39 -04:00
|
|
|
|
|
|
|
built-in exceptions
|
|
|
|
'''''''''''''''''''
|
|
|
|
|
|
|
|
Deprecating the old built-in exceptions cannot be done in a straightforward
|
|
|
|
fashion by intercepting all lookups in the builtins namespace, since these
|
|
|
|
are performance-critical. We also cannot work at the object level, since
|
|
|
|
the deprecated names will be aliased to non-deprecated objects.
|
|
|
|
|
|
|
|
A solution is to recognize these names at compilation time, and
|
|
|
|
then emit a separate ``LOAD_OLD_GLOBAL`` opcode instead of the regular
|
|
|
|
``LOAD_GLOBAL``. This specialized opcode will handle the output of a
|
|
|
|
DeprecationWarning (or PendingDeprecationWarning, depending on the policy
|
|
|
|
decided upon) when the name doesn't exist in the globals namespace, but
|
|
|
|
only in the builtins one. This will be enough to avoid false positives
|
|
|
|
(for example if someone defines their own ``OSError`` in a module), and
|
|
|
|
false negatives will be rare (for example when someone accesses ``OSError``
|
|
|
|
through the ``builtins`` module rather than directly).
|
|
|
|
|
|
|
|
module-level exceptions
|
|
|
|
'''''''''''''''''''''''
|
|
|
|
|
|
|
|
The above approach cannot be used easily, since it would require
|
|
|
|
special-casing some modules when compiling code objects. However, these
|
|
|
|
names are by construction much less visible (they don't appear in the
|
|
|
|
builtins namespace), and lesser-known too, so we might decide to let them
|
|
|
|
live in their own namespaces.
|
2010-07-21 13:18:39 -04:00
|
|
|
|
|
|
|
|
2010-07-22 07:41:49 -04:00
|
|
|
.. _Step 2:
|
|
|
|
|
2010-07-21 13:18:39 -04:00
|
|
|
Step 2: define additional subclasses
|
|
|
|
====================================
|
|
|
|
|
|
|
|
The second step of the resolution is to extend the hierarchy by defining
|
|
|
|
subclasses which will be raised, rather than their parent, for specific
|
|
|
|
errno values. Which errno values is subject to discussion, but a survey
|
2023-07-31 12:50:14 -04:00
|
|
|
of existing exception matching practices (see `Appendix A
|
|
|
|
<PEP 3151 Appendix A_>`_) helps us propose a reasonable subset of all values.
|
|
|
|
Trying to map all errno mnemonics, indeed, seems foolish, pointless,
|
|
|
|
and would pollute the root namespace.
|
2010-07-21 13:18:39 -04:00
|
|
|
|
|
|
|
Furthermore, in a couple of cases, different errno values could raise
|
|
|
|
the same exception subclass. For example, EAGAIN, EALREADY, EWOULDBLOCK
|
|
|
|
and EINPROGRESS are all used to signal that an operation on a non-blocking
|
|
|
|
socket would block (and therefore needs trying again later). They could
|
|
|
|
therefore all raise an identical subclass and let the user examine the
|
|
|
|
``errno`` attribute if (s)he so desires (see below "exception
|
|
|
|
attributes").
|
|
|
|
|
|
|
|
Prerequisite
|
|
|
|
------------
|
|
|
|
|
2010-07-22 07:41:49 -04:00
|
|
|
`Step 1`_ is a loose prerequisite for this.
|
2010-07-21 13:18:39 -04:00
|
|
|
|
|
|
|
Prerequisite, because some errnos can currently be attached to different
|
2011-07-26 18:43:27 -04:00
|
|
|
exception classes: for example, ENOENT can be attached to both OSError and
|
2010-07-21 13:18:39 -04:00
|
|
|
IOError, depending on the context. If we don't want to break *useful
|
|
|
|
compatibility*, we can't make an ``except OSError`` (or IOError) fail to
|
|
|
|
match an exception where it would succeed today.
|
|
|
|
|
|
|
|
Loose, because we could decide for a partial resolution of step 2
|
2011-07-26 18:43:27 -04:00
|
|
|
if existing exception classes are not coalesced: for example, ENOENT could
|
|
|
|
raise a hypothetical FileNotFoundError where an IOError was previously
|
2010-07-21 13:18:39 -04:00
|
|
|
raised, but continue to raise OSError otherwise.
|
|
|
|
|
|
|
|
The dependency on step 1 could be totally removed if the new subclasses
|
|
|
|
used multiple inheritance to match with all of the existing superclasses
|
|
|
|
(or, at least, OSError and IOError, which are arguable the most prevalent
|
|
|
|
ones). It would, however, make the hierarchy more complicated and
|
|
|
|
therefore harder to grasp for the user.
|
|
|
|
|
|
|
|
New exception classes
|
|
|
|
---------------------
|
|
|
|
|
|
|
|
The following tentative list of subclasses, along with a description and
|
|
|
|
the list of errnos mapped to them, is submitted to discussion:
|
|
|
|
|
2010-09-03 14:32:23 -04:00
|
|
|
* ``FileExistsError``: trying to create a file or directory which already
|
|
|
|
exists (EEXIST)
|
2010-07-21 13:18:39 -04:00
|
|
|
|
2010-09-03 13:07:56 -04:00
|
|
|
* ``FileNotFoundError``: for all circumstances where a file and directory is
|
2010-07-21 13:18:39 -04:00
|
|
|
requested but doesn't exist (ENOENT)
|
|
|
|
|
2010-09-03 13:07:56 -04:00
|
|
|
* ``IsADirectoryError``: file-level operation (open(), os.remove()...)
|
|
|
|
requested on a directory (EISDIR)
|
2010-07-21 13:18:39 -04:00
|
|
|
|
2010-09-03 13:07:56 -04:00
|
|
|
* ``NotADirectoryError``: directory-level operation requested on something
|
|
|
|
else (ENOTDIR)
|
2010-07-21 13:18:39 -04:00
|
|
|
|
2010-09-03 13:07:56 -04:00
|
|
|
* ``PermissionError``: trying to run an operation without the adequate access
|
2011-05-12 16:07:21 -04:00
|
|
|
rights - for example filesystem permissions (EACCES, EPERM)
|
2010-07-21 13:18:39 -04:00
|
|
|
|
|
|
|
* ``BlockingIOError``: an operation would block on an object (e.g. socket) set
|
|
|
|
for non-blocking operation (EAGAIN, EALREADY, EWOULDBLOCK, EINPROGRESS);
|
|
|
|
this is the existing ``io.BlockingIOError`` with an extended role
|
|
|
|
|
2011-07-26 20:11:38 -04:00
|
|
|
* ``BrokenPipeError``: trying to write on a pipe while the other end has been
|
|
|
|
closed, or trying to write on a socket which has been shutdown for writing
|
|
|
|
(EPIPE, ESHUTDOWN)
|
|
|
|
|
2011-07-26 18:44:35 -04:00
|
|
|
* ``InterruptedError``: a system call was interrupted by an incoming signal
|
|
|
|
(EINTR)
|
|
|
|
|
2010-09-03 13:07:56 -04:00
|
|
|
* ``ConnectionAbortedError``: connection attempt aborted by peer (ECONNABORTED)
|
2010-07-21 13:18:39 -04:00
|
|
|
|
2010-09-03 13:07:56 -04:00
|
|
|
* ``ConnectionRefusedError``: connection reset by peer (ECONNREFUSED)
|
2010-07-21 13:18:39 -04:00
|
|
|
|
2010-09-03 13:07:56 -04:00
|
|
|
* ``ConnectionResetError``: connection reset by peer (ECONNRESET)
|
2010-07-21 13:18:39 -04:00
|
|
|
|
2011-05-12 16:07:21 -04:00
|
|
|
* ``TimeoutError``: connection timed out (ETIMEDOUT); this can be re-cast
|
2010-11-10 05:01:57 -05:00
|
|
|
as a generic timeout exception, replacing ``socket.timeout`` and also useful
|
|
|
|
for other types of timeout (for example in Lock.acquire())
|
2010-07-21 13:18:39 -04:00
|
|
|
|
2011-08-30 12:35:14 -04:00
|
|
|
* ``ChildProcessError``: operation on a child process failed (ECHILD);
|
|
|
|
this is raised mainly by the wait() family of functions.
|
|
|
|
|
|
|
|
* ``ProcessLookupError``: the given process (as identified by, e.g., its
|
|
|
|
process id) doesn't exist (ESRCH).
|
|
|
|
|
2011-08-30 12:13:41 -04:00
|
|
|
In addition, the following exception class is proposed for inclusion:
|
2010-09-03 13:07:56 -04:00
|
|
|
|
|
|
|
* ``ConnectionError``: a base class for ``ConnectionAbortedError``,
|
|
|
|
``ConnectionRefusedError`` and ``ConnectionResetError``
|
|
|
|
|
|
|
|
The following drawing tries to sum up the proposed additions, along with
|
|
|
|
the corresponding errno values (where applicable). The root of the
|
2011-08-29 12:36:36 -04:00
|
|
|
sub-hierarchy (OSError, assuming `Step 1`_ is accepted in full) is not
|
2010-09-03 13:07:56 -04:00
|
|
|
shown::
|
|
|
|
|
|
|
|
+-- BlockingIOError EAGAIN, EALREADY, EWOULDBLOCK, EINPROGRESS
|
2011-08-30 12:35:14 -04:00
|
|
|
+-- ChildProcessError ECHILD
|
2010-09-03 13:07:56 -04:00
|
|
|
+-- ConnectionError
|
2011-07-26 20:11:38 -04:00
|
|
|
+-- BrokenPipeError EPIPE, ESHUTDOWN
|
2010-09-03 13:07:56 -04:00
|
|
|
+-- ConnectionAbortedError ECONNABORTED
|
|
|
|
+-- ConnectionRefusedError ECONNREFUSED
|
|
|
|
+-- ConnectionResetError ECONNRESET
|
2011-08-30 12:13:41 -04:00
|
|
|
+-- FileExistsError EEXIST
|
|
|
|
+-- FileNotFoundError ENOENT
|
2011-07-26 18:44:35 -04:00
|
|
|
+-- InterruptedError EINTR
|
2011-08-30 12:13:41 -04:00
|
|
|
+-- IsADirectoryError EISDIR
|
|
|
|
+-- NotADirectoryError ENOTDIR
|
2011-05-12 16:07:21 -04:00
|
|
|
+-- PermissionError EACCES, EPERM
|
2011-08-30 12:35:14 -04:00
|
|
|
+-- ProcessLookupError ESRCH
|
2011-05-12 16:07:21 -04:00
|
|
|
+-- TimeoutError ETIMEDOUT
|
2010-09-03 13:07:56 -04:00
|
|
|
|
|
|
|
Naming
|
|
|
|
------
|
|
|
|
|
|
|
|
Various naming controversies can arise. One of them is whether all
|
|
|
|
exception class names should end in "``Error``". In favour is consistency
|
2019-07-03 14:20:45 -04:00
|
|
|
with the rest of the exception hierarchy, against is concision (especially
|
2010-09-03 14:32:23 -04:00
|
|
|
with long names such as ``ConnectionAbortedError``).
|
2010-09-03 13:07:56 -04:00
|
|
|
|
2010-07-21 13:18:39 -04:00
|
|
|
Exception attributes
|
|
|
|
--------------------
|
|
|
|
|
|
|
|
In order to preserve *useful compatibility*, these subclasses should still
|
|
|
|
set adequate values for the various exception attributes defined on the
|
|
|
|
superclass (for example ``errno``, ``filename``, and optionally
|
|
|
|
``winerror``).
|
|
|
|
|
|
|
|
Implementation
|
|
|
|
--------------
|
|
|
|
|
|
|
|
Since it is proposed that the subclasses are raised based purely on the
|
|
|
|
value of ``errno``, little or no changes should be required in extension
|
2011-05-12 13:31:43 -04:00
|
|
|
modules (either standard or third-party).
|
2010-07-21 13:18:39 -04:00
|
|
|
|
2011-05-12 13:31:43 -04:00
|
|
|
The first possibility is to adapt the ``PyErr_SetFromErrno()`` family
|
|
|
|
of functions (``PyErr_SetFromWindowsErr()`` under Windows) to raise the
|
2011-08-29 12:36:36 -04:00
|
|
|
appropriate OSError subclass. This wouldn't cover, however, Python
|
|
|
|
code raising OSError directly, using the following idiom (seen in
|
2011-05-12 13:31:43 -04:00
|
|
|
``Lib/tempfile.py``)::
|
2010-07-21 13:18:39 -04:00
|
|
|
|
|
|
|
raise IOError(_errno.EEXIST, "No usable temporary file name found")
|
|
|
|
|
2011-05-12 13:31:43 -04:00
|
|
|
A second possibility, suggested by Marc-Andre Lemburg, is to adapt
|
2011-08-29 12:36:36 -04:00
|
|
|
``OSError.__new__`` to instantiate the appropriate subclass. This has
|
|
|
|
the benefit of also covering Python code such as the above.
|
2010-07-21 13:18:39 -04:00
|
|
|
|
|
|
|
|
|
|
|
Possible objections
|
|
|
|
===================
|
|
|
|
|
|
|
|
Namespace pollution
|
|
|
|
-------------------
|
|
|
|
|
|
|
|
Making the exception hierarchy finer-grained makes the root (or builtins)
|
|
|
|
namespace larger. This is to be moderated, however, as:
|
|
|
|
|
2017-03-24 17:11:33 -04:00
|
|
|
* only a handful of additional classes are proposed;
|
2010-07-21 13:18:39 -04:00
|
|
|
|
|
|
|
* while standard exception types live in the root namespace, they are
|
|
|
|
visually distinguished by the fact that they use the CamelCase convention,
|
|
|
|
while almost all other builtins use lowercase naming (except True, False,
|
|
|
|
None, Ellipsis and NotImplemented)
|
|
|
|
|
|
|
|
An alternative would be to provide a separate module containing the
|
|
|
|
finer-grained exceptions, but that would defeat the purpose of
|
|
|
|
encouraging careful code over careless code, since the user would first
|
|
|
|
have to import the new module instead of using names already accessible.
|
|
|
|
|
|
|
|
|
|
|
|
Earlier discussion
|
|
|
|
==================
|
|
|
|
|
|
|
|
While this is the first time such as formal proposal is made, the idea
|
|
|
|
has received informal support in the past [1]_; both the introduction
|
|
|
|
of finer-grained exception classes and the coalescing of OSError and
|
|
|
|
IOError.
|
|
|
|
|
|
|
|
The removal of WindowsError alone has been discussed and rejected
|
2022-01-21 06:03:51 -05:00
|
|
|
as part of :pep:`another PEP <348#removing-windowserror>`,
|
|
|
|
but there seemed to be a consensus that the
|
2010-07-21 13:18:39 -04:00
|
|
|
distinction with OSError wasn't meaningful. This supports at least its
|
|
|
|
aliasing with OSError.
|
|
|
|
|
|
|
|
|
2010-11-16 12:33:07 -05:00
|
|
|
Implementation
|
|
|
|
==============
|
|
|
|
|
2011-10-12 20:01:21 -04:00
|
|
|
The reference implementation has been integrated into Python 3.3.
|
|
|
|
It was formerly developed in http://hg.python.org/features/pep-3151/ in
|
|
|
|
branch ``pep-3151``, and also tracked on the bug tracker at
|
|
|
|
http://bugs.python.org/issue12555.
|
2011-07-26 18:40:58 -04:00
|
|
|
It has been successfully tested on a variety of systems: Linux, Windows,
|
|
|
|
OpenIndiana and FreeBSD buildbots.
|
2011-05-10 11:54:07 -04:00
|
|
|
|
2011-08-29 12:36:36 -04:00
|
|
|
One source of trouble has been with the respective constructors of ``OSError``
|
2011-05-10 12:31:19 -04:00
|
|
|
and ``WindowsError``, which were incompatible. The way it is solved is by
|
2011-08-29 12:36:36 -04:00
|
|
|
keeping the ``OSError`` signature and adding a fourth optional argument
|
2011-05-10 12:31:19 -04:00
|
|
|
to allow passing the Windows error code (which is different from the POSIX
|
|
|
|
errno). The fourth argument is stored as ``winerror`` and its POSIX
|
|
|
|
translation as ``errno``. The ``PyErr_SetFromWindowsErr*`` functions have
|
|
|
|
been adapted to use the right constructor call.
|
|
|
|
|
|
|
|
A slight complication is when the ``PyErr_SetExcFromWindowsErr*`` functions
|
2011-08-29 12:36:36 -04:00
|
|
|
are called with ``OSError`` rather than ``WindowsError``: the ``errno``
|
2011-05-10 12:31:19 -04:00
|
|
|
attribute of the exception object would store the Windows error code (such
|
|
|
|
as 109 for ERROR_BROKEN_PIPE) rather than its POSIX translation (such as 32
|
|
|
|
for EPIPE), which it does now. For non-socket error codes, this only occurs
|
|
|
|
in the private ``_multiprocessing`` module for which there is no compatibility
|
|
|
|
concern.
|
|
|
|
|
|
|
|
.. note::
|
|
|
|
For socket errors, the "POSIX errno" as reflected by the ``errno`` module
|
|
|
|
is numerically equal to the `Windows Socket error code
|
|
|
|
<http://msdn.microsoft.com/en-us/library/ms740668%28v=vs.85%29.aspx>`_
|
|
|
|
returned by the ``WSAGetLastError`` system call::
|
|
|
|
|
|
|
|
>>> errno.EWOULDBLOCK
|
|
|
|
10035
|
|
|
|
>>> errno.WSAEWOULDBLOCK
|
|
|
|
10035
|
2010-11-16 12:33:07 -05:00
|
|
|
|
|
|
|
|
2010-07-21 13:18:39 -04:00
|
|
|
Possible alternative
|
|
|
|
====================
|
|
|
|
|
|
|
|
Pattern matching
|
|
|
|
----------------
|
|
|
|
|
|
|
|
Another possibility would be to introduce an advanced pattern matching
|
|
|
|
syntax when catching exceptions. For example::
|
|
|
|
|
|
|
|
try:
|
|
|
|
os.remove(filename)
|
|
|
|
except OSError as e if e.errno == errno.ENOENT:
|
|
|
|
pass
|
|
|
|
|
|
|
|
Several problems with this proposal:
|
|
|
|
|
|
|
|
* it introduces new syntax, which is perceived by the author to be a heavier
|
|
|
|
change compared to reworking the exception hierarchy
|
|
|
|
* it doesn't decrease typing effort significantly
|
|
|
|
* it doesn't relieve the programmer from the burden of having to remember
|
|
|
|
errno mnemonics
|
|
|
|
|
|
|
|
|
|
|
|
Exceptions ignored by this PEP
|
|
|
|
==============================
|
|
|
|
|
|
|
|
This PEP ignores ``EOFError``, which signals a truncated input stream in
|
|
|
|
various protocol and file format implementations (for example ``GzipFile``).
|
|
|
|
``EOFError`` is not OS- or IO-related, it is a logical error raised at
|
|
|
|
a higher level.
|
|
|
|
|
|
|
|
This PEP also ignores ``SSLError``, which is raised by the ``ssl`` module
|
|
|
|
in order to propagate errors signalled by the ``OpenSSL`` library. Ideally,
|
|
|
|
``SSLError`` would benefit from a similar but separate treatment since it
|
|
|
|
defines its own constants for error types (``ssl.SSL_ERROR_WANT_READ``,
|
2011-05-10 11:23:14 -04:00
|
|
|
etc.). In Python 3.2, ``SSLError`` is already replaced with ``socket.timeout``
|
|
|
|
when it signals a socket timeout (see `issue 10272 <http://bugs.python.org/issue10272>`_).
|
2010-11-10 05:01:57 -05:00
|
|
|
|
2010-11-10 12:38:46 -05:00
|
|
|
Endly, the fate of ``socket.gaierror`` and ``socket.herror`` is not settled.
|
|
|
|
While they would deserve less cryptic names, this can be handled separately
|
|
|
|
from the exception hierarchy reorganization effort.
|
2010-07-21 13:18:39 -04:00
|
|
|
|
|
|
|
|
2023-07-31 12:50:14 -04:00
|
|
|
.. _PEP 3151 Appendix A:
|
2010-07-22 07:41:49 -04:00
|
|
|
|
2010-07-21 13:18:39 -04:00
|
|
|
Appendix A: Survey of common errnos
|
|
|
|
===================================
|
|
|
|
|
2010-07-22 06:52:13 -04:00
|
|
|
This is a quick inventory of the various errno mnemonics checked for in
|
2010-07-21 13:18:39 -04:00
|
|
|
the standard library and its tests, as part of ``except`` clauses.
|
|
|
|
|
|
|
|
Common errnos with OSError
|
|
|
|
--------------------------
|
|
|
|
|
|
|
|
* ``EBADF``: bad file descriptor (usually means the file descriptor was
|
|
|
|
closed)
|
|
|
|
|
|
|
|
* ``EEXIST``: file or directory exists
|
|
|
|
|
|
|
|
* ``EINTR``: interrupted function call
|
|
|
|
|
|
|
|
* ``EISDIR``: is a directory
|
|
|
|
|
|
|
|
* ``ENOTDIR``: not a directory
|
|
|
|
|
|
|
|
* ``ENOENT``: no such file or directory
|
|
|
|
|
|
|
|
* ``EOPNOTSUPP``: operation not supported on socket
|
|
|
|
(possible confusion with the existing io.UnsupportedOperation)
|
|
|
|
|
|
|
|
* ``EPERM``: operation not permitted (when using e.g. os.setuid())
|
|
|
|
|
|
|
|
Common errnos with IOError
|
|
|
|
--------------------------
|
|
|
|
|
|
|
|
* ``EACCES``: permission denied (for filesystem operations)
|
|
|
|
|
|
|
|
* ``EBADF``: bad file descriptor (with select.epoll); read operation on a
|
|
|
|
write-only GzipFile, or vice-versa
|
|
|
|
|
|
|
|
* ``EBUSY``: device or resource busy
|
|
|
|
|
|
|
|
* ``EISDIR``: is a directory (when trying to open())
|
|
|
|
|
|
|
|
* ``ENODEV``: no such device
|
|
|
|
|
|
|
|
* ``ENOENT``: no such file or directory (when trying to open())
|
|
|
|
|
|
|
|
* ``ETIMEDOUT``: connection timed out
|
|
|
|
|
|
|
|
Common errnos with socket.error
|
|
|
|
-------------------------------
|
|
|
|
|
|
|
|
All these errors may also be associated with a plain IOError, for example
|
|
|
|
when calling read() on a socket's file descriptor.
|
|
|
|
|
|
|
|
* ``EAGAIN``: resource temporarily unavailable (during a non-blocking socket
|
|
|
|
call except connect())
|
|
|
|
|
|
|
|
* ``EALREADY``: connection already in progress (during a non-blocking
|
|
|
|
connect())
|
|
|
|
|
|
|
|
* ``EINPROGRESS``: operation in progress (during a non-blocking connect())
|
|
|
|
|
|
|
|
* ``EINTR``: interrupted function call
|
|
|
|
|
|
|
|
* ``EISCONN``: the socket is connected
|
|
|
|
|
|
|
|
* ``ECONNABORTED``: connection aborted by peer (during an accept() call)
|
|
|
|
|
|
|
|
* ``ECONNREFUSED``: connection refused by peer
|
|
|
|
|
|
|
|
* ``ECONNRESET``: connection reset by peer
|
|
|
|
|
|
|
|
* ``ENOTCONN``: socket not connected
|
|
|
|
|
|
|
|
* ``ESHUTDOWN``: cannot send after transport endpoint shutdown
|
|
|
|
|
|
|
|
* ``EWOULDBLOCK``: same reasons as ``EAGAIN``
|
|
|
|
|
|
|
|
Common errnos with select.error
|
|
|
|
-------------------------------
|
|
|
|
|
|
|
|
* ``EINTR``: interrupted function call
|
|
|
|
|
|
|
|
|
2023-07-31 12:50:14 -04:00
|
|
|
.. _PEP 3151 Appendix B:
|
2010-07-22 07:41:49 -04:00
|
|
|
|
2010-07-21 13:18:39 -04:00
|
|
|
Appendix B: Survey of raised OS and IO errors
|
|
|
|
=============================================
|
|
|
|
|
2010-07-30 19:43:45 -04:00
|
|
|
About VMSError
|
|
|
|
--------------
|
|
|
|
|
|
|
|
VMSError is completely unused by the interpreter core and the standard
|
|
|
|
library. It was added as part of the OpenVMS patches submitted in 2002
|
|
|
|
by Jean-François Piéronne [4]_; the motivation for including VMSError was that
|
|
|
|
it could be raised by third-party packages.
|
|
|
|
|
2010-07-21 13:18:39 -04:00
|
|
|
Interpreter core
|
|
|
|
----------------
|
|
|
|
|
|
|
|
Handling of PYTHONSTARTUP raises IOError (but the error gets discarded)::
|
|
|
|
|
|
|
|
$ PYTHONSTARTUP=foox ./python
|
2017-03-24 17:11:33 -04:00
|
|
|
Python 3.2a0 (py3k:82920M, Jul 16 2010, 22:53:23)
|
2010-07-21 13:18:39 -04:00
|
|
|
[GCC 4.4.3] on linux2
|
|
|
|
Type "help", "copyright", "credits" or "license" for more information.
|
|
|
|
Could not open PYTHONSTARTUP
|
|
|
|
IOError: [Errno 2] No such file or directory: 'foox'
|
|
|
|
|
|
|
|
``PyObject_Print()`` raises IOError when ferror() signals an error on the
|
2023-09-01 15:27:57 -04:00
|
|
|
``FILE *`` parameter (which, in the source tree, is always either stdout or
|
2010-07-21 13:18:39 -04:00
|
|
|
stderr).
|
|
|
|
|
2010-07-21 14:49:25 -04:00
|
|
|
Unicode encoding and decoding using the ``mbcs`` encoding can raise
|
|
|
|
WindowsError for some error conditions.
|
|
|
|
|
2010-07-21 13:18:39 -04:00
|
|
|
Standard library
|
|
|
|
----------------
|
|
|
|
|
|
|
|
bz2
|
|
|
|
'''
|
|
|
|
|
|
|
|
Raises IOError throughout (OSError is unused)::
|
|
|
|
|
|
|
|
>>> bz2.BZ2File("foox", "rb")
|
|
|
|
Traceback (most recent call last):
|
|
|
|
File "<stdin>", line 1, in <module>
|
|
|
|
IOError: [Errno 2] No such file or directory
|
|
|
|
>>> bz2.BZ2File("LICENSE", "rb").read()
|
|
|
|
Traceback (most recent call last):
|
|
|
|
File "<stdin>", line 1, in <module>
|
|
|
|
IOError: invalid data stream
|
|
|
|
>>> bz2.BZ2File("/tmp/zzz.bz2", "wb").read()
|
|
|
|
Traceback (most recent call last):
|
|
|
|
File "<stdin>", line 1, in <module>
|
|
|
|
IOError: file is not ready for reading
|
|
|
|
|
|
|
|
curses
|
|
|
|
''''''
|
|
|
|
|
|
|
|
Not examined.
|
|
|
|
|
|
|
|
dbm.gnu, dbm.ndbm
|
|
|
|
'''''''''''''''''
|
|
|
|
|
|
|
|
_dbm.error and _gdbm.error inherit from IOError::
|
|
|
|
|
|
|
|
>>> dbm.gnu.open("foox")
|
|
|
|
Traceback (most recent call last):
|
|
|
|
File "<stdin>", line 1, in <module>
|
|
|
|
_gdbm.error: [Errno 2] No such file or directory
|
|
|
|
|
|
|
|
fcntl
|
|
|
|
'''''
|
|
|
|
|
|
|
|
Raises IOError throughout (OSError is unused).
|
|
|
|
|
|
|
|
imp module
|
|
|
|
''''''''''
|
|
|
|
|
|
|
|
Raises IOError for bad file descriptors::
|
|
|
|
|
|
|
|
>>> imp.load_source("foo", "foo", 123)
|
|
|
|
Traceback (most recent call last):
|
|
|
|
File "<stdin>", line 1, in <module>
|
|
|
|
IOError: [Errno 9] Bad file descriptor
|
|
|
|
|
|
|
|
io module
|
|
|
|
'''''''''
|
|
|
|
|
|
|
|
Raises IOError when trying to open a directory under Unix::
|
|
|
|
|
|
|
|
>>> open("Python/", "r")
|
|
|
|
Traceback (most recent call last):
|
|
|
|
File "<stdin>", line 1, in <module>
|
|
|
|
IOError: [Errno 21] Is a directory: 'Python/'
|
|
|
|
|
2010-07-21 13:44:30 -04:00
|
|
|
Raises IOError or io.UnsupportedOperation (which inherits from the former)
|
|
|
|
for unsupported operations::
|
2010-07-21 13:18:39 -04:00
|
|
|
|
|
|
|
>>> open("LICENSE").write("bar")
|
|
|
|
Traceback (most recent call last):
|
|
|
|
File "<stdin>", line 1, in <module>
|
|
|
|
IOError: not writable
|
|
|
|
>>> io.StringIO().fileno()
|
|
|
|
Traceback (most recent call last):
|
|
|
|
File "<stdin>", line 1, in <module>
|
|
|
|
io.UnsupportedOperation: fileno
|
|
|
|
>>> open("LICENSE").seek(1, 1)
|
|
|
|
Traceback (most recent call last):
|
|
|
|
File "<stdin>", line 1, in <module>
|
|
|
|
IOError: can't do nonzero cur-relative seeks
|
|
|
|
|
|
|
|
Raises either IOError or TypeError when the inferior I/O layer misbehaves
|
|
|
|
(i.e. violates the API it is expected to implement).
|
|
|
|
|
|
|
|
Raises IOError when the underlying OS resource becomes invalid::
|
|
|
|
|
|
|
|
>>> f = open("LICENSE")
|
|
|
|
>>> os.close(f.fileno())
|
|
|
|
>>> f.read()
|
|
|
|
Traceback (most recent call last):
|
|
|
|
File "<stdin>", line 1, in <module>
|
|
|
|
IOError: [Errno 9] Bad file descriptor
|
|
|
|
|
|
|
|
...or for implementation-specific optimizations::
|
|
|
|
|
|
|
|
>>> f = open("LICENSE")
|
|
|
|
>>> next(f)
|
|
|
|
'A. HISTORY OF THE SOFTWARE\n'
|
|
|
|
>>> f.tell()
|
|
|
|
Traceback (most recent call last):
|
|
|
|
File "<stdin>", line 1, in <module>
|
|
|
|
IOError: telling position disabled by next() call
|
|
|
|
|
2010-07-21 14:49:25 -04:00
|
|
|
Raises BlockingIOError (inheriting from IOError) when a call on a non-blocking
|
2010-07-21 13:18:39 -04:00
|
|
|
object would block.
|
|
|
|
|
2010-07-21 14:49:25 -04:00
|
|
|
mmap
|
|
|
|
''''
|
|
|
|
|
2010-07-26 18:20:52 -04:00
|
|
|
Under Unix, raises its own ``mmap.error`` (inheriting from EnvironmentError)
|
2010-07-21 14:49:25 -04:00
|
|
|
throughout::
|
|
|
|
|
|
|
|
>>> mmap.mmap(123, 10)
|
|
|
|
Traceback (most recent call last):
|
|
|
|
File "<stdin>", line 1, in <module>
|
|
|
|
mmap.error: [Errno 9] Bad file descriptor
|
|
|
|
>>> mmap.mmap(os.open("/tmp", os.O_RDONLY), 10)
|
|
|
|
Traceback (most recent call last):
|
|
|
|
File "<stdin>", line 1, in <module>
|
|
|
|
mmap.error: [Errno 13] Permission denied
|
|
|
|
|
|
|
|
Under Windows, however, it mostly raises WindowsError (the source code
|
|
|
|
also shows a few occurrences of ``mmap.error``)::
|
|
|
|
|
|
|
|
>>> fd = os.open("LICENSE", os.O_RDONLY)
|
|
|
|
>>> m = mmap.mmap(fd, 16384)
|
|
|
|
Traceback (most recent call last):
|
|
|
|
File "<stdin>", line 1, in <module>
|
|
|
|
WindowsError: [Error 5] Accès refusé
|
|
|
|
>>> sys.last_value.errno
|
|
|
|
13
|
|
|
|
>>> errno.errorcode[13]
|
|
|
|
'EACCES'
|
|
|
|
|
|
|
|
>>> m = mmap.mmap(-1, 4096)
|
|
|
|
>>> m.resize(16384)
|
|
|
|
Traceback (most recent call last):
|
|
|
|
File "<stdin>", line 1, in <module>
|
|
|
|
WindowsError: [Error 87] Paramètre incorrect
|
|
|
|
>>> sys.last_value.errno
|
|
|
|
22
|
|
|
|
>>> errno.errorcode[22]
|
|
|
|
'EINVAL'
|
|
|
|
|
2010-07-21 13:18:39 -04:00
|
|
|
multiprocessing
|
|
|
|
'''''''''''''''
|
|
|
|
|
|
|
|
Not examined.
|
|
|
|
|
2010-07-21 14:49:25 -04:00
|
|
|
os / posix
|
|
|
|
''''''''''
|
2010-07-21 13:54:21 -04:00
|
|
|
|
|
|
|
The ``os`` (or ``posix``) module raises OSError throughout, except under
|
2010-07-26 18:20:52 -04:00
|
|
|
Windows where WindowsError can be raised instead.
|
2010-07-21 13:54:21 -04:00
|
|
|
|
2010-07-21 13:18:39 -04:00
|
|
|
ossaudiodev
|
|
|
|
'''''''''''
|
|
|
|
|
|
|
|
Raises IOError throughout (OSError is unused)::
|
|
|
|
|
|
|
|
>>> ossaudiodev.open("foo", "r")
|
|
|
|
Traceback (most recent call last):
|
|
|
|
File "<stdin>", line 1, in <module>
|
|
|
|
IOError: [Errno 2] No such file or directory: 'foo'
|
|
|
|
|
|
|
|
readline
|
|
|
|
''''''''
|
|
|
|
|
|
|
|
Raises IOError in various file-handling functions::
|
|
|
|
|
|
|
|
>>> readline.read_history_file("foo")
|
|
|
|
Traceback (most recent call last):
|
|
|
|
File "<stdin>", line 1, in <module>
|
|
|
|
IOError: [Errno 2] No such file or directory
|
|
|
|
>>> readline.read_init_file("foo")
|
|
|
|
Traceback (most recent call last):
|
|
|
|
File "<stdin>", line 1, in <module>
|
|
|
|
IOError: [Errno 2] No such file or directory
|
|
|
|
>>> readline.write_history_file("/dev/nonexistent")
|
|
|
|
Traceback (most recent call last):
|
|
|
|
File "<stdin>", line 1, in <module>
|
|
|
|
IOError: [Errno 13] Permission denied
|
|
|
|
|
|
|
|
select
|
|
|
|
''''''
|
|
|
|
|
2010-07-21 14:59:23 -04:00
|
|
|
* select() and poll objects raise ``select.error``, which doesn't inherit from
|
2010-07-21 13:44:30 -04:00
|
|
|
anything (but poll.modify() raises IOError);
|
|
|
|
* epoll objects raise IOError;
|
|
|
|
* kqueue objects raise both OSError and IOError.
|
2010-07-21 13:18:39 -04:00
|
|
|
|
2011-05-12 12:09:06 -04:00
|
|
|
As a side-note, not deriving from ``EnvironmentError`` means ``select.error``
|
|
|
|
does not get the useful ``errno`` attribute. User code must check ``args[0]``
|
|
|
|
instead::
|
|
|
|
|
|
|
|
>>> signal.alarm(1); select.select([], [], [])
|
|
|
|
0
|
|
|
|
Traceback (most recent call last):
|
|
|
|
File "<stdin>", line 1, in <module>
|
|
|
|
select.error: (4, 'Interrupted system call')
|
|
|
|
>>> e = sys.last_value
|
|
|
|
>>> e
|
|
|
|
error(4, 'Interrupted system call')
|
|
|
|
>>> e.errno == errno.EINTR
|
|
|
|
Traceback (most recent call last):
|
|
|
|
File "<stdin>", line 1, in <module>
|
|
|
|
AttributeError: 'error' object has no attribute 'errno'
|
|
|
|
>>> e.args[0] == errno.EINTR
|
|
|
|
True
|
|
|
|
|
2010-07-21 13:18:39 -04:00
|
|
|
signal
|
|
|
|
''''''
|
|
|
|
|
2010-07-21 14:59:23 -04:00
|
|
|
``signal.ItimerError`` inherits from IOError.
|
2010-07-21 13:18:39 -04:00
|
|
|
|
|
|
|
socket
|
|
|
|
''''''
|
|
|
|
|
2010-07-21 14:59:23 -04:00
|
|
|
``socket.error`` inherits from IOError.
|
2010-07-21 13:18:39 -04:00
|
|
|
|
2010-07-21 14:49:25 -04:00
|
|
|
sys
|
|
|
|
'''
|
|
|
|
|
|
|
|
``sys.getwindowsversion()`` raises WindowsError with a bogus error number
|
|
|
|
if the ``GetVersionEx()`` call fails.
|
|
|
|
|
2010-07-21 13:18:39 -04:00
|
|
|
time
|
|
|
|
''''
|
|
|
|
|
|
|
|
Raises IOError for internal errors in time.time() and time.sleep().
|
|
|
|
|
|
|
|
zipimport
|
|
|
|
'''''''''
|
|
|
|
|
|
|
|
zipimporter.get_data() can raise IOError.
|
|
|
|
|
|
|
|
|
2010-07-22 07:41:49 -04:00
|
|
|
Acknowledgments
|
|
|
|
===============
|
|
|
|
|
2023-10-11 08:05:51 -04:00
|
|
|
Significant input has been received from Alyssa Coghlan.
|
2010-07-22 07:41:49 -04:00
|
|
|
|
2010-07-21 13:18:39 -04:00
|
|
|
References
|
|
|
|
==========
|
|
|
|
|
2010-07-22 07:41:49 -04:00
|
|
|
.. [1] "IO module precisions and exception hierarchy":
|
2017-06-11 15:02:39 -04:00
|
|
|
https://mail.python.org/pipermail/python-dev/2009-September/092130.html
|
2010-07-21 13:18:39 -04:00
|
|
|
|
2010-07-22 07:41:49 -04:00
|
|
|
.. [3] Google Code Search of ``IOError`` in Python code: `around 40000 results
|
|
|
|
<http://www.google.com/codesearch?q=lang%3Apython%20IOError>`_;
|
|
|
|
``OSError``: `around 15200 results
|
|
|
|
<http://www.google.com/codesearch?q=lang%3Apython%20OSError>`_;
|
|
|
|
``EnvironmentError``: `around 3000 results
|
|
|
|
<http://www.google.com/codesearch?q=lang%3Apython%20EnvironmentError>`_
|
|
|
|
|
2010-07-30 19:43:45 -04:00
|
|
|
.. [4] http://bugs.python.org/issue614055
|
|
|
|
|
2010-07-21 13:18:39 -04:00
|
|
|
Copyright
|
|
|
|
=========
|
|
|
|
|
|
|
|
This document has been placed in the public domain.
|