diff options
author | Sylvain Thénault <thenault@gmail.com> | 2014-03-26 14:20:42 +0100 |
---|---|---|
committer | Sylvain Thénault <thenault@gmail.com> | 2014-03-26 14:20:42 +0100 |
commit | fa89464e61ca7636123c6cd267d3730ea5f61f30 (patch) | |
tree | bb05f4ee19ba64dba439f33ec77a17d469c01fa9 | |
parent | 6cd480552017e6e44f0bc0c17687fc1c380fb9c7 (diff) | |
parent | 5d6c158ab8b2d2701bf22add64c2bf41b19f9a34 (diff) | |
download | astroid-git-fa89464e61ca7636123c6cd267d3730ea5f61f30.tar.gz |
Merged in flyingsheep/astroid (pull request #15)
AstroidBuilder.string_build was incompatible with file_stream
-rw-r--r-- | builder.py | 13 | ||||
-rw-r--r-- | scoped_nodes.py | 8 | ||||
-rwxr-xr-x | test/fulltest.sh | 2 | ||||
-rw-r--r-- | test/unittest_scoped_nodes.py | 14 |
4 files changed, 33 insertions, 4 deletions
@@ -131,13 +131,20 @@ class AstroidBuilder(InspectBuilder): except ImportError: modname = splitext(basename(path))[0] # build astroid representation - node = self.string_build(data, modname, path) - node.file_encoding = encoding - return node + module = self._data_build(data, modname, path) + return self._post_build(module, encoding) def string_build(self, data, modname='', path=None): """build astroid from source code string and return rebuilded astroid""" module = self._data_build(data, modname, path) + module.file_bytes = data.encode('utf-8') + return self._post_build(module, 'utf-8') + + def _post_build(self, module, encoding): + """handles encoding and delayed nodes + after a module has been built + """ + module.file_encoding = encoding self._manager.astroid_cache[module.name] = module # post tree building steps after we stored the module in the cache: for from_node in module._from_nodes: diff --git a/scoped_nodes.py b/scoped_nodes.py index d579913a..7141ef71 100644 --- a/scoped_nodes.py +++ b/scoped_nodes.py @@ -25,6 +25,10 @@ __doctype__ = "restructuredtext en" import sys from itertools import chain +try: + from io import BytesIO +except ImportError: + from cStringIO import StringIO as BytesIO from logilab.common.compat import builtins from logilab.common.decorators import cached @@ -217,6 +221,8 @@ class Module(LocalsDictNodeNG): # the file from which as been extracted the astroid representation. It may # be None if the representation has been built from a built-in module file = None + # Alternatively, if built from a string/bytes, this can be set + file_bytes = None # encoding of python source file, so we can get unicode out of it (python2 # only) file_encoding = None @@ -245,6 +251,8 @@ class Module(LocalsDictNodeNG): @property def file_stream(self): + if self.file_bytes is not None: + return BytesIO(self.file_bytes) if self.file is not None: return open(self.file, 'rb') return None diff --git a/test/fulltest.sh b/test/fulltest.sh index bfb28ffe..44999b45 100755 --- a/test/fulltest.sh +++ b/test/fulltest.sh @@ -1,6 +1,6 @@ #!/bin/sh -if [ $@ ] ; then +if [ $# -ne 0 ] ; then PYVERSIONS=$@ else PYVERSIONS="2.5 2.6 2.7 3.2 3.3" diff --git a/test/unittest_scoped_nodes.py b/test/unittest_scoped_nodes.py index aeda7489..a349ca03 100644 --- a/test/unittest_scoped_nodes.py +++ b/test/unittest_scoped_nodes.py @@ -19,6 +19,8 @@ function) """ +from __future__ import with_statement + import sys from os.path import join, abspath, dirname from textwrap import dedent @@ -166,6 +168,18 @@ del appli del sys.path[1] + def test_file_stream_in_memory(self): + data = '''irrelevant_variable is irrelevant''' + astroid = abuilder.string_build(data, 'in_memory') + self.assertEqual(astroid.file_stream.read(), data) + + def test_file_stream_physical(self): + path = join(DATA, 'all.py') + astroid = abuilder.file_build(path, 'all') + with open(path, 'rb') as file_io: + self.assertEqual(astroid.file_stream.read(), file_io.read()) + + class FunctionNodeTC(TestCase): def test_special_attributes(self): |