Last active
April 8, 2020 11:22
-
-
Save ndevenish/9f114ae974ad34a6b904c6d37dce7d5f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
PRFX=$(HOME)/autobuild/build-Linux-pre-release-gtk3-python | |
CXXFLAGS=-I$(PRFX)/include/python3.6m/ | |
LDFLAGS=-L$(PRFX)/lib -lpython3.6m -Wl,-rpath=$(PRFX)/lib | |
all: standalone-python-test | |
standalone-python-test: standalone-python-test.cc | |
$(CXX) $^ $(CXXFLAGS) -o $@ $(LDFLAGS) | |
test: | |
python3 -c "import myextension" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "Python.h" | |
#include <iostream> | |
struct module_state { | |
PyObject *error; | |
}; | |
#define GETSTATE(m) ((struct module_state *)PyModule_GetState(m)) | |
static PyObject *error_out(PyObject *m) { | |
struct module_state *st = GETSTATE(m); | |
PyErr_SetString(st->error, "something bad happened"); | |
return NULL; | |
} | |
static PyMethodDef myextension_methods[] = { | |
{"error_out", (PyCFunction)error_out, METH_NOARGS, NULL}, {NULL, NULL}}; | |
static int myextension_traverse(PyObject *m, visitproc visit, void *arg) { | |
Py_VISIT(GETSTATE(m)->error); | |
return 0; | |
} | |
static int myextension_clear(PyObject *m) { | |
Py_CLEAR(GETSTATE(m)->error); | |
return 0; | |
} | |
static struct PyModuleDef moduledef = { | |
PyModuleDef_HEAD_INIT, "myextension", NULL, | |
sizeof(struct module_state), myextension_methods, NULL, | |
myextension_traverse, myextension_clear, NULL}; | |
PyObject *PyInit_myextension(void) { | |
PyObject *module = PyModule_Create(&moduledef); | |
if (module == NULL) return NULL; | |
struct module_state *st = GETSTATE(module); | |
st->error = PyErr_NewException("myextension.Error", NULL, NULL); | |
if (st->error == NULL) { | |
Py_DECREF(module); | |
return NULL; | |
} | |
if (PyErr_Occurred()) PyErr_PrintEx(0); | |
return module; | |
} | |
void initcoot_python_gobject() { | |
if (true) { | |
PyObject *o = PyInit_myextension(); | |
// Insert this into sys.modules directly | |
PyObject *sys = PyImport_ImportModule("sys"); | |
PyObject *modules = PyObject_GetAttrString(sys, "modules"); | |
PyDict_SetItemString(modules, "myextension", o); | |
Py_DECREF(modules); | |
Py_DECREF(sys); | |
PyObject *impmod = PyImport_ImportModule("myextension"); | |
if (PyErr_Occurred()) PyErr_PrintEx(0); | |
std::cout << "impmod: " << impmod << std::endl; | |
} | |
} | |
int main(int argc, char **argv) { | |
int status = 0; | |
wchar_t **_argv = static_cast<wchar_t **>(PyMem_Malloc(sizeof(wchar_t *) * argc)); | |
for (int i = 0; i < argc; i++) { | |
wchar_t *arg = Py_DecodeLocale(argv[i], NULL); | |
_argv[i] = arg; | |
} | |
Py_InitializeEx(0); | |
initcoot_python_gobject(); | |
Py_Main(argc, _argv); | |
return status; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment