summaryrefslogtreecommitdiff
path: root/python/apt_pkgmodule.cc
diff options
context:
space:
mode:
authorJulian Andres Klode <jak@debian.org>2009-04-15 16:19:12 +0200
committerJulian Andres Klode <jak@debian.org>2009-04-15 16:19:12 +0200
commitc876c5095673a2f1c0f2c0eef6eadef2ce200e19 (patch)
treef0832dbbdeb688e0176294e9b487f996108555ad /python/apt_pkgmodule.cc
parent97d985d73d12da5578628418aa787d78a33b652d (diff)
downloadpython-apt-c876c5095673a2f1c0f2c0eef6eadef2ce200e19.tar.gz
* Introduce support for Python 3 (Closes: #523645)
This is the first initial port to Python 3. The API is almost completely identical to the one found in Python 2, except that functions working with binary data require bytes (md5sum,sha1sum,sha256sum,Base64Encode). Using setup3.py to install the modules will not work, because the apt package still has to be converted to Python 3. For the package, we call 2to3-3.1 in debian/rules to do this automatically.
Diffstat (limited to 'python/apt_pkgmodule.cc')
-rw-r--r--python/apt_pkgmodule.cc87
1 files changed, 64 insertions, 23 deletions
diff --git a/python/apt_pkgmodule.cc b/python/apt_pkgmodule.cc
index fe6a739e..e71d8ee6 100644
--- a/python/apt_pkgmodule.cc
+++ b/python/apt_pkgmodule.cc
@@ -173,12 +173,12 @@ static PyObject *md5sum(PyObject *Self,PyObject *Args)
return 0;
// Digest of a string.
- if (PyString_Check(Obj) != 0)
+ if (PyBytes_Check(Obj) != 0)
{
char *s;
Py_ssize_t len;
MD5Summation Sum;
- PyString_AsStringAndSize(Obj, &s, &len);
+ PyBytes_AsStringAndSize(Obj, &s, &len);
Sum.Add((const unsigned char*)s, len);
return CppPyString(Sum.Result().Value());
}
@@ -213,12 +213,12 @@ static PyObject *sha1sum(PyObject *Self,PyObject *Args)
return 0;
// Digest of a string.
- if (PyString_Check(Obj) != 0)
+ if (PyBytes_Check(Obj) != 0)
{
char *s;
Py_ssize_t len;
SHA1Summation Sum;
- PyString_AsStringAndSize(Obj, &s, &len);
+ PyBytes_AsStringAndSize(Obj, &s, &len);
Sum.Add((const unsigned char*)s, len);
return CppPyString(Sum.Result().Value());
}
@@ -253,12 +253,12 @@ static PyObject *sha256sum(PyObject *Self,PyObject *Args)
return 0;
// Digest of a string.
- if (PyString_Check(Obj) != 0)
+ if (PyBytes_Check(Obj) != 0)
{
char *s;
Py_ssize_t len;
SHA256Summation Sum;
- PyString_AsStringAndSize(Obj, &s, &len);
+ PyBytes_AsStringAndSize(Obj, &s, &len);
Sum.Add((const unsigned char*)s, len);
return CppPyString(Sum.Result().Value());
}
@@ -470,30 +470,68 @@ static void AddInt(PyObject *Dict,const char *Itm,unsigned long I)
Py_DECREF(Obj);
}
+#if PY_MAJOR_VERSION >= 3
+struct module_state {
+ PyObject *error;
+};
+
+#define GETSTATE(m) ((struct module_state*)PyModule_GetState(m))
+
+static int apt_inst_traverse(PyObject *m, visitproc visit, void *arg) {
+ Py_VISIT(GETSTATE(m)->error);
+ return 0;
+}
+
+static int apt_inst_clear(PyObject *m) {
+ Py_CLEAR(GETSTATE(m)->error);
+ return 0;
+}
+
+static struct PyModuleDef moduledef = {
+ PyModuleDef_HEAD_INIT,
+ "apt_inst",
+ NULL,
+ sizeof(struct module_state),
+ methods,
+ NULL,
+ apt_inst_traverse,
+ apt_inst_clear,
+ NULL
+};
+
+#define INIT_ERROR return 0
+extern "C" PyObject * PyInit_apt_pkg()
+#else
+#define INIT_ERROR return
extern "C" void initapt_pkg()
+#endif
{
// Finalize our types to add slots, etc.
- if (PyType_Ready(&TagSecType) == -1) return;
- if (PyType_Ready(&TagFileType) == -1) return;
- if (PyType_Ready(&ConfigurationType) == -1) return;
- if (PyType_Ready(&ConfigurationPtrType) == -1) return;
- if (PyType_Ready(&ConfigurationSubType) == -1) return;
- if (PyType_Ready(&PkgCdromType) == -1) return;
- if (PyType_Ready(&PkgProblemResolverType) == -1) return;
- if (PyType_Ready(&PkgActionGroupType) == -1) return;
- if (PyType_Ready(&PkgSourceListType) == -1) return;
- if (PyType_Ready(&PkgCacheType) == -1) return;
- if (PyType_Ready(&DependencyType) == -1) return;
- if (PyType_Ready(&PkgDepCacheType) == -1) return;
- if (PyType_Ready(&PkgAcquireType) == -1) return;
- if (PyType_Ready(&PackageIndexFileType) == -1) return;
- if (PyType_Ready(&PkgManagerType) == -1) return;
- if (PyType_Ready(&PkgSrcRecordsType) == -1) return;
- if (PyType_Ready(&PkgRecordsType) == -1) return;
+ if (PyType_Ready(&TagSecType) == -1) INIT_ERROR;
+ if (PyType_Ready(&TagFileType) == -1) INIT_ERROR;
+ if (PyType_Ready(&ConfigurationType) == -1) INIT_ERROR;
+ if (PyType_Ready(&ConfigurationPtrType) == -1) INIT_ERROR;
+ if (PyType_Ready(&ConfigurationSubType) == -1) INIT_ERROR;
+ if (PyType_Ready(&PkgCdromType) == -1) INIT_ERROR;
+ if (PyType_Ready(&PkgProblemResolverType) == -1) INIT_ERROR;
+ if (PyType_Ready(&PkgActionGroupType) == -1) INIT_ERROR;
+ if (PyType_Ready(&PkgSourceListType) == -1) INIT_ERROR;
+ if (PyType_Ready(&PkgCacheType) == -1) INIT_ERROR;
+ if (PyType_Ready(&DependencyType) == -1) INIT_ERROR;
+ if (PyType_Ready(&PkgDepCacheType) == -1) INIT_ERROR;
+ if (PyType_Ready(&PkgAcquireType) == -1) INIT_ERROR;
+ if (PyType_Ready(&PackageIndexFileType) == -1) INIT_ERROR;
+ if (PyType_Ready(&PkgManagerType) == -1) INIT_ERROR;
+ if (PyType_Ready(&PkgSrcRecordsType) == -1) INIT_ERROR;
+ if (PyType_Ready(&PkgRecordsType) == -1) INIT_ERROR;
// Initialize the module
+ #if PY_MAJOR_VERSION >= 3
+ PyObject *Module = PyModule_Create(&moduledef);
+ #else
PyObject *Module = Py_InitModule("apt_pkg",methods);
+ #endif
PyObject *Dict = PyModule_GetDict(Module);
// Global variable linked to the global configuration class
@@ -549,6 +587,9 @@ extern "C" void initapt_pkg()
AddInt(Dict,"InstStateReInstReq",pkgCache::State::ReInstReq);
AddInt(Dict,"InstStateHold",pkgCache::State::Hold);
AddInt(Dict,"InstStateHoldReInstReq",pkgCache::State::HoldReInstReq);
+ #if PY_MAJOR_VERSION >= 3
+ return Module;
+ #endif
}
/*}}}*/