0

So i'm doing some stuff with python c api. I'm trying to parse a string with function PyParser_ASTFromString

There is no error here, I then tried to change mod_ty i got to PyObject* with functiin PyAST_mod2obj

I then compile it with the command:

gcc src\main.c -IC:\Users\Malma\AppData\Local\Programs\Python\Python39\include -LC:\Users\Malma\AppData\Local\Programs\Python\Python39\libs -lpython -lpython39 -Wall

then i get error:

undefined reference to `PyAST_mod2obj'

My full code:

#include <Python.h>
#include <Python-ast.h>

int main(int argc, char** argv) {
    Py_Initialize();

    PyArena* arena = PyArena_New();

    mod_ty mod = PyParser_ASTFromString("x = 6", "<string>", Py_file_input, NULL, arena);

    PyObject* result = PyAST_mod2obj(mod);
    printf("%s\n", result->ob_type->tp_name);
    
    PyArena_Free(arena);
    
    return 0;
}
Malma
  • 71
  • 1
  • 4
  • Where are you getting `PyAST_mod2obj()` from? It's apparently not listed as a Python C function: https://docs.python.org/3/search.html?q=PyAST_mod2obj – Andrew Henle Jan 16 '22 at 15:01
  • @andrewhenle i get from `Python-ast.h` – Malma Jan 16 '22 at 15:07
  • You sure you're not trying to compile Python 2 source code under Python 3? – Andrew Henle Jan 16 '22 at 15:13
  • @andrewhenle Idk, but I've inspected the header file `Python-ast.h` and I found function `PyAST_mod2obj` has been defined. For python version problem, I don't know if that function only for python2, but if it is a function for python2, is there any alternative for this function in python3? – Malma Jan 16 '22 at 15:21

0 Answers0