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
......@@ -26,6 +26,7 @@
#
##############################################################################
import six
import sys
import traceback
......@@ -84,6 +85,11 @@ class TestERP5PythonScript(ERP5TypeTestCase):
self.script.setBody('return "Hello " + who')
self.assertEqual(self.script("world"), "Hello world")
if six.PY2:
filename = 'ERP5 Python Script'
else:
filename = 'ERP5 Python Script:%s' % self.script.getPath()
try:
self.script(123)
except TypeError:
......@@ -91,8 +97,8 @@ class TestERP5PythonScript(ERP5TypeTestCase):
# python script code is visible in traceback
self.assertEqual(
traceback.format_tb(tb)[-1],
' File "ERP5 Python Script", line 1, in %s\n'
' return "Hello " + who\n' % self.id()
' File "%s", line 1, in %s\n'
' return "Hello " + who\n' % (filename, self.id())
)
else:
self.fail('Exception not raised')
......@@ -126,6 +132,11 @@ class TestERP5WorkflowScript(ERP5TypeTestCase):
self.script.setBody('return "Hello " + state_change')
self.assertEqual(self.script("world"), "Hello world")
if six.PY2:
filename = 'ERP5 Workflow Script'
else:
filename = 'ERP5 Workflow Script:%s' % self.script.getPath()
try:
self.script(123)
except TypeError:
......@@ -133,8 +144,8 @@ class TestERP5WorkflowScript(ERP5TypeTestCase):
# python script code is visible in traceback
self.assertEqual(
traceback.format_tb(tb)[-1],
' File "ERP5 Workflow Script", line 1, in script_test_script\n'
' return "Hello " + state_change\n'
(' File "%s", line 1, in script_test_script\n'
' return "Hello " + state_change\n') % filename
)
else:
self.fail('Exception not raised')
......@@ -332,7 +332,10 @@ class ComponentDynamicPackage(ModuleType):
component = getattr(site.portal_components, component_id)
relative_url = component.getRelativeUrl()
module_file = '<' + relative_url + '>'
if six.PY2:
module_file = '<' + relative_url + '>'
else:
module_file = 'erp5://' + relative_url
module_fullname = '%s.%s_version.%s' % (self._namespace, version, name)
module = ModuleType(module_fullname, component.getDescription())
......
......@@ -155,17 +155,20 @@ def patch_linecache():
data = get_source(name)
except (ImportError, AttributeError):
pass
return data.splitlines(True) if data is not None else ()
if basename(filename) in ('Script (Python)', 'ERP5 Python Script', 'ERP5 Workflow Script'):
try:
script = module_globals['script']
if script._p_jar.opened:
return script.body().splitlines(True)
except Exception:
pass
return ()
if module_globals is not None:
# in-ZODB python scripts
if basename(filename) in ('Script (Python)', 'ERP5 Python Script', 'ERP5 Workflow Script'):
try:
script = module_globals['script']
if script._p_jar.opened:
return script.body().splitlines(True)
except Exception:
pass
return ()
# TALES expressions
x = expr_search(filename)
if x:
return x.groups()
......@@ -173,4 +176,5 @@ def patch_linecache():
linecache.getlines = getlines
patch_linecache()
if sys.version_info[:3] < (3, ):
patch_linecache()
......@@ -3401,13 +3401,18 @@ break_at_import()
return self._component_tool.readTestOutput()
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('''
File "<portal_components/test.erp5.testRunLiveTestImportError>", line 4, in <module>
File "%(module_file)s", line 4, in <module>
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
ImportError: No module named non.existing.module
''', output)
''' % dict(module_file=module_file), output)
output = runLiveTest('testDoesNotExist_import_error_because_module_does_not_exist')
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