source: publico/il.spdo/trunk/PasteDeploy-1.5.0-py2.6.egg/paste/deploy/util.py @ 5327

Última Alteração nesse arquivo desde 5327 foi 5327, incluída por fabianosantos, 8 anos atrás

Import inicial.

File size: 2.1 KB
Linha 
1# (c) 2005 Ian Bicking and contributors; written for Paste (http://pythonpaste.org)
2# Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php
3import inspect
4import sys
5
6from paste.deploy.compat import reraise
7
8
9def fix_type_error(exc_info, callable, varargs, kwargs):
10    """
11    Given an exception, this will test if the exception was due to a
12    signature error, and annotate the error with better information if
13    so.
14
15    Usage::
16
17      try:
18          val = callable(*args, **kw)
19      except TypeError:
20          exc_info = fix_type_error(None, callable, args, kw)
21          raise exc_info[0], exc_info[1], exc_info[2]
22    """
23    if exc_info is None:
24        exc_info = sys.exc_info()
25    if (exc_info[0] != TypeError
26        or str(exc_info[1]).find('arguments') == -1
27        or getattr(exc_info[1], '_type_error_fixed', False)):
28        return exc_info
29    exc_info[1]._type_error_fixed = True
30    argspec = inspect.formatargspec(*inspect.getargspec(callable))
31    args = ', '.join(map(_short_repr, varargs))
32    if kwargs and args:
33        args += ', '
34    if kwargs:
35        kwargs = kwargs.items()
36        kwargs.sort()
37        args += ', '.join(['%s=...' % n for n, v in kwargs])
38    gotspec = '(%s)' % args
39    msg = '%s; got %s, wanted %s' % (exc_info[1], gotspec, argspec)
40    exc_info[1].args = (msg,)
41    return exc_info
42
43
44def _short_repr(v):
45    v = repr(v)
46    if len(v) > 12:
47        v = v[:8] + '...' + v[-4:]
48    return v
49
50
51def fix_call(callable, *args, **kw):
52    """
53    Call ``callable(*args, **kw)`` fixing any type errors that come out.
54    """
55    try:
56        val = callable(*args, **kw)
57    except TypeError:
58        exc_info = fix_type_error(None, callable, args, kw)
59        reraise(*exc_info)
60    return val
61
62
63def lookup_object(spec):
64    """
65    Looks up a module or object from a some.module:func_name specification.
66    To just look up a module, omit the colon and everything after it.
67    """
68    parts, target = spec.split(':') if ':' in spec else (spec, None)
69    module = __import__(parts)
70
71    for part in parts.split('.')[1:] + ([target] if target else []):
72        module = getattr(module, part)
73
74    return module
Note: Veja TracBrowser para ajuda no uso do navegador do trac.
 

The contents and data of this website are published under license:
Creative Commons 4.0 Brasil - Atribuir Fonte - Compartilhar Igual.