From 8d0fa5407b29605f1b217fc0c9945b9404330970 Mon Sep 17 00:00:00 2001 From: Alex Gr?nholm Date: Tue, 17 May 2011 02:26:43 +0300 Subject: The threadinglocal module is no longer necessary, so replaced the util package with a util module containing the code from fixtypeerror.py --- paste/deploy/config.py | 3 +- paste/deploy/util.py | 58 +++++++++++++++++++++++++++++++++++ paste/deploy/util/__init__.py | 3 -- paste/deploy/util/fixtypeerror.py | 61 ------------------------------------- paste/deploy/util/threadinglocal.py | 39 ------------------------ 5 files changed, 59 insertions(+), 105 deletions(-) create mode 100644 paste/deploy/util.py delete mode 100644 paste/deploy/util/__init__.py delete mode 100644 paste/deploy/util/fixtypeerror.py delete mode 100644 paste/deploy/util/threadinglocal.py diff --git a/paste/deploy/config.py b/paste/deploy/config.py index d690ff3..562a61f 100644 --- a/paste/deploy/config.py +++ b/paste/deploy/config.py @@ -14,8 +14,7 @@ def local_dict(): try: return config_local.wsgi_dict except NameError: - from paste.deploy.util.threadinglocal import local - config_local = local() + config_local = threading.local() config_local.wsgi_dict = result = {} return result except AttributeError: 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 diff --git a/paste/deploy/util/__init__.py b/paste/deploy/util/__init__.py deleted file mode 100644 index 56bf54c..0000000 --- a/paste/deploy/util/__init__.py +++ /dev/null @@ -1,3 +0,0 @@ -# (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 -# diff --git a/paste/deploy/util/fixtypeerror.py b/paste/deploy/util/fixtypeerror.py deleted file mode 100644 index b1fa0ec..0000000 --- a/paste/deploy/util/fixtypeerror.py +++ /dev/null @@ -1,61 +0,0 @@ -# (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 -""" -Fixes the vague error message that you get when calling a function -with the wrong arguments. -""" -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 - import inspect - 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 diff --git a/paste/deploy/util/threadinglocal.py b/paste/deploy/util/threadinglocal.py deleted file mode 100644 index 57afa17..0000000 --- a/paste/deploy/util/threadinglocal.py +++ /dev/null @@ -1,39 +0,0 @@ -# (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 - -try: - import threading -except ImportError: - # No threads, so "thread local" means process-global - class local(object): - pass -else: - try: - local = threading.local - except AttributeError: - # Added in 2.4, but now we'll have to define it ourselves - import thread - class local(object): - - def __init__(self): - self.__dict__['__objs'] = {} - - def __getattr__(self, attr, g=thread.get_ident): - try: - return self.__dict__['__objs'][g()][attr] - except KeyError: - raise AttributeError( - "No variable %s defined for the thread %s" - % (attr, g())) - - def __setattr__(self, attr, value, g=thread.get_ident): - self.__dict__['__objs'].setdefault(g(), {})[attr] = value - - def __delattr__(self, attr, g=thread.get_ident): - try: - del self.__dict__['__objs'][g()][attr] - except KeyError: - raise AttributeError( - "No variable %s defined for thread %s" - % (attr, g())) - -- cgit v1.2.1