summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorClaudiu Popa <pcmanticore@gmail.com>2016-08-22 10:58:38 +0300
committerClaudiu Popa <pcmanticore@gmail.com>2016-08-22 10:58:38 +0300
commit261be12ec725ba0124e4ea402f45292ae9f6c78a (patch)
tree89fab2977d8436f03efcf002752fc0c1bddf37fb
parent5ff9e8f6f4d18d721df7cf50b8581f61e6094122 (diff)
downloadastroid-git-261be12ec725ba0124e4ea402f45292ae9f6c78a.tar.gz
Add brain tips for _io.TextIOWrapper's buffer and raw attributes.
-rw-r--r--ChangeLog2
-rw-r--r--astroid/brain/brain_io.py71
-rw-r--r--astroid/tests/unittest_brain.py18
3 files changed, 91 insertions, 0 deletions
diff --git a/ChangeLog b/ChangeLog
index e8bd4c71..c19b0d38 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -3,6 +3,8 @@ Change log for the astroid package (used to be astng)
--
+ * Add brain tips for _io.TextIOWrapper's buffer and raw attributes.
+
* Add `returns` into the proper order in FunctionDef._astroid_fields
The order is important, since it determines the last child,
diff --git a/astroid/brain/brain_io.py b/astroid/brain/brain_io.py
new file mode 100644
index 00000000..f869a4b6
--- /dev/null
+++ b/astroid/brain/brain_io.py
@@ -0,0 +1,71 @@
+# Copyright (c) 2016 Claudiu Popa <pcmanticore@gmail.com>
+
+# Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html
+# For details: https://github.com/PyCQA/astroid/blob/master/COPYING.LESSER
+
+'''Astroid brain hints for some of the _io C objects.'''
+
+import astroid
+from astroid import decorators
+from astroid import util
+from astroid.tree import treeabc
+
+
+BUFFERED = {'BufferedWriter', 'BufferedReader'}
+TextIOWrapper = 'TextIOWrapper'
+FileIO = 'FileIO'
+BufferedWriter = 'BufferedWriter'
+
+
+def _parametrized_lazy_interpreter_object(cls):
+ '''Get an InterpreterObject which delays its object until accessed
+
+ The class can be used when we are not able to access objects from
+ the _io module during transformation time.
+ '''
+
+ @util.register_implementation(treeabc.InterpreterObject)
+ class LazyInterpreterObject(astroid.InterpreterObject):
+
+ @decorators.cachedproperty
+ def _io_module(self):
+ return astroid.MANAGER.ast_from_module_name('_io')
+
+ @decorators.cachedproperty
+ def object(self):
+ attribute_object = self._io_module[cls]
+ return attribute_object.instantiate_class()
+
+ return LazyInterpreterObject
+
+
+def _generic_io_transform(node, name, cls):
+ '''Transform the given name, by adding the given *class* as a member of the node.'''
+
+ module = astroid.MANAGER.ast_from_module_name('io')
+ interpreter_object_cls = _parametrized_lazy_interpreter_object(
+ cls=cls)
+
+ # TODO: for some reason, this works only if it is inserted as the
+ # first value, please check why.
+ node.body.insert(0, interpreter_object_cls(name=name, parent=module))
+
+
+def _transform_text_io_wrapper(node):
+ # This is not always correct, since it can vary with the type of the descriptor,
+ # being stdout, stderr or stdin. But we cannot get access to the name of the
+ # stream, which is why we are using the BufferedWriter class as a default
+ # value
+ return _generic_io_transform(node, name='buffer', cls=BufferedWriter)
+
+
+def _transform_buffered(node):
+ return _generic_io_transform(node, name='raw', cls=FileIO)
+
+
+astroid.MANAGER.register_transform(astroid.ClassDef,
+ _transform_buffered,
+ lambda node: node.name in BUFFERED)
+astroid.MANAGER.register_transform(astroid.ClassDef,
+ _transform_text_io_wrapper,
+ lambda node: node.name == TextIOWrapper)
diff --git a/astroid/tests/unittest_brain.py b/astroid/tests/unittest_brain.py
index e79e5d46..12258eee 100644
--- a/astroid/tests/unittest_brain.py
+++ b/astroid/tests/unittest_brain.py
@@ -517,5 +517,23 @@ class PytestBrainTest(unittest.TestCase):
self.assertIn('mark', module)
+class IOBrainTest(unittest.TestCase):
+
+ @unittest.skipUnless(six.PY3, 'Needs Python 3 io model')
+ def test_sys_streams(self):
+ for name in {'stdout', 'stderr', 'stdin'}:
+ node = astroid.extract_node('''
+ import sys
+ sys.{}
+ '''.format(name))
+ inferred = next(node.infer())
+ buffer = next(inferred.igetattr('buffer'))
+ self.assertIsInstance(buffer, astroid.Instance)
+ self.assertEqual(buffer.name, 'BufferedWriter')
+ raw = next(buffer.igetattr('raw'))
+ self.assertIsInstance(raw, astroid.Instance)
+ self.assertEqual(raw.name, 'FileIO')
+
+
if __name__ == '__main__':
unittest.main()