From 6097808526ad57b3c4c7bca24870470b315af14f Mon Sep 17 00:00:00 2001 From: Stefan Behnel Date: Wed, 3 Nov 2010 20:20:06 +0100 Subject: [PATCH] support exec(cmd,g) as alternative signature for exec(cmd,g,l) in -3 mode --- Cython/Compiler/Builtin.py | 6 ++++++ Cython/Compiler/Scanning.py | 7 +++++-- tests/run/cython3.pyx | 19 +++++++++++++++++++ 3 files changed, 30 insertions(+), 2 deletions(-) diff --git a/Cython/Compiler/Builtin.py b/Cython/Compiler/Builtin.py index b88dba9b..f7b59806 100644 --- a/Cython/Compiler/Builtin.py +++ b/Cython/Compiler/Builtin.py @@ -18,6 +18,7 @@ builtin_function_table = [ ('dir', "O", "O", "PyObject_Dir"), ('divmod', "OO", "O", "PyNumber_Divmod"), ('exec', "OOO", "O", "__Pyx_PyRun"), + ('exec', "OO", "O", "__Pyx_PyRun2"), #('eval', "", "", ""), #('execfile', "", "", ""), #('filter', "", "", ""), @@ -181,8 +182,13 @@ proto = """ #endif #endif static PyObject* __Pyx_PyRun(PyObject*, PyObject*, PyObject*); +static CYTHON_INLINE PyObject* __Pyx_PyRun2(PyObject*, PyObject*); """, impl = """ +static CYTHON_INLINE PyObject* __Pyx_PyRun2(PyObject* o, PyObject* globals) { + return __Pyx_PyRun(o, globals, NULL); +} + static PyObject* __Pyx_PyRun(PyObject* o, PyObject* globals, PyObject* locals) { PyObject* result; PyObject* s = 0; diff --git a/Cython/Compiler/Scanning.py b/Cython/Compiler/Scanning.py index 44a27315..1f60f703 100644 --- a/Cython/Compiler/Scanning.py +++ b/Cython/Compiler/Scanning.py @@ -358,8 +358,11 @@ class PyrexScanner(Scanner): self.error("Unrecognized character") if sy == IDENT: if systring in self.keywords: - if systring == 'print' and \ - print_function in self.context.future_directives: + if systring == 'print' and print_function in self.context.future_directives: + self.keywords.remove('print') + systring = EncodedString(systring) + elif systring == 'exec' and self.context.language_level >= 3: + self.keywords.remove('exec') systring = EncodedString(systring) else: sy = systring diff --git a/tests/run/cython3.pyx b/tests/run/cython3.pyx index 8c79cec6..b197eb35 100644 --- a/tests/run/cython3.pyx +++ b/tests/run/cython3.pyx @@ -15,6 +15,25 @@ def print_function(*args): """ print(*args) # this isn't valid Py2 syntax +def exec3_function(cmd): + """ + >>> exec3_function('a = 1+1')['a'] + 2 + """ + g = {} + l = {} + exec(cmd, g, l) + return l + +def exec2_function(cmd): + """ + >>> exec2_function('a = 1+1')['a'] + 2 + """ + g = {} + exec(cmd, g) + return g + ustring = "abcdefg" def unicode_literals(): -- 2.26.2