diff options
author | Sylvain Thénault <sylvain.thenault@logilab.fr> | 2011-07-08 20:16:52 +0200 |
---|---|---|
committer | Sylvain Thénault <sylvain.thenault@logilab.fr> | 2011-07-08 20:16:52 +0200 |
commit | 3cb6ffe887497bd37af6dd57ed28b98de102ae65 (patch) | |
tree | 791c32ff4f03276fe09b63c6d04b1145ff227570 | |
parent | 11886551cfdcf969f0a661f8ab63c1fa1a6dd399 (diff) | |
download | astroid-git-3cb6ffe887497bd37af6dd57ed28b98de102ae65.tar.gz |
py3k: __builtin__ module renamed to builtins, we should consider this to properly build ast for builtin objects
-rw-r--r-- | ChangeLog | 2 | ||||
-rw-r--r-- | __init__.py | 23 | ||||
-rw-r--r-- | bases.py | 6 | ||||
-rw-r--r-- | node_classes.py | 11 | ||||
-rw-r--r-- | raw_building.py | 7 | ||||
-rw-r--r-- | scoped_nodes.py | 11 | ||||
-rw-r--r-- | test/unittest_builder.py | 7 | ||||
-rw-r--r-- | test/unittest_nodes.py | 32 | ||||
-rw-r--r-- | test/unittest_scoped_nodes.py | 6 |
9 files changed, 53 insertions, 52 deletions
@@ -7,6 +7,8 @@ Change log for the astng package * #70381: IndendationError in import causes crash * #70565: absolute imports treated as relative (patch by Jacek Konieczny) * #70494: fix file encoding detection with python2.x + * py3k: __builtin__ module renamed to builtins, we should consider this to properly + build ast for builtin objects 2011-01-11 -- 0.21.1 * python3: handle file encoding; fix a lot of tests diff --git a/__init__.py b/__init__.py index e3cd8a08..41bc707f 100644 --- a/__init__.py +++ b/__init__.py @@ -1,16 +1,4 @@ -# This program is free software; you can redistribute it and/or modify it under -# the terms of the GNU Lesser General Public License as published by the Free Software -# Foundation; either version 2 of the License, or (at your option) any later -# version. -# -# This program is distributed in the hope that it will be useful, but WITHOUT -# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS -# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. -# -# You should have received a copy of the GNU Lesser General Public License along with -# this program; if not, write to the Free Software Foundation, Inc., -# 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. -# copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved. +# copyright 2003-2011 LOGILAB S.A. (Paris, FRANCE), all rights reserved. # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr # copyright 2003-2010 Sylvain Thenault, all rights reserved. # contact mailto:thenault@gmail.com @@ -52,11 +40,15 @@ Main modules are: constructed tree for quick access * builder contains the class responsible to build astng trees - - """ __doctype__ = "restructuredtext en" +import sys +if sys.version_info >= (3, 0): + BUILTINS_MODULE = 'builtins' +else: + BUILTINS_MODULE = '__builtin__' + # WARNING: internal imports order matters ! # make all exception classes accessible from astng package @@ -79,4 +71,3 @@ from logilab.astng.scoped_nodes import builtin_lookup from logilab.astng.manager import ASTNGManager, Project MANAGER = ASTNGManager() del ASTNGManager - @@ -26,6 +26,8 @@ __docformat__ = "restructuredtext en" from logilab.common.compat import builtins + +from logilab.astng import BUILTINS_MODULE from logilab.astng.exceptions import InferenceError, ASTNGError, \ NotFoundError, UnresolvableName from logilab.astng.as_string import as_string @@ -242,7 +244,7 @@ class UnboundMethod(Proxy): # If we're unbound method __new__ of builtin object, the result is an # instance of the class given as first argument. if (self._proxied.name == '__new__' and - self._proxied.parent.frame().qname() == '__builtin__.object'): + self._proxied.parent.frame().qname() == '%s.object' % BUILTINS_MODULE): return (x is YES and x or Instance(x) for x in caller.args[0].infer()) return self._proxied.infer_call_result(caller, context) @@ -268,7 +270,7 @@ class Generator(Instance): return True def pytype(self): - return '__builtin__.generator' + return '%s.generator' % BUILTINS_MODULE def display_type(self): return 'Generator' diff --git a/node_classes.py b/node_classes.py index 2b50e0b9..4dd8520c 100644 --- a/node_classes.py +++ b/node_classes.py @@ -1,4 +1,4 @@ -# copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved. +# copyright 2003-2011 LOGILAB S.A. (Paris, FRANCE), all rights reserved. # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr # copyright 2003-2010 Sylvain Thenault, all rights reserved. # contact mailto:thenault@gmail.com @@ -22,6 +22,7 @@ import sys +from logilab.astng import BUILTINS_MODULE from logilab.astng.exceptions import NoDefault from logilab.astng.bases import (NodeNG, Statement, Instance, InferenceContext, _infer_stmts, YES) @@ -489,7 +490,7 @@ class Dict(NodeNG, Instance): for k,v in items.iteritems()] def pytype(self): - return '__builtin__.dict' + return '%s.dict' % BUILTINS_MODULE def get_children(self): """get children of a Dict node""" @@ -664,7 +665,7 @@ class List(NodeNG, Instance, ParentAssignTypeMixin): self.elts = [const_factory(e) for e in elts] def pytype(self): - return '__builtin__.list' + return '%s.list' % BUILTINS_MODULE def getitem(self, index, context=None): return self.elts[index] @@ -731,7 +732,7 @@ class Set(NodeNG, Instance, ParentAssignTypeMixin): self.elts = [const_factory(e) for e in elts] def pytype(self): - return '__builtin__.set' # XXX __builtin__ vs builtins + return '%s.set' % BUILTINS_MODULE def itered(self): return self.elts @@ -813,7 +814,7 @@ class Tuple(NodeNG, Instance, ParentAssignTypeMixin): self.elts = [const_factory(e) for e in elts] def pytype(self): - return '__builtin__.tuple' + return '%s.tuple' % BUILTINS_MODULE def getitem(self, index, context=None): return self.elts[index] diff --git a/raw_building.py b/raw_building.py index f6da03ed..d6b462bc 100644 --- a/raw_building.py +++ b/raw_building.py @@ -1,4 +1,4 @@ -# copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved. +# copyright 2003-2011 LOGILAB S.A. (Paris, FRANCE), all rights reserved. # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr # copyright 2003-2010 Sylvain Thenault, all rights reserved. # contact mailto:thenault@gmail.com @@ -28,6 +28,7 @@ from os.path import abspath from inspect import (getargspec, isdatadescriptor, isfunction, ismethod, ismethoddescriptor, isclass, isbuiltin) +from logilab.astng import BUILTINS_MODULE from logilab.astng.node_classes import CONST_CLS from logilab.astng.nodes import (Module, Class, Const, const_factory, From, Function, EmptyNode, Name, Arguments, Dict, List, Set, Tuple) @@ -245,6 +246,8 @@ class InspectBuilder(object): object_build_function(node, member, name) elif isbuiltin(member): if self.imported_member(node, member, name): + if obj is object: + print 'skippp', obj, name, member continue object_build_methoddescriptor(node, member, name) elif isclass(member): @@ -290,7 +293,7 @@ class InspectBuilder(object): # Python 2.5.1 (r251:54863, Sep 1 2010, 22:03:14) # >>> print object.__new__.__module__ # None - modname = '__builtin__' + modname = BUILTINS_MODULE else: attach_dummy_node(node, name, member) return True diff --git a/scoped_nodes.py b/scoped_nodes.py index a287a348..c6c24fee 100644 --- a/scoped_nodes.py +++ b/scoped_nodes.py @@ -30,6 +30,7 @@ from itertools import chain from logilab.common.compat import builtins from logilab.common.decorators import cached +from logilab.astng import BUILTINS_MODULE from logilab.astng.exceptions import NotFoundError, NoDefault, \ ASTNGBuildingException, InferenceError from logilab.astng.node_classes import Const, DelName, DelAttr, \ @@ -257,7 +258,7 @@ class Module(LocalsDictNodeNG): return self._scope_lookup(node, name, offset) def pytype(self): - return '__builtin__.module' + return '%s.module' % BUILTINS_MODULE def display_type(self): return 'Module' @@ -469,8 +470,8 @@ class Lambda(LocalsDictNodeNG, FilterStmtsMixin): def pytype(self): if 'method' in self.type: - return '__builtin__.instancemethod' - return '__builtin__.function' + return '%s.instancemethod' % BUILTINS_MODULE + return '%s.function' % BUILTINS_MODULE def display_type(self): if 'method' in self.type: @@ -726,8 +727,8 @@ class Class(Statement, LocalsDictNodeNG, FilterStmtsMixin): def pytype(self): if self.newstyle: - return '__builtin__.type' - return '__builtin__.classobj' + return '%s.type' % BUILTINS_MODULE + return '%s.classobj' % BUILTINS_MODULE def display_type(self): return 'Class' diff --git a/test/unittest_builder.py b/test/unittest_builder.py index 137f0449..4dd203c9 100644 --- a/test/unittest_builder.py +++ b/test/unittest_builder.py @@ -38,7 +38,7 @@ from os.path import join, abspath, dirname from logilab.common.testlib import TestCase, unittest_main from pprint import pprint -from logilab.astng import builder, nodes, InferenceError, NotFoundError +from logilab.astng import BUILTINS_MODULE, builder, nodes, InferenceError, NotFoundError from logilab.astng.nodes import Module from logilab.astng.bases import YES, BUILTINS_NAME from logilab.astng.as_string import as_string @@ -286,14 +286,15 @@ class BuilderTC(TestCase): self.assert_('mode' in fclass) self.assert_('read' in fclass) self.assert_(fclass.newstyle) - self.assert_(fclass.pytype(), '__builtin__.type') + self.assert_(fclass.pytype(), '%s.type' % BUILTINS_MODULE) self.assertIsInstance(fclass['read'], nodes.Function) # check builtin function has args.args == None dclass = builtin_astng['dict'] self.assertEqual(dclass['has_key'].args.args, None) # just check type and object are there builtin_astng.getattr('type') - builtin_astng.getattr('object') + objectastng = builtin_astng.getattr('object')[0] + self.assertIsInstance(objectastng.getattr('__new__')[0], nodes.Function) # check open file alias builtin_astng.getattr('open') # check 'help' is there (defined dynamically by site.py) diff --git a/test/unittest_nodes.py b/test/unittest_nodes.py index 8af5f790..0932f651 100644 --- a/test/unittest_nodes.py +++ b/test/unittest_nodes.py @@ -35,7 +35,7 @@ import sys from logilab.common import testlib from logilab.astng.exceptions import ASTNGBuildingException, NotFoundError -from logilab.astng import builder, nodes +from logilab.astng import BUILTINS_MODULE, builder, nodes from logilab.astng.as_string import as_string from data import module as test_module @@ -134,7 +134,7 @@ elif func(): else: raise """ - + def test_if_elif_else_node(self): """test transformation for If node""" self.assertEqual(len(self.astng.body), 4) @@ -231,25 +231,25 @@ MODULE2 = abuilder.file_build(join(DATA, 'module2.py'), 'data.module2') class ImportNodeTC(testlib.TestCase): - + def test_import_self_resolve(self): myos = MODULE2.igetattr('myos').next() self.failUnless(isinstance(myos, nodes.Module), myos) self.failUnlessEqual(myos.name, 'os') self.failUnlessEqual(myos.qname(), 'os') - self.failUnlessEqual(myos.pytype(), '__builtin__.module') + self.failUnlessEqual(myos.pytype(), '%s.module' % BUILTINS_MODULE) def test_from_self_resolve(self): spawn = MODULE.igetattr('spawn').next() self.failUnless(isinstance(spawn, nodes.Class), spawn) self.failUnlessEqual(spawn.root().name, 'logilab.common.shellutils') self.failUnlessEqual(spawn.qname(), 'logilab.common.shellutils.Execute') - self.failUnlessEqual(spawn.pytype(), '__builtin__.classobj') + self.failUnlessEqual(spawn.pytype(), '%s.classobj' % BUILTINS_MODULE) abspath = MODULE2.igetattr('abspath').next() self.failUnless(isinstance(abspath, nodes.Function), abspath) self.failUnlessEqual(abspath.root().name, 'os.path') self.failUnlessEqual(abspath.qname(), 'os.path.abspath') - self.failUnlessEqual(abspath.pytype(), '__builtin__.function') + self.failUnlessEqual(abspath.pytype(), '%s.function' % BUILTINS_MODULE) def test_real_name(self): from_ = MODULE['spawn'] @@ -285,7 +285,7 @@ class CmpNodeTC(testlib.TestCase): class ConstNodeTC(testlib.TestCase): - + def _test(self, value): node = nodes.const_factory(value) self.assertIsInstance(node._proxied, nodes.Class) @@ -293,25 +293,25 @@ class ConstNodeTC(testlib.TestCase): self.assertIs(node.value, value) self.failUnless(node._proxied.parent) self.assertEqual(node._proxied.root().name, value.__class__.__module__) - + def test_none(self): self._test(None) - + def test_bool(self): self._test(True) - + def test_int(self): self._test(1) - + def test_float(self): self._test(1.0) - + def test_complex(self): self._test(1.0j) - + def test_str(self): self._test('a') - + def test_unicode(self): self._test(u'a') @@ -335,7 +335,7 @@ del True del_true = ast.body[2].targets[0] self.assertIsInstance(del_true, nodes.DelName) self.assertEqual(del_true.name, "True") - + class ArgumentsNodeTC(testlib.TestCase): def test_linenumbering(self): @@ -387,6 +387,6 @@ class EllipsisNodeTC(testlib.TestCase): def test(self): ast = abuilder.string_build('a[...]').body[0] self.assertEqual(ast.as_string(), 'a[...]') - + if __name__ == '__main__': testlib.unittest_main() diff --git a/test/unittest_scoped_nodes.py b/test/unittest_scoped_nodes.py index a25894db..e2e6f6a5 100644 --- a/test/unittest_scoped_nodes.py +++ b/test/unittest_scoped_nodes.py @@ -27,7 +27,7 @@ from os.path import join, abspath, dirname from logilab.common.testlib import TestCase, unittest_main from logilab.astng import builder, nodes, scoped_nodes, \ - InferenceError, NotFoundError + BUILTINS_MODULE, InferenceError, NotFoundError from logilab.astng.bases import Instance, BoundMethod, UnboundMethod abuilder = builder.ASTNGBuilder() @@ -238,7 +238,7 @@ def nested_args(a, (b, c, d)): method = MODULE2['AbstractClass']['to_override'] self.assert_(method.is_abstract(pass_is_abstract=False)) self.failUnlessEqual(method.qname(), 'data.module2.AbstractClass.to_override') - self.failUnlessEqual(method.pytype(), '__builtin__.instancemethod') + self.failUnlessEqual(method.pytype(), '%s.instancemethod' % BUILTINS_MODULE) method = MODULE2['AbstractClass']['return_something'] self.assert_(not method.is_abstract(pass_is_abstract=False)) # non regression : test raise "string" doesn't cause an exception in is_abstract @@ -263,7 +263,7 @@ def f(): ''' astng = abuilder.string_build(data, __name__, __file__) g = list(astng['f'].ilookup('g'))[0] - self.failUnlessEqual(g.pytype(), '__builtin__.function') + self.failUnlessEqual(g.pytype(), '%s.function' % BUILTINS_MODULE) def test_is_method(self): data = ''' |