diff options
Diffstat (limited to 'python')
| -rw-r--r-- | python/cache.cc | 13 | ||||
| -rw-r--r-- | python/generic.cc | 3 | ||||
| -rw-r--r-- | python/generic.h | 27 |
3 files changed, 34 insertions, 9 deletions
diff --git a/python/cache.cc b/python/cache.cc index 4ce3178c..2de9c76a 100644 --- a/python/cache.cc +++ b/python/cache.cc @@ -219,21 +219,20 @@ static PyGetSetDef PkgCacheGetSet[] = { {} }; + + // Map access, operator [] static PyObject *CacheMapOp(PyObject *Self,PyObject *Arg) { pkgCache *Cache = GetCpp<pkgCache *>(Self); - if (PyString_Check(Arg) == 0) - { - PyObject *repr = PyObject_Repr(Arg); - PyErr_SetObject(PyExc_TypeError, repr); - Py_DECREF(repr); + // Get the name of the package, unicode and normal strings. + const char *Name = PyObject_AsString(Arg); + if (Name == NULL) return 0; - } + // Search for the package - const char *Name = PyString_AsString(Arg); pkgCache::PkgIterator Pkg = Cache->FindPkg(Name); if (Pkg.end() == true) { diff --git a/python/generic.cc b/python/generic.cc index 7309d978..51439a1b 100644 --- a/python/generic.cc +++ b/python/generic.cc @@ -25,8 +25,9 @@ PyObject *HandleErrors(PyObject *Res) return Res; } - if (Res != 0) + if (Res != 0) { Py_DECREF(Res); + } string Err; int errcnt = 0; diff --git a/python/generic.h b/python/generic.h index 4fe1f915..2a87b39c 100644 --- a/python/generic.h +++ b/python/generic.h @@ -52,22 +52,47 @@ typedef int Py_ssize_t; #define PyString_Check PyUnicode_Check #define PyString_FromString PyUnicode_FromString #define PyString_FromStringAndSize PyUnicode_FromStringAndSize -#define PyString_AsString(op) PyBytes_AsString(PyUnicode_AsUTF8String(op)) +#define PyString_AsString PyUnicode_AsString #define PyInt_Check PyLong_Check #define PyInt_AsLong PyLong_AsLong // Force 0.7 compatibility to be off in Python 3 builds #undef COMPAT_0_7 #else +// Compatibility for Python 2.5 and previous. +#if (PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION <= 5) #define PyBytes_Check PyString_Check #define PyBytes_AsString PyString_AsString #define PyBytes_AsStringAndSize PyString_AsStringAndSize #endif +#endif // Hacks to make Python 2.4 build. #if PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION <= 4 #define PyErr_WarnEx(cat,msg,stacklevel) PyErr_Warn(cat,msg) #endif + +static inline const char *PyUnicode_AsString(PyObject *op) { + // Convert to bytes object, using the default encoding. + PyObject *bytes = PyUnicode_AsEncodedString(op,0,0); + if (!bytes) + return 0; + const char *result = PyBytes_AsString(bytes); + Py_DECREF(bytes); + return result; +} + +// Convert any type of string based object to a const char. +static inline const char *PyObject_AsString(PyObject *object) { + if (PyBytes_Check(object)) + return PyBytes_AsString(object); + else if (PyUnicode_Check(object)) + return PyUnicode_AsString(object); + else + PyErr_SetObject(PyExc_TypeError, object); + return 0; +} + template <class T> struct CppPyObject : public PyObject { // We are only using CppPyObject and friends as dumb structs only, ie the |
