Staging
v0.5.1
v0.5.1
Revision 7167b00542cc1af288c3f09565bb41c436c4d77b authored by Georg Brandl on 03 January 2009, 23:25:33 UTC, committed by Georg Brandl on 03 January 2009, 23:25:33 UTC
svn+ssh://svn.python.org/python/branches/py3k ........ r67653 | neal.norwitz | 2008-12-07 23:42:03 +0100 (Sun, 07 Dec 2008) | 1 line Remove unnecessary import ........ r67655 | georg.brandl | 2008-12-07 23:45:56 +0100 (Sun, 07 Dec 2008) | 2 lines #4586: fix usage of Py_InitModule. ........ r67682 | georg.brandl | 2008-12-10 00:48:44 +0100 (Wed, 10 Dec 2008) | 1 line #4592: fix embedding example with new C API changes. ........ r67683 | fred.drake | 2008-12-10 07:02:39 +0100 (Wed, 10 Dec 2008) | 2 lines simplify imports ........ r67724 | benjamin.peterson | 2008-12-13 04:03:41 +0100 (Sat, 13 Dec 2008) | 1 line string.maketrans -> str.maketrans ........ r67755 | benjamin.peterson | 2008-12-14 16:09:34 +0100 (Sun, 14 Dec 2008) | 1 line tip-toe around dictionary keys view in the tutorial ........ r67780 | jeremy.hylton | 2008-12-15 04:00:50 +0100 (Mon, 15 Dec 2008) | 2 lines Use True/False for ints instead of 1/0. That's so Python 2.0. ........ r67781 | jeremy.hylton | 2008-12-15 04:08:30 +0100 (Mon, 15 Dec 2008) | 2 lines Reflow long line. ........ r67782 | georg.brandl | 2008-12-15 09:28:37 +0100 (Mon, 15 Dec 2008) | 2 lines #4667: fix some 2.x leftovers in the tutorial. ........ r67783 | georg.brandl | 2008-12-15 09:29:32 +0100 (Mon, 15 Dec 2008) | 2 lines #4668: wrap iterator returns in list() in functional howto. ........ r67786 | georg.brandl | 2008-12-15 09:43:10 +0100 (Mon, 15 Dec 2008) | 2 lines #4603: Note that inconsistent tab/space use is now illegal. ........ r67789 | georg.brandl | 2008-12-15 10:16:15 +0100 (Mon, 15 Dec 2008) | 2 lines Use :samp: role. ........ r67841 | kristjan.jonsson | 2008-12-18 18:08:57 +0100 (Thu, 18 Dec 2008) | 2 lines Add missing Py_CHARMASK when calling isspace(). Found by enabling runtime tests on windows, by disabling the _set_invalid_parameter_handler() fiddling. ........ r67843 | kristjan.jonsson | 2008-12-18 18:15:54 +0100 (Thu, 18 Dec 2008) | 5 lines Fix an issue in the tokenizer, where a file is opened by fd, but the underlying PyFileIO object wasn created with the closefd attribute true. Also fix error handling for close() int _fileio.c . It was incorrect, looking for a negative refcount, and so errors weren't raised. This is why this issue wasn't caught. There is a second reason why it isn't seen: Class IOBase in io.py has a try:/except: around the close() funtion in the __del__() method. This also masks these error conditions. This issue was discovered by removing the _set_invalid_parameter_handler() fiddling, thus enabling the C runtime checks on windows. ........ r67865 | benjamin.peterson | 2008-12-20 04:20:23 +0100 (Sat, 20 Dec 2008) | 1 line fix syntax ........ r67881 | benjamin.peterson | 2008-12-20 23:50:25 +0100 (Sat, 20 Dec 2008) | 1 line unpy3kize this; it require 2.x only Mac modules ........ r67884 | benjamin.peterson | 2008-12-21 00:06:29 +0100 (Sun, 21 Dec 2008) | 1 line don't build bsddb anymore ........ r67925 | benjamin.peterson | 2008-12-24 17:27:25 +0100 (Wed, 24 Dec 2008) | 1 line return the module object from PyMODINIT_FUNC ........ r67929 | benjamin.peterson | 2008-12-27 03:58:34 +0100 (Sat, 27 Dec 2008) | 1 line string -> bytes in error message #4745 ........ r67931 | hirokazu.yamamoto | 2008-12-27 05:21:44 +0100 (Sat, 27 Dec 2008) | 2 lines Issue #4740: Use HIGHEST_PROTOCOL in pickle test. This enables test for protocol 3 (== HIGHEST_PROTOCOL in 3.x) ........
1 parent 99113b7
contextlib.py
"""Utilities for with-statement contexts. See PEP 343."""
import sys
from functools import wraps
__all__ = ["contextmanager", "nested", "closing"]
class GeneratorContextManager(object):
"""Helper for @contextmanager decorator."""
def __init__(self, gen):
self.gen = gen
def __enter__(self):
try:
return next(self.gen)
except StopIteration:
raise RuntimeError("generator didn't yield")
def __exit__(self, type, value, traceback):
if type is None:
try:
next(self.gen)
except StopIteration:
return
else:
raise RuntimeError("generator didn't stop")
else:
if value is None:
# Need to force instantiation so we can reliably
# tell if we get the same exception back
value = type()
try:
self.gen.throw(type, value, traceback)
raise RuntimeError("generator didn't stop after throw()")
except StopIteration as exc:
# Suppress the exception *unless* it's the same exception that
# was passed to throw(). This prevents a StopIteration
# raised inside the "with" statement from being suppressed
return exc is not value
except:
# only re-raise if it's *not* the exception that was
# passed to throw(), because __exit__() must not raise
# an exception unless __exit__() itself failed. But throw()
# has to raise the exception to signal propagation, so this
# fixes the impedance mismatch between the throw() protocol
# and the __exit__() protocol.
#
if sys.exc_info()[1] is not value:
raise
def contextmanager(func):
"""@contextmanager decorator.
Typical usage:
@contextmanager
def some_generator(<arguments>):
<setup>
try:
yield <value>
finally:
<cleanup>
This makes this:
with some_generator(<arguments>) as <variable>:
<body>
equivalent to this:
<setup>
try:
<variable> = <value>
<body>
finally:
<cleanup>
"""
@wraps(func)
def helper(*args, **kwds):
return GeneratorContextManager(func(*args, **kwds))
return helper
@contextmanager
def nested(*managers):
"""Support multiple context managers in a single with-statement.
Code like this:
with nested(A, B, C) as (X, Y, Z):
<body>
is equivalent to this:
with A as X:
with B as Y:
with C as Z:
<body>
"""
exits = []
vars = []
exc = (None, None, None)
try:
for mgr in managers:
exit = mgr.__exit__
enter = mgr.__enter__
vars.append(enter())
exits.append(exit)
yield vars
except:
exc = sys.exc_info()
finally:
while exits:
exit = exits.pop()
try:
if exit(*exc):
exc = (None, None, None)
except:
exc = sys.exc_info()
if exc != (None, None, None):
# Don't rely on sys.exc_info() still containing
# the right information. Another exception may
# have been raised and caught by an exit method
# exc[1] already has the __traceback__ attribute populated
raise exc[1]
class closing(object):
"""Context to automatically close something at the end of a block.
Code like this:
with closing(<module>.open(<arguments>)) as f:
<block>
is equivalent to this:
f = <module>.open(<arguments>)
try:
<block>
finally:
f.close()
"""
def __init__(self, thing):
self.thing = thing
def __enter__(self):
return self.thing
def __exit__(self, *exc_info):
self.thing.close()
![swh spinner](/static/img/swh-spinner.gif)
Computing file changes ...