'Getting a class' function using the Python C API

I'm trying to import the ftp module of Python to a C++ application using the Python C API, but I'm having trouble getting a reference to the FTP.nlst function. (Or any function of the FTP class for that matter.)

My current code snippet looks like this:

Py_Initialize();

PyObject* module = PyImport_ImportModule("ftplib");
PyObject* dict = PyModule_GetDict(module);
PyObject* function = PyDict_GetItemString(dict, "FTP.nlst");

The lines coming after this don't really matter as function is a NULL pointer no matter what I try, so this will always be UB.

Trying something arbitrary, like importing the math module and assigning function to be the fabs function works on the other hand.

Any help is appreciated!



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source