diff --git a/product/ERP5Type/patches/pylint.py b/product/ERP5Type/patches/pylint.py index 8e8963a1e206294a820b43e1280b19e543a369ad..b399590f1284516ebbb0dbf2ec6a4149b920c7a1 100644 --- a/product/ERP5Type/patches/pylint.py +++ b/product/ERP5Type/patches/pylint.py @@ -70,6 +70,11 @@ def string_build(self, data, modname='', path=None): module.file_bytes = data.encode('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) if encoding is None: # Encoding not defined in the source file, assuming utf-8... diff --git a/product/ERP5Type/tests/testDynamicClassGeneration.py b/product/ERP5Type/tests/testDynamicClassGeneration.py index fe80e97d1b52b10330d2c77b98aa2dfb0ef7ff7e..2e003536133f754d8f9c57056253ccb8a3072a14 100644 --- a/product/ERP5Type/tests/testDynamicClassGeneration.py +++ b/product/ERP5Type/tests/testDynamicClassGeneration.py @@ -2308,6 +2308,20 @@ undefined() imported_module2_with_version]) 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 class TestZodbExtensionComponent(_TestZodbComponent):