Commit 9d2c4626 authored by Boxiang Sun's avatar Boxiang Sun Committed by Kirill Smelkov

Add support for PyFrame_SetLineNumber & PyCode_HasFreeVars

In Pyston C API, there have new added PyFrame_SetLineNumber and
PyCode_HasFreeVars two APIs. Add these two inlined C functions for
CPython.

PyFrame_SetLineNumber is for get frame line number.
PyCode_HasFreeVars is for check whether PyCodeObject has freevars.

[ @kirr: reworked patch for better structure ]

/reviewed-on nexedi/pycapicompat!1
parent 97751356
...@@ -33,7 +33,7 @@ ...@@ -33,7 +33,7 @@
* https://lab.nexedi.com/nexedi/pycapicompat/raw/master/pycapicompat.h * https://lab.nexedi.com/nexedi/pycapicompat/raw/master/pycapicompat.h
* (or https://lab.nexedi.com/nexedi/pycapicompat.git for Git access) * (or https://lab.nexedi.com/nexedi/pycapicompat.git for Git access)
* *
* Last updated: 2016-06-13 * Last updated: 2016-06-17
*/ */
#include <Python.h> #include <Python.h>
...@@ -42,32 +42,37 @@ ...@@ -42,32 +42,37 @@
extern "C" { extern "C" {
#endif #endif
// TODO(Daetalus) /* we support: CPython, Pyston, PyPy.
#if 0 * define also CPYTHON_VERSION as PY_VERSION is defined on all implementations */
#ifndef IMPLEMENTATION_SUPPORTS_EXTENDED_C_API #if !defined(PYSTON_VERSION) && !defined(PYPY_VERSION)
#define PyErr_GetExcInfoType() PyThreadState_GET()->exc_type # define CPYTHON_VERSION PY_VERSION
#endif
#ifdef PYSTON_VERSION
foo = PyErr_GetExcInfoType(); // a new API function
#else
foo = PyThreadState_GET()->exc_type; // direct field access
#endif
#endif #endif
// info about free variables
// TODO(Daetalus) verify and fill more
#ifdef CPYTHON_VERSION #ifdef CPYTHON_VERSION
// TODO(Daetalus) see how Cython does this check
#elif defined(PYSTON_VERSION) // PyCode. Check whether there are free variables
void PyCode_HasFreeVars(BoxedCode* code) { static inline int PyCode_HasFreeVars(PyCodeObject *co) {
return code->source->getScopeInfo()->takesClosure(); return PyCode_GetNumFree(co) > 0 ? 1 : 0;
} }
#else
# error TODO pypy:PyCode_HasFreeVars not implemented for your python version
#endif
// TODO(Daetalus) add more stuff. // PyFrame. Set frame line number
static inline void
PyFrame_SetLineNumber(PyFrameObject *f, int lineno) {
f->f_lineno = lineno;
}
#elif defined(PYSTON_VERSION)
// PyCode_HasFreeVars(co) provided out of the box
// PyFrame_SetLineNumber(f, lineno) provided out of the box
#elif defined(PYPY_VERSION)
// TODO: PyFrame_SetLineNumber(f, lineno)
// TODO: PyCode_HasFreeVars(co)
#endif
#ifdef __cplusplus #ifdef __cplusplus
} }
......
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