Changeset 988 for vendor/current/lib/tdb/pytdb.c
- Timestamp:
- Nov 24, 2016, 1:14:11 PM (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
vendor/current/lib/tdb/pytdb.c
r986 r988 29 29 #include "system/filesys.h" 30 30 31 #ifndef Py_RETURN_NONE32 #define Py_RETURN_NONE return Py_INCREF(Py_None), Py_None33 #endif34 35 31 /* Include tdb headers */ 36 32 #include <tdb.h> 33 34 #if PY_MAJOR_VERSION >= 3 35 #define PyStr_FromString PyUnicode_FromString 36 #define PyStr_FromFormat PyUnicode_FromFormat 37 #define PyInt_FromLong PyLong_FromLong 38 #define PyInt_Check PyLong_Check 39 #define PyInt_AsLong PyLong_AsLong 40 #define Py_TPFLAGS_HAVE_ITER 0 41 #else 42 #define PyStr_FromString PyString_FromString 43 #define PyStr_FromFormat PyString_FromFormat 44 #endif 37 45 38 46 typedef struct { … … 42 50 } PyTdbObject; 43 51 44 static forwardPyTypeObject PyTdb;52 static PyTypeObject PyTdb; 45 53 46 54 static void PyErr_SetTDBError(TDB_CONTEXT *tdb) … … 50 58 } 51 59 52 static TDB_DATA Py String_AsTDB_DATA(PyObject *data)60 static TDB_DATA PyBytes_AsTDB_DATA(PyObject *data) 53 61 { 54 62 TDB_DATA ret; 55 ret.dptr = (unsigned char *)Py String_AsString(data);56 ret.dsize = Py String_Size(data);63 ret.dptr = (unsigned char *)PyBytes_AsString(data); 64 ret.dsize = PyBytes_Size(data); 57 65 return ret; 58 66 } 59 67 60 static PyObject *Py String_FromTDB_DATA(TDB_DATA data)68 static PyObject *PyBytes_FromTDB_DATA(TDB_DATA data) 61 69 { 62 70 if (data.dptr == NULL && data.dsize == 0) { 63 71 Py_RETURN_NONE; 64 72 } else { 65 PyObject *ret = Py String_FromStringAndSize((const char *)data.dptr,66 73 PyObject *ret = PyBytes_FromStringAndSize((const char *)data.dptr, 74 data.dsize); 67 75 free(data.dptr); 68 76 return ret; … … 76 84 } 77 85 86 #define PyErr_TDB_RAISE_IF_CLOSED(self) \ 87 if (self->closed) { \ 88 PyErr_SetObject(PyExc_RuntimeError, \ 89 Py_BuildValue("(i,s)", TDB_ERR_IO, "Database is already closed")); \ 90 return NULL; \ 91 } 92 93 #define PyErr_TDB_RAISE_RETURN_MINUS_1_IF_CLOSED(self) \ 94 if (self->closed) { \ 95 PyErr_SetObject(PyExc_RuntimeError, \ 96 Py_BuildValue("(i,s)", TDB_ERR_IO, "Database is already closed")); \ 97 return -1; \ 98 } 99 78 100 static PyObject *py_tdb_open(PyTypeObject *type, PyObject *args, PyObject *kwargs) 79 101 { … … 82 104 TDB_CONTEXT *ctx; 83 105 PyTdbObject *ret; 84 const char *kwnames[] = { "name", "hash_size", "tdb_flags", "flags", "mode", NULL }; 85 86 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|siiii", (char **)kwnames, &name, &hash_size, &tdb_flags, &flags, &mode)) 106 const char *_kwnames[] = { "name", "hash_size", "tdb_flags", "flags", "mode", NULL }; 107 char **kwnames = discard_const_p(char *, _kwnames); 108 109 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|siiii", kwnames, &name, &hash_size, &tdb_flags, &flags, &mode)) 87 110 return NULL; 88 111 … … 110 133 static PyObject *obj_transaction_cancel(PyTdbObject *self) 111 134 { 112 int ret = tdb_transaction_cancel(self->ctx); 135 int ret; 136 137 PyErr_TDB_RAISE_IF_CLOSED(self); 138 139 ret = tdb_transaction_cancel(self->ctx); 113 140 PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx); 114 141 Py_RETURN_NONE; … … 117 144 static PyObject *obj_transaction_commit(PyTdbObject *self) 118 145 { 119 int ret = tdb_transaction_commit(self->ctx); 146 int ret; 147 PyErr_TDB_RAISE_IF_CLOSED(self); 148 ret = tdb_transaction_commit(self->ctx); 120 149 PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx); 121 150 Py_RETURN_NONE; … … 124 153 static PyObject *obj_transaction_prepare_commit(PyTdbObject *self) 125 154 { 126 int ret = tdb_transaction_prepare_commit(self->ctx); 155 int ret; 156 PyErr_TDB_RAISE_IF_CLOSED(self); 157 ret = tdb_transaction_prepare_commit(self->ctx); 127 158 PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx); 128 159 Py_RETURN_NONE; … … 131 162 static PyObject *obj_transaction_start(PyTdbObject *self) 132 163 { 133 int ret = tdb_transaction_start(self->ctx); 164 int ret; 165 PyErr_TDB_RAISE_IF_CLOSED(self); 166 ret = tdb_transaction_start(self->ctx); 134 167 PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx); 135 168 Py_RETURN_NONE; … … 138 171 static PyObject *obj_reopen(PyTdbObject *self) 139 172 { 140 int ret = tdb_reopen(self->ctx); 141 PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx); 173 int ret; 174 PyErr_TDB_RAISE_IF_CLOSED(self); 175 ret = tdb_reopen(self->ctx); 176 if (ret != 0) { 177 self->closed = true; 178 PyErr_SetObject(PyExc_RuntimeError, 179 Py_BuildValue("(i,s)", 180 TDB_ERR_IO, 181 "Failed to reopen database")); 182 return NULL; 183 } 142 184 Py_RETURN_NONE; 143 185 } … … 145 187 static PyObject *obj_lockall(PyTdbObject *self) 146 188 { 147 int ret = tdb_lockall(self->ctx); 189 int ret; 190 PyErr_TDB_RAISE_IF_CLOSED(self); 191 ret = tdb_lockall(self->ctx); 148 192 PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx); 149 193 Py_RETURN_NONE; … … 152 196 static PyObject *obj_unlockall(PyTdbObject *self) 153 197 { 154 int ret = tdb_unlockall(self->ctx); 198 int ret; 199 PyErr_TDB_RAISE_IF_CLOSED(self); 200 ret = tdb_unlockall(self->ctx); 155 201 PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx); 156 202 Py_RETURN_NONE; … … 159 205 static PyObject *obj_lockall_read(PyTdbObject *self) 160 206 { 161 int ret = tdb_lockall_read(self->ctx); 207 int ret; 208 PyErr_TDB_RAISE_IF_CLOSED(self); 209 ret = tdb_lockall_read(self->ctx); 162 210 PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx); 163 211 Py_RETURN_NONE; … … 178 226 ret = tdb_close(self->ctx); 179 227 self->closed = true; 180 PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx); 228 if (ret != 0) { 229 PyErr_SetObject(PyExc_RuntimeError, 230 Py_BuildValue("(i,s)", 231 TDB_ERR_IO, 232 "Failed to close database")); 233 return NULL; 234 } 181 235 Py_RETURN_NONE; 182 236 } … … 186 240 TDB_DATA key; 187 241 PyObject *py_key; 242 243 PyErr_TDB_RAISE_IF_CLOSED(self); 244 188 245 if (!PyArg_ParseTuple(args, "O", &py_key)) 189 246 return NULL; 190 247 191 key = PyString_AsTDB_DATA(py_key); 192 193 return PyString_FromTDB_DATA(tdb_fetch(self->ctx, key)); 248 key = PyBytes_AsTDB_DATA(py_key); 249 if (!key.dptr) 250 return NULL; 251 252 return PyBytes_FromTDB_DATA(tdb_fetch(self->ctx, key)); 194 253 } 195 254 … … 199 258 PyObject *py_key, *py_data; 200 259 int ret; 260 261 PyErr_TDB_RAISE_IF_CLOSED(self); 262 201 263 if (!PyArg_ParseTuple(args, "OO", &py_key, &py_data)) 202 264 return NULL; 203 265 204 key = PyString_AsTDB_DATA(py_key); 205 data = PyString_AsTDB_DATA(py_data); 266 key = PyBytes_AsTDB_DATA(py_key); 267 if (!key.dptr) 268 return NULL; 269 data = PyBytes_AsTDB_DATA(py_data); 270 if (!data.dptr) 271 return NULL; 206 272 207 273 ret = tdb_append(self->ctx, key, data); … … 212 278 static PyObject *obj_firstkey(PyTdbObject *self) 213 279 { 214 return PyString_FromTDB_DATA(tdb_firstkey(self->ctx)); 280 PyErr_TDB_RAISE_IF_CLOSED(self); 281 282 return PyBytes_FromTDB_DATA(tdb_firstkey(self->ctx)); 215 283 } 216 284 … … 219 287 TDB_DATA key; 220 288 PyObject *py_key; 289 PyErr_TDB_RAISE_IF_CLOSED(self); 290 221 291 if (!PyArg_ParseTuple(args, "O", &py_key)) 222 292 return NULL; 223 293 224 key = PyString_AsTDB_DATA(py_key); 294 key = PyBytes_AsTDB_DATA(py_key); 295 if (!key.dptr) 296 return NULL; 225 297 226 return Py String_FromTDB_DATA(tdb_nextkey(self->ctx, key));298 return PyBytes_FromTDB_DATA(tdb_nextkey(self->ctx, key)); 227 299 } 228 300 … … 232 304 PyObject *py_key; 233 305 int ret; 306 PyErr_TDB_RAISE_IF_CLOSED(self); 307 234 308 if (!PyArg_ParseTuple(args, "O", &py_key)) 235 309 return NULL; 236 310 237 key = PyString_AsTDB_DATA(py_key); 311 key = PyBytes_AsTDB_DATA(py_key); 312 if (!key.dptr) 313 return NULL; 238 314 ret = tdb_delete(self->ctx, key); 239 315 PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx); … … 241 317 } 242 318 319 static int obj_contains(PyTdbObject *self, PyObject *py_key) 320 { 321 TDB_DATA key; 322 int ret; 323 PyErr_TDB_RAISE_RETURN_MINUS_1_IF_CLOSED(self); 324 325 key = PyBytes_AsTDB_DATA(py_key); 326 if (!key.dptr) { 327 PyErr_BadArgument(); 328 return -1; 329 } 330 ret = tdb_exists(self->ctx, key); 331 if (ret) 332 return 1; 333 return 0; 334 } 335 336 #if PY_MAJOR_VERSION < 3 243 337 static PyObject *obj_has_key(PyTdbObject *self, PyObject *args) 244 338 { 245 TDB_DATA key;246 339 int ret; 247 340 PyObject *py_key; 341 PyErr_TDB_RAISE_IF_CLOSED(self); 342 248 343 if (!PyArg_ParseTuple(args, "O", &py_key)) 249 344 return NULL; 250 345 251 key = PyString_AsTDB_DATA(py_key); 252 ret = tdb_exists(self->ctx, key); 253 if (ret != TDB_ERR_NOEXIST) { 254 PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx); 255 } 256 257 return (ret == TDB_ERR_NOEXIST)?Py_False:Py_True; 258 } 346 ret = obj_contains(self, py_key); 347 if (ret == -1) 348 return NULL; 349 if (ret) 350 Py_RETURN_TRUE; 351 Py_RETURN_FALSE; 352 353 } 354 #endif 259 355 260 356 static PyObject *obj_store(PyTdbObject *self, PyObject *args) … … 265 361 PyObject *py_key, *py_value; 266 362 363 PyErr_TDB_RAISE_IF_CLOSED(self); 364 267 365 if (!PyArg_ParseTuple(args, "OO|i", &py_key, &py_value, &flag)) 268 366 return NULL; 269 367 270 key = PyString_AsTDB_DATA(py_key); 271 value = PyString_AsTDB_DATA(py_value); 368 key = PyBytes_AsTDB_DATA(py_key); 369 if (!key.dptr) 370 return NULL; 371 value = PyBytes_AsTDB_DATA(py_value); 372 if (!value.dptr) 373 return NULL; 272 374 273 375 ret = tdb_store(self->ctx, key, value, flag); … … 280 382 unsigned flags; 281 383 384 PyErr_TDB_RAISE_IF_CLOSED(self); 385 282 386 if (!PyArg_ParseTuple(args, "I", &flags)) 283 387 return NULL; … … 290 394 { 291 395 unsigned flags; 396 397 PyErr_TDB_RAISE_IF_CLOSED(self); 292 398 293 399 if (!PyArg_ParseTuple(args, "I", &flags)) … … 312 418 current = self->current; 313 419 self->current = tdb_nextkey(self->iteratee->ctx, self->current); 314 ret = Py String_FromTDB_DATA(current);420 ret = PyBytes_FromTDB_DATA(current); 315 421 return ret; 316 422 } … … 335 441 PyTdbIteratorObject *ret; 336 442 443 PyErr_TDB_RAISE_IF_CLOSED(self); 444 337 445 ret = PyObject_New(PyTdbIteratorObject, &PyTdbIterator); 338 446 if (!ret) … … 346 454 static PyObject *obj_clear(PyTdbObject *self) 347 455 { 348 int ret = tdb_wipe_all(self->ctx); 456 int ret; 457 PyErr_TDB_RAISE_IF_CLOSED(self); 458 ret = tdb_wipe_all(self->ctx); 349 459 PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx); 350 460 Py_RETURN_NONE; … … 353 463 static PyObject *obj_repack(PyTdbObject *self) 354 464 { 355 int ret = tdb_repack(self->ctx); 465 int ret; 466 PyErr_TDB_RAISE_IF_CLOSED(self); 467 ret = tdb_repack(self->ctx); 356 468 PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx); 357 469 Py_RETURN_NONE; … … 360 472 static PyObject *obj_enable_seqnum(PyTdbObject *self) 361 473 { 474 PyErr_TDB_RAISE_IF_CLOSED(self); 362 475 tdb_enable_seqnum(self->ctx); 363 476 Py_RETURN_NONE; … … 366 479 static PyObject *obj_increment_seqnum_nonblock(PyTdbObject *self) 367 480 { 481 PyErr_TDB_RAISE_IF_CLOSED(self); 368 482 tdb_increment_seqnum_nonblock(self->ctx); 369 483 Py_RETURN_NONE; … … 395 509 { "firstkey", (PyCFunction)obj_firstkey, METH_NOARGS, "S.firstkey() -> data\n" 396 510 "Return the first key in this database." }, 397 { "nextkey", (PyCFunction)obj_nextkey, METH_ NOARGS, "S.nextkey(key) -> data\n"511 { "nextkey", (PyCFunction)obj_nextkey, METH_VARARGS, "S.nextkey(key) -> data\n" 398 512 "Return the next key in this database." }, 399 513 { "delete", (PyCFunction)obj_delete, METH_VARARGS, "S.delete(key) -> None\n" 400 514 "Delete an entry." }, 515 #if PY_MAJOR_VERSION < 3 401 516 { "has_key", (PyCFunction)obj_has_key, METH_VARARGS, "S.has_key(key) -> None\n" 402 517 "Check whether key exists in this database." }, 518 #endif 403 519 { "store", (PyCFunction)obj_store, METH_VARARGS, "S.store(key, data, flag=REPLACE) -> None" 404 520 "Store data." }, 405 521 { "add_flags", (PyCFunction)obj_add_flags, METH_VARARGS, "S.add_flags(flags) -> None" }, 406 522 { "remove_flags", (PyCFunction)obj_remove_flags, METH_VARARGS, "S.remove_flags(flags) -> None" }, 523 #if PY_MAJOR_VERSION >= 3 524 { "keys", (PyCFunction)tdb_object_iter, METH_NOARGS, "S.iterkeys() -> iterator" }, 525 #else 407 526 { "iterkeys", (PyCFunction)tdb_object_iter, METH_NOARGS, "S.iterkeys() -> iterator" }, 527 #endif 408 528 { "clear", (PyCFunction)obj_clear, METH_NOARGS, "S.clear() -> None\n" 409 529 "Wipe the entire database." }, … … 419 539 static PyObject *obj_get_hash_size(PyTdbObject *self, void *closure) 420 540 { 541 PyErr_TDB_RAISE_IF_CLOSED(self); 421 542 return PyInt_FromLong(tdb_hash_size(self->ctx)); 422 543 } … … 424 545 static int obj_set_max_dead(PyTdbObject *self, PyObject *max_dead, void *closure) 425 546 { 547 PyErr_TDB_RAISE_RETURN_MINUS_1_IF_CLOSED(self); 426 548 if (!PyInt_Check(max_dead)) 427 549 return -1; … … 432 554 static PyObject *obj_get_map_size(PyTdbObject *self, void *closure) 433 555 { 556 PyErr_TDB_RAISE_IF_CLOSED(self); 434 557 return PyInt_FromLong(tdb_map_size(self->ctx)); 435 558 } … … 437 560 static PyObject *obj_get_freelist_size(PyTdbObject *self, void *closure) 438 561 { 562 PyErr_TDB_RAISE_IF_CLOSED(self); 439 563 return PyInt_FromLong(tdb_freelist_size(self->ctx)); 440 564 } … … 442 566 static PyObject *obj_get_flags(PyTdbObject *self, void *closure) 443 567 { 568 PyErr_TDB_RAISE_IF_CLOSED(self); 444 569 return PyInt_FromLong(tdb_get_flags(self->ctx)); 445 570 } … … 447 572 static PyObject *obj_get_filename(PyTdbObject *self, void *closure) 448 573 { 449 return PyString_FromString(tdb_name(self->ctx)); 574 PyErr_TDB_RAISE_IF_CLOSED(self); 575 return PyBytes_FromString(tdb_name(self->ctx)); 450 576 } 451 577 452 578 static PyObject *obj_get_seqnum(PyTdbObject *self, void *closure) 453 579 { 580 PyErr_TDB_RAISE_IF_CLOSED(self); 454 581 return PyInt_FromLong(tdb_get_seqnum(self->ctx)); 455 582 } 456 583 584 static PyObject *obj_get_text(PyTdbObject *self, void *closure) 585 { 586 PyObject *mod, *cls, *inst; 587 mod = PyImport_ImportModule("_tdb_text"); 588 if (mod == NULL) 589 return NULL; 590 cls = PyObject_GetAttrString(mod, "TdbTextWrapper"); 591 if (cls == NULL) { 592 Py_DECREF(mod); 593 return NULL; 594 } 595 inst = PyObject_CallFunction(cls, discard_const_p(char, "O"), self); 596 Py_DECREF(mod); 597 Py_DECREF(cls); 598 return inst; 599 } 457 600 458 601 static PyGetSetDef tdb_object_getsetters[] = { 459 { (char *)"hash_size", (getter)obj_get_hash_size, NULL, NULL }, 460 { (char *)"map_size", (getter)obj_get_map_size, NULL, NULL }, 461 { (char *)"freelist_size", (getter)obj_get_freelist_size, NULL, NULL }, 462 { (char *)"flags", (getter)obj_get_flags, NULL, NULL }, 463 { (char *)"max_dead", NULL, (setter)obj_set_max_dead, NULL }, 464 { (char *)"filename", (getter)obj_get_filename, NULL, (char *)"The filename of this TDB file."}, 465 { (char *)"seqnum", (getter)obj_get_seqnum, NULL, NULL }, 602 { discard_const_p(char, "hash_size"), 603 (getter)obj_get_hash_size, NULL, NULL }, 604 { discard_const_p(char, "map_size"), 605 (getter)obj_get_map_size, NULL, NULL }, 606 { discard_const_p(char, "freelist_size"), 607 (getter)obj_get_freelist_size, NULL, NULL }, 608 { discard_const_p(char, "flags"), 609 (getter)obj_get_flags, NULL, NULL }, 610 { discard_const_p(char, "max_dead"), 611 NULL, (setter)obj_set_max_dead, NULL }, 612 { discard_const_p(char, "filename"), 613 (getter)obj_get_filename, NULL, 614 discard_const_p(char, "The filename of this TDB file.") }, 615 { discard_const_p(char, "seqnum"), 616 (getter)obj_get_seqnum, NULL, NULL }, 617 { discard_const_p(char, "text"), 618 (getter)obj_get_text, NULL, NULL }, 466 619 { NULL } 467 620 }; … … 469 622 static PyObject *tdb_object_repr(PyTdbObject *self) 470 623 { 624 PyErr_TDB_RAISE_IF_CLOSED(self); 471 625 if (tdb_get_flags(self->ctx) & TDB_INTERNAL) { 472 return PyStr ing_FromString("Tdb(<internal>)");626 return PyStr_FromString("Tdb(<internal>)"); 473 627 } else { 474 return PyStr ing_FromFormat("Tdb('%s')", tdb_name(self->ctx));628 return PyStr_FromFormat("Tdb('%s')", tdb_name(self->ctx)); 475 629 } 476 630 } … … 480 634 if (!self->closed) 481 635 tdb_close(self->ctx); 482 self->ob_type->tp_free(self);636 Py_TYPE(self)->tp_free(self); 483 637 } 484 638 … … 486 640 { 487 641 TDB_DATA tkey, val; 488 if (!PyString_Check(key)) { 489 PyErr_SetString(PyExc_TypeError, "Expected string as key"); 490 return NULL; 491 } 492 493 tkey.dptr = (unsigned char *)PyString_AsString(key); 494 tkey.dsize = PyString_Size(key); 642 PyErr_TDB_RAISE_IF_CLOSED(self); 643 if (!PyBytes_Check(key)) { 644 PyErr_SetString(PyExc_TypeError, "Expected bytestring as key"); 645 return NULL; 646 } 647 648 tkey.dptr = (unsigned char *)PyBytes_AsString(key); 649 tkey.dsize = PyBytes_Size(key); 495 650 496 651 val = tdb_fetch(self->ctx, tkey); 497 652 if (val.dptr == NULL) { 498 PyErr_SetString(PyExc_KeyError, "No such TDB entry"); 653 /* 654 * if the key doesn't exist raise KeyError(key) to be 655 * consistent with python dict 656 */ 657 PyErr_SetObject(PyExc_KeyError, key); 499 658 return NULL; 500 659 } else { 501 return Py String_FromTDB_DATA(val);660 return PyBytes_FromTDB_DATA(val); 502 661 } 503 662 } … … 507 666 TDB_DATA tkey, tval; 508 667 int ret; 509 if (!PyString_Check(key)) { 510 PyErr_SetString(PyExc_TypeError, "Expected string as key"); 668 PyErr_TDB_RAISE_RETURN_MINUS_1_IF_CLOSED(self); 669 if (!PyBytes_Check(key)) { 670 PyErr_SetString(PyExc_TypeError, "Expected bytestring as key"); 511 671 return -1; 512 672 } 513 673 514 tkey = Py String_AsTDB_DATA(key);674 tkey = PyBytes_AsTDB_DATA(key); 515 675 516 676 if (value == NULL) { 517 677 ret = tdb_delete(self->ctx, tkey); 518 678 } else { 519 if (!Py String_Check(value)) {679 if (!PyBytes_Check(value)) { 520 680 PyErr_SetString(PyExc_TypeError, "Expected string as value"); 521 681 return -1; 522 682 } 523 683 524 tval = Py String_AsTDB_DATA(value);684 tval = PyBytes_AsTDB_DATA(value); 525 685 526 686 ret = tdb_store(self->ctx, tkey, tval, TDB_REPLACE); … … 539 699 .mp_ass_subscript = (objobjargproc)obj_setitem, 540 700 }; 701 static PySequenceMethods tdb_object_seq = { 702 .sq_contains = (objobjproc)obj_contains, 703 }; 541 704 static PyTypeObject PyTdb = { 542 .tp_name = " Tdb",705 .tp_name = "tdb.Tdb", 543 706 .tp_basicsize = sizeof(PyTdbObject), 544 707 .tp_methods = tdb_object_methods, … … 549 712 .tp_dealloc = (destructor)tdb_object_dealloc, 550 713 .tp_as_mapping = &tdb_object_mapping, 714 .tp_as_sequence = &tdb_object_seq, 551 715 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE|Py_TPFLAGS_HAVE_ITER, 552 716 .tp_iter = (getiterfunc)tdb_object_iter, … … 559 723 }; 560 724 725 #define MODULE_DOC "simple key-value database that supports multiple writers." 726 727 #if PY_MAJOR_VERSION >= 3 728 static struct PyModuleDef moduledef = { 729 PyModuleDef_HEAD_INIT, 730 .m_name = "tdb", 731 .m_doc = MODULE_DOC, 732 .m_size = -1, 733 .m_methods = tdb_methods, 734 }; 735 #endif 736 737 PyObject* module_init(void); 738 PyObject* module_init(void) 739 { 740 PyObject *m; 741 742 if (PyType_Ready(&PyTdb) < 0) 743 return NULL; 744 745 if (PyType_Ready(&PyTdbIterator) < 0) 746 return NULL; 747 748 #if PY_MAJOR_VERSION >= 3 749 m = PyModule_Create(&moduledef); 750 #else 751 m = Py_InitModule3("tdb", tdb_methods, MODULE_DOC); 752 #endif 753 if (m == NULL) 754 return NULL; 755 756 PyModule_AddIntConstant(m, "REPLACE", TDB_REPLACE); 757 PyModule_AddIntConstant(m, "INSERT", TDB_INSERT); 758 PyModule_AddIntConstant(m, "MODIFY", TDB_MODIFY); 759 760 PyModule_AddIntConstant(m, "DEFAULT", TDB_DEFAULT); 761 PyModule_AddIntConstant(m, "CLEAR_IF_FIRST", TDB_CLEAR_IF_FIRST); 762 PyModule_AddIntConstant(m, "INTERNAL", TDB_INTERNAL); 763 PyModule_AddIntConstant(m, "NOLOCK", TDB_NOLOCK); 764 PyModule_AddIntConstant(m, "NOMMAP", TDB_NOMMAP); 765 PyModule_AddIntConstant(m, "CONVERT", TDB_CONVERT); 766 PyModule_AddIntConstant(m, "BIGENDIAN", TDB_BIGENDIAN); 767 PyModule_AddIntConstant(m, "NOSYNC", TDB_NOSYNC); 768 PyModule_AddIntConstant(m, "SEQNUM", TDB_SEQNUM); 769 PyModule_AddIntConstant(m, "VOLATILE", TDB_VOLATILE); 770 PyModule_AddIntConstant(m, "ALLOW_NESTING", TDB_ALLOW_NESTING); 771 PyModule_AddIntConstant(m, "DISALLOW_NESTING", TDB_DISALLOW_NESTING); 772 PyModule_AddIntConstant(m, "INCOMPATIBLE_HASH", TDB_INCOMPATIBLE_HASH); 773 774 PyModule_AddStringConstant(m, "__docformat__", "restructuredText"); 775 776 PyModule_AddStringConstant(m, "__version__", PACKAGE_VERSION); 777 778 Py_INCREF(&PyTdb); 779 PyModule_AddObject(m, "Tdb", (PyObject *)&PyTdb); 780 781 Py_INCREF(&PyTdbIterator); 782 783 return m; 784 } 785 786 787 #if PY_MAJOR_VERSION >= 3 788 PyMODINIT_FUNC PyInit_tdb(void); 789 PyMODINIT_FUNC PyInit_tdb(void) 790 { 791 return module_init(); 792 } 793 #else 561 794 void inittdb(void); 562 795 void inittdb(void) 563 796 { 564 PyObject *m; 565 566 if (PyType_Ready(&PyTdb) < 0) 567 return; 568 569 if (PyType_Ready(&PyTdbIterator) < 0) 570 return; 571 572 m = Py_InitModule3("tdb", tdb_methods, "TDB is a simple key-value database similar to GDBM that supports multiple writers."); 573 if (m == NULL) 574 return; 575 576 PyModule_AddObject(m, "REPLACE", PyInt_FromLong(TDB_REPLACE)); 577 PyModule_AddObject(m, "INSERT", PyInt_FromLong(TDB_INSERT)); 578 PyModule_AddObject(m, "MODIFY", PyInt_FromLong(TDB_MODIFY)); 579 580 PyModule_AddObject(m, "DEFAULT", PyInt_FromLong(TDB_DEFAULT)); 581 PyModule_AddObject(m, "CLEAR_IF_FIRST", PyInt_FromLong(TDB_CLEAR_IF_FIRST)); 582 PyModule_AddObject(m, "INTERNAL", PyInt_FromLong(TDB_INTERNAL)); 583 PyModule_AddObject(m, "NOLOCK", PyInt_FromLong(TDB_NOLOCK)); 584 PyModule_AddObject(m, "NOMMAP", PyInt_FromLong(TDB_NOMMAP)); 585 PyModule_AddObject(m, "CONVERT", PyInt_FromLong(TDB_CONVERT)); 586 PyModule_AddObject(m, "BIGENDIAN", PyInt_FromLong(TDB_BIGENDIAN)); 587 PyModule_AddObject(m, "NOSYNC", PyInt_FromLong(TDB_NOSYNC)); 588 PyModule_AddObject(m, "SEQNUM", PyInt_FromLong(TDB_SEQNUM)); 589 PyModule_AddObject(m, "VOLATILE", PyInt_FromLong(TDB_VOLATILE)); 590 PyModule_AddObject(m, "ALLOW_NESTING", PyInt_FromLong(TDB_ALLOW_NESTING)); 591 PyModule_AddObject(m, "DISALLOW_NESTING", PyInt_FromLong(TDB_DISALLOW_NESTING)); 592 PyModule_AddObject(m, "INCOMPATIBLE_HASH", PyInt_FromLong(TDB_INCOMPATIBLE_HASH)); 593 594 PyModule_AddObject(m, "__docformat__", PyString_FromString("restructuredText")); 595 596 PyModule_AddObject(m, "__version__", PyString_FromString(PACKAGE_VERSION)); 597 598 Py_INCREF(&PyTdb); 599 PyModule_AddObject(m, "Tdb", (PyObject *)&PyTdb); 600 601 Py_INCREF(&PyTdbIterator); 602 } 797 module_init(); 798 } 799 #endif
Note:
See TracChangeset
for help on using the changeset viewer.