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
168e99f6
Commit
168e99f6
authored
Mar 28, 2006
by
Phillip J. Eby
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Document objects that can be used with the ``with`` statement.
parent
bdfd6938
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
94 additions
and
2 deletions
+94
-2
Doc/lib/libdecimal.tex
Doc/lib/libdecimal.tex
+26
-2
Doc/lib/libstdtypes.tex
Doc/lib/libstdtypes.tex
+32
-0
Doc/lib/libthread.tex
Doc/lib/libthread.tex
+13
-0
Doc/lib/libthreading.tex
Doc/lib/libthreading.tex
+23
-0
No files found.
Doc/lib/libdecimal.tex
View file @
168e99f6
...
...
@@ -442,9 +442,33 @@ the \function{getcontext()} and \function{setcontext()} functions:
Set the current context for the active thread to
\var
{
c
}
.
\end{funcdesc}
New contexts can formed using the
\class
{
Context
}
constructor described below.
In addition, the module provides three pre-made contexts:
Beginning with Python 2.5, you can also use the
\keyword
{
with
}
statement
to temporarily change the active context. For example the following code
increases the current decimal precision by 2 places, performs a
calculation, and then automatically restores the previous context:
\begin{verbatim}
from
__
future
__
import with
_
statement
import decimal
with decimal.getcontext() as ctx:
ctx.prec += 2 # add 2 more digits of precision
calculate
_
something()
\end{verbatim}
The context that's active in the body of the
\keyword
{
with
}
statement is
a
\emph
{
copy
}
of the context you provided to the
\keyword
{
with
}
statement, so modifying its attributes doesn't affect anything except
that temporary copy.
You can use any decimal context in a
\keyword
{
with
}
statement, but if
you just want to make a temporary change to some aspect of the current
context, it's easiest to just use
\function
{
getcontext()
}
as shown
above.
New contexts can also be created using the
\class
{
Context
}
constructor
described below. In addition, the module provides three pre-made
contexts:
\begin{classdesc*}
{
BasicContext
}
This is a standard context defined by the General Decimal Arithmetic
...
...
Doc/lib/libstdtypes.tex
View file @
168e99f6
...
...
@@ -1500,6 +1500,38 @@ Files have the following methods:
Any operation which requires that the file be open will raise a
\exception
{
ValueError
}
after the file has been closed. Calling
\method
{
close()
}
more than once is allowed.
As of Python 2.5, you can avoid having to call this method explicitly
if you use the
\keyword
{
with
}
statement. For example, the following
code will automatically close
\code
{
f
}
when the
\keyword
{
with
}
block
is exited:
\begin{verbatim}
from
__
future
__
import with
_
statement
with open("hello.txt") as f:
for line in f:
print line
\end{verbatim}
In older versions of Python, you would have needed to do this to get
the same effect:
\begin{verbatim}
f = open("hello.txt")
try:
for line in f:
print line
finally:
f.close()
\end{verbatim}
\note
{
Not all ``file-like'' types in Python support use as a context
manager for the
\keyword
{
with
}
statement. If your code is intended to
work with any file-like object, you can use the
\function
{
closing()
}
function in the
\module
{
contextlib
}
module instead of using the object
directly. See section~
\ref
{
context-closing
}
for details.
}
\end{methoddesc}
\begin{methoddesc}
[file]
{
flush
}{}
...
...
Doc/lib/libthread.tex
View file @
168e99f6
...
...
@@ -100,6 +100,19 @@ Return the status of the lock:\ \code{True} if it has been acquired by
some thread,
\code
{
False
}
if not.
\end{methoddesc}
In addition to these methods, lock objects can also be used via the
\keyword
{
with
}
statement, e.g.:
\begin{verbatim}
from
__
future
__
import with
_
statement
import thread
a
_
lock = thread.allocate
_
lock()
with a
_
lock:
print "a
_
lock is locked while this executes"
\end{verbatim}
\strong
{
Caveats:
}
\begin{itemize}
...
...
Doc/lib/libthreading.tex
View file @
168e99f6
...
...
@@ -675,3 +675,26 @@ keyword arguments \var{kwargs}, after \var{interval} seconds have passed.
Stop the timer, and cancel the execution of the timer's action. This
will only work if the timer is still in its waiting stage.
\end{methoddesc}
\subsection
{
Using locks, conditions, and semaphores in the
\keyword
{
with
}
statement
\label
{
with-locks
}}
All of the objects provided by this module that have
\method
{
acquire()
}
and
\method
{
release()
}
methods can be used as context managers for a
\keyword
{
with
}
statement. The
\method
{
acquire()
}
method will be called when the block is
entered, and
\method
{
release()
}
will be called when the block is exited.
Currently,
\class
{
Lock
}
,
\class
{
RLock
}
,
\class
{
Condition
}
,
\class
{
Semaphore
}
,
and
\class
{
BoundedSemaphore
}
objects may be used as
\keyword
{
with
}
statement context managers. For example:
\begin{verbatim}
from
__
future
__
import with
_
statement
import threading
some
_
rlock = threading.RLock()
with some
_
rlock:
print "some
_
rlock is locked while this executes"
\end{verbatim}
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