summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>2006-05-02 19:47:52 (GMT)
committerGuido van Rossum <guido@python.org>2006-05-02 19:47:52 (GMT)
commitda5b701aeef755f2317a41e36cc950cfdc0c95cb (patch)
tree11a37e4e8714ffbccce921aa0ef1878b7dc779b2 /Lib
parent8f6cbe150228f175b57b7a774d0a630febe72244 (diff)
downloadcpython-da5b701aeef755f2317a41e36cc950cfdc0c95cb.zip
cpython-da5b701aeef755f2317a41e36cc950cfdc0c95cb.tar.gz
cpython-da5b701aeef755f2317a41e36cc950cfdc0c95cb.tar.bz2
Get rid of __context__, per the latest changes to PEP 343 and python-dev
discussion. There are two places of documentation that still mention __context__: Doc/lib/libstdtypes.tex -- I wasn't quite sure how to rewrite that without spending a whole lot of time thinking about it; and whatsnew, which Andrew usually likes to change himself.
Diffstat (limited to 'Lib')
-rw-r--r--Lib/calendar.py3
-rw-r--r--Lib/compiler/pycodegen.py2
-rw-r--r--Lib/contextlib.py10
-rw-r--r--Lib/decimal.py2
-rw-r--r--Lib/dummy_thread.py3
-rw-r--r--Lib/test/test_contextlib.py10
-rw-r--r--Lib/test/test_with.py81
-rw-r--r--Lib/threading.py13
8 files changed, 19 insertions, 105 deletions
diff --git a/Lib/calendar.py b/Lib/calendar.py
index 7800aae..00948ef 100644
--- a/Lib/calendar.py
+++ b/Lib/calendar.py
@@ -484,9 +484,6 @@ class TimeEncoding:
def __init__(self, locale):
self.locale = locale
- def __context__(self):
- return self
-
def __enter__(self):
self.oldlocale = locale.setlocale(locale.LC_TIME, self.locale)
return locale.getlocale(locale.LC_TIME)[1]
diff --git a/Lib/compiler/pycodegen.py b/Lib/compiler/pycodegen.py
index f25b3fb..d5d68aa 100644
--- a/Lib/compiler/pycodegen.py
+++ b/Lib/compiler/pycodegen.py
@@ -833,8 +833,6 @@ class CodeGenerator:
self.__with_count += 1
self.set_lineno(node)
self.visit(node.expr)
- self.emit('LOAD_ATTR', '__context__')
- self.emit('CALL_FUNCTION', 0)
self.emit('DUP_TOP')
self.emit('LOAD_ATTR', '__exit__')
self._implicitNameOp('STORE', exitvar)
diff --git a/Lib/contextlib.py b/Lib/contextlib.py
index 4e3b9c2..9d2c6a3 100644
--- a/Lib/contextlib.py
+++ b/Lib/contextlib.py
@@ -10,9 +10,6 @@ class GeneratorContext(object):
def __init__(self, gen):
self.gen = gen
- def __context__(self):
- return self
-
def __enter__(self):
try:
return self.gen.next()
@@ -88,7 +85,7 @@ def contextfactory(func):
@contextfactory
-def nested(*contexts):
+def nested(*managers):
"""Support multiple context managers in a single with-statement.
Code like this:
@@ -109,8 +106,7 @@ def nested(*contexts):
exc = (None, None, None)
try:
try:
- for context in contexts:
- mgr = context.__context__()
+ for mgr in managers:
exit = mgr.__exit__
enter = mgr.__enter__
vars.append(enter())
@@ -152,8 +148,6 @@ class closing(object):
"""
def __init__(self, thing):
self.thing = thing
- def __context__(self):
- return self
def __enter__(self):
return self.thing
def __exit__(self, *exc_info):
diff --git a/Lib/decimal.py b/Lib/decimal.py
index 875e38a..2f989a8 100644
--- a/Lib/decimal.py
+++ b/Lib/decimal.py
@@ -2248,7 +2248,7 @@ class Context(object):
s.append('traps=[' + ', '.join([t.__name__ for t, v in self.traps.items() if v]) + ']')
return ', '.join(s) + ')'
- def __context__(self):
+ def context_manager(self):
return WithStatementContext(self.copy())
def clear_flags(self):
diff --git a/Lib/dummy_thread.py b/Lib/dummy_thread.py
index d69d840..21fd03f 100644
--- a/Lib/dummy_thread.py
+++ b/Lib/dummy_thread.py
@@ -118,9 +118,6 @@ class LockType(object):
def __exit__(self, typ, val, tb):
self.release()
- def __context__(self):
- return self
-
def release(self):
"""Release the dummy lock."""
# XXX Perhaps shouldn't actually bother to test? Could lead
diff --git a/Lib/test/test_contextlib.py b/Lib/test/test_contextlib.py
index 53f23b2..8d3dd1a 100644
--- a/Lib/test/test_contextlib.py
+++ b/Lib/test/test_contextlib.py
@@ -51,7 +51,7 @@ class ContextManagerTestCase(unittest.TestCase):
@contextfactory
def whee():
yield
- ctx = whee().__context__()
+ ctx = whee()
ctx.__enter__()
# Calling __exit__ should not result in an exception
self.failIf(ctx.__exit__(TypeError, TypeError("foo"), None))
@@ -63,7 +63,7 @@ class ContextManagerTestCase(unittest.TestCase):
yield
except:
yield
- ctx = whoo().__context__()
+ ctx = whoo()
ctx.__enter__()
self.assertRaises(
RuntimeError, ctx.__exit__, TypeError, TypeError("foo"), None
@@ -152,8 +152,6 @@ class NestedTestCase(unittest.TestCase):
def a():
yield 1
class b(object):
- def __context__(self):
- return self
def __enter__(self):
return 2
def __exit__(self, *exc_info):
@@ -341,12 +339,12 @@ class DecimalContextTestCase(unittest.TestCase):
orig_context = ctx.copy()
try:
ctx.prec = save_prec = decimal.ExtendedContext.prec + 5
- with decimal.ExtendedContext:
+ with decimal.ExtendedContext.context_manager():
self.assertEqual(decimal.getcontext().prec,
decimal.ExtendedContext.prec)
self.assertEqual(decimal.getcontext().prec, save_prec)
try:
- with decimal.ExtendedContext:
+ with decimal.ExtendedContext.context_manager():
self.assertEqual(decimal.getcontext().prec,
decimal.ExtendedContext.prec)
1/0
diff --git a/Lib/test/test_with.py b/Lib/test/test_with.py
index 7adb05e..765bfec 100644
--- a/Lib/test/test_with.py
+++ b/Lib/test/test_with.py
@@ -17,15 +17,10 @@ from test.test_support import run_unittest
class MockContextManager(GeneratorContext):
def __init__(self, gen):
GeneratorContext.__init__(self, gen)
- self.context_called = False
self.enter_called = False
self.exit_called = False
self.exit_args = None
- def __context__(self):
- self.context_called = True
- return GeneratorContext.__context__(self)
-
def __enter__(self):
self.enter_called = True
return GeneratorContext.__enter__(self)
@@ -60,21 +55,17 @@ def mock_contextmanager_generator():
class Nested(object):
- def __init__(self, *contexts):
- self.contexts = contexts
+ def __init__(self, *managers):
+ self.managers = managers
self.entered = None
- def __context__(self):
- return self
-
def __enter__(self):
if self.entered is not None:
raise RuntimeError("Context is not reentrant")
self.entered = deque()
vars = []
try:
- for context in self.contexts:
- mgr = context.__context__()
+ for mgr in self.managers:
vars.append(mgr.__enter__())
self.entered.appendleft(mgr)
except:
@@ -99,17 +90,12 @@ class Nested(object):
class MockNested(Nested):
- def __init__(self, *contexts):
- Nested.__init__(self, *contexts)
- self.context_called = False
+ def __init__(self, *managers):
+ Nested.__init__(self, *managers)
self.enter_called = False
self.exit_called = False
self.exit_args = None
- def __context__(self):
- self.context_called = True
- return Nested.__context__(self)
-
def __enter__(self):
self.enter_called = True
return Nested.__enter__(self)
@@ -126,24 +112,8 @@ class FailureTestCase(unittest.TestCase):
with foo: pass
self.assertRaises(NameError, fooNotDeclared)
- def testContextAttributeError(self):
- class LacksContext(object):
- def __enter__(self):
- pass
-
- def __exit__(self, type, value, traceback):
- pass
-
- def fooLacksContext():
- foo = LacksContext()
- with foo: pass
- self.assertRaises(AttributeError, fooLacksContext)
-
def testEnterAttributeError(self):
class LacksEnter(object):
- def __context__(self):
- pass
-
def __exit__(self, type, value, traceback):
pass
@@ -154,9 +124,6 @@ class FailureTestCase(unittest.TestCase):
def testExitAttributeError(self):
class LacksExit(object):
- def __context__(self):
- pass
-
def __enter__(self):
pass
@@ -192,27 +159,10 @@ class FailureTestCase(unittest.TestCase):
'with mock as (foo, None, bar):\n'
' pass')
- def testContextThrows(self):
- class ContextThrows(object):
- def __context__(self):
- raise RuntimeError("Context threw")
-
- def shouldThrow():
- ct = ContextThrows()
- self.foo = None
- with ct as self.foo:
- pass
- self.assertRaises(RuntimeError, shouldThrow)
- self.assertEqual(self.foo, None)
-
def testEnterThrows(self):
class EnterThrows(object):
- def __context__(self):
- return self
-
def __enter__(self):
- raise RuntimeError("Context threw")
-
+ raise RuntimeError("Enter threw")
def __exit__(self, *args):
pass
@@ -226,8 +176,6 @@ class FailureTestCase(unittest.TestCase):
def testExitThrows(self):
class ExitThrows(object):
- def __context__(self):
- return self
def __enter__(self):
return
def __exit__(self, *args):
@@ -241,13 +189,11 @@ class ContextmanagerAssertionMixin(object):
TEST_EXCEPTION = RuntimeError("test exception")
def assertInWithManagerInvariants(self, mock_manager):
- self.assertTrue(mock_manager.context_called)
self.assertTrue(mock_manager.enter_called)
self.assertFalse(mock_manager.exit_called)
self.assertEqual(mock_manager.exit_args, None)
def assertAfterWithManagerInvariants(self, mock_manager, exit_args):
- self.assertTrue(mock_manager.context_called)
self.assertTrue(mock_manager.enter_called)
self.assertTrue(mock_manager.exit_called)
self.assertEqual(mock_manager.exit_args, exit_args)
@@ -268,7 +214,6 @@ class ContextmanagerAssertionMixin(object):
raise self.TEST_EXCEPTION
def assertAfterWithManagerInvariantsWithError(self, mock_manager):
- self.assertTrue(mock_manager.context_called)
self.assertTrue(mock_manager.enter_called)
self.assertTrue(mock_manager.exit_called)
self.assertEqual(mock_manager.exit_args[0], RuntimeError)
@@ -472,7 +417,6 @@ class ExceptionalTestCase(unittest.TestCase, ContextmanagerAssertionMixin):
# The inner statement stuff should never have been touched
self.assertEqual(self.bar, None)
- self.assertFalse(mock_b.context_called)
self.assertFalse(mock_b.enter_called)
self.assertFalse(mock_b.exit_called)
self.assertEqual(mock_b.exit_args, None)
@@ -506,13 +450,9 @@ class ExceptionalTestCase(unittest.TestCase, ContextmanagerAssertionMixin):
self.assertRaises(StopIteration, shouldThrow)
def testRaisedStopIteration2(self):
- class cm (object):
- def __context__(self):
- return self
-
+ class cm(object):
def __enter__(self):
pass
-
def __exit__(self, type, value, traceback):
pass
@@ -535,12 +475,8 @@ class ExceptionalTestCase(unittest.TestCase, ContextmanagerAssertionMixin):
def testRaisedGeneratorExit2(self):
class cm (object):
- def __context__(self):
- return self
-
def __enter__(self):
pass
-
def __exit__(self, type, value, traceback):
pass
@@ -629,7 +565,6 @@ class AssignmentTargetTestCase(unittest.TestCase):
def testMultipleComplexTargets(self):
class C:
- def __context__(self): return self
def __enter__(self): return 1, 2, 3
def __exit__(self, t, v, tb): pass
targets = {1: [0, 1, 2]}
@@ -651,7 +586,6 @@ class ExitSwallowsExceptionTestCase(unittest.TestCase):
def testExitTrueSwallowsException(self):
class AfricanSwallow:
- def __context__(self): return self
def __enter__(self): pass
def __exit__(self, t, v, tb): return True
try:
@@ -662,7 +596,6 @@ class ExitSwallowsExceptionTestCase(unittest.TestCase):
def testExitFalseDoesntSwallowException(self):
class EuropeanSwallow:
- def __context__(self): return self
def __enter__(self): pass
def __exit__(self, t, v, tb): return False
try:
diff --git a/Lib/threading.py b/Lib/threading.py
index 27ec6b4..c27140d 100644
--- a/Lib/threading.py
+++ b/Lib/threading.py
@@ -90,9 +90,6 @@ class _RLock(_Verbose):
self.__owner and self.__owner.getName(),
self.__count)
- def __context__(self):
- return self
-
def acquire(self, blocking=1):
me = currentThread()
if self.__owner is me:
@@ -182,8 +179,11 @@ class _Condition(_Verbose):
pass
self.__waiters = []
- def __context__(self):
- return self.__lock.__context__()
+ def __enter__(self):
+ return self.__lock.__enter__()
+
+ def __exit__(self, *args):
+ return self.__lock.__exit__(*args)
def __repr__(self):
return "<Condition(%s, %d)>" % (self.__lock, len(self.__waiters))
@@ -278,9 +278,6 @@ class _Semaphore(_Verbose):
self.__cond = Condition(Lock())
self.__value = value
- def __context__(self):
- return self
-
def acquire(self, blocking=1):
rc = False
self.__cond.acquire()