Commit 5027502c authored by Jérome Perrin's avatar Jérome Perrin

patches/pylint: fix regression with namedtuples

with namedtuples and unicode literals enabled, our patch break with:

    Module py2stdlib, line 266, in infer_named_tuple
      ''' % {'name': name, 'fields': attributes})
    Module Products.ERP5Type.patches.pylint, line 74, in string_build
      encoding = _guess_encoding(data)
    Module astroid.builder, line 65, in _guess_encoding
      if string.startswith('\xef\xbb\xbf'):
    UnicodeDecodeError: 'ascii' codec can't decode byte 0xef in position 0: ordinal not in range(128)

because pylint/astroid uses string_build to evaluate the named tuple and the source code enabled 
unicode_literals future, string_build is called with an unicode object.

    -> for inferred in node.func.infer():
      astroid-1.3.8-py2.7.egg/astroid/bases.py(391)infer()
    -> return self._explicit_inference(self, context, **kwargs)
      astroid-1.3.8-py2.7.egg/astroid/brain/py2stdlib.py(266)infer_named_tuple()
    -> ''' % {'name': name, 'fields': attributes})
    > erp5/product/ERP5Type/patches/pylint.py(77)string_build()
    -> data = data.encode('utf-8')
    (Pdb) up
    > astroid-1.3.8-py2.7.egg/astroid/brain/py2stdlib.py(266)infer_named_tuple()
    -> ''' % {'name': name, 'fields': attributes})
    (Pdb) l
    249     def infer_named_tuple(node, context=None):
    250         """Specific inference function for namedtuple CallFunc node"""
    251         class_node, name, attributes = infer_func_form(node, nodes.Tuple._proxied,
    252                                                        context=context)
    253         fake = AstroidBuilder(MANAGER).string_build('''
    254     class %(name)s(tuple):
    255         _fields = %(fields)r
    256         def _asdict(self):
    257             return self.__dict__
    258         @classmethod
    259         def _make(cls, iterable, new=tuple.__new__, len=len):
    260             return new(cls, iterable)
    261         def _replace(_self, **kwds):
    262             result = _self._make(map(kwds.pop, %(fields)r, _self))
    263             if kwds:
    264                 raise ValueError('Got unexpected field names: %%r' %% list(kwds))
    265             return result
    266  ->     ''' % {'name': name, 'fields': attributes})
    (Pdb) pp name
    u'NamedTuple'
    (Pdb) pp attributes
    [u'foo', u'bar']
    (Pdb) pp [ (arg, arg.value) for arg in node.args ]
    [(<Const(unicode) l.4 [checkPythonSourceCodelXOzr3] at 0x7f9f1caee250>,
      u'NamedTuple'),
     (<Const(unicode) l.4 [checkPythonSourceCodelXOzr3] at 0x7f9f1caeebd0>,
      u'foo bar')]
    
    

/reviewed-on !978
parent df0701cf
...@@ -70,6 +70,11 @@ def string_build(self, data, modname='', path=None): ...@@ -70,6 +70,11 @@ def string_build(self, data, modname='', path=None):
module.file_bytes = data.encode('utf-8') module.file_bytes = data.encode('utf-8')
return self._post_build(module, 'utf-8') return self._post_build(module, 'utf-8')
""" """
if isinstance(data, unicode):
# When called internally by pylint/astroid and if the source code imports
# `unicode_literals`, the source code may end up being an unicode object
# (example: `infer_named_tuple()`)
data = data.encode('utf-8')
encoding = _guess_encoding(data) encoding = _guess_encoding(data)
if encoding is None: if encoding is None:
# Encoding not defined in the source file, assuming utf-8... # Encoding not defined in the source file, assuming utf-8...
......
...@@ -2308,6 +2308,20 @@ undefined() ...@@ -2308,6 +2308,20 @@ undefined()
imported_module2_with_version]) imported_module2_with_version])
self.assertEqual(component.getTextContentWarningMessageList(), []) self.assertEqual(component.getTextContentWarningMessageList(), [])
def testPylintNamedtupleUnicodeLiteralsRegression(self):
# regression for a bug with our pylint patches on guess encoding
# a named tuple with unicode_literals enabled cause UnicodeDecodeError
component = self._newComponent(
self._generateReference('TestPylintNamedtupleUnicodeLiteralsRegression'))
component.setTextContent("""
from __future__ import unicode_literals
from collections import namedtuple
namedtuple('NamedTuple', 'foo bar')(1, 2)
""")
self.tic()
self.assertEqual(component.getTextContentWarningMessageList(), [])
self.assertEqual(component.getTextContentErrorMessageList(), [])
from Products.ERP5Type.Core.ExtensionComponent import ExtensionComponent from Products.ERP5Type.Core.ExtensionComponent import ExtensionComponent
class TestZodbExtensionComponent(_TestZodbComponent): class TestZodbExtensionComponent(_TestZodbComponent):
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment