implement 1-arg exec()
authorStefan Behnel <stefan_ml@behnel.de>
Mon, 27 Aug 2012 18:33:08 +0000 (20:33 +0200)
committerStefan Behnel <stefan_ml@behnel.de>
Mon, 27 Aug 2012 18:33:08 +0000 (20:33 +0200)
Cython/Compiler/Builtin.py
Cython/Compiler/Nodes.py
Cython/Utility/Builtins.c
tests/run/cython3.pyx

index 6a03d91..6b466ee 100644 (file)
@@ -55,6 +55,7 @@ bad:
 
 globals_utility_code = UtilityCode.load_cached("Globals", "Builtins.c")
 pyexec_utility_code = UtilityCode.load_cached("PyExec", "Builtins.c")
+pyexec_globals_utility_code = UtilityCode.load_cached("PyExecGlobals", "Builtins.c")
 
 intern_utility_code = UtilityCode(
 proto = """
@@ -215,10 +216,12 @@ builtin_function_table = [
     BuiltinFunction('delattr',    "OO",   "r",     "PyObject_DelAttr"),
     BuiltinFunction('dir',        "O",    "O",     "PyObject_Dir"),
     BuiltinFunction('divmod',     "OO",   "O",     "PyNumber_Divmod"),
-    BuiltinFunction('exec',       "OOO",  "O",     "__Pyx_PyRun3",
+    BuiltinFunction('exec',       "OOO",  "O",     "__Pyx_PyExec3",
                     utility_code = pyexec_utility_code),
-    BuiltinFunction('exec',       "OO",   "O",     "__Pyx_PyRun2",
+    BuiltinFunction('exec',       "OO",   "O",     "__Pyx_PyExec2",
                     utility_code = pyexec_utility_code),
+    BuiltinFunction('exec',       "O",    "O",     "__Pyx_PyExecGlobals",
+                    utility_code = pyexec_globals_utility_code),
     #('eval',      "",     "",      ""),
     #('execfile',  "",     "",      ""),
     #('filter',    "",     "",      ""),
index f1a2cf3..e7daa6b 100644 (file)
@@ -4685,7 +4685,7 @@ class ExecStatNode(StatNode):
             args.append( arg.py_result() )
         args = tuple(args + ['0', '0'][:3-len(args)])
         temp_result = code.funcstate.allocate_temp(PyrexTypes.py_object_type, manage_ref=True)
-        code.putln("%s = __Pyx_PyRun3(%s, %s, %s);" % (
+        code.putln("%s = __Pyx_PyExec3(%s, %s, %s);" % (
                 (temp_result,) + args))
         for arg in self.args:
             arg.generate_disposal_code(code)
index bcd4129..db24fd0 100644 (file)
@@ -11,7 +11,7 @@ static PyObject* __Pyx_Globals(void); /*proto*/
 // of Python names.  Supporting cdef names in the module and write
 // access requires a rewrite as a dedicated class.
 
-static PyObject* __Pyx_Globals() {
+static PyObject* __Pyx_Globals(void) {
     Py_ssize_t i;
     /*PyObject *d;*/
     PyObject *names = NULL;
@@ -47,19 +47,37 @@ bad:
     return NULL;
 }
 
+//////////////////// PyExecGlobals.proto ////////////////////
+
+static PyObject* __Pyx_PyExecGlobals(PyObject*);
+
+//////////////////// PyExecGlobals ////////////////////
+//@requires: Globals
+//@requires: PyExec
+
+static PyObject* __Pyx_PyExecGlobals(PyObject* code) {
+    PyObject* result;
+    PyObject* globals = __Pyx_Globals();
+    if (unlikely(!globals))
+        return NULL;
+    result = __Pyx_PyExec2(code, globals);
+    Py_DECREF(globals);
+    return result;
+}
+
 //////////////////// PyExec.proto ////////////////////
 
-static PyObject* __Pyx_PyRun3(PyObject*, PyObject*, PyObject*);
-static CYTHON_INLINE PyObject* __Pyx_PyRun2(PyObject*, PyObject*);
+static PyObject* __Pyx_PyExec3(PyObject*, PyObject*, PyObject*);
+static CYTHON_INLINE PyObject* __Pyx_PyExec2(PyObject*, PyObject*);
 
 //////////////////// PyExec ////////////////////
 //@substitute: naming
 
-static CYTHON_INLINE PyObject* __Pyx_PyRun2(PyObject* o, PyObject* globals) {
-    return __Pyx_PyRun3(o, globals, NULL);
+static CYTHON_INLINE PyObject* __Pyx_PyExec2(PyObject* o, PyObject* globals) {
+    return __Pyx_PyExec3(o, globals, NULL);
 }
 
-static PyObject* __Pyx_PyRun3(PyObject* o, PyObject* globals, PyObject* locals) {
+static PyObject* __Pyx_PyExec3(PyObject* o, PyObject* globals, PyObject* locals) {
     PyObject* result;
     PyObject* s = 0;
     char *code = 0;
index c1bfb70..0e14c26 100644 (file)
@@ -5,8 +5,7 @@
 cimport cython
 
 __doc__ = """
->>> items = list(locals_function(1).items())
->>> items.sort()
+>>> items = sorted(locals_function(1).items())
 >>> for item in items:
 ...     print('%s = %r' % item)
 a = 1
@@ -49,6 +48,17 @@ def exec2_function(cmd):
     exec(cmd, g)
     return g
 
+EXEC_GLOBAL = [5]
+
+def exec1_function(cmd):
+    """
+    >>> exec1_function('EXEC_GLOBAL.append(1)')
+    [1]
+    """
+    old = len(EXEC_GLOBAL)
+    exec(cmd)
+    return EXEC_GLOBAL[old:]
+
 ustring = "abcdefg"
 
 def unicode_literals():