[Python-checkins] r58028 - sandbox/trunk/pep362/pep362.py sandbox/trunk/pep362/pep362_fodder.py sandbox/trunk/pep362/pep362_py3k_fodder.py sandbox/trunk/pep362/test_pep362.py
brett.cannon
python-checkins at python.org
Fri Sep 7 04:26:50 CEST 2007
Author: brett.cannon
Date: Fri Sep 7 04:26:50 2007
New Revision: 58028
Modified:
sandbox/trunk/pep362/pep362.py
sandbox/trunk/pep362/pep362_fodder.py
sandbox/trunk/pep362/pep362_py3k_fodder.py
sandbox/trunk/pep362/test_pep362.py
Log:
Make compatible with Python 3.0a1.
Modified: sandbox/trunk/pep362/pep362.py
==============================================================================
--- sandbox/trunk/pep362/pep362.py (original)
+++ sandbox/trunk/pep362/pep362.py Fri Sep 7 04:26:50 2007
@@ -1,4 +1,5 @@
-from inspect import getargspec
+import inspect
+
class BindError(TypeError):
"""Represent a failure of inspect.Signature.bind() being able to to
@@ -74,11 +75,17 @@
"""Initialize from a function or method object."""
if hasattr(func, 'im_func'):
func = func.im_func
- func_code = func.func_code
+ func_code = func.__code__
self.name = func.__name__
- argspec = getargspec(func) # Needed only for tuple parameters.
+ try:
+ # Unneeded once 2.x support is removed; can easily get info the
+ # "hard" way.
+ argspec = inspect.getfullargspec(func)[:4]
+ except AttributeError:
+ # Needed only for tuple parameters.
+ argspec = inspect.getargspec(func)
parameters = []
# Parameter information.
@@ -90,8 +97,8 @@
positional = argspec[0]
keyword_only = func_code.co_varnames[pos_count:
pos_count+keyword_only_count]
- if func.func_defaults:
- pos_default_count = len(func.func_defaults)
+ if func.__defaults__:
+ pos_default_count = len(func.__defaults__)
else:
pos_default_count = 0
@@ -107,7 +114,7 @@
for offset, name in enumerate(positional[non_default_count:]):
name = self._convert_name(name)
has_annotation, annotation = self._find_annotation(func, name)
- default_value = func.func_defaults[offset]
+ default_value = func.__defaults__[offset]
param = Parameter(name, offset+non_default_count,
has_default=True, default_value=default_value,
has_annotation=has_annotation,
@@ -117,9 +124,9 @@
for offset, name in enumerate(keyword_only):
has_annotation, annotation = self._find_annotation(func, name)
has_default, default_value = False, None
- if func.func_kwdefaults and name in func.func_kwdefaults:
+ if func.__kwdefaults__ and name in func.__kwdefaults__:
has_default = True
- default_value = func.func_kwdefaults[name]
+ default_value = func.__kwdefaults__[name]
param = Parameter(name, offset+pos_count, keyword_only=True,
has_default=has_default,
default_value=default_value,
@@ -135,7 +142,7 @@
self.var_args)
if has_annotation:
self.var_annotations[self.var_args] = (
- func.func_annotations[self.var_args])
+ func.__annotations__[self.var_args])
index += 1
else:
self.var_args = ''
@@ -145,7 +152,7 @@
self.var_args)
if has_annotation:
self.var_annotations[self.var_kw_args] = (
- func.func_annotations[self.var_kw_args])
+ func.__annotations__[self.var_kw_args])
index += 1
else:
self.var_kw_args = ''
@@ -154,19 +161,19 @@
# Return annotation.
self.has_annotation = False
- if hasattr(func, 'func_annotations'):
- if 'return' in func.func_annotations:
+ if hasattr(func, '__annotations__'):
+ if 'return' in func.__annotations__:
self.has_annotation = True
- self.annotation = func.func_annotations['return']
+ self.annotation = func.__annotations__['return']
def _find_annotation(self, func, name):
"""Return True if an annotation exists for the named parameter along
with its annotation, else return False and None."""
has_annotation, annotation = False, None
- if hasattr(func, 'func_annotations'):
- if name in func.func_annotations:
+ if hasattr(func, '__annotations__'):
+ if name in func.__annotations__:
has_annotation = True
- annotation = func.func_annotations[name]
+ annotation = func.__annotations__[name]
return has_annotation, annotation
def _convert_name(self, name):
Modified: sandbox/trunk/pep362/pep362_fodder.py
==============================================================================
--- sandbox/trunk/pep362/pep362_fodder.py (original)
+++ sandbox/trunk/pep362/pep362_fodder.py Fri Sep 7 04:26:50 2007
@@ -12,12 +12,3 @@
def default_args(a=42):
pass
-
-def tuple_args((a, (b,))):
- return a, b
-
-def default_tuple_args((a, (b,))=(1, (2,))):
- pass
-
-def all_args(a, (b, (c,)), d=0, (e, (f,))=(4, (5,)), *g, **h):
- return a, b, c, d, e, f, g, h
Modified: sandbox/trunk/pep362/pep362_py3k_fodder.py
==============================================================================
--- sandbox/trunk/pep362/pep362_py3k_fodder.py (original)
+++ sandbox/trunk/pep362/pep362_py3k_fodder.py Fri Sep 7 04:26:50 2007
@@ -19,6 +19,6 @@
def return_annotation() -> int:
pass
-def all_args(a:int, (b, (c,)), d=0, (e, (f,))=(0, (0,)), *args:int,
+def all_args(a:int, d=0, *args:int,
g:int, h:int=8, **kwargs:int) -> int:
- return a, b, c, d, e, f, g, h, args, kwargs
+ return a, d, g, h, args, kwargs
Modified: sandbox/trunk/pep362/test_pep362.py
==============================================================================
--- sandbox/trunk/pep362/test_pep362.py (original)
+++ sandbox/trunk/pep362/test_pep362.py Fri Sep 7 04:26:50 2007
@@ -3,15 +3,20 @@
import unittest
from test import test_support
import pep362_fodder
-from sys import version_info
-if version_info[0] >= 3:
+try:
+ import pep362_py2_fodder
+except SyntaxError:
import pep362_py3k_fodder
+from sys import version_info
-def py3k_test(fxn):
- if version_info[0] >= 3:
- return fxn
- else:
- return lambda self: self
+
+def version_specific(major_number):
+ def inner(fxn):
+ if version_info[0] == major_number:
+ return fxn
+ else:
+ return lambda self: self
+ return inner
class ParameterObjectTests(unittest.TestCase):
@@ -111,9 +116,10 @@
self.failUnless(param.has_default)
self.failUnlessEqual(42, param.default_value)
+ @version_specific(2)
def test_parameter_tuple(self):
# A function with a tuple as a parameter should work.
- sig = pep362.Signature(pep362_fodder.tuple_args)
+ sig = pep362.Signature(pep362_py2_fodder.tuple_args)
self.failUnlessEqual('tuple_args', sig.name)
param = sig.parameters[0]
self.failUnless(isinstance(param.name, tuple))
@@ -122,9 +128,10 @@
self.failUnless(not param.has_default)
self.failUnless(not hasattr(param, 'default_value'))
+ @version_specific(2)
def test_parameter_tuple_default(self):
# A default argument for a tuple parameter needs to work.
- sig = pep362.Signature(pep362_fodder.default_tuple_args)
+ sig = pep362.Signature(pep362_py2_fodder.default_tuple_args)
self.failUnlessEqual('default_tuple_args', sig.name)
param = sig.parameters[0]
self.failUnlessEqual(('a', ('b',)), param.name)
@@ -132,13 +139,7 @@
self.failUnless(param.has_default)
self.failUnlessEqual((1, (2,)), param.default_value)
- def test_getitem(self):
- # Signature objects should have __getitem__ defined.
- sig = pep362.Signature(pep362_fodder.all_args)
- param = sig.parameters[2]
- self.failUnlessEqual('d', param.name)
-
- @py3k_test
+ @version_specific(3)
def test_keyword_only(self):
# Is a function containing keyword-only parameters handled properly?
sig = pep362.Signature(pep362_py3k_fodder.keyword_only)
@@ -147,7 +148,7 @@
self.failUnless(param.keyword_only)
self.failUnlessEqual(param.position, 0)
- @py3k_test
+ @version_specific(3)
def test_keyword_only_default(self):
# Default arguments can work for keyword-only parameters.
sig = pep362.Signature(pep362_py3k_fodder.keyword_only_default)
@@ -158,7 +159,7 @@
self.failUnless(param.has_default)
self.failUnlessEqual(param.default_value, 42)
- @py3k_test
+ @version_specific(3)
def test_annotations(self):
# Make sure the proper annotation is found.
sig = pep362.Signature(pep362_py3k_fodder.arg_annotation)
@@ -167,7 +168,7 @@
self.failUnless(param.has_annotation)
self.failUnlessEqual(param.annotation, int)
- @py3k_test
+ @version_specific(3)
def test_annotations_default(self):
# Annotations with a default value should work.
sig = pep362.Signature(pep362_py3k_fodder.arg_annotation_default)
@@ -178,7 +179,7 @@
self.failUnless(param.has_default)
self.failUnlessEqual(param.default_value, 42)
- @py3k_test
+ @version_specific(3)
def test_annotation_keyword_only(self):
# Keyword-only parameters can have an annotation.
sig = pep362.Signature(pep362_py3k_fodder.arg_annotation_keyword_only)
@@ -188,14 +189,14 @@
self.failUnlessEqual(param.annotation, int)
self.failUnless(param.keyword_only)
- @py3k_test
+ @version_specific(3)
def test_return_annotation(self):
# The return value annotation.
sig = pep362.Signature(pep362_py3k_fodder.return_annotation)
self.failUnless(sig.has_annotation)
self.failUnlessEqual(sig.annotation, int)
- @py3k_test
+ @version_specific(3)
def test_var_annotations(self):
# Annotation on variable arguments (*args & **kwargs).
sig = pep362.Signature(pep362_py3k_fodder.arg_annotation_var)
@@ -266,24 +267,27 @@
self.failUnlessRaises(pep362.BindError, sig.bind, a=0, b=1)
self.failUnlessRaises(pep362.BindError, sig.bind, b=1)
+ @version_specific(2)
def test_tuple_parameter(self):
- sig = pep362.Signature(pep362_fodder.tuple_args)
+ sig = pep362.Signature(pep362_py2_fodder.tuple_args)
arg = (1, ((2,),))
binding = sig.bind(arg)
self.failUnlessEqual({'a':1, 'b':(2,)}, binding)
self.failUnlessRaises(pep362.BindError, sig.bind, (1,2,3))
self.failUnlessRaises(pep362.BindError, sig.bind, (1, 2))
+ @version_specific(2)
def test_default_tuple_parameter(self):
- sig = pep362.Signature(pep362_fodder.default_tuple_args)
+ sig = pep362.Signature(pep362_py2_fodder.default_tuple_args)
binding = sig.bind()
self.failUnlessEqual({'a':1, 'b':2}, binding)
arg = (0, (1,))
binding = sig.bind(arg)
self.failUnlessEqual({'a':0, 'b':1}, binding)
- def test_all_args(self):
- sig = pep362.Signature(pep362_fodder.all_args)
+ @version_specific(2)
+ def test_py2_all_args(self):
+ sig = pep362.Signature(pep362_py2_fodder.all_args)
# a, (b, (c,)), d=0, (e, (f,))=(4, (5,)), *g, **h
# name, position, has_default, default value
expect = (('a', 0, False, None),
@@ -312,7 +316,7 @@
'h':{'i':7}}
self.failUnlessEqual(expected, binding)
- @py3k_test
+ @version_specific(3)
def test_keyword_only(self):
sig = pep362.Signature(pep362_py3k_fodder.keyword_only)
binding = sig.bind(a=42)
@@ -320,7 +324,7 @@
self.failUnlessRaises(pep362.BindError, sig.bind)
self.failUnlessRaises(pep362.BindError, sig.bind, 42)
- @py3k_test
+ @version_specific(3)
def test_keyword_only_default(self):
sig = pep362.Signature(pep362_py3k_fodder.keyword_only_default)
binding = sig.bind()
@@ -329,17 +333,15 @@
self.failUnlessEqual(binding, {'a':1})
self.failUnlessRaises(pep362.BindError, sig.bind, 1)
- @py3k_test
+ @version_specific(3)
def test_all_py3k_args(self):
# a, (b, (c,)), d=0, (e, (f,))=(0, (0,)), *args, g, h=8, **kwargs
sig = pep362.Signature(pep362_py3k_fodder.all_args)
# name, position, kw only, has_default, default, has anno, anno
expected = (('a', 0, False, False, None, True, int),
- (('b', ('c',)), 1, False, False, None, False, None),
- ('d', 2, False, True, 0, False, None),
- (('e', ('f',)), 3, False, True, (0, (0,)), False, None),
- ('g', 4, True, False, None, True, int),
- ('h', 5, True, True, 8, True, int))
+ ('d', 1, False, True, 0, False, None),
+ ('g', 2, True, False, None, True, int),
+ ('h', 3, True, True, 8, True, int))
self.failUnlessEqual(len(sig.parameters), len(expected),
"len(%r) != len(%r)" % ([param.name
for param in sig.parameters],
@@ -370,9 +372,8 @@
self.failUnlessEqual(sig.var_kw_args, 'kwargs')
self.failUnless(sig.var_kw_args in sig.var_annotations)
self.failUnlessEqual(sig.var_annotations[sig.var_kw_args], int)
- binding = sig.bind(0, (1, (2,)), 3, (4, (5,)), 6, g=7, i=9)
- expected = {'a':0, 'b':1, 'c':2, 'd':3, 'e':4, 'f':5, 'g':7, 'h':8,
- 'args':(6,), 'kwargs':{'i':9}}
+ binding = sig.bind(0, 3, 6, g=7, i=9)
+ expected = {'a':0, 'd':3, 'g':7, 'h':8, 'args':(6,), 'kwargs':{'i':9}}
self.failUnlessEqual(binding, expected)
def test_too_many_arguments(self):
More information about the Python-checkins
mailing list