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
5ef9d9fd
Commit
5ef9d9fd
authored
Apr 23, 2006
by
Nick Coghlan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Update contextlib documentation to use the same terminology as the module implementation
parent
84faa857
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
8 additions
and
7 deletions
+8
-7
Doc/lib/libcontextlib.tex
Doc/lib/libcontextlib.tex
+8
-7
No files found.
Doc/lib/libcontextlib.tex
View file @
5ef9d9fd
...
@@ -49,8 +49,9 @@ occurred. Thus, you can use a
...
@@ -49,8 +49,9 @@ occurred. Thus, you can use a
the error (if any), or ensure that some cleanup takes place.
the error (if any), or ensure that some cleanup takes place.
Note that you can use
\code
{
@contextmanager
}
to define a context
Note that you can use
\code
{
@contextmanager
}
to define a context
manager's
\method
{__
context
__}
method. This is usually more convenient
object's
\method
{__
context
__}
method. This is usually more convenient
than creating another class just to serve as a context. For example:
than creating another class just to serve as a context manager.
For example:
\begin{verbatim}
\begin{verbatim}
from
__
future
__
import with
_
statement
from
__
future
__
import with
_
statement
...
@@ -97,10 +98,10 @@ with A as X:
...
@@ -97,10 +98,10 @@ with A as X:
do
_
something()
do
_
something()
\end{verbatim}
\end{verbatim}
Note that if
one of the nested contexts'
\method
{__
exit
__
()
}
method
Note that if
the
\method
{__
exit
__
()
}
method of one of the nested context managers
raises an exception, any previous exception state will be lost; the new
raises an exception, any previous exception state will be lost; the new
exception will be passed to the
outer contexts'
\method
{__
exit
__
()
}
exception will be passed to the
\method
{__
exit
__
()
}
methods of any remaining
method(s), if any
. In general,
\method
{__
exit
__
()
}
methods should avoid
outer context managers
. In general,
\method
{__
exit
__
()
}
methods should avoid
raising exceptions, and in particular they should not re-raise a
raising exceptions, and in particular they should not re-raise a
passed-in exception.
passed-in exception.
\end{funcdesc}
\end{funcdesc}
...
@@ -127,9 +128,9 @@ from __future__ import with_statement
...
@@ -127,9 +128,9 @@ from __future__ import with_statement
from contextlib import closing
from contextlib import closing
import codecs
import codecs
with closing(
codecs.open("foo", encoding="utf8"
)) as f:
with closing(
urllib.urlopen('http://www.python.org'
)) as f:
for line in f:
for line in f:
print line
.encode("latin1")
print line
\end{verbatim}
\end{verbatim}
without needing to explicitly close
\code
{
f
}
. Even if an error occurs,
without needing to explicitly close
\code
{
f
}
. Even if an error occurs,
...
...
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