summaryrefslogtreecommitdiff
path: root/lib/sqlalchemy/util/langhelpers.py
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2011-09-28 13:04:42 -0400
committerMike Bayer <mike_mp@zzzcomputing.com>2011-09-28 13:04:42 -0400
commit4f1321c3e97f9bb1c92b378452a7810874927c71 (patch)
tree14a0c10a2e276405a182f31bbe758ffefaeb3dd0 /lib/sqlalchemy/util/langhelpers.py
parent98a79154155c83f407139f484dcbb114d2891ece (diff)
downloadsqlalchemy-4f1321c3e97f9bb1c92b378452a7810874927c71.tar.gz
- Enhanced the instrumentation in the ORM to support
Py3K's new argument style of "required kw arguments", i.e. fn(a, b, *, c, d), fn(a, b, *args, c, d). Argument signatures of mapped object's __init__ method will be preserved, including required kw rules. [ticket:2237]
Diffstat (limited to 'lib/sqlalchemy/util/langhelpers.py')
-rw-r--r--lib/sqlalchemy/util/langhelpers.py35
1 files changed, 29 insertions, 6 deletions
diff --git a/lib/sqlalchemy/util/langhelpers.py b/lib/sqlalchemy/util/langhelpers.py
index cf8b2acac..722003796 100644
--- a/lib/sqlalchemy/util/langhelpers.py
+++ b/lib/sqlalchemy/util/langhelpers.py
@@ -15,7 +15,7 @@ import re
import sys
import types
import warnings
-from compat import update_wrapper, set_types, threading
+from compat import update_wrapper, set_types, threading, callable, inspect_getfullargspec, py3k
from sqlalchemy import exc
def _unique_symbols(used, *bases):
@@ -38,7 +38,7 @@ def decorator(target):
def decorate(fn):
if not inspect.isfunction(fn):
raise Exception("not a decoratable function")
- spec = inspect.getargspec(fn)
+ spec = inspect_getfullargspec(fn)
names = tuple(spec[0]) + spec[1:3] + (fn.func_name,)
targ_name, fn_name = _unique_symbols(names, 'target', 'fn')
@@ -149,7 +149,11 @@ def format_argspec_plus(fn, grouped=True):
'apply_pos': '(self, a, b, c, **d)'}
"""
- spec = callable(fn) and inspect.getargspec(fn) or fn
+ if callable(fn):
+ spec = inspect_getfullargspec(fn)
+ else:
+ # we accept an existing argspec...
+ spec = fn
args = inspect.formatargspec(*spec)
if spec[0]:
self_arg = spec[0][0]
@@ -157,9 +161,28 @@ def format_argspec_plus(fn, grouped=True):
self_arg = '%s[0]' % spec[1]
else:
self_arg = None
- apply_pos = inspect.formatargspec(spec[0], spec[1], spec[2])
- defaulted_vals = spec[3] is not None and spec[0][0-len(spec[3]):] or ()
- apply_kw = inspect.formatargspec(spec[0], spec[1], spec[2], defaulted_vals,
+
+ if py3k:
+ apply_pos = inspect.formatargspec(spec[0], spec[1], spec[2], None, spec[4])
+ num_defaults = 0
+ if spec[3]:
+ num_defaults += len(spec[3])
+ if spec[4]:
+ num_defaults += len(spec[4])
+ name_args = spec[0] + spec[4]
+ else:
+ apply_pos = inspect.formatargspec(spec[0], spec[1], spec[2])
+ num_defaults = 0
+ if spec[3]:
+ num_defaults += len(spec[3])
+ name_args = spec[0]
+
+ if num_defaults:
+ defaulted_vals = name_args[0-num_defaults:]
+ else:
+ defaulted_vals = ()
+
+ apply_kw = inspect.formatargspec(name_args, spec[1], spec[2], defaulted_vals,
formatvalue=lambda x: '=' + x)
if grouped:
return dict(args=args, self_arg=self_arg,