cpython: e5df201c0846 (original) (raw)
--- a/Doc/library/exceptions.rst +++ b/Doc/library/exceptions.rst @@ -232,44 +232,71 @@ The following exceptions are the excepti classes to override the method. -.. exception:: OSError +.. exception:: OSError([arg])
OSError(errno, strerror[, filename[, winerror[, filename2]]])[](#l1.9)
.. index:: module: errno This exception is raised when a system function returns a system-related error, including I/O failures such as "file not found" or "disk full"
- (not for illegal argument types or other incidental errors). Often a
- subclass of :exc:
OSError
will actually be raised as described in OS exceptions
_ below. The :attr:errno
attribute is a numeric error- code from the C variable :c:data:
errno
.
- (not for illegal argument types or other incidental errors). +
- The second form of the constructor sets the corresponding attributes,
- described below. The attributes default to :const:
None
if not - specified. For backwards compatibility, if three arguments are passed,
- the :attr:
~BaseException.args
attribute contains only a 2-tuple - of the first two constructor arguments.
- Under Windows, the :attr:
winerror
attribute gives you the native - Windows error code. The :attr:
errno
attribute is then an approximate - translation, in POSIX terms, of that native error code.
- The constructor often actually returns a subclass of :exc:
OSError
, as - described in
OS exceptions
_ below. The particular subclass depends on - the final :attr:
.errno
value. This behaviour only occurs when - constructing :exc:
OSError
directly or via an alias, and is not - inherited when subclassing. +
- .. attribute:: errno +
A numeric error code from the C variable :c:data:`errno`.[](#l1.38)
- Under all platforms, the :attr:
strerror
attribute is the corresponding - error message as provided by the operating system (as formatted by the C
- functions :c:func:
perror
under POSIX, and :c:func:FormatMessage
- Windows).
Under Windows, this gives you the native[](#l1.46)
Windows error code. The :attr:`.errno` attribute is then an approximate[](#l1.47)
translation, in POSIX terms, of that native error code.[](#l1.48)
Under Windows, if the *winerror* constructor argument is an integer,[](#l1.50)
the :attr:`.errno` attribute is determined from the Windows error code,[](#l1.51)
and the *errno* argument is ignored. On other platforms, the[](#l1.52)
*winerror* argument is ignored, and the :attr:`winerror` attribute[](#l1.53)
does not exist.[](#l1.54)
- For exceptions that involve a file system path (such as :func:
open
or - :func:
os.unlink
), the exception instance will contain an additional - attribute, :attr:
filename
, which is the file name passed to the function. - For functions that involve two file system paths (such as
- :func:
os.rename
), the exception instance will contain a second - :attr:
filename2
attribute corresponding to the second file name passed - to the function.
The corresponding error message, as provided by[](#l1.65)
the operating system. It is formatted by the C[](#l1.66)
functions :c:func:`perror` under POSIX, and :c:func:`FormatMessage`[](#l1.67)
under Windows.[](#l1.68)
For exceptions that involve a file system path (such as :func:`open` or[](#l1.73)
:func:`os.unlink`), :attr:`filename` is the file name passed to the function.[](#l1.74)
For functions that involve two file system paths (such as[](#l1.75)
:func:`os.rename`), :attr:`filename2` corresponds to the second[](#l1.76)
file name passed to the function.[](#l1.77)
.. versionchanged:: 3.3
:exc:EnvironmentError
, :exc:IOError
, :exc:WindowsError
,
:exc:VMSError
, :exc:socket.error
, :exc:select.error
and
:exc:`mmap.error` have been merged into :exc:`OSError`.[](#l1.83)
:exc:`mmap.error` have been merged into :exc:`OSError`, and the[](#l1.84)
constructor may return a subclass.[](#l1.85)
.. versionchanged:: 3.4
The :attr:filename
attribute is now the original file name passed to
the function, instead of the name encoded to or decoded from the
filesystem encoding. Also, the :attr:`filename2` attribute was added.[](#l1.90)
filesystem encoding. Also, the *filename2* constructor argument and[](#l1.91)
attribute was added.[](#l1.92)
--- a/Lib/test/test_exceptions.py +++ b/Lib/test/test_exceptions.py @@ -233,6 +233,7 @@ class ExceptionTests(unittest.TestCase): self.assertEqual(w.winerror, 3) self.assertEqual(w.strerror, 'foo') self.assertEqual(w.filename, 'bar')
self.assertEqual(w.filename2, None)[](#l2.7) self.assertEqual(str(w), "[WinError 3] foo: 'bar'")[](#l2.8) # Unknown win error becomes EINVAL (22)[](#l2.9) w = OSError(0, 'foo', None, 1001)[](#l2.10)
@@ -240,6 +241,7 @@ class ExceptionTests(unittest.TestCase): self.assertEqual(w.winerror, 1001) self.assertEqual(w.strerror, 'foo') self.assertEqual(w.filename, None)
self.assertEqual(w.filename2, None)[](#l2.15) self.assertEqual(str(w), "[WinError 1001] foo")[](#l2.16) # Non-numeric "errno"[](#l2.17) w = OSError('bar', 'foo')[](#l2.18)
@@ -247,6 +249,7 @@ class ExceptionTests(unittest.TestCase): self.assertEqual(w.winerror, None) self.assertEqual(w.strerror, 'foo') self.assertEqual(w.filename, None)
self.assertEqual(w.filename2, None)[](#l2.23)
@unittest.skipUnless(sys.platform == 'win32', 'test specific to Windows') @@ -271,13 +274,15 @@ class ExceptionTests(unittest.TestCase): (SystemExit, ('foo',), {'args' : ('foo',), 'code' : 'foo'}), (OSError, ('foo',),
{'args' : ('foo',), 'filename' : None,[](#l2.31)
{'args' : ('foo',), 'filename' : None, 'filename2' : None,[](#l2.32) 'errno' : None, 'strerror' : None}),[](#l2.33) (OSError, ('foo', 'bar'),[](#l2.34)
{'args' : ('foo', 'bar'), 'filename' : None,[](#l2.35)
{'args' : ('foo', 'bar'),[](#l2.36)
'filename' : None, 'filename2' : None,[](#l2.37) 'errno' : 'foo', 'strerror' : 'bar'}),[](#l2.38) (OSError, ('foo', 'bar', 'baz'),[](#l2.39)
{'args' : ('foo', 'bar'), 'filename' : 'baz',[](#l2.40)
{'args' : ('foo', 'bar'),[](#l2.41)
'filename' : 'baz', 'filename2' : None,[](#l2.42) 'errno' : 'foo', 'strerror' : 'bar'}),[](#l2.43) (OSError, ('foo', 'bar', 'baz', None, 'quux'),[](#l2.44) {'args' : ('foo', 'bar'), 'filename' : 'baz', 'filename2': 'quux'}),[](#l2.45)
@@ -287,7 +292,8 @@ class ExceptionTests(unittest.TestCase): 'filename' : 'filenameStr'}), (OSError, (1, 'strErrorStr', 'filenameStr'), {'args' : (1, 'strErrorStr'), 'errno' : 1,
'strerror' : 'strErrorStr', 'filename' : 'filenameStr'}),[](#l2.50)
'strerror' : 'strErrorStr',[](#l2.51)
'filename' : 'filenameStr', 'filename2' : None}),[](#l2.52) (SyntaxError, (), {'msg' : None, 'text' : None,[](#l2.53) 'filename' : None, 'lineno' : None, 'offset' : None,[](#l2.54) 'print_file_and_line' : None}),[](#l2.55)
@@ -343,7 +349,8 @@ class ExceptionTests(unittest.TestCase): (WindowsError, (1, 'strErrorStr', 'filenameStr'), {'args' : (1, 'strErrorStr'), 'strerror' : 'strErrorStr', 'winerror' : None,
'errno' : 1, 'filename' : 'filenameStr'})[](#l2.60)
'errno' : 1,[](#l2.61)
'filename' : 'filenameStr', 'filename2' : None})[](#l2.62) )[](#l2.63) except NameError:[](#l2.64) pass[](#l2.65)