Author: Amaury Forgeot d'Arc <[email protected]>
Branch:
Changeset: r52822:116c15429aa4
Date: 2012-02-23 02:05 +0100
http://bitbucket.org/pypy/pypy/changeset/116c15429aa4/
Log: cpyext: Implement PyEval_MergeCompilerFlags()
diff --git a/pypy/module/cpyext/eval.py b/pypy/module/cpyext/eval.py
--- a/pypy/module/cpyext/eval.py
+++ b/pypy/module/cpyext/eval.py
@@ -1,4 +1,5 @@
from pypy.interpreter.error import OperationError
+from pypy.interpreter.astcompiler import consts
from pypy.rpython.lltypesystem import rffi, lltype
from pypy.module.cpyext.api import (
cpython_api, CANNOT_FAIL, CONST_STRING, FILEP, fread, feof, Py_ssize_tP,
@@ -12,6 +13,12 @@
"PyCompilerFlags", (("cf_flags", rffi.INT),))
PyCompilerFlagsPtr = lltype.Ptr(PyCompilerFlags)
+PyCF_MASK = (consts.CO_FUTURE_DIVISION |
+ consts.CO_FUTURE_ABSOLUTE_IMPORT |
+ consts.CO_FUTURE_WITH_STATEMENT |
+ consts.CO_FUTURE_PRINT_FUNCTION |
+ consts.CO_FUTURE_UNICODE_LITERALS)
+
@cpython_api([PyObject, PyObject, PyObject], PyObject)
def PyEval_CallObjectWithKeywords(space, w_obj, w_arg, w_kwds):
return space.call(w_obj, w_arg, w_kwds)
@@ -194,3 +201,23 @@
else:
flags = 0
return compile_string(space, source, filename, start, flags)
+
+@cpython_api([PyCompilerFlagsPtr], rffi.INT_real, error=CANNOT_FAIL)
+def PyEval_MergeCompilerFlags(space, cf):
+ """This function changes the flags of the current evaluation
+ frame, and returns true on success, false on failure."""
+ result = cf.c_cf_flags != 0
+ current_frame = space.getexecutioncontext().gettopframe_nohidden()
+ if current_frame:
+ codeflags = current_frame.pycode.co_flags
+ compilerflags = codeflags & PyCF_MASK
+ if compilerflags:
+ result = 1;
+ cf.c_cf_flags |= compilerflags
+ # No future keyword at the moment
+ # if codeflags & CO_GENERATOR_ALLOWED:
+ # result = 1
+ # cf.c_cf_flags |= CO_GENERATOR_ALLOWED
+ return result
+
+
diff --git a/pypy/module/cpyext/include/code.h
b/pypy/module/cpyext/include/code.h
--- a/pypy/module/cpyext/include/code.h
+++ b/pypy/module/cpyext/include/code.h
@@ -13,13 +13,19 @@
/* Masks for co_flags above */
/* These values are also in funcobject.py */
-#define CO_OPTIMIZED 0x0001
-#define CO_NEWLOCALS 0x0002
-#define CO_VARARGS 0x0004
-#define CO_VARKEYWORDS 0x0008
+#define CO_OPTIMIZED 0x0001
+#define CO_NEWLOCALS 0x0002
+#define CO_VARARGS 0x0004
+#define CO_VARKEYWORDS 0x0008
#define CO_NESTED 0x0010
#define CO_GENERATOR 0x0020
+#define CO_FUTURE_DIVISION 0x02000
+#define CO_FUTURE_ABSOLUTE_IMPORT 0x04000
+#define CO_FUTURE_WITH_STATEMENT 0x08000
+#define CO_FUTURE_PRINT_FUNCTION 0x10000
+#define CO_FUTURE_UNICODE_LITERALS 0x20000
+
#ifdef __cplusplus
}
#endif
diff --git a/pypy/module/cpyext/include/pythonrun.h
b/pypy/module/cpyext/include/pythonrun.h
--- a/pypy/module/cpyext/include/pythonrun.h
+++ b/pypy/module/cpyext/include/pythonrun.h
@@ -19,7 +19,13 @@
int cf_flags; /* bitmask of CO_xxx flags relevant to future */
} PyCompilerFlags;
+#define PyCF_MASK (CO_FUTURE_DIVISION | CO_FUTURE_ABSOLUTE_IMPORT | \
+ CO_FUTURE_WITH_STATEMENT | CO_FUTURE_PRINT_FUNCTION | \
+ CO_FUTURE_UNICODE_LITERALS)
+#define PyCF_MASK_OBSOLETE (CO_NESTED)
#define PyCF_SOURCE_IS_UTF8 0x0100
+#define PyCF_DONT_IMPLY_DEDENT 0x0200
+#define PyCF_ONLY_AST 0x0400
#define Py_CompileString(str, filename, start) Py_CompileStringFlags(str,
filename, start, NULL)
diff --git a/pypy/module/cpyext/stubs.py b/pypy/module/cpyext/stubs.py
--- a/pypy/module/cpyext/stubs.py
+++ b/pypy/module/cpyext/stubs.py
@@ -2527,12 +2527,6 @@
throw() methods of generator objects."""
raise NotImplementedError
-@cpython_api([PyCompilerFlags], rffi.INT_real, error=CANNOT_FAIL)
-def PyEval_MergeCompilerFlags(space, cf):
- """This function changes the flags of the current evaluation frame, and
returns
- true on success, false on failure."""
- raise NotImplementedError
-
@cpython_api([PyObject], rffi.INT_real, error=CANNOT_FAIL)
def PyWeakref_Check(space, ob):
"""Return true if ob is either a reference or proxy object.
diff --git a/pypy/module/cpyext/test/test_eval.py
b/pypy/module/cpyext/test/test_eval.py
--- a/pypy/module/cpyext/test/test_eval.py
+++ b/pypy/module/cpyext/test/test_eval.py
@@ -283,3 +283,21 @@
print dir(mod)
print mod.__dict__
assert mod.f(42) == 47
+
+ def test_merge_compiler_flags(self):
+ module = self.import_extension('foo', [
+ ("get_flags", "METH_NOARGS",
+ """
+ PyCompilerFlags flags;
+ flags.cf_flags = 0;
+ int result = PyEval_MergeCompilerFlags(&flags);
+ return Py_BuildValue("ii", result, flags.cf_flags);
+ """),
+ ])
+ assert module.get_flags() == (0, 0)
+
+ ns = {'module':module}
+ exec """from __future__ import division \nif 1:
+ def nested_flags():
+ return module.get_flags()""" in ns
+ assert ns['nested_flags']() == (1, 0x2000) # CO_FUTURE_DIVISION
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit