| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
#include "Python.h" |
| 18 |
|
| 19 |
static PyObject *ErrorObject; |
| 20 |
|
| 21 |
typedef struct { |
| 22 |
PyObject_HEAD |
| 23 |
PyObject *x_attr; |
| 24 |
} XxoObject; |
| 25 |
|
| 26 |
static PyTypeObject Xxo_Type; |
| 27 |
|
| 28 |
#define XxoObject_Check(v) (Py_TYPE(v) == &Xxo_Type) |
| 29 |
|
| 30 |
static XxoObject * |
| 31 |
newXxoObject(PyObject *arg) |
| 32 |
{ |
| 33 |
XxoObject *self; |
| 34 |
self = PyObject_New(XxoObject, &Xxo_Type); |
| 35 |
if (self == NULL) |
| 36 |
return NULL; |
| 37 |
self->x_attr = NULL; |
| 38 |
return self; |
| 39 |
} |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
static void |
| 44 |
Xxo_dealloc(XxoObject *self) |
| 45 |
{ |
| 46 |
Py_XDECREF(self->x_attr); |
| 47 |
PyObject_Del(self); |
| 48 |
} |
| 49 |
|
| 50 |
static PyObject * |
| 51 |
Xxo_demo(XxoObject *self, PyObject *args) |
| 52 |
{ |
| 53 |
if (!PyArg_ParseTuple(args, ":demo")) |
| 54 |
return NULL; |
| 55 |
Py_INCREF(Py_None); |
| 56 |
return Py_None; |
| 57 |
} |
| 58 |
|
| 59 |
static PyMethodDef Xxo_methods[] = { |
| 60 |
{"demo", (PyCFunction)Xxo_demo, METH_VARARGS, |
| 61 |
PyDoc_STR("demo() -> None")}, |
| 62 |
{NULL, NULL} |
| 63 |
}; |
| 64 |
|
| 65 |
static PyObject * |
| 66 |
Xxo_getattr(XxoObject *self, char *name) |
| 67 |
{ |
| 68 |
if (self->x_attr != NULL) { |
| 69 |
PyObject *v = PyDict_GetItemString(self->x_attr, name); |
| 70 |
if (v != NULL) { |
| 71 |
Py_INCREF(v); |
| 72 |
return v; |
| 73 |
} |
| 74 |
} |
| 75 |
return Py_FindMethod(Xxo_methods, (PyObject *)self, name); |
| 76 |
} |
| 77 |
|
| 78 |
static int |
| 79 |
Xxo_setattr(XxoObject *self, char *name, PyObject *v) |
| 80 |
{ |
| 81 |
if (self->x_attr == NULL) { |
| 82 |
self->x_attr = PyDict_New(); |
| 83 |
if (self->x_attr == NULL) |
| 84 |
return -1; |
| 85 |
} |
| 86 |
if (v == NULL) { |
| 87 |
int rv = PyDict_DelItemString(self->x_attr, name); |
| 88 |
if (rv < 0) |
| 89 |
PyErr_SetString(PyExc_AttributeError, |
| 90 |
"delete non-existing Xxo attribute"); |
| 91 |
return rv; |
| 92 |
} |
| 93 |
else |
| 94 |
return PyDict_SetItemString(self->x_attr, name, v); |
| 95 |
} |
| 96 |
|
| 97 |
static PyTypeObject Xxo_Type = { |
| 98 |
|
| 99 |
|
| 100 |
PyVarObject_HEAD_INIT(NULL, 0) |
| 101 |
"xxmodule.Xxo", |
| 102 |
sizeof(XxoObject), |
| 103 |
0, |
| 104 |
|
| 105 |
(destructor)Xxo_dealloc, |
| 106 |
0, |
| 107 |
(getattrfunc)Xxo_getattr, |
| 108 |
(setattrfunc)Xxo_setattr, |
| 109 |
0, |
| 110 |
0, |
| 111 |
0, |
| 112 |
0, |
| 113 |
0, |
| 114 |
0, |
| 115 |
0, |
| 116 |
0, |
| 117 |
0, |
| 118 |
0, |
| 119 |
0, |
| 120 |
Py_TPFLAGS_DEFAULT, |
| 121 |
0, |
| 122 |
0, |
| 123 |
0, |
| 124 |
0, |
| 125 |
0, |
| 126 |
0, |
| 127 |
0, |
| 128 |
0, |
| 129 |
0, |
| 130 |
0, |
| 131 |
0, |
| 132 |
0, |
| 133 |
0, |
| 134 |
0, |
| 135 |
0, |
| 136 |
0, |
| 137 |
0, |
| 138 |
0, |
| 139 |
0, |
| 140 |
0, |
| 141 |
}; |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
PyDoc_STRVAR(xx_foo_doc, |
| 147 |
"foo(i,j)\n\ |
| 148 |
\n\ |
| 149 |
Return the sum of i and j."); |
| 150 |
|
| 151 |
static PyObject * |
| 152 |
xx_foo(PyObject *self, PyObject *args) |
| 153 |
{ |
| 154 |
long i, j; |
| 155 |
long res; |
| 156 |
if (!PyArg_ParseTuple(args, "ll:foo", &i, &j)) |
| 157 |
return NULL; |
| 158 |
res = i+j; |
| 159 |
return PyInt_FromLong(res); |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
static PyObject * |
| 166 |
xx_new(PyObject *self, PyObject *args) |
| 167 |
{ |
| 168 |
XxoObject *rv; |
| 169 |
|
| 170 |
if (!PyArg_ParseTuple(args, ":new")) |
| 171 |
return NULL; |
| 172 |
rv = newXxoObject(args); |
| 173 |
if (rv == NULL) |
| 174 |
return NULL; |
| 175 |
return (PyObject *)rv; |
| 176 |
} |
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
static PyObject * |
| 181 |
xx_bug(PyObject *self, PyObject *args) |
| 182 |
{ |
| 183 |
PyObject *list, *item; |
| 184 |
|
| 185 |
if (!PyArg_ParseTuple(args, "O:bug", &list)) |
| 186 |
return NULL; |
| 187 |
|
| 188 |
item = PyList_GetItem(list, 0); |
| 189 |
|
| 190 |
PyList_SetItem(list, 1, PyInt_FromLong(0L)); |
| 191 |
PyObject_Print(item, stdout, 0); |
| 192 |
printf("\n"); |
| 193 |
|
| 194 |
|
| 195 |
Py_INCREF(Py_None); |
| 196 |
return Py_None; |
| 197 |
} |
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
static PyObject * |
| 202 |
xx_roj(PyObject *self, PyObject *args) |
| 203 |
{ |
| 204 |
PyObject *a; |
| 205 |
long b; |
| 206 |
if (!PyArg_ParseTuple(args, "O#:roj", &a, &b)) |
| 207 |
return NULL; |
| 208 |
Py_INCREF(Py_None); |
| 209 |
return Py_None; |
| 210 |
} |
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
static PyTypeObject Str_Type = { |
| 216 |
|
| 217 |
|
| 218 |
PyVarObject_HEAD_INIT(NULL, 0) |
| 219 |
"xxmodule.Str", |
| 220 |
0, |
| 221 |
0, |
| 222 |
|
| 223 |
0, |
| 224 |
0, |
| 225 |
0, |
| 226 |
0, |
| 227 |
0, |
| 228 |
0, |
| 229 |
0, |
| 230 |
0, |
| 231 |
0, |
| 232 |
0, |
| 233 |
0, |
| 234 |
0, |
| 235 |
0, |
| 236 |
0, |
| 237 |
0, |
| 238 |
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, |
| 239 |
0, |
| 240 |
0, |
| 241 |
0, |
| 242 |
0, |
| 243 |
0, |
| 244 |
0, |
| 245 |
0, |
| 246 |
0, |
| 247 |
0, |
| 248 |
0, |
| 249 |
0, |
| 250 |
0, |
| 251 |
0, |
| 252 |
0, |
| 253 |
0, |
| 254 |
0, |
| 255 |
0, |
| 256 |
0, |
| 257 |
0, |
| 258 |
0, |
| 259 |
}; |
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
static PyObject * |
| 264 |
null_richcompare(PyObject *self, PyObject *other, int op) |
| 265 |
{ |
| 266 |
Py_INCREF(Py_NotImplemented); |
| 267 |
return Py_NotImplemented; |
| 268 |
} |
| 269 |
|
| 270 |
static PyTypeObject Null_Type = { |
| 271 |
|
| 272 |
|
| 273 |
PyVarObject_HEAD_INIT(NULL, 0) |
| 274 |
"xxmodule.Null", |
| 275 |
0, |
| 276 |
0, |
| 277 |
|
| 278 |
0, |
| 279 |
0, |
| 280 |
0, |
| 281 |
0, |
| 282 |
0, |
| 283 |
0, |
| 284 |
0, |
| 285 |
0, |
| 286 |
0, |
| 287 |
0, |
| 288 |
0, |
| 289 |
0, |
| 290 |
0, |
| 291 |
0, |
| 292 |
0, |
| 293 |
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, |
| 294 |
0, |
| 295 |
0, |
| 296 |
0, |
| 297 |
null_richcompare, |
| 298 |
0, |
| 299 |
0, |
| 300 |
0, |
| 301 |
0, |
| 302 |
0, |
| 303 |
0, |
| 304 |
0, |
| 305 |
0, |
| 306 |
0, |
| 307 |
0, |
| 308 |
0, |
| 309 |
0, |
| 310 |
0, |
| 311 |
0, |
| 312 |
0, |
| 313 |
0, |
| 314 |
}; |
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
static PyMethodDef xx_methods[] = { |
| 323 |
{"roj", xx_roj, METH_VARARGS, |
| 324 |
PyDoc_STR("roj(a,b) -> None")}, |
| 325 |
{"foo", xx_foo, METH_VARARGS, |
| 326 |
xx_foo_doc}, |
| 327 |
{"new", xx_new, METH_VARARGS, |
| 328 |
PyDoc_STR("new() -> new Xx object")}, |
| 329 |
{"bug", xx_bug, METH_VARARGS, |
| 330 |
PyDoc_STR("bug(o) -> None")}, |
| 331 |
{NULL, NULL} |
| 332 |
}; |
| 333 |
|
| 334 |
PyDoc_STRVAR(module_doc, |
| 335 |
"This is a template module just for instruction."); |
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
PyMODINIT_FUNC |
| 340 |
initxx(void) |
| 341 |
{ |
| 342 |
PyObject *m; |
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
Null_Type.tp_base = &PyBaseObject_Type; |
| 348 |
Null_Type.tp_new = PyType_GenericNew; |
| 349 |
Str_Type.tp_base = &PyUnicode_Type; |
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
if (PyType_Ready(&Xxo_Type) < 0) |
| 354 |
return; |
| 355 |
|
| 356 |
|
| 357 |
m = Py_InitModule3("xx", xx_methods, module_doc); |
| 358 |
if (m == NULL) |
| 359 |
return; |
| 360 |
|
| 361 |
|
| 362 |
if (ErrorObject == NULL) { |
| 363 |
ErrorObject = PyErr_NewException("xx.error", NULL, NULL); |
| 364 |
if (ErrorObject == NULL) |
| 365 |
return; |
| 366 |
} |
| 367 |
Py_INCREF(ErrorObject); |
| 368 |
PyModule_AddObject(m, "error", ErrorObject); |
| 369 |
|
| 370 |
|
| 371 |
if (PyType_Ready(&Str_Type) < 0) |
| 372 |
return; |
| 373 |
PyModule_AddObject(m, "Str", (PyObject *)&Str_Type); |
| 374 |
|
| 375 |
|
| 376 |
if (PyType_Ready(&Null_Type) < 0) |
| 377 |
return; |
| 378 |
PyModule_AddObject(m, "Null", (PyObject *)&Null_Type); |
| 379 |
} |