diff options
| author | Ceridwen <ceridwenv@gmail.com> | 2016-03-10 11:17:10 -0500 |
|---|---|---|
| committer | Ceridwen <ceridwenv@gmail.com> | 2016-03-10 11:17:10 -0500 |
| commit | 3c601315485baeb237927e0770c28db7a88f1e06 (patch) | |
| tree | 0664e24db2dd024647c0c4639c47aaf588a2db42 /astroid | |
| parent | 18020b8b0b406f0a4f4f6c19d6896fbc07f411f8 (diff) | |
| download | astroid-git-3c601315485baeb237927e0770c28db7a88f1e06.tar.gz | |
Start zipperification of the rest of the project, fix util.singledispatch to work with the zipper
Diffstat (limited to 'astroid')
| -rw-r--r-- | astroid/__init__.py | 2 | ||||
| -rw-r--r-- | astroid/builder.py | 3 | ||||
| -rw-r--r-- | astroid/util.py | 24 |
3 files changed, 27 insertions, 2 deletions
diff --git a/astroid/__init__.py b/astroid/__init__.py index 3132300f..e32e6ba0 100644 --- a/astroid/__init__.py +++ b/astroid/__init__.py @@ -84,7 +84,7 @@ from astroid.util import Uninferable, YES # ??? from astroid.tree import zipper - +from astroid.tree.base import NodeNG # transform utilities (filters and decorator) diff --git a/astroid/builder.py b/astroid/builder.py index b3a5d0f0..5cd2ae42 100644 --- a/astroid/builder.py +++ b/astroid/builder.py @@ -32,6 +32,7 @@ from astroid import manager from astroid import modutils from astroid.tree import rebuilder from astroid.tree import treeabc +from astroid.tree import zipper from astroid import util raw_building = util.lazy_import('raw_building') @@ -107,6 +108,7 @@ class AstroidBuilder(object): node = raw_building.ast_from_object(module, name=modname) # FIXME node.source_file = path + node = zipper.Zipper(node) if self._apply_transforms: # We have to handle transformation by ourselves since the # rebuilder isn't called for builtin nodes @@ -153,6 +155,7 @@ class AstroidBuilder(object): def _post_build(self, module, encoding): """Handles encoding and delayed nodes after a module has been built""" module.file_encoding = encoding + module = zipper.Zipper(module) self._manager.cache_module(module) delayed_assignments(module) diff --git a/astroid/util.py b/astroid/util.py index 6fd59cfb..78bbcb05 100644 --- a/astroid/util.py +++ b/astroid/util.py @@ -37,10 +37,32 @@ except ImportError: def singledispatch(func): + '''Modified singledispatch decorator that doesn't crash on old-style classes. + + This is a workaround in particular for the Python 2 standard + library, because some modules export old-style classes that cause + raw_building.py to crash. Old-style classes themselves (not their + instances) have no __class__ attribute, so the standard + library/backport singledispatch decorator raises an + AttributeError. For forward compatibility, in particular for + proxy objects like the zipper, the wrapper function tries + __class__ first and if that fails, falls back to calling type(), + which doesn't raise an exception on old-style classes. This new + wrapped function is then used to replace the singledispatch + wrapper function, avoiding the overhead of another function call. + This will be slightly slower than the standard library/backport + function because of the try/except block, but hopefully not enough + to matter. + + ''' + old_generic_func = _singledispatch(func) @wrapt.decorator def wrapper(func, instance, args, kws): - return old_generic_func.dispatch(type(args[0]))(*args, **kws) + try: + return old_generic_func.dispatch(args[0].__class__)(*args, **kws) + except AttributeError: + return old_generic_func.dispatch(type(args[0]))(*args, **kws) new_generic_func = wrapper(func) new_generic_func.register = old_generic_func.register new_generic_func.dispatch = old_generic_func.dispatch |
