Skip to content

Commit 2489ea3

Browse files
committed
Working C extension module.
1 parent ac59616 commit 2489ea3

File tree

8 files changed

+83
-0
lines changed

8 files changed

+83
-0
lines changed

.clang-format

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
BasedOnStyle: Google
2+
3+
# For calculating line lengths.
4+
IndentWidth: 2
5+
TabWidth: 2
6+
7+
UseTab: ForIndentation
8+
ColumnLimit: 100
9+
10+
AlignOperands: true
11+
AllowShortFunctionsOnASingleLine: false
12+
BinPackArguments: false
13+
BinPackParameters: false
14+
BreakBeforeBinaryOperators: NonAssignment
15+
ConstructorInitializerAllOnOneLineOrOnePerLine: true
16+
Standard: Cpp11

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
__pycache__
2+
build

.style.yapf

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# YAPF can be customized.
2+
# https://github.com/google/yapf#knobs
3+
4+
[style]
5+
based_on_style = pep8
6+
column_limit = 100

Makefile

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
test: install
3+
python3 -m unittest
4+
5+
install: build
6+
python3 setup.py install --user
7+
8+
build: Makefile module.c
9+
python3 setup.py build
10+
11+
format:
12+
python3 -m yapf -i -r --style .style.yapf .
13+
clang-format-7 -i module.c

module.c

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include <Python.h>
2+
3+
static PyObject *test(PyObject *self, PyObject *args) {
4+
return Py_BuildValue("i", 42);
5+
}
6+
7+
struct module_state {};
8+
9+
static PyMethodDef myextension_methods[] = {{"test", (PyCFunction)test, METH_NOARGS, NULL},
10+
{NULL, NULL}};
11+
12+
static struct PyModuleDef moduledef = {PyModuleDef_HEAD_INIT,
13+
"quickjs",
14+
NULL,
15+
sizeof(struct module_state),
16+
myextension_methods,
17+
NULL,
18+
NULL,
19+
NULL,
20+
NULL};
21+
22+
PyMODINIT_FUNC PyInit__quickjs(void) {
23+
return PyModule_Create(&moduledef);
24+
}

quickjs/__init__.py

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import _quickjs
2+
3+
4+
def test():
5+
return _quickjs.test()

setup.py

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from distutils.core import setup, Extension
2+
3+
_quickjs = Extension('_quickjs', sources=['module.c'])
4+
5+
setup(name='quickjs',
6+
version='1.0',
7+
description='Wrapping the quickjs C library.',
8+
packages=["quickjs"],
9+
ext_modules=[_quickjs])

test_quickjs.py

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import unittest
2+
3+
import quickjs
4+
5+
6+
class LoadModule(unittest.TestCase):
7+
def test_42(self):
8+
self.assertEqual(quickjs.test(), 42)

0 commit comments

Comments
 (0)