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
bc3e63ad
Commit
bc3e63ad
authored
Apr 16, 2006
by
Andrew M. Kuchling
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Describe contextlib module. (Done for today...)
parent
a7b0af8e
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
77 additions
and
19 deletions
+77
-19
Doc/whatsnew/whatsnew25.tex
Doc/whatsnew/whatsnew25.tex
+77
-19
No files found.
Doc/whatsnew/whatsnew25.tex
View file @
bc3e63ad
...
@@ -585,11 +585,7 @@ executed.
...
@@ -585,11 +585,7 @@ executed.
First, I'll discuss the statement as it will commonly be used, and
First, I'll discuss the statement as it will commonly be used, and
then a subsection will examine the implementation details and how to
then a subsection will examine the implementation details and how to
write objects (called ``context managers'') that can be used with this
write objects (called ``context managers'') that can be used with this
statement. Most people will only use
\keyword
{
with
}
in company with
statement.
existing objects that are documented to work as context managers, and
don't need to know these details, so you can skip the subsection if
you like. Authors of new context managers will need to understand the
details of the underlying implementation.
The
\keyword
{
with
}
statement is a new control-flow structure whose
The
\keyword
{
with
}
statement is a new control-flow structure whose
basic structure is:
basic structure is:
...
@@ -663,7 +659,11 @@ with decimal.Context(prec=16):
...
@@ -663,7 +659,11 @@ with decimal.Context(prec=16):
\subsection
{
Writing Context Managers
}
\subsection
{
Writing Context Managers
}
Under the hood, the
\keyword
{
with
}
statement is fairly complicated.
Under the hood, the
\keyword
{
with
}
statement is fairly complicated.
The interface demanded of context managers contains several methods.
Most people will only use
\keyword
{
with
}
in company with
existing objects that are documented to work as context managers, and
don't need to know these details, so you can skip the following section if
you like. Authors of new context managers will need to understand the
details of the underlying implementation.
A high-level explanation of the context management protocol is:
A high-level explanation of the context management protocol is:
...
@@ -826,19 +826,74 @@ finally:
...
@@ -826,19 +826,74 @@ finally:
\end{verbatim}
\end{verbatim}
\end{comment}
\end{comment}
\subsection
{
The contextlib module
\label
{
module-contextlib
}}
The new
\module
{
contextlib
}
module provides some functions and a
The new
\module
{
contextlib
}
module provides some functions and a
decorator that are useful for writing context managers.
decorator that are useful for writing context managers.
Future versions will go into more detail.
% XXX describe further
The decorator is called
\function
{
contextmanager
}
, and lets you write
a simple context manager as a generator. The generator should yield
exactly one value. The code up to the
\keyword
{
yield
}
will be
executed as the
\method
{__
enter
__
()
}
method, and the value yielded
will be the method's return value that will get bound to the variable
in the
\keyword
{
with
}
statement's
\keyword
{
as
}
clause, if any. The
code after the
\keyword
{
yield
}
will be executed in the
\method
{__
exit
__
()
}
method. Any exception raised in the block
will be raised by the
\keyword
{
yield
}
statement.
Our database example from the previous section could be written
using this decorator as:
\begin{verbatim}
from contextlib import contextmanager
@contextmanager
def db
_
transaction (connection):
cursor = connection.cursor()
try:
yield cursor
except:
connection.rollback()
raise
else:
connection.commit()
db = DatabaseConnection()
with db
_
transaction(db) as cursor:
...
\end{verbatim}
There's a
\function
{
nested(
\var
{
mgr1
}
,
\var
{
mgr2
}
, ...)
}
manager that
combines a number of context managers so you don't need to write
nested
\keyword
{
with
}
statements. This example
both uses a database transaction and also acquires a thread lock:
\begin{verbatim}
lock = threading.Lock()
with nested (db
_
transaction(db), lock) as (cursor, locked):
...
\end{verbatim}
Finally, the
\function
{
closing(
\var
{
object
}
)
}
context manager
returns
\var
{
object
}
so that it can be bound to a variable,
and calls
\code
{
\var
{
object
}
.close()
}
at the end of the block.
\begin{verbatim}
with closing(open('/tmp/file', 'r')) as f:
for line in f:
...
\end{verbatim}
\begin{seealso}
\begin{seealso}
\seepep
{
343
}{
The ``with'' statement
}{
PEP written by
\seepep
{
343
}{
The ``with'' statement
}{
PEP written by Guido van Rossum
Guido van Rossum and Nick Coghlan.
and Nick Coghlan; implemented by Mike Bland, Guido van Rossum, and
The PEP shows the code generated for a
\keyword
{
with
}
statement,
Neal Norwitz. The PEP shows the code generated for a
\keyword
{
with
}
which can be helpful in learning how context managers work.
}
statement, which can be helpful in learning how context managers
work.
}
\seeurl
{
../lib/module-contextlib.html
}{
The documentation
for the
\module
{
contextlib
}
module.
}
\end{seealso}
\end{seealso}
...
@@ -1140,12 +1195,11 @@ pystone benchmark around XXX\% faster than Python 2.4.
...
@@ -1140,12 +1195,11 @@ pystone benchmark around XXX\% faster than Python 2.4.
%======================================================================
%======================================================================
\section
{
New, Improved, and Deprecated Modules
}
\section
{
New, Improved, and Deprecated Modules
}
As usual, Python's standard library received many enhancements and
The standard library received many enhancements and bug fixes in
bug fixes. Here's a partial list of the most notable changes, sorted
Python 2.5. Here's a partial list of the most notable changes, sorted
alphabetically by module name. Consult the
alphabetically by module name. Consult the
\file
{
Misc/NEWS
}
file in
\file
{
Misc/NEWS
}
file in the source tree for a more
the source tree for a more complete list of changes, or look through
complete list of changes, or look through the SVN logs for all the
the SVN logs for all the details.
details.
\begin{itemize}
\begin{itemize}
...
@@ -1206,6 +1260,10 @@ The \class{deque} double-ended queue type supplied by the
...
@@ -1206,6 +1260,10 @@ The \class{deque} double-ended queue type supplied by the
method that removes the first occurrence of
\var
{
value
}
in the queue,
method that removes the first occurrence of
\var
{
value
}
in the queue,
raising
\exception
{
ValueError
}
if the value isn't found.
raising
\exception
{
ValueError
}
if the value isn't found.
\item
The
\module
{
contextlib
}
module contains helper functions for use
with the new
\keyword
{
with
}
statement. See section~
\ref
{
module-contextlib
}
for more about this module. (Contributed by Phillip J. Eby.)
\item
The
\module
{
cProfile
}
module is a C implementation of
\item
The
\module
{
cProfile
}
module is a C implementation of
the existing
\module
{
profile
}
module that has much lower overhead.
the existing
\module
{
profile
}
module that has much lower overhead.
The module's interface is the same as
\module
{
profile
}
: you run
The module's interface is the same as
\module
{
profile
}
: you run
...
...
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