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
f322d683
Commit
f322d683
authored
May 02, 2006
by
Andrew M. Kuchling
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Update context manager section for removal of __context__
parent
214db63d
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
16 additions
and
54 deletions
+16
-54
Doc/whatsnew/whatsnew25.tex
Doc/whatsnew/whatsnew25.tex
+16
-54
No files found.
Doc/whatsnew/whatsnew25.tex
View file @
f322d683
...
...
@@ -638,7 +638,8 @@ The lock is acquired before the block is executed and always released once
the block is complete.
The
\module
{
decimal
}
module's contexts, which encapsulate the desired
precision and rounding characteristics for computations, also work.
precision and rounding characteristics for computations, provide a
\method
{
context
_
manager()
}
method for getting a context manager:
\begin{verbatim}
import decimal
...
...
@@ -647,7 +648,8 @@ import decimal
v1 = decimal.Decimal('578')
print v1.sqrt()
with decimal.Context(prec=16):
ctx = decimal.Context(prec=16)
with ctx.context
_
manager():
# All code in this block uses a precision of 16 digits.
# The original context is restored on exiting the block.
print v1.sqrt()
...
...
@@ -665,14 +667,12 @@ keep reading.
A high-level explanation of the context management protocol is:
\begin{itemize}
\item
The expression is evaluated and should result in an object
with a
\method
{__
context
__
()
}
method (called a ``context manager'').
\item
The
context specifier's
\method
{__
context
__
()
}
method is called,
and must return another object (called a ``with-statement context object'') that has
\item
The
expression is evaluated and should result in an object
called a ``context manager''. The context manager must have
\method
{__
enter
__
()
}
and
\method
{__
exit
__
()
}
methods.
\item
The context
object
's
\method
{__
enter
__
()
}
method is called. The value
\item
The context
manager
's
\method
{__
enter
__
()
}
method is called. The value
returned is assigned to
\var
{
VAR
}
. If no
\code
{
'as
\var
{
VAR
}
'
}
clause
is present, the value is simply discarded.
...
...
@@ -680,7 +680,7 @@ is present, the value is simply discarded.
\item
If
\var
{
BLOCK
}
raises an exception, the
\method
{__
exit
__
(
\var
{
type
}
,
\var
{
value
}
,
\var
{
traceback
}
)
}
is called
with the exception
's information
, the same values returned by
with the exception
details
, the same values returned by
\function
{
sys.exc
_
info()
}
. The method's return value controls whether
the exception is re-raised: any false value re-raises the exception,
and
\code
{
True
}
will result in suppressing it. You'll only rarely
...
...
@@ -719,20 +719,11 @@ with db_connection as cursor:
The transaction should be committed if the code in the block
runs flawlessly or rolled back if there's an exception.
First, the
\class
{
DatabaseConnection
}
needs a
\method
{__
context
__
()
}
method. Sometimes an object can simply return
\code
{
self
}
; the
\module
{
threading
}
module's lock objects do this, for example. For
our database example, though, we need to create a new object; I'll
call this class
\class
{
DatabaseContext
}
. Our
\method
{__
context
__
()
}
method must therefore look like this:
Here's the basic interface
for
\class
{
DatabaseConnection
}
that I'll assume:
\begin{verbatim}
class DatabaseConnection:
...
def
__
context
__
(self):
return DatabaseContext(self)
# Database interface
def cursor (self):
"Returns a cursor object and starts a new transaction"
...
...
@@ -742,16 +733,6 @@ class DatabaseConnection:
"Rolls back current transaction"
\end{verbatim}
Instances of
\class
{
DatabaseContext
}
need the connection object so that
the connection object's
\method
{
commit()
}
or
\method
{
rollback()
}
methods can be called:
\begin{verbatim}
class DatabaseContext:
def
__
init
__
(self, connection):
self.connection = connection
\end{verbatim}
The
\method
{__
enter
__
()
}
method is pretty easy, having only to start
a new transaction. For this application the resulting cursor object
would be a useful result, so the method will return it. The user can
...
...
@@ -759,11 +740,11 @@ then add \code{as cursor} to their '\keyword{with}' statement to bind
the cursor to a variable name.
\begin{verbatim}
class DatabaseCon
text
:
class DatabaseCon
nection
:
...
def
__
enter
__
(self):
# Code to start a new transaction
cursor = self.c
onnection.c
ursor()
cursor = self.cursor()
return cursor
\end{verbatim}
...
...
@@ -779,15 +760,15 @@ wished, you could be more explicit and add a \keyword{return}
statement at the marked location.
\begin{verbatim}
class DatabaseCon
text
:
class DatabaseCon
nection
:
...
def
__
exit
__
(self, type, value, tb):
if tb is None:
# No exception, so commit
self.co
nnection.co
mmit()
self.commit()
else:
# Exception occurred, so rollback.
self.
connection.
rollback()
self.rollback()
# return False
\end{verbatim}
...
...
@@ -830,27 +811,8 @@ with db_transaction(db) as cursor:
...
\end{verbatim}
You can also use this decorator to write the
\method
{__
context
__
()
}
method for a class:
\begin{verbatim}
class DatabaseConnection:
@contextfactory
def
__
context
__
(self):
cursor = self.cursor()
try:
yield cursor
except:
self.rollback()
raise
else:
self.commit()
\end{verbatim}
The
\module
{
contextlib
}
module also has a
\function
{
nested(
\var
{
mgr1
}
,
\var
{
mgr2
}
, ...)
}
function that combines a number of contexts so you
\var
{
mgr2
}
, ...)
}
function that combines a number of context
manager
s so you
don't need to write nested '
\keyword
{
with
}
' statements. In this
example, the single '
\keyword
{
with
}
' statement both starts a database
transaction and acquires a thread lock:
...
...
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