diff options
author | Anthony Baxter <anthonybaxter@gmail.com> | 2004-08-02 06:10:11 (GMT) |
---|---|---|
committer | Anthony Baxter <anthonybaxter@gmail.com> | 2004-08-02 06:10:11 (GMT) |
commit | c2a5a636545a88f349dbe3e452ffb4494b68e534 (patch) | |
tree | aaa24074dcdcce5afa51523969971bdd05381b01 /Lib/test | |
parent | fd7dc5169c3ca7d64109512f38762c4ce9e96c5f (diff) | |
download | cpython-c2a5a636545a88f349dbe3e452ffb4494b68e534.zip cpython-c2a5a636545a88f349dbe3e452ffb4494b68e534.tar.gz cpython-c2a5a636545a88f349dbe3e452ffb4494b68e534.tar.bz2 |
PEP-0318, @decorator-style. In Guido's words:
"@ seems the syntax that everybody can hate equally"
Implementation by Mark Russell, from SF #979728.
Diffstat (limited to 'Lib/test')
-rw-r--r-- | Lib/test/output/test_tokenize | 13 | ||||
-rw-r--r-- | Lib/test/pyclbr_input.py | 33 | ||||
-rw-r--r-- | Lib/test/test_decorators.py | 194 | ||||
-rw-r--r-- | Lib/test/test_parser.py | 12 | ||||
-rw-r--r-- | Lib/test/test_pyclbr.py | 44 | ||||
-rw-r--r-- | Lib/test/tokenize_tests.txt | 3 |
6 files changed, 284 insertions, 15 deletions
diff --git a/Lib/test/output/test_tokenize b/Lib/test/output/test_tokenize index ea55181..b78a223 100644 --- a/Lib/test/output/test_tokenize +++ b/Lib/test/output/test_tokenize @@ -645,4 +645,15 @@ test_tokenize 174,29-174,30: OP ')' 174,30-174,31: NEWLINE '\n' 175,0-175,1: NL '\n' -176,0-176,0: ENDMARKER '' +176,0-176,1: OP '@' +176,1-176,13: NAME 'staticmethod' +176,13-176,14: NEWLINE '\n' +177,0-177,3: NAME 'def' +177,4-177,7: NAME 'foo' +177,7-177,8: OP '(' +177,8-177,9: OP ')' +177,9-177,10: OP ':' +177,11-177,15: NAME 'pass' +177,15-177,16: NEWLINE '\n' +178,0-178,1: NL '\n' +179,0-179,0: ENDMARKER '' diff --git a/Lib/test/pyclbr_input.py b/Lib/test/pyclbr_input.py new file mode 100644 index 0000000..b410fcc --- /dev/null +++ b/Lib/test/pyclbr_input.py @@ -0,0 +1,33 @@ +"""Test cases for test_pyclbr.py""" + +def f(): pass + +class Other(object): + @classmethod + def foo(c): pass + + def om(self): pass + +class B (object): + def bm(self): pass + +class C (B): + foo = Other().foo + om = Other.om + + d = 10 + + # XXX: This causes test_pyclbr.py to fail, but only because the + # introspection-based is_method() code in the test can't + # distinguish between this and a geniune method function like m(). + # The pyclbr.py module gets this right as it parses the text. + # + #f = f + + def m(self): pass + + @staticmethod + def sm(self): pass + + @classmethod + def cm(self): pass diff --git a/Lib/test/test_decorators.py b/Lib/test/test_decorators.py new file mode 100644 index 0000000..98d5d3e --- /dev/null +++ b/Lib/test/test_decorators.py @@ -0,0 +1,194 @@ +import unittest +from test import test_support + +def funcattrs(**kwds): + def decorate(func): + func.__dict__.update(kwds) + return func + return decorate + +class MiscDecorators (object): + @staticmethod + def author(name): + def decorate(func): + func.__dict__['author'] = name + return func + return decorate + +# ----------------------------------------------- + +class DbcheckError (Exception): + def __init__(self, exprstr, func, args, kwds): + # A real version of this would set attributes here + Exception.__init__(self, "dbcheck %r failed (func=%s args=%s kwds=%s)" % + (exprstr, func, args, kwds)) + + +def dbcheck(exprstr, globals=None, locals=None): + "Decorator to implement debugging assertions" + def decorate(func): + expr = compile(exprstr, "dbcheck-%s" % func.func_name, "eval") + def check(*args, **kwds): + if not eval(expr, globals, locals): + raise DbcheckError(exprstr, func, args, kwds) + return func(*args, **kwds) + return check + return decorate + +# ----------------------------------------------- + +def countcalls(counts): + "Decorator to count calls to a function" + def decorate(func): + name = func.func_name + counts[name] = 0 + def call(*args, **kwds): + counts[name] += 1 + return func(*args, **kwds) + # XXX: Would like to say: call.func_name = func.func_name here + # to make nested decorators work in any order, but func_name + # is a readonly attribute + return call + return decorate + +# ----------------------------------------------- + +def memoize(func): + saved = {} + def call(*args): + try: + return saved[args] + except KeyError: + res = func(*args) + saved[args] = res + return res + except TypeError: + # Unhashable argument + return func(*args) + return call + +# ----------------------------------------------- + +class TestDecorators(unittest.TestCase): + + def test_single(self): + class C(object): + @staticmethod + def foo(): return 42 + self.assertEqual(C.foo(), 42) + self.assertEqual(C().foo(), 42) + + def test_dotted(self): + decorators = MiscDecorators() + @decorators.author('Cleese') + def foo(): return 42 + self.assertEqual(foo(), 42) + self.assertEqual(foo.author, 'Cleese') + + def test_argforms(self): + # A few tests of argument passing, as we use restricted form + # of expressions for decorators. + + def noteargs(*args, **kwds): + def decorate(func): + setattr(func, 'dbval', (args, kwds)) + return func + return decorate + + args = ( 'Now', 'is', 'the', 'time' ) + kwds = dict(one=1, two=2) + @noteargs(*args, **kwds) + def f1(): return 42 + self.assertEqual(f1(), 42) + self.assertEqual(f1.dbval, (args, kwds)) + + @noteargs('terry', 'gilliam', eric='idle', john='cleese') + def f2(): return 84 + self.assertEqual(f2(), 84) + self.assertEqual(f2.dbval, (('terry', 'gilliam'), + dict(eric='idle', john='cleese'))) + + @noteargs(1, 2,) + def f3(): pass + self.assertEqual(f3.dbval, ((1, 2), {})) + + def test_dbcheck(self): + @dbcheck('args[1] is not None') + def f(a, b): + return a + b + self.assertEqual(f(1, 2), 3) + self.assertRaises(DbcheckError, f, 1, None) + + def test_memoize(self): + # XXX: This doesn't work unless memoize is the last decorator - + # see the comment in countcalls. + counts = {} + @countcalls(counts) @memoize + def double(x): + return x * 2 + + self.assertEqual(counts, dict(double=0)) + + # Only the first call with a given argument bumps the call count: + # + self.assertEqual(double(2), 4) + self.assertEqual(counts['double'], 1) + self.assertEqual(double(2), 4) + self.assertEqual(counts['double'], 1) + self.assertEqual(double(3), 6) + self.assertEqual(counts['double'], 2) + + # Unhashable arguments do not get memoized: + # + self.assertEqual(double([10]), [10, 10]) + self.assertEqual(counts['double'], 3) + self.assertEqual(double([10]), [10, 10]) + self.assertEqual(counts['double'], 4) + + def test_errors(self): + # Test syntax restrictions - these are all compile-time errors: + # + for expr in [ "1+2", "x[3]", "(1, 2)" ]: + # Sanity check: is expr is a valid expression by itself? + compile(expr, "testexpr", "exec") + + codestr = "@%s\ndef f(): pass" % expr + self.assertRaises(SyntaxError, compile, codestr, "test", "exec") + + # Test runtime errors + + def unimp(func): + raise NotImplementedError + context = dict(nullval=None, unimp=unimp) + + for expr, exc in [ ("undef", NameError), + ("nullval", TypeError), + ("nullval.attr", AttributeError), + ("unimp", NotImplementedError)]: + codestr = "@%s\ndef f(): pass\nassert f() is None" % expr + code = compile(codestr, "test", "exec") + self.assertRaises(exc, eval, code, context) + + def test_double(self): + class C(object): + @funcattrs(abc=1, xyz="haha") + @funcattrs(booh=42) + def foo(self): return 42 + self.assertEqual(C().foo(), 42) + self.assertEqual(C.foo.abc, 1) + self.assertEqual(C.foo.xyz, "haha") + self.assertEqual(C.foo.booh, 42) + + def test_order(self): + class C(object): + @funcattrs(abc=1) @staticmethod + def foo(): return 42 + # This wouldn't work if staticmethod was called first + self.assertEqual(C.foo(), 42) + self.assertEqual(C().foo(), 42) + +def test_main(): + test_support.run_unittest(TestDecorators) + +if __name__=="__main__": + test_main() diff --git a/Lib/test/test_parser.py b/Lib/test/test_parser.py index 9652f6b..978ce57 100644 --- a/Lib/test/test_parser.py +++ b/Lib/test/test_parser.py @@ -15,8 +15,8 @@ class RoundtripLegalSyntaxTestCase(unittest.TestCase): t = st1.totuple() try: st2 = parser.sequence2st(t) - except parser.ParserError: - self.fail("could not roundtrip %r" % s) + except parser.ParserError, why: + self.fail("could not roundtrip %r: %s" % (s, why)) self.assertEquals(t, st2.totuple(), "could not re-generate syntax tree") @@ -119,6 +119,14 @@ class RoundtripLegalSyntaxTestCase(unittest.TestCase): self.check_suite("def f(a, b, foo=bar, *args, **kw): pass") self.check_suite("def f(a, b, foo=bar, **kw): pass") + self.check_suite("@staticmethod\n" + "def f(): pass") + self.check_suite("@staticmethod\n" + "@funcattrs(x, y)\n" + "def f(): pass") + self.check_suite("@funcattrs()\n" + "def f(): pass") + def test_import_from_statement(self): self.check_suite("from sys.path import *") self.check_suite("from sys.path import dirname") diff --git a/Lib/test/test_pyclbr.py b/Lib/test/test_pyclbr.py index 48e8bf7..eddd593 100644 --- a/Lib/test/test_pyclbr.py +++ b/Lib/test/test_pyclbr.py @@ -8,6 +8,9 @@ from types import ClassType, FunctionType, MethodType import pyclbr from unittest import TestCase +StaticMethodType = type(staticmethod(lambda: None)) +ClassMethodType = type(classmethod(lambda c: None)) + # This next line triggers an error on old versions of pyclbr. from commands import getstatus @@ -43,11 +46,10 @@ class PyclbrTest(TestCase): print >>sys.stderr, "***",key self.failUnless(obj.has_key(key)) - def assertEquals(self, a, b, ignore=None): + def assertEqualsOrIgnored(self, a, b, ignore): ''' succeed iff a == b or a in ignore or b in ignore ''' - if (ignore == None) or (a in ignore) or (b in ignore): return - - unittest.TestCase.assertEquals(self, a, b) + if a not in ignore and b not in ignore: + self.assertEquals(a, b) def checkModule(self, moduleName, module=None, ignore=()): ''' succeed iff pyclbr.readmodule_ex(modulename) corresponds @@ -62,11 +64,22 @@ class PyclbrTest(TestCase): dict = pyclbr.readmodule_ex(moduleName) - def ismethod(obj, name): - if not isinstance(obj, MethodType): - return False - if obj.im_self is not None: - return False + def ismethod(oclass, obj, name): + classdict = oclass.__dict__ + if isinstance(obj, FunctionType): + if not isinstance(classdict[name], StaticMethodType): + return False + else: + if not isinstance(obj, MethodType): + return False + if obj.im_self is not None: + if (not isinstance(classdict[name], ClassMethodType) or + obj.im_self is not oclass): + return False + else: + if not isinstance(classdict[name], FunctionType): + return False + objname = obj.__name__ if objname.startswith("__") and not objname.endswith("__"): objname = "_%s%s" % (obj.im_class.__name__, objname) @@ -81,7 +94,7 @@ class PyclbrTest(TestCase): if isinstance(value, pyclbr.Function): self.assertEquals(type(py_item), FunctionType) else: - self.assertEquals(type(py_item), ClassType) + self.failUnless(isinstance(py_item, (ClassType, type))) real_bases = [base.__name__ for base in py_item.__bases__] pyclbr_bases = [ getattr(base, 'name', base) for base in value.super ] @@ -94,7 +107,7 @@ class PyclbrTest(TestCase): actualMethods = [] for m in py_item.__dict__.keys(): - if ismethod(getattr(py_item, m), m): + if ismethod(py_item, getattr(py_item, m), m): actualMethods.append(m) foundMethods = [] for m in value.methods.keys(): @@ -107,7 +120,8 @@ class PyclbrTest(TestCase): self.assertListEq(foundMethods, actualMethods, ignore) self.assertEquals(py_item.__module__, value.module) - self.assertEquals(py_item.__name__, value.name, ignore) + self.assertEqualsOrIgnored(py_item.__name__, value.name, + ignore) # can't check file or lineno except: print >>sys.stderr, "class=%s" % py_item @@ -132,6 +146,12 @@ class PyclbrTest(TestCase): self.checkModule('rfc822') self.checkModule('difflib') + def test_decorators(self): + # XXX: See comment in pyclbr_input.py for a test that would fail + # if it were not commented out. + # + self.checkModule('test.pyclbr_input') + def test_others(self): cm = self.checkModule diff --git a/Lib/test/tokenize_tests.txt b/Lib/test/tokenize_tests.txt index e990a36..4ef3bf1 100644 --- a/Lib/test/tokenize_tests.txt +++ b/Lib/test/tokenize_tests.txt @@ -173,3 +173,6 @@ x = -1*1/1 + 1*1 - ---1*1 import sys, time x = sys.modules['time'].time() +@staticmethod +def foo(): pass + |