Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d7475fb

Browse files
committed
Issue #23391: Merge OSError doc from 3.5
2 parents 1c8222c + 084b368 commit d7475fb

2 files changed

Lines changed: 60 additions & 26 deletions

File tree

Doc/library/exceptions.rst

Lines changed: 48 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -232,44 +232,71 @@ The following exceptions are the exceptions that are usually raised.
232232
classes to override the method.
233233

234234

235-
.. exception:: OSError
235+
.. exception:: OSError([arg])
236+
OSError(errno, strerror[, filename[, winerror[, filename2]]])
236237

237238
.. index:: module: errno
238239

239240
This exception is raised when a system function returns a system-related
240241
error, including I/O failures such as "file not found" or "disk full"
241-
(not for illegal argument types or other incidental errors). Often a
242-
subclass of :exc:`OSError` will actually be raised as described in
243-
`OS exceptions`_ below. The :attr:`errno` attribute is a numeric error
244-
code from the C variable :c:data:`errno`.
242+
(not for illegal argument types or other incidental errors).
245243

246-
Under Windows, the :attr:`winerror` attribute gives you the native
247-
Windows error code. The :attr:`errno` attribute is then an approximate
248-
translation, in POSIX terms, of that native error code.
244+
The second form of the constructor sets the corresponding attributes,
245+
described below. The attributes default to :const:`None` if not
246+
specified. For backwards compatibility, if three arguments are passed,
247+
the :attr:`~BaseException.args` attribute contains only a 2-tuple
248+
of the first two constructor arguments.
249249

250-
Under all platforms, the :attr:`strerror` attribute is the corresponding
251-
error message as provided by the operating system (as formatted by the C
252-
functions :c:func:`perror` under POSIX, and :c:func:`FormatMessage`
253-
Windows).
250+
The constructor often actually returns a subclass of :exc:`OSError`, as
251+
described in `OS exceptions`_ below. The particular subclass depends on
252+
the final :attr:`.errno` value. This behaviour only occurs when
253+
constructing :exc:`OSError` directly or via an alias, and is not
254+
inherited when subclassing.
254255

255-
For exceptions that involve a file system path (such as :func:`open` or
256-
:func:`os.unlink`), the exception instance will contain an additional
257-
attribute, :attr:`filename`, which is the file name passed to the function.
258-
For functions that involve two file system paths (such as
259-
:func:`os.rename`), the exception instance will contain a second
260-
:attr:`filename2` attribute corresponding to the second file name passed
261-
to the function.
256+
.. attribute:: errno
257+
258+
A numeric error code from the C variable :c:data:`errno`.
259+
260+
.. attribute:: winerror
261+
262+
Under Windows, this gives you the native
263+
Windows error code. The :attr:`.errno` attribute is then an approximate
264+
translation, in POSIX terms, of that native error code.
265+
266+
Under Windows, if the *winerror* constructor argument is an integer,
267+
the :attr:`.errno` attribute is determined from the Windows error code,
268+
and the *errno* argument is ignored. On other platforms, the
269+
*winerror* argument is ignored, and the :attr:`winerror` attribute
270+
does not exist.
271+
272+
.. attribute:: strerror
273+
274+
The corresponding error message, as provided by
275+
the operating system. It is formatted by the C
276+
functions :c:func:`perror` under POSIX, and :c:func:`FormatMessage`
277+
under Windows.
278+
279+
.. attribute:: filename
280+
filename2
281+
282+
For exceptions that involve a file system path (such as :func:`open` or
283+
:func:`os.unlink`), :attr:`filename` is the file name passed to the function.
284+
For functions that involve two file system paths (such as
285+
:func:`os.rename`), :attr:`filename2` corresponds to the second
286+
file name passed to the function.
262287

263288

264289
.. versionchanged:: 3.3
265290
:exc:`EnvironmentError`, :exc:`IOError`, :exc:`WindowsError`,
266291
:exc:`VMSError`, :exc:`socket.error`, :exc:`select.error` and
267-
:exc:`mmap.error` have been merged into :exc:`OSError`.
292+
:exc:`mmap.error` have been merged into :exc:`OSError`, and the
293+
constructor may return a subclass.
268294

