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
ddd4d236
Commit
ddd4d236
authored
Apr 23, 2006
by
Nick Coghlan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Update with statement documentation to use same terminology as 2.5a1 implementation
parent
525294ee
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
38 additions
and
27 deletions
+38
-27
Doc/ref/ref3.tex
Doc/ref/ref3.tex
+32
-20
Doc/ref/ref7.tex
Doc/ref/ref7.tex
+6
-7
No files found.
Doc/ref/ref3.tex
View file @
ddd4d236
...
...
@@ -2116,42 +2116,54 @@ implement a \method{__coerce__()} method, for use by the built-in
\versionadded
{
2.5
}
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.
A
\dfn
{
context object
}
is an object that defines the runtime context
to be established when executing a
\keyword
{
with
}
statement. The
context object provides a
\dfn
{
context manager
}
which manages the
entry into, and the exit from, the desired runtime context for the
execution of the 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
}
\index
{
context
object
}
Typical uses of context
managers include saving and restoring various
kinds of global state, locking and unlocking resources, closing opened
files, etc.
Typical uses of context
s and 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
}
For more information on contexts and context manager objects, see
``
\ulink
{
Context Types
}{
../lib/typecontext.html
}
'' in the
\citetitle
[../lib/lib.html]
{
Python Library Reference
}
.
\begin{methoddesc}
[context]
{__
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
ma
nagers that wish to directly
implement
\method
{__
enter
__
()
}
and
\method
{__
exit
__
()
}
should just
return
\var
{
self
}
.
\method
{__
enter
__
()
}
and
\method
{__
exit
__
()
}
methods. Simple context
s
ma
y be able to implement
\method
{__
enter
__
()
}
and
\method
{__
exit
__
()
}
directly without requiring a separate context manager object and
should just
return
\var
{
self
}
.
Context
manager
s written in Python can also implement this method using
Context
object
s 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.
methods on a separate object when the state to be managed is complex.
Context manager objects also need to implement this method; they are
required to return themselves.
\end{methoddesc}
\begin{methoddesc}
[context]
{__
enter
__}{
self
}
Enter the context
defin
ed by this object. The
\keyword
{
with
}
statement
\begin{methoddesc}
[context
manager
]
{__
enter
__}{
self
}
Enter the context
manag
ed 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
\begin{methoddesc}
[context manager]
{__
exit
__}
{
self, exc
_
type, exc
_
value, traceback
}
Exit the context managed 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
}
.
...
...
Doc/ref/ref7.tex
View file @
ddd4d236
...
...
@@ -329,13 +329,12 @@ The execution of the \keyword{with} statement proceeds as follows:
\begin{enumerate}
\item
The expression is evaluated, to obtain a context manager
object.
\item
The expression is evaluated, to obtain a context object.
\item
The context
manager
's
\method
{__
context
__
()
}
method is invoked to
obtain a context object.
\item
The context
object
's
\method
{__
context
__
()
}
method is invoked to
obtain a context
manager
object.
\item
The context
object
's
\method
{__
enter
__
()
}
method is invoked.
\item
The context
manager
's
\method
{__
enter
__
()
}
method is invoked.
\item
If a target list was included in the
\keyword
{
with
}
statement, the return value from
\method
{__
enter
__
()
}
is assigned to it.
...
...
@@ -348,8 +347,8 @@ an error occurring within the suite would be. See step 6 below.}
\item
The suite is executed.
\item
The context
object's
\method
{__
exit
__
()
}
method is invoked. If an
exception caused the suite to be exited, its type, value, and
\item
The context
manager's
\method
{__
exit
__
()
}
method is invoked. If
an
exception caused the suite to be exited, its type, value, and
traceback are passed as arguments to
\method
{__
exit
__
()
}
. Otherwise,
three
\constant
{
None
}
arguments are supplied.
...
...
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