summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--__init__.py6
-rw-r--r--bases.py23
-rw-r--r--node_classes.py11
-rw-r--r--raw_building.py5
-rw-r--r--scoped_nodes.py15
-rw-r--r--test/unittest_builder.py11
-rw-r--r--test/unittest_inference.py20
-rw-r--r--test/unittest_manager.py10
-rw-r--r--test/unittest_nodes.py11
-rw-r--r--test/unittest_scoped_nodes.py8
10 files changed, 57 insertions, 63 deletions
diff --git a/__init__.py b/__init__.py
index 12ffce33..1214341f 100644
--- a/__init__.py
+++ b/__init__.py
@@ -1,4 +1,4 @@
-# copyright 2003-2012 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
+# copyright 2003-2013 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
@@ -44,10 +44,6 @@ Main modules are:
__doctype__ = "restructuredtext en"
import sys
-if sys.version_info >= (3, 0):
- BUILTINS_MODULE = 'builtins'
-else:
- BUILTINS_MODULE = '__builtin__'
# WARNING: internal imports order matters !
diff --git a/bases.py b/bases.py
index 293ae329..09540f09 100644
--- a/bases.py
+++ b/bases.py
@@ -24,20 +24,23 @@ inference utils.
__docformat__ = "restructuredtext en"
+import sys
from contextlib import contextmanager
-from logilab.common.compat import builtins
-
-from logilab.astng import BUILTINS_MODULE
-from logilab.astng.exceptions import InferenceError, ASTNGError, \
- NotFoundError, UnresolvableName
+from logilab.astng.exceptions import (InferenceError, ASTNGError,
+ NotFoundError, UnresolvableName)
from logilab.astng.as_string import as_string
-BUILTINS_NAME = builtins.__name__
+
+if sys.version_info >= (3, 0):
+ BUILTINS = 'builtins'
+else:
+ BUILTINS = '__builtin__'
class Proxy(object):
"""a simple proxy object"""
- _proxied = None
+
+ _proxied = None # proxied object may be set by class or by instance
def __init__(self, proxied=None):
if proxied is not None:
@@ -188,7 +191,7 @@ class Instance(Proxy):
"""wrap bound methods of attrs in a InstanceMethod proxies"""
for attr in attrs:
if isinstance(attr, UnboundMethod):
- if BUILTINS_NAME + '.property' in attr.decoratornames():
+ if BUILTINS + '.property' in attr.decoratornames():
for infered in attr.infer_call_result(self, context):
yield infered
else:
@@ -253,7 +256,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() == '%s.object' % BUILTINS_MODULE):
+ self._proxied.parent.frame().qname() == '%s.object' % BUILTINS):
return (x is YES and x or Instance(x) for x in caller.args[0].infer())
return self._proxied.infer_call_result(caller, context)
@@ -279,7 +282,7 @@ class Generator(Instance):
return True
def pytype(self):
- return '%s.generator' % BUILTINS_MODULE
+ return '%s.generator' % BUILTINS
def display_type(self):
return 'Generator'
diff --git a/node_classes.py b/node_classes.py
index a4282b62..5aeae446 100644
--- a/node_classes.py
+++ b/node_classes.py
@@ -22,10 +22,9 @@
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)
+ _infer_stmts, YES, BUILTINS)
from logilab.astng.mixins import BlockRangeMixIn, AssignTypeMixin, \
ParentAssignTypeMixin, FromImportMixIn
@@ -495,7 +494,7 @@ class Dict(NodeNG, Instance):
for k,v in items.iteritems()]
def pytype(self):
- return '%s.dict' % BUILTINS_MODULE
+ return '%s.dict' % BUILTINS
def get_children(self):
"""get children of a Dict node"""
@@ -672,7 +671,7 @@ class List(NodeNG, Instance, ParentAssignTypeMixin):
self.elts = [const_factory(e) for e in elts]
def pytype(self):
- return '%s.list' % BUILTINS_MODULE
+ return '%s.list' % BUILTINS
def getitem(self, index, context=None):
return self.elts[index]
@@ -739,7 +738,7 @@ class Set(NodeNG, Instance, ParentAssignTypeMixin):
self.elts = [const_factory(e) for e in elts]
def pytype(self):
- return '%s.set' % BUILTINS_MODULE
+ return '%s.set' % BUILTINS
def itered(self):
return self.elts
@@ -821,7 +820,7 @@ class Tuple(NodeNG, Instance, ParentAssignTypeMixin):
self.elts = [const_factory(e) for e in elts]
def pytype(self):
- return '%s.tuple' % BUILTINS_MODULE
+ return '%s.tuple' % BUILTINS
def getitem(self, index, context=None):
return self.elts[index]
diff --git a/raw_building.py b/raw_building.py
index 76b071d6..d86b008b 100644
--- a/raw_building.py
+++ b/raw_building.py
@@ -28,11 +28,10 @@ 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)
-from logilab.astng.bases import Generator
+from logilab.astng.bases import BUILTINS, Generator
from logilab.astng.manager import ASTNGManager
MANAGER = ASTNGManager()
@@ -301,7 +300,7 @@ class InspectBuilder(object):
# Python 2.5.1 (r251:54863, Sep 1 2010, 22:03:14)
# >>> print object.__new__.__module__
# None
- modname = BUILTINS_MODULE
+ modname = BUILTINS
else:
attach_dummy_node(node, name, member)
return True
diff --git a/scoped_nodes.py b/scoped_nodes.py
index dc4ca14d..e09892b9 100644
--- a/scoped_nodes.py
+++ b/scoped_nodes.py
@@ -31,7 +31,6 @@ 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, \
@@ -39,7 +38,7 @@ from logilab.astng.node_classes import Const, DelName, DelAttr, \
are_exclusive, LookupMixIn, const_factory as cf, unpack_infer
from logilab.astng.bases import NodeNG, InferenceContext, Instance,\
YES, Generator, UnboundMethod, BoundMethod, _infer_stmts, copy_context, \
- BUILTINS_NAME
+ BUILTINS
from logilab.astng.mixins import FilterStmtsMixin
from logilab.astng.bases import Statement
from logilab.astng.manager import ASTNGManager
@@ -268,7 +267,7 @@ class Module(LocalsDictNodeNG):
return self._scope_lookup(node, name, offset)
def pytype(self):
- return '%s.module' % BUILTINS_MODULE
+ return '%s.module' % BUILTINS
def display_type(self):
return 'Module'
@@ -481,8 +480,8 @@ class Lambda(LocalsDictNodeNG, FilterStmtsMixin):
def pytype(self):
if 'method' in self.type:
- return '%s.instancemethod' % BUILTINS_MODULE
- return '%s.function' % BUILTINS_MODULE
+ return '%s.instancemethod' % BUILTINS
+ return '%s.function' % BUILTINS
def display_type(self):
if 'method' in self.type:
@@ -740,8 +739,8 @@ class Class(Statement, LocalsDictNodeNG, FilterStmtsMixin):
def pytype(self):
if self.newstyle:
- return '%s.type' % BUILTINS_MODULE
- return '%s.classobj' % BUILTINS_MODULE
+ return '%s.type' % BUILTINS
+ return '%s.classobj' % BUILTINS
def display_type(self):
return 'Class'
@@ -931,7 +930,7 @@ class Class(Statement, LocalsDictNodeNG, FilterStmtsMixin):
#if self.newstyle: XXX cause an infinite recursion error
try:
getattribute = self.getattr('__getattribute__', context)[0]
- if getattribute.root().name != BUILTINS_NAME:
+ if getattribute.root().name != BUILTINS:
# class has a custom __getattribute__ defined
return True
except NotFoundError:
diff --git a/test/unittest_builder.py b/test/unittest_builder.py
index 7a52fdb6..d4048f0d 100644
--- a/test/unittest_builder.py
+++ b/test/unittest_builder.py
@@ -24,10 +24,9 @@ from os.path import join, abspath, dirname
from logilab.common.testlib import TestCase, unittest_main
from pprint import pprint
-from logilab.astng import BUILTINS_MODULE, builder, nodes, InferenceError, NotFoundError
+from logilab.astng import 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
+from logilab.astng.bases import YES, BUILTINS
from logilab.astng.manager import ASTNGManager
MANAGER = ASTNGManager()
@@ -265,14 +264,14 @@ class BuilderTC(TestCase):
def test_inspect_build0(self):
"""test astng tree build from a living object"""
- builtin_astng = MANAGER.astng_from_module_name(BUILTINS_NAME)
+ builtin_astng = MANAGER.astng_from_module_name(BUILTINS)
if sys.version_info < (3, 0):
fclass = builtin_astng['file']
self.assertIn('name', fclass)
self.assertIn('mode', fclass)
self.assertIn('read', fclass)
self.assertTrue(fclass.newstyle)
- self.assertTrue(fclass.pytype(), '%s.type' % BUILTINS_MODULE)
+ self.assertTrue(fclass.pytype(), '%s.type' % BUILTINS)
self.assertIsInstance(fclass['read'], nodes.Function)
# check builtin function has args.args == None
dclass = builtin_astng['dict']
@@ -333,7 +332,7 @@ class BuilderTC(TestCase):
self.assertIn('filename', container)
def test_inspect_build_type_object(self):
- builtin_astng = MANAGER.astng_from_module_name(BUILTINS_NAME)
+ builtin_astng = MANAGER.astng_from_module_name(BUILTINS)
infered = list(builtin_astng.igetattr('object'))
self.assertEqual(len(infered), 1)
diff --git a/test/unittest_inference.py b/test/unittest_inference.py
index 438ee265..fdcff517 100644
--- a/test/unittest_inference.py
+++ b/test/unittest_inference.py
@@ -25,7 +25,7 @@ from logilab.common.testlib import TestCase, unittest_main
from logilab.astng import InferenceError, builder, nodes
from logilab.astng.inference import infer_end as inference_infer_end
from logilab.astng.bases import YES, Instance, BoundMethod, UnboundMethod,\
- path_wrapper, BUILTINS_NAME
+ path_wrapper, BUILTINS
def get_name_node(start_from, name, index=0):
return [n for n in start_from.nodes_of_class(nodes.Name) if n.name == name][index]
@@ -49,7 +49,7 @@ class InferenceUtilsTC(TestCase):
if sys.version_info < (3, 0):
EXC_MODULE = 'exceptions'
else:
- EXC_MODULE = BUILTINS_NAME
+ EXC_MODULE = BUILTINS
class InferenceTC(TestCase):
@@ -112,7 +112,7 @@ a, b= b, a # Gasp !
infered = self.astng['C']['meth1']['var'].infer()
var = infered.next()
self.assertEqual(var.name, 'object')
- self.assertEqual(var.root().name, BUILTINS_NAME)
+ self.assertEqual(var.root().name, BUILTINS)
self.assertRaises(StopIteration, infered.next)
def test_tupleassign_name_inference(self):
@@ -159,7 +159,7 @@ a, b= b, a # Gasp !
infered = self.astng['h'].infer()
var = infered.next()
self.assertEqual(var.name, 'object')
- self.assertEqual(var.root().name, BUILTINS_NAME)
+ self.assertEqual(var.root().name, BUILTINS)
self.assertRaises(StopIteration, infered.next)
def test_advanced_tupleassign_name_inference2(self):
@@ -176,7 +176,7 @@ a, b= b, a # Gasp !
infered = self.astng['k'].infer()
var = infered.next()
self.assertEqual(var.name, 'object')
- self.assertEqual(var.root().name, BUILTINS_NAME)
+ self.assertEqual(var.root().name, BUILTINS)
self.assertRaises(StopIteration, infered.next)
def test_swap_assign_inference(self):
@@ -226,7 +226,7 @@ a, b= b, a # Gasp !
meth1 = infered.next()
self.assertIsInstance(meth1, Instance)
self.assertEqual(meth1.name, 'object')
- self.assertEqual(meth1.root().name, BUILTINS_NAME)
+ self.assertEqual(meth1.root().name, BUILTINS)
self.assertRaises(StopIteration, infered.next)
def test_unbound_method_inference(self):
@@ -487,7 +487,7 @@ class Warning(Warning):
self.assertEqual(ancestor.root().name, EXC_MODULE)
ancestor = ancestors.next()
self.assertEqual(ancestor.name, 'object')
- self.assertEqual(ancestor.root().name, BUILTINS_NAME)
+ self.assertEqual(ancestor.root().name, BUILTINS)
self.assertRaises(StopIteration, ancestors.next)
def test_qqch(self):
@@ -787,7 +787,7 @@ print (make_code)
self.assertEqual(len(infered), 1)
self.assertIsInstance(infered[0], Instance)
self.assertEqual(str(infered[0]),
- 'Instance of %s.type' % BUILTINS_NAME)
+ 'Instance of %s.type' % BUILTINS)
def _test_const_infered(self, node, value):
infered = list(node.infer())
@@ -895,7 +895,7 @@ x = randint(1)
# (__name__ == '__main__') and through pytest (__name__ ==
# 'unittest_inference')
self.assertEqual(value, ['Instance of %s.myarray' % __name__,
- 'Instance of %s.int' % BUILTINS_NAME])
+ 'Instance of %s.int' % BUILTINS])
def test_nonregr_lambda_arg(self):
code = '''
@@ -996,7 +996,7 @@ class SendMailController(object):
my_smtp = SendMailController().smtp
my_me = SendMailController().me
'''
- decorators = set(['%s.property' % BUILTINS_NAME])
+ decorators = set(['%s.property' % BUILTINS])
astng = builder.string_build(code, __name__, __file__)
self.assertEqual(astng['SendMailController']['smtp'].decoratornames(),
decorators)
diff --git a/test/unittest_manager.py b/test/unittest_manager.py
index 07070c49..3fad64a9 100644
--- a/test/unittest_manager.py
+++ b/test/unittest_manager.py
@@ -20,7 +20,7 @@ from logilab.common.testlib import TestCase, unittest_main
import sys
from os.path import join, abspath, dirname
from logilab.astng.manager import ASTNGManager, _silent_no_wrap
-from logilab.astng.bases import BUILTINS_NAME
+from logilab.astng.bases import BUILTINS
DATA = join(dirname(abspath(__file__)), 'data')
@@ -40,11 +40,11 @@ class ASTNGManagerTC(TestCase):
def test_astng_from_class(self):
astng = self.manager.astng_from_class(int)
self.assertEqual(astng.name, 'int')
- self.assertEqual(astng.parent.frame().name, BUILTINS_NAME)
+ self.assertEqual(astng.parent.frame().name, BUILTINS)
astng = self.manager.astng_from_class(object)
self.assertEqual(astng.name, 'object')
- self.assertEqual(astng.parent.frame().name, BUILTINS_NAME)
+ self.assertEqual(astng.parent.frame().name, BUILTINS)
self.assertIn('__setattr__', astng)
def _test_astng_from_zip(self, archive):
@@ -100,10 +100,10 @@ class BorgASTNGManagerTC(TestCase):
"""test that the ASTNGManager is really a borg, i.e. that two different
instances has same cache"""
first_manager = ASTNGManager()
- built = first_manager.astng_from_module_name(BUILTINS_NAME)
+ built = first_manager.astng_from_module_name(BUILTINS)
second_manager = ASTNGManager()
- second_built = first_manager.astng_from_module_name(BUILTINS_NAME)
+ second_built = first_manager.astng_from_module_name(BUILTINS)
self.assertIs(built, second_built)
diff --git a/test/unittest_nodes.py b/test/unittest_nodes.py
index 37f99cb3..736d8657 100644
--- a/test/unittest_nodes.py
+++ b/test/unittest_nodes.py
@@ -21,10 +21,9 @@ import sys
from logilab.common import testlib
from logilab.astng.node_classes import unpack_infer
-from logilab.astng.bases import YES, InferenceContext
+from logilab.astng.bases import BUILTINS, YES, InferenceContext
from logilab.astng.exceptions import ASTNGBuildingException, NotFoundError
-from logilab.astng import BUILTINS_MODULE, builder, nodes
-from logilab.astng.as_string import as_string
+from logilab.astng import builder, nodes
from data import module as test_module
@@ -229,19 +228,19 @@ class ImportNodeTC(testlib.TestCase):
self.assertTrue(isinstance(myos, nodes.Module), myos)
self.assertEqual(myos.name, 'os')
self.assertEqual(myos.qname(), 'os')
- self.assertEqual(myos.pytype(), '%s.module' % BUILTINS_MODULE)
+ self.assertEqual(myos.pytype(), '%s.module' % BUILTINS)
def test_from_self_resolve(self):
spawn = MODULE.igetattr('spawn').next()
self.assertTrue(isinstance(spawn, nodes.Class), spawn)
self.assertEqual(spawn.root().name, 'logilab.common.shellutils')
self.assertEqual(spawn.qname(), 'logilab.common.shellutils.Execute')
- self.assertEqual(spawn.pytype(), '%s.classobj' % BUILTINS_MODULE)
+ self.assertEqual(spawn.pytype(), '%s.classobj' % BUILTINS)
abspath = MODULE2.igetattr('abspath').next()
self.assertTrue(isinstance(abspath, nodes.Function), abspath)
self.assertEqual(abspath.root().name, 'os.path')
self.assertEqual(abspath.qname(), 'os.path.abspath')
- self.assertEqual(abspath.pytype(), '%s.function' % BUILTINS_MODULE)
+ self.assertEqual(abspath.pytype(), '%s.function' % BUILTINS)
def test_real_name(self):
from_ = MODULE['spawn']
diff --git a/test/unittest_scoped_nodes.py b/test/unittest_scoped_nodes.py
index 2046cf54..e0cd55a1 100644
--- a/test/unittest_scoped_nodes.py
+++ b/test/unittest_scoped_nodes.py
@@ -25,8 +25,8 @@ from os.path import join, abspath, dirname
from logilab.common.testlib import TestCase, unittest_main
from logilab.astng import builder, nodes, scoped_nodes, \
- BUILTINS_MODULE, InferenceError, NotFoundError
-from logilab.astng.bases import Instance, BoundMethod, UnboundMethod
+ InferenceError, NotFoundError
+from logilab.astng.bases import BUILTINS, Instance, BoundMethod, UnboundMethod
abuilder = builder.ASTNGBuilder()
DATA = join(dirname(abspath(__file__)), 'data')
@@ -241,7 +241,7 @@ def nested_args(a, (b, c, d)):
method = MODULE2['AbstractClass']['to_override']
self.assertTrue(method.is_abstract(pass_is_abstract=False))
self.assertEqual(method.qname(), 'data.module2.AbstractClass.to_override')
- self.assertEqual(method.pytype(), '%s.instancemethod' % BUILTINS_MODULE)
+ self.assertEqual(method.pytype(), '%s.instancemethod' % BUILTINS)
method = MODULE2['AbstractClass']['return_something']
self.assertFalse(method.is_abstract(pass_is_abstract=False))
# non regression : test raise "string" doesn't cause an exception in is_abstract
@@ -266,7 +266,7 @@ def f():
'''
astng = abuilder.string_build(data, __name__, __file__)
g = list(astng['f'].ilookup('g'))[0]
- self.assertEqual(g.pytype(), '%s.function' % BUILTINS_MODULE)
+ self.assertEqual(g.pytype(), '%s.function' % BUILTINS)
def test_lambda_qname(self):
astng = abuilder.string_build('''