269295
.. versionchanged:: 3.4
270296
The :attr:`filename` attribute is now the original file name passed to
271297
the function, instead of the name encoded to or decoded from the
272-
filesystem encoding. Also, the :attr:`filename2` attribute was added.
298+
filesystem encoding. Also, the *filename2* constructor argument and
299+
attribute was added.
273300

274301

275302
.. exception:: OverflowError

Lib/test/test_exceptions.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -233,20 +233,23 @@ def test_WindowsError(self):
233233
self.assertEqual(w.winerror, 3)
234234
self.assertEqual(w.strerror, 'foo')
235235
self.assertEqual(w.filename, 'bar')
236+
self.assertEqual(w.filename2, None)
236237
self.assertEqual(str(w), "[WinError 3] foo: 'bar'")
237238
# Unknown win error becomes EINVAL (22)
238239
w = OSError(0, 'foo', None, 1001)
239240
self.assertEqual(w.errno, 22)
240241
self.assertEqual(w.winerror, 1001)
241242
self.assertEqual(w.strerror, 'foo')
242243
self.assertEqual(w.filename, None)
244+
self.assertEqual(w.filename2, None)
243245
self.assertEqual(str(w), "[WinError 1001] foo")
244246
# Non-numeric "errno"
245247
w = OSError('bar', 'foo')
246248
self.assertEqual(w.errno, 'bar')
247249
self.assertEqual(w.winerror, None)
248250
self.assertEqual(w.strerror, 'foo')
249251
self.assertEqual(w.filename, None)
252+
self.assertEqual(w.filename2, None)
250253

251254
@unittest.skipUnless(sys.platform == 'win32',
252255
'test specific to Windows')
@@ -271,13 +274,15 @@ def testAttributes(self):
271274
(SystemExit, ('foo',),
272275
{'args' : ('foo',), 'code' : 'foo'}),
273276
(OSError, ('foo',),
274-
{'args' : ('foo',), 'filename' : None,
277+
{'args' : ('foo',), 'filename' : None, 'filename2' : None,
275278
'errno' : None, 'strerror' : None}),
276279
(OSError, ('foo', 'bar'),
277-
{'args' : ('foo', 'bar'), 'filename' : None,
280+
{'args' : ('foo', 'bar'),
281+
'filename' : None, 'filename2' : None,
278282
'errno' : 'foo', 'strerror' : 'bar'}),
279283
(OSError, ('foo', 'bar', 'baz'),
280-
{'args' : ('foo', 'bar'), 'filename' : 'baz',
284+
{'args' : ('foo', 'bar'),
285+
'filename' : 'baz', 'filename2' : None,
281286
'errno' : 'foo', 'strerror' : 'bar'}),
282287
(OSError, ('foo', 'bar', 'baz', None, 'quux'),
283288
{'args' : ('foo', 'bar'), 'filename' : 'baz', 'filename2': 'quux'}),
@@ -287,7 +292,8 @@ def testAttributes(self):
287292
'filename' : 'filenameStr'}),
288293
(OSError, (1, 'strErrorStr', 'filenameStr'),
289294
{'args' : (1, 'strErrorStr'), 'errno' : 1,
290-
'strerror' : 'strErrorStr', 'filename' : 'filenameStr'}),
295+
'strerror' : 'strErrorStr',
296+
'filename' : 'filenameStr', 'filename2' : None}),
291297
(SyntaxError, (), {'msg' : None, 'text' : None,
292298
'filename' : None, 'lineno' : None, 'offset' : None,
293299
'print_file_and_line' : None}),
@@ -343,7 +349,8 @@ def testAttributes(self):
343349
(WindowsError, (1, 'strErrorStr', 'filenameStr'),
344350
{'args' : (1, 'strErrorStr'),
345351
'strerror' : 'strErrorStr', 'winerror' : None,
346-
'errno' : 1, 'filename' : 'filenameStr'})
352+
'errno' : 1,
353+
'filename' : 'filenameStr', 'filename2' : None})
347354
)
348355
except NameError:
349356
pass

0 commit comments

Comments
 (0)