浏览代码

Starting cturmit implementation & tests

Yann Weber 6 年前
父节点
当前提交
4861d470d4
共有 6 个文件被更改,包括 172 次插入1 次删除
  1. 14
    0
      libs/Makefile
  2. 33
    0
      libs/cturmit.c
  3. 104
    0
      libs/cturmit.h
  4. 12
    0
      libs/setup.py
  5. 1
    1
      runtest.sh
  6. 8
    0
      tests/test_cturmit.py

+ 14
- 0
libs/Makefile 查看文件

@@ -0,0 +1,14 @@
1
+PYTHON=/usr/bin/python3
2
+
3
+all: cturmit*.so
4
+
5
+cturmit*.so: setup.py build/lib.*/cturmit*.so
6
+	cp build/lib.*/cturmit*.so ./
7
+
8
+build/lib.*/cturmit*.so: cturmit.c cturmit.h
9
+	$(PYTHON) setup.py --verbose build
10
+
11
+.PHONY: clean
12
+
13
+clean:
14
+	-rm -Rf build cturmit*.so

+ 33
- 0
libs/cturmit.c 查看文件

@@ -0,0 +1,33 @@
1
+#include "cturmit.h"
2
+
3
+PyMODINIT_FUNC
4
+PyInit_cturmit(void)
5
+{
6
+	PyObject* m;
7
+
8
+	CTurmitType.tp_new = PyType_GenericNew;
9
+	if (PyType_Ready(&CTurmitType) < 0)
10
+		return NULL;
11
+
12
+	m = PyModule_Create(&cturmitmodule);
13
+	if (m == NULL)
14
+		return NULL;
15
+
16
+	Py_INCREF(&CTurmitType);
17
+	PyModule_AddObject(m, "CTurmit", (PyObject *)&CTurmitType);
18
+	return m;
19
+}
20
+
21
+void
22
+CTurmit_dealloc(CTurmit *self)
23
+{
24
+	Py_TYPE(self)->tp_free((PyObject*)self);
25
+}
26
+
27
+Py_ssize_t
28
+CTurmit_len(CTurmit *self)
29
+{
30
+	return self->len;
31
+}
32
+
33
+

+ 104
- 0
libs/cturmit.h 查看文件

@@ -0,0 +1,104 @@
1
+#include<Python.h>
2
+#include"structmember.h"
3
+
4
+typedef struct
5
+{
6
+	PyObject VAR_HEAD;
7
+	Py_ssize_t len;
8
+} CTurmit;
9
+
10
+static PyModuleDef cturmitmodule = {
11
+	PyModuleDef_HEAD_INIT,
12
+	"cturmit",
13
+	"Module that implements turmits in C",
14
+	-1,
15
+	NULL, NULL, NULL, NULL, NULL
16
+};
17
+
18
+
19
+static PyMemberDef CTurmit_members[] = {
20
+	{"len", T_PYSSIZET, offsetof(CTurmit, len), 0,
21
+	 "Expression len"},
22
+	/*
23
+	{"heap_sz", T_PYSSIZET, offsetof(CTurmit, heap_sz), 0,
24
+	 "Item heap counter"},
25
+	 */
26
+	{NULL}  /* Sentinel */
27
+};
28
+
29
+Py_ssize_t CTurmit_len(CTurmit*);
30
+
31
+static PyMappingMethods CTurmit_asmapping[] = {{
32
+	(void*)CTurmit_len,
33
+}};
34
+
35
+static PyMethodDef CTurmit_methods[] = {
36
+	{NULL}  /* Sentinel */
37
+};
38
+
39
+
40
+static PyObject *
41
+CTurmit_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
42
+{
43
+	CTurmit *self;
44
+	self = (CTurmit *)type->tp_alloc(type, 0);
45
+	if(self != NULL) {
46
+		//do stuff
47
+	}
48
+	return (PyObject *)self;
49
+}
50
+
51
+void
52
+CTurmit_dealloc(CTurmit *self);
53
+
54
+static int
55
+CTurmit_init(CTurmit *self, PyObject *args, PyObject *kwds)
56
+{
57
+	//Init attributes
58
+	self->len = 0;
59
+	return 0;
60
+}
61
+
62
+static PyTypeObject CTurmitType = {
63
+	PyVarObject_HEAD_INIT(NULL, 0)
64
+	"cturmit.CTurmit",			 /* tp_name */
65
+	sizeof(CTurmit),			 /* tp_basicsize */
66
+	0,						 /* tp_itemsize */
67
+	(destructor)CTurmit_dealloc, /* tp_dealloc */
68
+	0,						 /* tp_print */
69
+	0,						 /* tp_getattr */
70
+	0,						 /* tp_setattr */
71
+	0,						 /* tp_reserved */
72
+	0,						 /* tp_repr */
73
+	0,						 /* tp_as_number */
74
+	0,						 /* tp_as_sequence */
75
+	CTurmit_asmapping,				 /* tp_as_mapping */
76
+	0,						 /* tp_hash  */
77
+	0,						 /* tp_call */
78
+	0,						 /* tp_str */
79
+	0,						 /* tp_getattro */
80
+	0,						 /* tp_setattro */
81
+	0,						 /* tp_as_buffer */
82
+	Py_TPFLAGS_DEFAULT |
83
+		Py_TPFLAGS_BASETYPE,   /* tp_flags */
84
+	"CTurmit objects",		   /* tp_doc */
85
+	0,						 /* tp_traverse */
86
+	0,						 /* tp_clear */
87
+	0,						 /* tp_richcompare */
88
+	0,						 /* tp_weaklistoffset */
89
+	0,						 /* tp_iter */
90
+	0,						 /* tp_iternext */
91
+	CTurmit_methods,			 /* tp_methods */
92
+	CTurmit_members,			 /* tp_members */
93
+	0,						 /* tp_getset */
94
+	0,						 /* tp_base */
95
+	0,						 /* tp_dict */
96
+	0,						 /* tp_descr_get */
97
+	0,						 /* tp_descr_set */
98
+	0,						 /* tp_dictoffset */
99
+	(initproc)CTurmit_init,	  /* tp_init */
100
+	0,						 /* tp_alloc */
101
+	CTurmit_new,				 /* tp_new */
102
+};
103
+
104
+

+ 12
- 0
libs/setup.py 查看文件

@@ -0,0 +1,12 @@
1
+from distutils.core import setup, Extension
2
+
3
+module1 = Extension('cturmit',
4
+    sources = ['cturmit.c'])
5
+
6
+
7
+setup(
8
+    name = "Cturmit",
9
+    version = '1.0',
10
+    description = 'Turmit implementation in c',
11
+    ext_modules = [module1]
12
+)

+ 1
- 1
runtest.sh 查看文件

@@ -1,3 +1,3 @@
1 1
 #!/bin/sh
2 2
 
3
-python3 -m unittest $@
3
+PYTHONPATH=":`pwd`/libs" python3 -m unittest $@

+ 8
- 0
tests/test_cturmit.py 查看文件

@@ -0,0 +1,8 @@
1
+import unittest
2
+
3
+import cturmit
4
+
5
+class CTurmitTestCase(unittest.TestCase):
6
+    
7
+    def test_init(self):
8
+        ct = cturmit.CTurmit()

正在加载...
取消
保存