5
__main__
スコープで定義されたPythonクラスのインスタンスをC APIで作成したいと考えています。例えばPythonで宣言されたPythonクラスのインスタンスをC APIで作成する
、クラスMyClass
と呼ばれ、以下のように定義される:
class MyClass:
def __init__(self):
pass
クラスタイプが__main__
スコープ下で暮らしています。
Cアプリケーションでは、このクラスのインスタンスを作成します。 PyInstance_New
で簡単に可能だったのは、クラス名を取るためです。しかし、この関数はPython3では利用できません。
代替案についての助言や提案は高く評価されます。コースのエラーチェックの
/* get sys.modules dict */
PyObject* sys_mod_dict = PyImport_GetModuleDict();
/* get the __main__ module object */
PyObject* main_mod = PyMapping_GetItemString(sys_mod_dict, "__main__");
/* call the class inside the __main__ module */
PyObject* instance = PyObject_CallMethod(main_mod, "MyClass", "");
プラス:
おかげで、ポール・