Address some comments from Guido by cutting back on the overengineering in the terminology section. Also a few minor grammar cleanups and fixes I missed last night.

This commit is contained in:
Nick Coghlan 2006-05-03 09:41:53 +00:00
parent e55f539dc3
commit 10535b0182
1 changed files with 29 additions and 38 deletions

View File

@ -15,8 +15,8 @@ Abstract
it possible to factor out standard uses of try/finally statements. it possible to factor out standard uses of try/finally statements.
In this PEP, context managers provide __enter__() and __exit__() In this PEP, context managers provide __enter__() and __exit__()
methods that are invoked on entry to and exit from the managed methods that are invoked on entry to and exit from the body of the
context that forms the body of the with statement. with statement.
Author's Note Author's Note
@ -50,8 +50,8 @@ Introduction
[2] and universally approved of. I'm also changing the keyword to [2] and universally approved of. I'm also changing the keyword to
'with'. 'with'.
Following acceptance of this PEP, the following PEPs have been After acceptance of this PEP, the following PEPs were rejected due
rejected due to overlap: to overlap:
- PEP 310, Reliable Acquisition/Release Pairs. This is the - PEP 310, Reliable Acquisition/Release Pairs. This is the
original with-statement proposal. original with-statement proposal.
@ -469,37 +469,29 @@ Standard Terminology
and that objects that implement that protocol be known as "context and that objects that implement that protocol be known as "context
managers". [4] managers". [4]
The code in the body of the with statement is a "managed context".
This term refers primarily to the code location, rather than to the
runtime environment established by the context manager.
The expression immediately following the with keyword in the The expression immediately following the with keyword in the
statement is a "context expression" as that expression provides the statement is a "context expression" as that expression provides the
main clue as to the runtime environment the context manager main clue as to the runtime environment the context manager
establishes for the duration of the managed context. establishes for the duration of the statement body.
The value assigned to the target list after the as keyword is the The code in the body of the with statement and the variable name
"context entry value", as that value is returned as the result of (or names) after the as keyword don't really have special terms at
entering the context. this point in time. The general terms "statement body" and "target
list" can be used, prefixing with "with" or "with statement" if the
terms would otherwise be unclear.
These terms are based on the idea that the context expression Given the existence of objects such as the decimal module's
provides a context manager to appropriately handle entry into the arithmetic context, a term "context" is unfortunately ambiguous.
managed context. The context manager may also provide a meaningful If necessary, it can be made more specific by using the terms
context entry value and perform clean up operations on exit from "context manager" for the concrete object created by the context
the managed context. expression and "runtime context" or (preferably) "runtime
environment" for the actual state modifications made by the context
The general term "context" is unfortunately ambiguous. If necessary, manager. When simply discussing use of the with statement, the
it can be made more explicit by using the terms "context manager" ambiguity shouldn't matter too much as the context expression fully
for the concrete object created by the context expression, defines the changes made to the runtime environment.
"managed context" for the code in the body of the with statement, The distinction is more important when discussing the mechanics of
and "runtime context" or (preferebly) "runtime environment" for the the with statement itself and how to go about actually implementing
actual state modifications made by the context manager. When solely context managers.
discussing use of the with statement, the distinction between these
shouldn't matter too much as the context manager fully defines the
changes made to the runtime environment, and those changes apply for
the duration of the managed context. The distinction is more
important when discussing the process of implementing context
managers and the mechanics of the with statement itself.
Caching Context Managers Caching Context Managers
@ -531,7 +523,7 @@ Open Issues
1. Greg Ewing raised the question of whether or not the term 1. Greg Ewing raised the question of whether or not the term
"context manager" was too generic and suggested "context guard" "context manager" was too generic and suggested "context guard"
as an alternative name. as an alternative name. [16]
2. In Python 2.5a2, the decorator in contextlib to create a 2. In Python 2.5a2, the decorator in contextlib to create a
context manager from a generator function is called context manager from a generator function is called
@ -594,9 +586,9 @@ Rejected Options
Examples Examples
The generator based examples rely on PEP 342. Also, some of the The generator based examples rely on PEP 342. Also, some of the
examples are likely to be unnecessary in practice, as the examples are unnecessary in practice, as the appropriate objects,
appropriate objects, such as threading.RLock, will be able to be such as threading.RLock, are able to be used directly in with
used directly in with statements. statements.
The tense used in the names of the example contexts is not The tense used in the names of the example contexts is not
arbitrary. Past tense ("-ed") is used when the name refers to an arbitrary. Past tense ("-ed") is used when the name refers to an
@ -755,9 +747,8 @@ Examples
# so this must be outside the with-statement: # so this must be outside the with-statement:
return +s return +s
9. Here's a proposed context manager for the decimal module: 9. Here's a simple context manager for the decimal module:
# This would be a new decimal.Context method
@contextmanager @contextmanager
def localcontext(ctx=None): def localcontext(ctx=None):
"""Set a new local decimal context for the block""" """Set a new local decimal context for the block"""
@ -906,7 +897,7 @@ Reference Implementation
This PEP was first accepted by Guido at his EuroPython This PEP was first accepted by Guido at his EuroPython
keynote, 27 June 2005. keynote, 27 June 2005.
It was accepted again later, with the __context__ method added. It was accepted again later, with the __context__ method added.
The PEP was implemented in subversion for Python 2.5a1 The PEP was implemented in Subversion for Python 2.5a1
The __context__() method will be removed in Python 2.5a3 The __context__() method will be removed in Python 2.5a3
@ -970,7 +961,7 @@ References
[15] Guido kills the __context__() method [15] Guido kills the __context__() method
http://mail.python.org/pipermail/python-dev/2006-April/064632.html http://mail.python.org/pipermail/python-dev/2006-April/064632.html
[16] Greg propose 'context guard' instead of 'context manager' [16] Proposal to use 'context guard' instead of 'context manager'
http://mail.python.org/pipermail/python-dev/2006-May/064676.html http://mail.python.org/pipermail/python-dev/2006-May/064676.html
Copyright Copyright