This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: PyMethodDef does NOT have any fields contain context in embedded C
Type: enhancement Stage:
Components: C API Versions: Python 3.8
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: dexter
Priority: normal Keywords:

Created on 2020-09-30 17:38 by dexter, last changed 2022-04-11 14:59 by admin.

Messages (2)
msg377714 - (view) Author: dexter (dexter) Date: 2020-09-30 17:38
I am trying to create app embedded python as interpreter, and I use PyImport_AppendInittab to import some helper functions to python. on website the example is excellent when you already know how many functions we want to import. but when my functions are based on a runtime dynamic list, even I create all PyModuleDef and PyMethodDef, but when the function all goes to PyCFunction which only has two PyObject* and return PyObject* i lost context information about C. may be I am not clear, but what I am expecting is PyMethodDef should define like
struct PyMethodDef {
    const char  *ml_name;   /* The name of the built-in function/method */
    PyCFunction ml_meth;    /* The C function that implements it */
    int         ml_flags;   /* Combination of METH_xxx flags, which mostly
                               describe the args expected by the C func */
    const char  *ml_doc;    /* The __doc__ attribute, or NULL */
    void* extra; /* <================= here is NEW */ 
};

and PyCFunction should define like

typedef PyObject *(*PyCFunction)(PyObject *, PyObject *, void * /* for extra void* */);
msg377720 - (view) Author: dexter (dexter) Date: 2020-09-30 20:01
for example, there are some of shared libs, all these libs have a function named getList() exported. and getList() will return a list of names which also as a function name been exported.

shared lib A
export getList() -> return ["aaa", "bbb"]
export aaa()
export bbb();

shared lib A
export getList() -> return ["xyz", "abc", "mno"]
export xyz()
export abc();
export mno();

and I want expose shared lib A aaa, bbb or shared lib B xyz abc mno when I load them, (maybe A, maybe B, or maybe both if they exists, work like plugins)

I have a loop like

for (int i = 0; i < length_of_list; ++i) {
    char* funcName = getName(i);
    PyMethodDef def { funcName, fooCall, MATH_VARARGS, nullptr, /* funcname here */};
    /// ....
}

PyObject* fooCall(PyObject* self, PyObject* args, void* context) {
    char* funcname = (char*) context;
    /// .... load func based on name.
}
History
Date User Action Args
2022-04-11 14:59:36adminsetgithub: 86061
2020-09-30 20:01:45dextersetmessages: + msg377720
2020-09-30 19:03:33dextersettitle: PyMethodDef does NOT have any field contains context in embedded C -> PyMethodDef does NOT have any fields contain context in embedded C
2020-09-30 17:38:05dextercreate