Skip to content

Commit 1ec5325

Browse files
committed
Add minimal_python_extension_module/
1 parent ec24c9f commit 1ec5325

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
set -x
2+
rm -f hello_extenstion_module.so
3+
clang++ -std=c++11 -I/usr/include/python3.11 -shared -fPIC -g -O0 hello_extenstion_module.cpp -o hello_extenstion_module.so
4+
python3 use_hello_extenstion_module.py
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include <Python.h>
2+
3+
namespace {
4+
5+
static PyObject *wrapHelloWorld(PyObject *, PyObject *) {
6+
return PyUnicode_FromString("Hello from a Python extension module.");
7+
}
8+
9+
static PyMethodDef ThisMethodDef[]
10+
= {{"HelloWorld", wrapHelloWorld, METH_NOARGS, "HelloWorld() -> str"},
11+
{nullptr, nullptr, 0, nullptr}};
12+
13+
static struct PyModuleDef ThisModuleDef = {
14+
PyModuleDef_HEAD_INIT, // m_base
15+
"hello_extenstion_module", // m_name
16+
nullptr, // m_doc
17+
-1, // m_size
18+
ThisMethodDef, // m_methods
19+
nullptr, // m_slots
20+
nullptr, // m_traverse
21+
nullptr, // m_clear
22+
nullptr // m_free
23+
};
24+
25+
} // namespace
26+
27+
extern "C" PyObject *PyInit_hello_extenstion_module() { return PyModule_Create(&ThisModuleDef); }
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import hello_extenstion_module
2+
3+
4+
def run():
5+
print(hello_extenstion_module.HelloWorld())
6+
7+
8+
if __name__ == "__main__":
9+
run()

0 commit comments

Comments
 (0)