LEFT | RIGHT |
1 | 1 |
2 /* DBM module using dictionary interface */ | 2 /* DBM module using dictionary interface */ |
3 /* Author: Anthony Baxter, after dbmmodule.c */ | 3 /* Author: Anthony Baxter, after dbmmodule.c */ |
4 /* Doc strings: Mitch Chapman */ | 4 /* Doc strings: Mitch Chapman */ |
5 | 5 |
6 | 6 |
7 #include "Python.h" | 7 #include "Python.h" |
8 | 8 |
9 #include <sys/types.h> | 9 #include <sys/types.h> |
10 #include <sys/stat.h> | 10 #include <sys/stat.h> |
(...skipping 671 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
682 PyErr_SetString(DbmError, gdbm_strerror(gdbm_errno)); | 682 PyErr_SetString(DbmError, gdbm_strerror(gdbm_errno)); |
683 PyErr_SetString(DbmError, "Cannot add item to database"); | 683 PyErr_SetString(DbmError, "Cannot add item to database"); |
684 return NULL; | 684 return NULL; |
685 } | 685 } |
686 } | 686 } |
687 Py_RETURN_NONE; | 687 Py_RETURN_NONE; |
688 } | 688 } |
689 | 689 |
690 PyDoc_STRVAR(dbm_update__doc__, | 690 PyDoc_STRVAR(dbm_update__doc__, |
691 "update([other][, **keywords]) -> None\n\ | 691 "update([other][, **keywords]) -> None\n\ |
692 Add all the items in other if other is a mapping object, raise TypeError " | 692 Add all the items in keywords to the databases.\n\ |
693 "else, as well as those in keywords."); | 693 If other is a mapping object, does: for k in other: self[k] = other[k]\n\ |
| 694 Else if other has a \"keys()\" method, does: for k in other.keys(): \ |
| 695 self[k] = other[k]\n\ |
| 696 Else does: for k, v in other: self[k] = v"); |
694 | 697 |
695 typedef struct { | 698 typedef struct { |
696 PyObject_HEAD | 699 PyObject_HEAD |
697 dbmobject *dbm; /* Set to NULL when iterator is exhausted */ | 700 dbmobject *dbm; /* Set to NULL when iterator is exhausted */ |
698 datum curr_key; | 701 datum curr_key; |
699 } dbmiterobject; | 702 } dbmiterobject; |
700 | 703 |
701 static void | 704 static void |
702 dbmiter_dealloc(dbmiterobject *di) | 705 dbmiter_dealloc(dbmiterobject *di) |
703 { | 706 { |
(...skipping 319 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1023 return ok; | 1026 return ok; |
1024 } | 1027 } |
1025 | 1028 |
1026 static PyObject * | 1029 static PyObject * |
1027 dbmview_richcompare(PyObject *self, PyObject *other, int op) | 1030 dbmview_richcompare(PyObject *self, PyObject *other, int op) |
1028 { | 1031 { |
1029 Py_ssize_t len_self, len_other; | 1032 Py_ssize_t len_self, len_other; |
1030 int ok; | 1033 int ok; |
1031 PyObject *result; | 1034 PyObject *result; |
1032 | 1035 |
1033 assert(self != NULL); | 1036 assert(self != NULL && other != NULL); |
1034 assert(is_dbmviewsetobject(self)); | 1037 assert(is_dbmviewsetobject(self)); |
1035 assert(other != NULL); | |
1036 | 1038 |
1037 if (!PyAnySet_Check(other) && !is_dbmviewsetobject(other)) { | 1039 if (!PyAnySet_Check(other) && !is_dbmviewsetobject(other)) { |
1038 Py_INCREF(Py_NotImplemented); | 1040 Py_INCREF(Py_NotImplemented); |
1039 return Py_NotImplemented; | 1041 return Py_NotImplemented; |
1040 } | 1042 } |
1041 | 1043 |
1042 len_self = PyObject_Size(self); | 1044 len_self = PyObject_Size(self); |
1043 if (len_self < 0) | 1045 if (len_self < 0) |
1044 return NULL; | 1046 return NULL; |
1045 len_other = PyObject_Size(other); | 1047 len_other = PyObject_Size(other); |
(...skipping 250 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1296 0, /* tp_weaklistoffset */ | 1298 0, /* tp_weaklistoffset */ |
1297 (getiterfunc)dbmkeys_iter, /* tp_iter */ | 1299 (getiterfunc)dbmkeys_iter, /* tp_iter */ |
1298 0, /* tp_iternext */ | 1300 0, /* tp_iternext */ |
1299 dbmviews_methods, /* tp_methods */ | 1301 dbmviews_methods, /* tp_methods */ |
1300 0, | 1302 0, |
1301 }; | 1303 }; |
1302 | 1304 |
1303 /* XXX Should return a set or a set view */ | 1305 /* XXX Should return a set or a set view */ |
1304 PyDoc_STRVAR(dbm_keys__doc__, | 1306 PyDoc_STRVAR(dbm_keys__doc__, |
1305 "keys() -> keys view\n\ | 1307 "keys() -> keys view\n\ |
1306 Get a set-like object providing a view on database's keys."); | 1308 Get a set-like object providing a view on the database's keys."); |
1307 | 1309 |
1308 static PyObject * | 1310 static PyObject * |
1309 dbmkeys_new(PyObject *dbm) | 1311 dbmkeys_new(PyObject *dbm) |
1310 { | 1312 { |
1311 return dbmview_new(dbm, &DbmKeys_Type); | 1313 return dbmview_new(dbm, &DbmKeys_Type); |
1312 } | 1314 } |
1313 | 1315 |
1314 | 1316 |
1315 /*** dbm_values ***/ | 1317 /*** dbm_values ***/ |
1316 | 1318 |
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1466 dbmview_richcompare, /* tp_richcompare */ | 1468 dbmview_richcompare, /* tp_richcompare */ |
1467 0, /* tp_weaklistoffset */ | 1469 0, /* tp_weaklistoffset */ |
1468 (getiterfunc)dbmitems_iter, /* tp_iter */ | 1470 (getiterfunc)dbmitems_iter, /* tp_iter */ |
1469 0, /* tp_iternext */ | 1471 0, /* tp_iternext */ |
1470 dbmviews_methods, /* tp_methods */ | 1472 dbmviews_methods, /* tp_methods */ |
1471 0, | 1473 0, |
1472 }; | 1474 }; |
1473 | 1475 |
1474 PyDoc_STRVAR(dbm_items__doc__, | 1476 PyDoc_STRVAR(dbm_items__doc__, |
1475 "items() -> items view\n\ | 1477 "items() -> items view\n\ |
1476 Get a set-like object providing a view on database's items."); | 1478 Get a set-like object providing a view on the database's items."); |
1477 | 1479 |
1478 static PyObject * | 1480 static PyObject * |
1479 dbmitems_new(PyObject *dbm) | 1481 dbmitems_new(PyObject *dbm) |
1480 { | 1482 { |
1481 return dbmview_new(dbm, &DbmItems_Type); | 1483 return dbmview_new(dbm, &DbmItems_Type); |
1482 } | 1484 } |
1483 | 1485 |
1484 PyDoc_STRVAR(dbm_values__doc__, | 1486 PyDoc_STRVAR(dbm_values__doc__, |
1485 "values() -> values view\n\ | 1487 "values() -> values view\n\ |
1486 Get an object providing a view on database's values."); | 1488 Get an object providing a view on the database's values."); |
1487 | 1489 |
1488 static PyObject * | 1490 static PyObject * |
1489 dbmvalues_new(PyObject *dbm) | 1491 dbmvalues_new(PyObject *dbm) |
1490 { | 1492 { |
1491 return dbmview_new(dbm, &DbmValues_Type); | 1493 return dbmview_new(dbm, &DbmValues_Type); |
1492 } | 1494 } |
1493 | 1495 |
1494 static PyMethodDef dbm_methods[] = { | 1496 static PyMethodDef dbm_methods[] = { |
1495 {"close", (PyCFunction)dbm_close, METH_NOARGS, dbm_close__doc__}, | 1497 {"close", (PyCFunction)dbm_close, METH_NOARGS, dbm_close__doc__}, |
1496 {"keys", (PyCFunction)dbmkeys_new, METH_NOARGS, dbm_keys__doc__}, | 1498 {"keys", (PyCFunction)dbmkeys_new, METH_NOARGS, dbm_keys__doc__}, |
(...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1650 dbmmodule_methods, | 1652 dbmmodule_methods, |
1651 NULL, | 1653 NULL, |
1652 NULL, | 1654 NULL, |
1653 NULL, | 1655 NULL, |
1654 NULL | 1656 NULL |
1655 }; | 1657 }; |
1656 | 1658 |
1657 PyMODINIT_FUNC | 1659 PyMODINIT_FUNC |
1658 PyInit__gdbm(void) { | 1660 PyInit__gdbm(void) { |
1659 PyObject *m, *d, *s; | 1661 PyObject *m, *d, *s; |
1660 PyObject *coll_mod, *mapping_abc, *keysview_abc, *valuesview_abc, | |
1661 *itemsview_abc; | |
1662 | 1662 |
1663 if (PyType_Ready(&Dbmtype) < 0) | 1663 if (PyType_Ready(&Dbmtype) < 0) |
1664 return NULL; | 1664 return NULL; |
1665 | |
1666 if ((coll_mod = PyImport_ImportModule("collections")) == NULL) | |
1667 return NULL; | |
1668 if ((mapping_abc = PyObject_GetAttrString(coll_mod, | |
1669 "MutableMapping")) == NULL) { | |
1670 Py_DECREF(coll_mod); | |
1671 return NULL; | |
1672 } | |
1673 if ((keysview_abc = PyObject_GetAttrString(coll_mod, | |
1674 "KeysView")) == NULL) { | |
1675 Py_DECREF(coll_mod); | |
1676 return NULL; | |
1677 } | |
1678 if ((valuesview_abc = PyObject_GetAttrString(coll_mod, | |
1679 "ValuesView")) == NULL) { | |
1680 Py_DECREF(coll_mod); | |
1681 return NULL; | |
1682 } | |
1683 if ((itemsview_abc = PyObject_GetAttrString(coll_mod, | |
1684 "ItemsView")) == NULL) { | |
1685 Py_DECREF(coll_mod); | |
1686 return NULL; | |
1687 } | |
1688 Py_DECREF(coll_mod); | |
1689 if (PyObject_CallMethod(mapping_abc, "register", "O", &Dbmtype) == NULL) { | |
1690 Py_DECREF(mapping_abc); | |
1691 return NULL; | |
1692 } | |
1693 Py_DECREF(mapping_abc); | |
1694 | |
1695 if (PyObject_CallMethod(keysview_abc, "register", "O", | |
1696 &DbmKeys_Type) == NULL) { | |
1697 Py_DECREF(keysview_abc); | |
1698 return NULL; | |
1699 } | |
1700 Py_DECREF(keysview_abc); | |
1701 | |
1702 if (PyObject_CallMethod(valuesview_abc, "register", "O", | |
1703 &DbmValues_Type) == NULL) { | |
1704 Py_DECREF(valuesview_abc); | |
1705 return NULL; | |
1706 } | |
1707 Py_DECREF(valuesview_abc); | |
1708 | |
1709 if (PyObject_CallMethod(itemsview_abc, "register", "O", | |
1710 &DbmItems_Type) == NULL) { | |
1711 Py_DECREF(itemsview_abc); | |
1712 return NULL; | |
1713 } | |
1714 Py_DECREF(itemsview_abc); | |
1715 | 1665 |
1716 m = PyModule_Create(&_gdbmmodule); | 1666 m = PyModule_Create(&_gdbmmodule); |
1717 if (m == NULL) | 1667 if (m == NULL) |
1718 return NULL; | 1668 return NULL; |
1719 d = PyModule_GetDict(m); | 1669 d = PyModule_GetDict(m); |
1720 DbmError = PyErr_NewException("_gdbm.error", PyExc_IOError, NULL); | 1670 DbmError = PyErr_NewException("_gdbm.error", PyExc_IOError, NULL); |
1721 if (DbmError != NULL) { | 1671 if (DbmError != NULL) { |
1722 PyDict_SetItemString(d, "error", DbmError); | 1672 PyDict_SetItemString(d, "error", DbmError); |
1723 s = PyUnicode_FromString(dbmmodule_open_flags); | 1673 s = PyUnicode_FromString(dbmmodule_open_flags); |
1724 PyDict_SetItemString(d, "open_flags", s); | 1674 PyDict_SetItemString(d, "open_flags", s); |
1725 Py_DECREF(s); | 1675 Py_DECREF(s); |
1726 } | 1676 } |
1727 return m; | 1677 return m; |
1728 } | 1678 } |
LEFT | RIGHT |