开发者

Python 3 C API. Initialising MIMEText object fails

开发者 https://www.devze.com 2023-03-16 21:56 出处:网络
I am looking to embed Python into a C program. Among other things Python will look after sending email. I have no trouble using the base types from C, but I have trouble using other objects within the

I am looking to embed Python into a C program. Among other things Python will look after sending email. I have no trouble using the base types from C, but I have trouble using other objects within the library.

I am looking to instantiate a MIMEText instance by:

PyObj开发者_Go百科ect *mimeTextModule = PyImport_ImportModule("email.mime.text");    
PyObject* sys_mod_dict = PyImport_GetModuleDict();
PyObject* main_mod = PyMapping_GetItemString(sys_mod_dict, "__main__");
PyObject* mimeText = PyObject_CallMethod(main_mod, "MIMEText", "s", "test email body");

but mimeText is NULL. I have also tried the below, with the same result:

PyObject *mimeTextModule = PyImport_ImportModule("email.mime.text");    
PyObject *mimeTextClass = PyObject_GetAttrString(mimeTextModule, "MIMEText");
PyObject *mimeText = PyObject_CallMethod(mimeTextClass, "__init__", "s", "test email body");

Help appreciated.


Returning NULL means that an exception occurred and you need to check what it is.

If you want to instantiate a class then you do it the same way in C as you do in Python: call it.

0

精彩评论

暂无评论...
验证码 换一张
取 消