Commit 40303944 authored by Jérome Perrin's avatar Jérome Perrin

patches/python: fix linecache for python scripts on py3

parent a6e7a832
...@@ -143,8 +143,7 @@ def patch_linecache(): ...@@ -143,8 +143,7 @@ def patch_linecache():
if module_globals is None: if module_globals is None:
module_globals = get_globals(sys._getframe(1)) module_globals = get_globals(sys._getframe(1))
# Get source code of ZODB Components following PEP 302, when # Get source code of ZODB Components following PEP 302
# cache is not pre-filled by lazycache.
if (filename.startswith('<portal_components/') and if (filename.startswith('<portal_components/') and
module_globals is not None): module_globals is not None):
data = None data = None
...@@ -158,6 +157,7 @@ def patch_linecache(): ...@@ -158,6 +157,7 @@ def patch_linecache():
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 # 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:
...@@ -190,6 +190,29 @@ def patch_linecache(): ...@@ -190,6 +190,29 @@ def patch_linecache():
# reconsider), for now, we add an arbitrary prefix for cache. # reconsider), for now, we add an arbitrary prefix for cache.
if (filename.startswith('<') and filename.endswith('>')): if (filename.startswith('<') and filename.endswith('>')):
filename = 'erp5-linecache://' + filename filename = 'erp5-linecache://' + filename
# For python scripts, insert a fake PEP302 loader so that
# linecache can find the source code
if basename(filename) in (
'Script (Python)',
'ERP5 Python Script',
'ERP5 Workflow Script',
) and module_globals is not None:
script = module_globals['script']
body = ''
if script._p_jar is None or script._p_jar.opened:
body = script.body()
class PythonScriptLoader:
def __init__(self, filename, body):
self.filename = filename
self.body = body
def get_source(self, name, *args, **kw):
return self.body
assert '__loader___' not in module_globals
module_globals['__loader__'] = PythonScriptLoader(filename, body)
return linecache_lazycache(filename, module_globals) return linecache_lazycache(filename, module_globals)
linecache.lazycache = lazycache linecache.lazycache = lazycache
......
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