I am attempting to emulate the Python/C API's PyRun_InteractiveLoop() function, but from a different input system used by my employer. The Python FAQ (http://docs.python.org/3/faq/extending.html#how-do-i-tell-incomplete-input-from-invalid-input) has the following code, used to check if a given character array is a complete Python block:
#include <Python.h>
#include <node.h>
#include <errcode.h>
#include <grammar.h>
#include <parsetok.h>
#include <compile.h>
int testcomplete(char *code)
/* code should end in \n */
/* return -1 for error, 0 for incomplete, 1 for complete */
{
node *n;
perrdetail e;
n = PyParser_ParseString(code, &_PyParser_Grammar,
Py_file_input, &e);
if (n == NULL) {
if (e.error == E_EOF)
return 0;
return -1;
}
PyNode_Free(n);
return 1;
}
This gives an undeclared variable error for _PyParser_Grammar. Searching the Python source code, I did not find any headers that declared _PyParser_Grammar. Looking further, I found it referenced by a few functions, in particular meta_grammar() and Py_meta_grammar() as defined in metagrammar.c.
While meta_grammar() is defined in pgen.h, it gives an undefined symbol error despite compiling (g++ for my test code) with -lpython3.3m. A quick nm revealed that meta_grammar() is not a visible symbol in libpython3.3m.so, while Py_meta_grammar() is. However, when searching the Python source code, I have not found any header that declares Py_meta_grammar().
Am I missing something? Where are these symbols declared/defined?
After a great deal of faffing about, it seems there are no headers declaring _PyParser_Grammar. However, declaring
extern grammar _PyParser_Grammar;
in each of the C++ source files that need access works perfectly. Not particularly elegant, but at least it compiles.