diff options
| author | Alex Gr?nholm <alex.gronholm@nextday.fi> | 2011-05-17 02:26:43 +0300 |
|---|---|---|
| committer | Alex Gr?nholm <alex.gronholm@nextday.fi> | 2011-05-17 02:26:43 +0300 |
| commit | 8d0fa5407b29605f1b217fc0c9945b9404330970 (patch) | |
| tree | 0667502c6f72b8ee3fc5c6b1759658165c0e966c /paste/deploy/util.py | |
| parent | d66a3541af72d509c886259a81e2e21d62e03eda (diff) | |
| download | pastedeploy-8d0fa5407b29605f1b217fc0c9945b9404330970.tar.gz | |
The threadinglocal module is no longer necessary, so replaced the util package with a util module containing the code from fixtypeerror.py
Diffstat (limited to 'paste/deploy/util.py')
| -rw-r--r-- | paste/deploy/util.py | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/paste/deploy/util.py b/paste/deploy/util.py new file mode 100644 index 0000000..86c6972 --- /dev/null +++ b/paste/deploy/util.py @@ -0,0 +1,58 @@ +# (c) 2005 Ian Bicking and contributors; written for Paste (http://pythonpaste.org) +# Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php +import inspect +import sys + + +def fix_type_error(exc_info, callable, varargs, kwargs): + """ + Given an exception, this will test if the exception was due to a + signature error, and annotate the error with better information if + so. + + Usage:: + + try: + val = callable(*args, **kw) + except TypeError: + exc_info = fix_type_error(None, callable, args, kw) + raise exc_info[0], exc_info[1], exc_info[2] + """ + if exc_info is None: + exc_info = sys.exc_info() + if (exc_info[0] != TypeError + or str(exc_info[1]).find('arguments') == -1 + or getattr(exc_info[1], '_type_error_fixed', False)): + return exc_info + exc_info[1]._type_error_fixed = True + argspec = inspect.formatargspec(*inspect.getargspec(callable)) + args = ', '.join(map(_short_repr, varargs)) + if kwargs and args: + args += ', ' + if kwargs: + kwargs = kwargs.items() + kwargs.sort() + args += ', '.join(['%s=...' % n for n, v in kwargs]) + gotspec = '(%s)' % args + msg = '%s; got %s, wanted %s' % (exc_info[1], gotspec, argspec) + exc_info[1].args = (msg,) + return exc_info + + +def _short_repr(v): + v = repr(v) + if len(v) > 12: + v = v[:8]+'...'+v[-4:] + return v + + +def fix_call(callable, *args, **kw): + """ + Call ``callable(*args, **kw)`` fixing any type errors that come out. + """ + try: + val = callable(*args, **kw) + except TypeError: + exc_info = fix_type_error(None, callable, args, kw) + raise exc_info[0], exc_info[1], exc_info[2] + return val |
