Skip to content
Projeler
Gruplar
Parçacıklar
Yardım
Yükleniyor...
Oturum aç / Kaydol
Gezinmeyi değiştir
C
cpython
Proje
Proje
Ayrıntılar
Etkinlik
Cycle Analytics
Depo (repository)
Depo (repository)
Dosyalar
Kayıtlar (commit)
Dallar (branch)
Etiketler
Katkıda bulunanlar
Grafik
Karşılaştır
Grafikler
Konular (issue)
0
Konular (issue)
0
Liste
Pano
Etiketler
Kilometre Taşları
Birleştirme (merge) Talepleri
0
Birleştirme (merge) Talepleri
0
CI / CD
CI / CD
İş akışları (pipeline)
İşler
Zamanlamalar
Grafikler
Paketler
Paketler
Wiki
Wiki
Parçacıklar
Parçacıklar
Üyeler
Üyeler
Collapse sidebar
Close sidebar
Etkinlik
Grafik
Grafikler
Yeni bir konu (issue) oluştur
İşler
Kayıtlar (commit)
Konu (issue) Panoları
Kenar çubuğunu aç
Batuhan Osman TASKAYA
cpython
Commits
877cf234
Kaydet (Commit)
877cf234
authored
Nis 24, 2006
tarafından
Nick Coghlan
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Change PEP 343 related documentation to use the term context specifier instead of context object
üst
c195d8a9
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
78 additions
and
67 deletions
+78
-67
libcontextlib.tex
Doc/lib/libcontextlib.tex
+19
-10
libstdtypes.tex
Doc/lib/libstdtypes.tex
+33
-29
ref3.tex
Doc/ref/ref3.tex
+21
-23
ref7.tex
Doc/ref/ref7.tex
+5
-5
No files found.
Doc/lib/libcontextlib.tex
Dosyayı görüntüle @
877cf234
...
@@ -46,12 +46,17 @@ after the block is exited. If an unhandled exception occurs in the
...
@@ -46,12 +46,17 @@ after the block is exited. If an unhandled exception occurs in the
block, it is reraised inside the generator at the point where the yield
block, it is reraised inside the generator at the point where the yield
occurred. Thus, you can use a
occurred. Thus, you can use a
\keyword
{
try
}
...
\keyword
{
except
}
...
\keyword
{
finally
}
statement to trap
\keyword
{
try
}
...
\keyword
{
except
}
...
\keyword
{
finally
}
statement to trap
the error (if any), or ensure that some cleanup takes place.
the error (if any), or ensure that some cleanup takes place. If an
exception is trapped merely in order to log it or to perform some
action (rather than to suppress it entirely), the generator must
reraise that exception. Otherwise the
\keyword
{
with
}
statement will
treat the exception as having been handled, and resume execution with
the statement immediately following the
\keyword
{
with
}
statement.
Note that you can use
\code
{
@contextmanager
}
to define a context
Note that you can use
\code
{
@contextmanager
}
to define a context
object's
\method
{__
context
__}
method. This is usually more convenient
specifier's
\method
{__
context
__}
method. This is usually more
than creating another class just to serve as a context manager.
convenient than creating another class just to serve as a context
For example:
manager.
For example:
\begin{verbatim}
\begin{verbatim}
from
__
future
__
import with
_
statement
from
__
future
__
import with
_
statement
...
@@ -78,7 +83,7 @@ hello from <__main__.Tag instance at 0x402ce8ec>
...
@@ -78,7 +83,7 @@ hello from <__main__.Tag instance at 0x402ce8ec>
\end{funcdesc}
\end{funcdesc}
\begin{funcdesc}
{
nested
}{
ctx1
\optional
{
, ctx2
\optional
{
, ...
}}}
\begin{funcdesc}
{
nested
}{
ctx1
\optional
{
, ctx2
\optional
{
, ...
}}}
Combine multiple context
manag
ers into a single nested context manager.
Combine multiple context
specifi
ers into a single nested context manager.
Code like this:
Code like this:
...
@@ -98,11 +103,15 @@ with A as X:
...
@@ -98,11 +103,15 @@ with A as X:
do
_
something()
do
_
something()
\end{verbatim}
\end{verbatim}
Note that if the
\method
{__
exit
__
()
}
method of one of the nested context managers
Note that if the
\method
{__
exit
__
()
}
method of one of the nested
raises an exception, any previous exception state will be lost; the new
context managers indicates an exception should be suppressed, no
exception will be passed to the
\method
{__
exit
__
()
}
methods of any remaining
exception information will be passed to any remaining outer context
outer context managers. In general,
\method
{__
exit
__
()
}
methods should avoid
managers. Similarly, if the
\method
{__
exit
__
()
}
method of one of the
raising exceptions, and in particular they should not re-raise a
nested context managers raises an exception, any previous exception
state will be lost; the new exception will be passed to the
\method
{__
exit
__
()
}
methods of any remaining outer context managers.
In general,
\method
{__
exit
__
()
}
methods should avoid raising
exceptions, and in particular they should not re-raise a
passed-in exception.
passed-in exception.
\end{funcdesc}
\end{funcdesc}
...
...
Doc/lib/libstdtypes.tex
Dosyayı görüntüle @
877cf234
...
@@ -1756,20 +1756,21 @@ implemented in C will have to provide a writable
...
@@ -1756,20 +1756,21 @@ implemented in C will have to provide a writable
\subsection
{
Context Types
\label
{
typecontext
}}
\subsection
{
Context Types
\label
{
typecontext
}}
\versionadded
{
2.5
}
\versionadded
{
2.5
}
\index
{
context protocol
}
\index
{
context
specification
protocol
}
\index
{
context management protocol
}
\index
{
context management protocol
}
\index
{
protocol!context
}
\index
{
protocol!context
specification
}
\index
{
protocol!context management
}
\index
{
protocol!context management
}
Python's
\keyword
{
with
}
statement supports the concept of a runtime
Python's
\keyword
{
with
}
statement supports the concept of a runtime
context defined by a context
object. This is implemented using three
context defined by a context
specifier. This is implemented using
distinct methods; these are used to allow user-defined classes to
three distinct methods; these are used to allow user-defined
define a context.
classes to
define a context.
The
\dfn
{
context protocol
}
consists of a single method that needs
The
\dfn
{
context specification protocol
}
consists of a single
to be provided for an object to define a runtime context:
method that needs to be provided for a context specifier object to
define a runtime context:
\begin{methoddesc}
[context]
{__
context
__}{}
\begin{methoddesc}
[context
specifier
]
{__
context
__}{}
Return a context manager object. The object is required to support
Return a context manager object. The object is required to support
the context management protocol described below. If an object
the context management protocol described below. If an object
supports different kinds of runtime context, additional methods can
supports different kinds of runtime context, additional methods can
...
@@ -1787,27 +1788,29 @@ following three methods, which together form the
...
@@ -1787,27 +1788,29 @@ following three methods, which together form the
\begin{methoddesc}
[context manager]
{__
context
__}{}
\begin{methoddesc}
[context manager]
{__
context
__}{}
Return the context manager object itself. This is required to
Return the context manager object itself. This is required to
allow both context
s and context managers to be used with the
allow both context
specifiers and context managers to be used with
\keyword
{
with
}
statement.
the
\keyword
{
with
}
statement.
\end{methoddesc}
\end{methoddesc}
\begin{methoddesc}
[context manager]
{__
enter
__}{}
\begin{methoddesc}
[context manager]
{__
enter
__}{}
Set up
the runtime context and return either the defining context
Enter
the runtime context and return either the defining context
object
or another object related to the runtime context. The value
specifier
or another object related to the runtime context. The value
returned by this method is bound to the identifier in the
returned by this method is bound to the identifier in the
\keyword
{
as
}
clause of
\keyword
{
with
}
statements using this context.
\keyword
{
as
}
clause of
\keyword
{
with
}
statements using this context.
(An example of a context with a context manager that returns the
(An example of a context with a context manager that returns the
original context
object
is file objects, which are returned from
original context
specifier
is file objects, which are returned from
__
enter
__
() to allow
\function
{
open()
}
to be used directly in a with
__
enter
__
() to allow
\function
{
open()
}
to be used directly in a with
statement. An example of a context manager that returns a related
statement. An example of a context manager that returns a related
object is
\code
{
decimal.Context
}
which returns a copy of the original
object is
\code
{
decimal.Context
}
which sets the active decimal
context to allow changes to be made to the current decimal context
context to a copy of the context specifier and then returns the copy
without affecting code outside the
\keyword
{
with
}
statement).
to allow changes to be made to the current decimal context in the
body of the
\keyword
{
with
}
statement) without affecting code outside
the
\keyword
{
with
}
statement).
\end{methoddesc}
\end{methoddesc}
\begin{methoddesc}
[context manager]
{__
exit
__}{
exc
_
type, exc
_
val, exc
_
tb
}
\begin{methoddesc}
[context manager]
{__
exit
__}{
exc
_
type, exc
_
val, exc
_
tb
}
Tear down the runtime context and return a Boolean flag indicating if
Exit the runtime context and return a Boolean flag indicating if any
an
expection that occurred should be suppressed. If an exception
expection that occurred should be suppressed. If an exception
occurred while executing the body of the
\keyword
{
with
}
statement, the
occurred while executing the body of the
\keyword
{
with
}
statement, the
arguments contain the exception type, value and traceback information.
arguments contain the exception type, value and traceback information.
Otherwise, all three arguments are
\var
{
None
}
.
Otherwise, all three arguments are
\var
{
None
}
.
...
@@ -1826,20 +1829,21 @@ following three methods, which together form the
...
@@ -1826,20 +1829,21 @@ following three methods, which together form the
\method
{__
exit
__
()
}
method has actually failed.
\method
{__
exit
__
()
}
method has actually failed.
\end{methoddesc}
\end{methoddesc}
Python defines several context objects to support easy thread
Python defines several context specifiers and managers to support
synchronisation, prompt closure of files or other objects, and
easy thread synchronisation, prompt closure of files or other
thread-safe manipulation of the decimal arithmetic context. The
objects, and thread-safe manipulation of the decimal arithmetic
specific types are not important beyond their implementation of
context. The specific types are not important beyond their
the context protocol.
implementation of the context specification and context
management protocols.
Python's generators and the
\code
{
contextlib.contextmanager
}
Python's generators and the
\code
{
contextlib.contextmanager
}
decorator provide a convenient way to implement the context
decorator provide a convenient way to implement the context
and context management protocols. If a context object's
specification and context management protocols. If a context
\method
{__
context
__
()
}
method is implemented as a generator decorated
specifier's
\method
{__
context
__
()
}
method is implemented as a
with the
\code
{
contextlib.contextmanager
}
decorator, it will
generator decorated with the
\code
{
contextlib.contextmanager
}
automatically return a context manager object supplying the
decorator, it will automatically return a context manager
necessary
\method
{__
context
__
()
}
,
\method
{__
enter
__
()
}
and
object supplying the necessary
\method
{__
context
__
()
}
,
\method
{__
exit
__
()
}
methods.
\method
{__
e
nter
__
()
}
and
\method
{__
e
xit
__
()
}
methods.
Note that there is no specific slot for any of these methods in the
Note that there is no specific slot for any of these methods in the
type structure for Python objects in the Python/C API. Extension
type structure for Python objects in the Python/C API. Extension
...
...
Doc/ref/ref3.tex
Dosyayı görüntüle @
877cf234
...
@@ -2112,47 +2112,45 @@ implement a \method{__coerce__()} method, for use by the built-in
...
@@ -2112,47 +2112,45 @@ implement a \method{__coerce__()} method, for use by the built-in
\end{itemize}
\end{itemize}
\subsection
{
Context
Managers and Context
s
\label
{
context-managers
}}
\subsection
{
Context
Specifiers and Manager
s
\label
{
context-managers
}}
\versionadded
{
2.5
}
\versionadded
{
2.5
}
A
\dfn
{
context
object
}
is an object that defines the runtime context
A
\dfn
{
context
specifier
}
is an object that defines the runtime
to be established when executing a
\keyword
{
with
}
statement. The
context to be established when executing a
\keyword
{
with
}
context object provides a
\dfn
{
context manager
}
which manages the
statement. The context specifier provides a
\dfn
{
context manager
}
entry into, and the exit from, the desired runtime context for the
which manages the entry into, and the exit from, the desired
execution of the block of code. Context managers are normally
runtime context for the execution of the block of code. Context
invoked using the
\keyword
{
with
}
statement (described in
managers are normally invoked using the
\keyword
{
with
}
statement
section~
\ref
{
with
}
), but can also be used by directly invoking
(described in section~
\ref
{
with
}
), but can also be used by
their methods.
directly invoking
their methods.
\stindex
{
with
}
\stindex
{
with
}
\index
{
context manager
}
\index
{
context manager
}
\index
{
context
object
}
\index
{
context
specifier
}
Typical uses of context
s and context
managers include saving and
Typical uses of context
specifiers and
managers include saving and
restoring various kinds of global state, locking and unlocking
restoring various kinds of global state, locking and unlocking
resources, closing opened files, etc.
resources, closing opened files, etc.
For more information on context
s and context manager objects, see
For more information on context
specifiers and context manager objects,
``
\ulink
{
Context Types
}{
../lib/typecontext.html
}
'' in the
see
``
\ulink
{
Context Types
}{
../lib/typecontext.html
}
'' in the
\citetitle
[../lib/lib.html]
{
Python Library Reference
}
.
\citetitle
[../lib/lib.html]
{
Python Library Reference
}
.
\begin{methoddesc}
[context]
{__
context
__}{
self
}
\begin{methoddesc}
[context
specifier
]
{__
context
__}{
self
}
Invoked when the object is used as the context expression of a
Invoked when the object is used as the context expression of a
\keyword
{
with
}
statement. The return value must implement
\keyword
{
with
}
statement. The returned object must implement
\method
{__
enter
__
()
}
and
\method
{__
exit
__
()
}
methods. Simple contexts
\method
{__
enter
__
()
}
and
\method
{__
exit
__
()
}
methods.
may be able to implement
\method
{__
enter
__
()
}
and
\method
{__
exit
__
()
}
directly without requiring a separate context manager object and
Context specifiers written in Python can also implement this method
should just return
\var
{
self
}
.
using a generator function decorated with the
Context objects written in Python can also implement this method using
a generator function decorated with the
\function
{
contextlib.contextmanager
}
decorator, as this can be simpler
\function
{
contextlib.contextmanager
}
decorator, as this can be simpler
than writing individual
\method
{__
enter
__
()
}
and
\method
{__
exit
__
()
}
than writing individual
\method
{__
enter
__
()
}
and
\method
{__
exit
__
()
}
methods on a separate object 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
Context manager objects also need to implement this method; they are
required to return themselves.
required to return themselves (that is, this method will simply
return
\var
{
self
}
).
\end{methoddesc}
\end{methoddesc}
\begin{methoddesc}
[context manager]
{__
enter
__}{
self
}
\begin{methoddesc}
[context manager]
{__
enter
__}{
self
}
...
...
Doc/ref/ref7.tex
Dosyayı görüntüle @
877cf234
...
@@ -315,10 +315,10 @@ statement to generate exceptions may be found in section~\ref{raise}.
...
@@ -315,10 +315,10 @@ statement to generate exceptions may be found in section~\ref{raise}.
\versionadded
{
2.5
}
\versionadded
{
2.5
}
The
\keyword
{
with
}
statement is used to wrap the execution of a block
The
\keyword
{
with
}
statement is used to wrap the execution of a block
with methods defined by a context manager (see
with methods defined by a context
specifier or
manager (see
section~
\ref
{
context-managers
}
). This allows common
section~
\ref
{
context-managers
}
). This allows common
\keyword
{
try
}
...
\keyword
{
except
}
...
\keyword
{
finally
}
usage patterns to
\keyword
{
try
}
...
\keyword
{
except
}
...
\keyword
{
finally
}
usage patterns to
be encapsulated as context managers for convenient reuse.
be encapsulated as context
specifiers or
managers for convenient reuse.
\begin{productionlist}
\begin{productionlist}
\production
{
with
_
stmt
}
\production
{
with
_
stmt
}
...
@@ -329,10 +329,10 @@ The execution of the \keyword{with} statement proceeds as follows:
...
@@ -329,10 +329,10 @@ The execution of the \keyword{with} statement proceeds as follows:
\begin{enumerate}
\begin{enumerate}
\item
The expression is evaluated, to obtain a context
object
.
\item
The expression is evaluated, to obtain a context
specifier
.
\item
The context
object's
\method
{__
context
__
()
}
method is invoked to
\item
The context
specifier's
\method
{__
context
__
()
}
method is
obtain a context manager object.
invoked to
obtain a context manager object.
\item
The context manager's
\method
{__
enter
__
()
}
method is invoked.
\item
The context manager's
\method
{__
enter
__
()
}
method is invoked.
...
...
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