Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
084b3688
Commit
084b3688
authored
Oct 26, 2015
by
Martin Panter
Browse files
Options
Browse Files
Download
Plain Diff
Issue #23391: Merge OSError doc from 3.4 into 3.5
parents
8e66a737
5487c13e
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
60 additions
and
26 deletions
+60
-26
Doc/library/exceptions.rst
Doc/library/exceptions.rst
+48
-21
Lib/test/test_exceptions.py
Lib/test/test_exceptions.py
+12
-5
No files found.
Doc/library/exceptions.rst
View file @
084b3688
...
@@ -232,44 +232,71 @@ The following exceptions are the exceptions that are usually raised.
...
@@ -232,44 +232,71 @@ The following exceptions are the exceptions that are usually raised.
classes to override the method.
classes to override the method.
.. exception:: OSError
.. exception:: OSError([arg])
OSError(errno, strerror[, filename[, winerror[, filename2]]])
.. index:: module: errno
.. index:: module: errno
This exception is raised when a system function returns a system-related
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"
error, including I/O failures such as "file not found" or "disk full"
(not for illegal argument types or other incidental errors). Often a
(not for illegal argument types or other incidental errors).
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`.
Under Windows, the :attr:`winerror` attribute gives you the native
The second form of the constructor sets the corresponding attributes,
Windows error code. The :attr:`errno` attribute is then an approximate
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.
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`.
.. attribute:: winerror
Under Windows, this gives you the native
Windows error code. The :attr:`.errno` attribute is then an approximate
translation, in POSIX terms, of that native error code.
translation, in POSIX terms, of that native error code.
Under all platforms, the :attr:`strerror` attribute is the corresponding
Under Windows, if the *winerror* constructor argument is an integer,
error message as provided by the operating system (as formatted by the C
the :attr:`.errno` attribute is determined from the Windows error code,
and the *errno* argument is ignored. On other platforms, the
*winerror* argument is ignored, and the :attr:`winerror` attribute
does not exist.
.. attribute:: strerror
The corresponding error message, as provided by
the operating system. It is formatted by the C
functions :c:func:`perror` under POSIX, and :c:func:`FormatMessage`
functions :c:func:`perror` under POSIX, and :c:func:`FormatMessage`
Windows).
under Windows.
.. attribute:: filename
filename2
For exceptions that involve a file system path (such as :func:`open` or
For exceptions that involve a file system path (such as :func:`open` or
:func:`os.unlink`), the exception instance will contain an additional
:func:`os.unlink`), :attr:`filename` is the file name passed to the function.
attribute, :attr:`filename`, which is the file name passed to the function.
For functions that involve two file system paths (such as
For functions that involve two file system paths (such as
:func:`os.rename`), the exception instance will contain a second
:func:`os.rename`), :attr:`filename2` corresponds to the second
:attr:`filename2` attribute corresponding to the second file name passed
file name passed to the function.
to the function.
.. versionchanged:: 3.3
.. versionchanged:: 3.3
:exc:`EnvironmentError`, :exc:`IOError`, :exc:`WindowsError`,
:exc:`EnvironmentError`, :exc:`IOError`, :exc:`WindowsError`,
:exc:`VMSError`, :exc:`socket.error`, :exc:`select.error` and
:exc:`VMSError`, :exc:`socket.error`, :exc:`select.error` and
:exc:`mmap.error` have been merged into :exc:`OSError`.
:exc:`mmap.error` have been merged into :exc:`OSError`, and the
constructor may return a subclass.
.. versionchanged:: 3.4
.. versionchanged:: 3.4
The :attr:`filename` attribute is now the original file name passed to
The :attr:`filename` attribute is now the original file name passed to
the function, instead of the name encoded to or decoded from the
the function, instead of the name encoded to or decoded from the
filesystem encoding. Also, the :attr:`filename2` attribute was added.
filesystem encoding. Also, the *filename2* constructor argument and
attribute was added.
.. exception:: OverflowError
.. exception:: OverflowError
...
...
Lib/test/test_exceptions.py
View file @
084b3688
...
@@ -233,6 +233,7 @@ class ExceptionTests(unittest.TestCase):
...
@@ -233,6 +233,7 @@ class ExceptionTests(unittest.TestCase):
self
.
assertEqual
(
w
.
winerror
,
3
)
self
.
assertEqual
(
w
.
winerror
,
3
)
self
.
assertEqual
(
w
.
strerror
,
'foo'
)
self
.
assertEqual
(
w
.
strerror
,
'foo'
)
self
.
assertEqual
(
w
.
filename
,
'bar'
)
self
.
assertEqual
(
w
.
filename
,
'bar'
)
self
.
assertEqual
(
w
.
filename2
,
None
)
self
.
assertEqual
(
str
(
w
),
"[WinError 3] foo: 'bar'"
)
self
.
assertEqual
(
str
(
w
),
"[WinError 3] foo: 'bar'"
)
# Unknown win error becomes EINVAL (22)
# Unknown win error becomes EINVAL (22)
w
=
OSError
(
0
,
'foo'
,
None
,
1001
)
w
=
OSError
(
0
,
'foo'
,
None
,
1001
)
...
@@ -240,6 +241,7 @@ class ExceptionTests(unittest.TestCase):
...
@@ -240,6 +241,7 @@ class ExceptionTests(unittest.TestCase):
self
.
assertEqual
(
w
.
winerror
,
1001
)
self
.
assertEqual
(
w
.
winerror
,
1001
)
self
.
assertEqual
(
w
.
strerror
,
'foo'
)
self
.
assertEqual
(
w
.
strerror
,
'foo'
)
self
.
assertEqual
(
w
.
filename
,
None
)
self
.
assertEqual
(
w
.
filename
,
None
)
self
.
assertEqual
(
w
.
filename2
,
None
)
self
.
assertEqual
(
str
(
w
),
"[WinError 1001] foo"
)
self
.
assertEqual
(
str
(
w
),
"[WinError 1001] foo"
)
# Non-numeric "errno"
# Non-numeric "errno"
w
=
OSError
(
'bar'
,
'foo'
)
w
=
OSError
(
'bar'
,
'foo'
)
...
@@ -247,6 +249,7 @@ class ExceptionTests(unittest.TestCase):
...
@@ -247,6 +249,7 @@ class ExceptionTests(unittest.TestCase):
self
.
assertEqual
(
w
.
winerror
,
None
)
self
.
assertEqual
(
w
.
winerror
,
None
)
self
.
assertEqual
(
w
.
strerror
,
'foo'
)
self
.
assertEqual
(
w
.
strerror
,
'foo'
)
self
.
assertEqual
(
w
.
filename
,
None
)
self
.
assertEqual
(
w
.
filename
,
None
)
self
.
assertEqual
(
w
.
filename2
,
None
)
@
unittest
.
skipUnless
(
sys
.
platform
==
'win32'
,
@
unittest
.
skipUnless
(
sys
.
platform
==
'win32'
,
'test specific to Windows'
)
'test specific to Windows'
)
...
@@ -271,13 +274,15 @@ class ExceptionTests(unittest.TestCase):
...
@@ -271,13 +274,15 @@ class ExceptionTests(unittest.TestCase):
(
SystemExit
,
(
'foo'
,),
(
SystemExit
,
(
'foo'
,),
{
'args'
:
(
'foo'
,),
'code'
:
'foo'
}),
{
'args'
:
(
'foo'
,),
'code'
:
'foo'
}),
(
OSError
,
(
'foo'
,),
(
OSError
,
(
'foo'
,),
{
'args'
:
(
'foo'
,),
'filename'
:
None
,
{
'args'
:
(
'foo'
,),
'filename'
:
None
,
'filename2'
:
None
,
'errno'
:
None
,
'strerror'
:
None
}),
'errno'
:
None
,
'strerror'
:
None
}),
(
OSError
,
(
'foo'
,
'bar'
),
(
OSError
,
(
'foo'
,
'bar'
),
{
'args'
:
(
'foo'
,
'bar'
),
'filename'
:
None
,
{
'args'
:
(
'foo'
,
'bar'
),
'filename'
:
None
,
'filename2'
:
None
,
'errno'
:
'foo'
,
'strerror'
:
'bar'
}),
'errno'
:
'foo'
,
'strerror'
:
'bar'
}),
(
OSError
,
(
'foo'
,
'bar'
,
'baz'
),
(
OSError
,
(
'foo'
,
'bar'
,
'baz'
),
{
'args'
:
(
'foo'
,
'bar'
),
'filename'
:
'baz'
,
{
'args'
:
(
'foo'
,
'bar'
),
'filename'
:
'baz'
,
'filename2'
:
None
,
'errno'
:
'foo'
,
'strerror'
:
'bar'
}),
'errno'
:
'foo'
,
'strerror'
:
'bar'
}),
(
OSError
,
(
'foo'
,
'bar'
,
'baz'
,
None
,
'quux'
),
(
OSError
,
(
'foo'
,
'bar'
,
'baz'
,
None
,
'quux'
),
{
'args'
:
(
'foo'
,
'bar'
),
'filename'
:
'baz'
,
'filename2'
:
'quux'
}),
{
'args'
:
(
'foo'
,
'bar'
),
'filename'
:
'baz'
,
'filename2'
:
'quux'
}),
...
@@ -287,7 +292,8 @@ class ExceptionTests(unittest.TestCase):
...
@@ -287,7 +292,8 @@ class ExceptionTests(unittest.TestCase):
'filename'
:
'filenameStr'
}),
'filename'
:
'filenameStr'
}),
(
OSError
,
(
1
,
'strErrorStr'
,
'filenameStr'
),
(
OSError
,
(
1
,
'strErrorStr'
,
'filenameStr'
),
{
'args'
:
(
1
,
'strErrorStr'
),
'errno'
:
1
,
{
'args'
:
(
1
,
'strErrorStr'
),
'errno'
:
1
,
'strerror'
:
'strErrorStr'
,
'filename'
:
'filenameStr'
}),
'strerror'
:
'strErrorStr'
,
'filename'
:
'filenameStr'
,
'filename2'
:
None
}),
(
SyntaxError
,
(),
{
'msg'
:
None
,
'text'
:
None
,
(
SyntaxError
,
(),
{
'msg'
:
None
,
'text'
:
None
,
'filename'
:
None
,
'lineno'
:
None
,
'offset'
:
None
,
'filename'
:
None
,
'lineno'
:
None
,
'offset'
:
None
,
'print_file_and_line'
:
None
}),
'print_file_and_line'
:
None
}),
...
@@ -343,7 +349,8 @@ class ExceptionTests(unittest.TestCase):
...
@@ -343,7 +349,8 @@ class ExceptionTests(unittest.TestCase):
(
WindowsError
,
(
1
,
'strErrorStr'
,
'filenameStr'
),
(
WindowsError
,
(
1
,
'strErrorStr'
,
'filenameStr'
),
{
'args'
:
(
1
,
'strErrorStr'
),
{
'args'
:
(
1
,
'strErrorStr'
),
'strerror'
:
'strErrorStr'
,
'winerror'
:
None
,
'strerror'
:
'strErrorStr'
,
'winerror'
:
None
,
'errno'
:
1
,
'filename'
:
'filenameStr'
})
'errno'
:
1
,
'filename'
:
'filenameStr'
,
'filename2'
:
None
})
)
)
except
NameError
:
except
NameError
:
pass
pass
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment