Commit 3b11c726 authored by Jérome Perrin's avatar Jérome Perrin

core: enable print_function in checkPythonSourceCodeAsJSON

This flags python2 style usage of print as a statement
parent 4029d75f
from six import string_types as basestring from six import string_types as basestring
import re
import json import json
from Products.ERP5Type.Utils import checkPythonSourceCode from Products.ERP5Type.Utils import checkPythonSourceCode
match_PEP263 = re.compile(r'^[ \t\f]*#.*?coding[:=][ \t]*([-_.a-zA-Z0-9]+)').match
def checkPythonSourceCodeAsJSON(self, data, REQUEST=None): def checkPythonSourceCodeAsJSON(self, data, REQUEST=None):
""" """
Check Python source suitable for Source Code Editor and return a JSON object Check Python source suitable for Source Code Editor and return a JSON object
...@@ -24,8 +29,19 @@ def checkPythonSourceCodeAsJSON(self, data, REQUEST=None): ...@@ -24,8 +29,19 @@ def checkPythonSourceCodeAsJSON(self, data, REQUEST=None):
signature_parts += [data['params']] signature_parts += [data['params']]
signature = ", ".join(signature_parts) signature = ", ".join(signature_parts)
function_name = "function_name" # keep the PEP263 magic comment
body = "def %s(%s):\n%s" % (function_name, pep263_comment = '#'
lines = data['code'].splitlines() + ['', '']
for line in lines[0], lines[1]:
m = match_PEP263(line)
if m:
pep263_comment = '# coding=' + m.groups()[0]
break
body = "%s\n"\
"from __future__ import print_function\n"\
"def function_name(%s):\n%s" % (
pep263_comment,
signature, signature,
indent(data['code']) or " pass") indent(data['code']) or " pass")
else: else:
...@@ -34,7 +50,7 @@ def checkPythonSourceCodeAsJSON(self, data, REQUEST=None): ...@@ -34,7 +50,7 @@ def checkPythonSourceCodeAsJSON(self, data, REQUEST=None):
message_list = checkPythonSourceCode(body.encode('utf8'), data.get('portal_type')) message_list = checkPythonSourceCode(body.encode('utf8'), data.get('portal_type'))
for message_dict in message_list: for message_dict in message_list:
if is_script: if is_script:
message_dict['row'] = message_dict['row'] - 2 message_dict['row'] = message_dict['row'] - 4
else: else:
message_dict['row'] = message_dict['row'] - 1 message_dict['row'] = message_dict['row'] - 1
......
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