Commit 9903570f authored by Jérome Perrin's avatar Jérome Perrin Committed by Arnaud Fontaine

py3: review linecache patches

 - Products.PythonScripts now has a __loader__
 - do not use < > as filename for component on py3, this makes linecache
 work out of the box. On py2 I think it was causing errors trying to
 actually open the file, but this does not seem needed on py3 and
 simplifies everything
parent 0920371a
Pipeline #35907 failed with stage
in 0 seconds
...@@ -26,6 +26,7 @@ ...@@ -26,6 +26,7 @@
# #
############################################################################## ##############################################################################
import six
import sys import sys
import traceback import traceback
...@@ -84,6 +85,11 @@ class TestERP5PythonScript(ERP5TypeTestCase): ...@@ -84,6 +85,11 @@ class TestERP5PythonScript(ERP5TypeTestCase):
self.script.setBody('return "Hello " + who') self.script.setBody('return "Hello " + who')
self.assertEqual(self.script("world"), "Hello world") self.assertEqual(self.script("world"), "Hello world")
if six.PY2:
filename = 'ERP5 Python Script'
else:
filename = 'ERP5 Python Script:%s' % self.script.getPath()
try: try:
self.script(123) self.script(123)
except TypeError: except TypeError:
...@@ -91,8 +97,8 @@ class TestERP5PythonScript(ERP5TypeTestCase): ...@@ -91,8 +97,8 @@ class TestERP5PythonScript(ERP5TypeTestCase):
# python script code is visible in traceback # python script code is visible in traceback
self.assertEqual( self.assertEqual(
traceback.format_tb(tb)[-1], traceback.format_tb(tb)[-1],
' File "ERP5 Python Script", line 1, in %s\n' ' File "%s", line 1, in %s\n'
' return "Hello " + who\n' % self.id() ' return "Hello " + who\n' % (filename, self.id())
) )
else: else:
self.fail('Exception not raised') self.fail('Exception not raised')
...@@ -126,6 +132,11 @@ class TestERP5WorkflowScript(ERP5TypeTestCase): ...@@ -126,6 +132,11 @@ class TestERP5WorkflowScript(ERP5TypeTestCase):
self.script.setBody('return "Hello " + state_change') self.script.setBody('return "Hello " + state_change')
self.assertEqual(self.script("world"), "Hello world") self.assertEqual(self.script("world"), "Hello world")
if six.PY2:
filename = 'ERP5 Workflow Script'
else:
filename = 'ERP5 Workflow Script:%s' % self.script.getPath()
try: try:
self.script(123) self.script(123)
except TypeError: except TypeError:
...@@ -133,8 +144,8 @@ class TestERP5WorkflowScript(ERP5TypeTestCase): ...@@ -133,8 +144,8 @@ class TestERP5WorkflowScript(ERP5TypeTestCase):
# python script code is visible in traceback # python script code is visible in traceback
self.assertEqual( self.assertEqual(
traceback.format_tb(tb)[-1], traceback.format_tb(tb)[-1],
' File "ERP5 Workflow Script", line 1, in script_test_script\n' (' File "%s", line 1, in script_test_script\n'
' return "Hello " + state_change\n' ' return "Hello " + state_change\n') % filename
) )
else: else:
self.fail('Exception not raised') self.fail('Exception not raised')
...@@ -332,7 +332,10 @@ class ComponentDynamicPackage(ModuleType): ...@@ -332,7 +332,10 @@ class ComponentDynamicPackage(ModuleType):
component = getattr(site.portal_components, component_id) component = getattr(site.portal_components, component_id)
relative_url = component.getRelativeUrl() relative_url = component.getRelativeUrl()
if six.PY2:
module_file = '<' + relative_url + '>' module_file = '<' + relative_url + '>'
else:
module_file = 'erp5://' + relative_url
module_fullname = '%s.%s_version.%s' % (self._namespace, version, name) module_fullname = '%s.%s_version.%s' % (self._namespace, version, name)
module = ModuleType(module_fullname, component.getDescription()) module = ModuleType(module_fullname, component.getDescription())
......
...@@ -155,9 +155,10 @@ def patch_linecache(): ...@@ -155,9 +155,10 @@ def patch_linecache():
data = get_source(name) data = get_source(name)
except (ImportError, AttributeError): except (ImportError, AttributeError):
pass pass
return data.splitlines(True) if data is not None else () return data.splitlines(True) if data is not None else ()
if module_globals is not None:
# in-ZODB python scripts
if basename(filename) in ('Script (Python)', 'ERP5 Python Script', 'ERP5 Workflow Script'): if basename(filename) in ('Script (Python)', 'ERP5 Python Script', 'ERP5 Workflow Script'):
try: try:
script = module_globals['script'] script = module_globals['script']
...@@ -166,6 +167,8 @@ def patch_linecache(): ...@@ -166,6 +167,8 @@ def patch_linecache():
except Exception: except Exception:
pass pass
return () return ()
# TALES expressions
x = expr_search(filename) x = expr_search(filename)
if x: if x:
return x.groups() return x.groups()
...@@ -173,4 +176,5 @@ def patch_linecache(): ...@@ -173,4 +176,5 @@ def patch_linecache():
linecache.getlines = getlines linecache.getlines = getlines
patch_linecache() if sys.version_info[:3] < (3, ):
patch_linecache()
...@@ -3401,13 +3401,18 @@ break_at_import() ...@@ -3401,13 +3401,18 @@ break_at_import()
return self._component_tool.readTestOutput() return self._component_tool.readTestOutput()
output = runLiveTest('testRunLiveTestImportError') output = runLiveTest('testRunLiveTestImportError')
relative_url = 'portal_components/test.erp5.testRunLiveTestImportError'
if six.PY2:
module_file = '<' + relative_url + '>'
else:
module_file = 'erp5://' + relative_url
self.assertIn(''' self.assertIn('''
File "<portal_components/test.erp5.testRunLiveTestImportError>", line 4, in <module> File "%(module_file)s", line 4, in <module>
break_at_import() break_at_import()
File "<portal_components/test.erp5.testRunLiveTestImportError>", line 3, in break_at_import File "%(module_file)s", line 3, in break_at_import
import non.existing.module # pylint:disable=import-error import non.existing.module # pylint:disable=import-error
ImportError: No module named non.existing.module ImportError: No module named non.existing.module
''', output) ''' % dict(module_file=module_file), output)
output = runLiveTest('testDoesNotExist_import_error_because_module_does_not_exist') output = runLiveTest('testDoesNotExist_import_error_because_module_does_not_exist')
self.assertIn( self.assertIn(
......
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