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
5d0f4c61
Commit
5d0f4c61
authored
Mar 27, 2006
by
Phillip J. Eby
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Document the PEP 343 context manager protocol methods.
parent
06b3ddea
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
58 additions
and
0 deletions
+58
-0
Doc/ref/ref3.tex
Doc/ref/ref3.tex
+58
-0
No files found.
Doc/ref/ref3.tex
View file @
5d0f4c61
...
...
@@ -2106,3 +2106,61 @@ implement a \method{__coerce__()} method, for use by the built-in
\function
{
coerce()
}
function.
\end{itemize}
\subsection
{
Context Managers and Contexts
\label
{
context-managers
}}
A
\dfn
{
context manager
}
is an object that manages the entry to, and exit
from, a
\dfn
{
context
}
surrounding a block of code. Context managers are
normally invoked using the
\keyword
{
with
}
statement (described in
section~
\ref
{
with
}
), but can also be used by directly invoking their
methods.
\stindex
{
with
}
\index
{
context manager
}
\index
{
context
}
Typical uses of context managers include saving and restoring various
kinds of global state, locking and unlocking resources, closing opened
files, etc.
\begin{methoddesc}
[context manager]
{__
context
__}{
self
}
Invoked when the object is used as the context expression of a
\keyword
{
with
}
statement. The return value must implement
\method
{__
enter
__
()
}
and
\method
{__
exit
__
()
}
methods. Simple context
managers that wish to directly
implement
\method
{__
enter
__
()
}
and
\method
{__
exit
__
()
}
should just
return
\var
{
self
}
.
Context managers written in Python can also implement this method using
a generator function decorated with the
\function
{
contextlib.contextmanager
}
decorator, as this can be simpler
than writing individual
\method
{__
enter
__
()
}
and
\method
{__
exit
__
()
}
methods when the state to be managed is complex.
\end{methoddesc}
\begin{methoddesc}
[context]
{__
enter
__}{
self
}
Enter the context defined by this object. The
\keyword
{
with
}
statement
will bind this method's return value to the target(s) specified in the
\keyword
{
as
}
clause of the statement, if any.
\end{methoddesc}
\begin{methoddesc}
[context]
{__
exit
__}{
exc
_
type, exc
_
value, traceback
}
Exit the context defined by this object. The parameters describe the
exception that caused the context to be exited. If the context was
exited without an exception, all three arguments will be
\constant
{
None
}
.
If an exception is supplied, and the method wishes to suppress the
exception (i.e., prevent it from being propagated), it should return a
true value. Otherwise, the exception will be processed normally upon
exit from this method.
Note that
\method
{__
exit
__}
methods should not reraise the passed-in
exception; this is the caller's responsibility.
\end{methoddesc}
\begin{seealso}
\seepep
{
0343
}{
The "with" statement
}
{
The specification, background, and examples for the
Python
\keyword
{
with
}
statement.
}
\end{seealso}
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