Skip to content

Commit fe75764

Browse files
author
dmilos
committedMar 9, 2016
init: [init] initial commit
0 parents  commit fe75764

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+823
-0
lines changed
 

‎capi/mymodule.c

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
#include "Python.h"
2+
3+
struct module_state {
4+
PyObject *error;
5+
};
6+
7+
#if PY_MAJOR_VERSION >= 3
8+
#define GETSTATE(m) ((struct module_state*)PyModule_GetState(m))
9+
#else
10+
#define GETSTATE(m) (&_state)
11+
static struct module_state _state;
12+
#endif
13+
14+
static PyObject *
15+
error_out(PyObject *m) {
16+
struct module_state *st = GETSTATE(m);
17+
PyErr_SetString(st->error, "something bad happened");
18+
return NULL;
19+
}
20+
21+
static PyMethodDef myextension_methods[] = {
22+
{"error_out", (PyCFunction)error_out, METH_NOARGS, NULL},
23+
{NULL, NULL}
24+
};
25+
26+
#if PY_MAJOR_VERSION >= 3
27+
28+
static int myextension_traverse(PyObject *m, visitproc visit, void *arg) {
29+
Py_VISIT(GETSTATE(m)->error);
30+
return 0;
31+
}
32+
33+
static int myextension_clear(PyObject *m) {
34+
Py_CLEAR(GETSTATE(m)->error);
35+
return 0;
36+
}
37+
38+
39+
static struct PyModuleDef moduledef = {
40+
PyModuleDef_HEAD_INIT,
41+
"myextension",
42+
NULL,
43+
sizeof(struct module_state),
44+
myextension_methods,
45+
NULL,
46+
myextension_traverse,
47+
myextension_clear,
48+
NULL
49+
};
50+
51+
#define INITERROR return NULL
52+
53+
PyObject *
54+
PyInit_myextension(void)
55+
56+
#else
57+
#define INITERROR return
58+
59+
void
60+
initmyextension(void)
61+
#endif
62+
{
63+
#if PY_MAJOR_VERSION >= 3
64+
PyObject *module = PyModule_Create(&moduledef);
65+
#else
66+
PyObject *module = Py_InitModule("myextension", myextension_methods);
67+
#endif
68+
69+
if (module == NULL)
70+
INITERROR;
71+
struct module_state *st = GETSTATE(module);
72+
73+
st->error = PyErr_NewException("myextension.Error", NULL, NULL);
74+
if (st->error == NULL) {
75+
Py_DECREF(module);
76+
INITERROR;
77+
}
78+
79+
#if PY_MAJOR_VERSION >= 3
80+
return module;
81+
#endif
82+
}

‎core_class/test.py

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class FooType(object):
2+
def __init__(self, id):
3+
self.id = id
4+
print self.id, 'born'
5+
6+
def __del__(self):
7+
print self.id, 'died'
8+
9+
def make_foo():
10+
print 'Making...'
11+
ft = FooType(1)
12+
print 'Returning...'
13+
return ft
14+
15+
print 'Calling...'
16+
ft = make_foo()
17+
print 'End...'

0 commit comments

Comments
 (0)
Please sign in to comment.