From 044322b4dca1135671a93ebfe1601214f7f6e655 Mon Sep 17 00:00:00 2001 From: Julian Andres Klode Date: Mon, 26 Aug 2013 13:39:57 +0200 Subject: Remove old API compatibility C++ support code --- python/generic.h | 6 ------ 1 file changed, 6 deletions(-) (limited to 'python/generic.h') diff --git a/python/generic.h b/python/generic.h index 914456e2..e4679c6c 100644 --- a/python/generic.h +++ b/python/generic.h @@ -58,8 +58,6 @@ typedef int Py_ssize_t; #define PyInt_Check PyLong_Check #define PyInt_AsLong PyLong_AsLong #define PyInt_FromLong PyLong_FromLong -// 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) @@ -251,10 +249,6 @@ inline PyObject *MkPyNumber(char o) { return PyInt_FromLong(o); } inline PyObject *MkPyNumber(double o) { return PyFloat_FromDouble(o); } -# ifdef COMPAT_0_7 -PyObject *_PyAptObject_getattro(PyObject *self, PyObject *attr); -# else # define _PyAptObject_getattro 0 -# endif #endif -- cgit v1.2.3 From 7c03e56db77a3ae69f3710f588342a6e1fde2250 Mon Sep 17 00:00:00 2001 From: Julian Andres Klode Date: Tue, 8 Oct 2013 17:36:16 +0200 Subject: python/generic.h: Introduce a PyApt_Filename class On Python 3, we need to encode filenames. We could use PyUnicode_FSConverter but this introduces unneeded complexity in all callees, and is only available in Python 3.1 anyway. --- python/generic.h | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) (limited to 'python/generic.h') diff --git a/python/generic.h b/python/generic.h index e4679c6c..97806e17 100644 --- a/python/generic.h +++ b/python/generic.h @@ -251,4 +251,64 @@ inline PyObject *MkPyNumber(double o) { return PyFloat_FromDouble(o); } # define _PyAptObject_getattro 0 + +/** + * Magic class for file name handling + * + * This manages decoding file names from Python objects; bytes and unicode + * objects. On Python 2, this does the same conversion as PyObject_AsString, + * on Python3, it uses PyUnicode_EncodeFSDefault for unicode objects. + */ +class PyApt_Filename { +public: + PyObject *object; + const char *path; + + PyApt_Filename() { + object = NULL; + path = NULL; + } + + int init(PyObject *object) { + this->object = NULL; + this->path = NULL; + +#if PY_MAJOR_VERSION < 3 || (PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION < 2) + this->path = PyObject_AsString(object); + return this->path ? 1 : 0; +#else + if (PyUnicode_Check(object)) { + object = PyUnicode_EncodeFSDefault(object); + } else if (PyBytes_Check(object)) { + Py_INCREF(object); + } else { + return 0; + } + + this->object = object; + this->path = PyBytes_AS_STRING(this->object); + return 1; +#endif + } + + ~PyApt_Filename() { + Py_XDECREF(object); + } + + static int Converter(PyObject *object, void *out) { + return static_cast(out)->init(object); + } + + operator const char *() { + return path; + } + operator const std::string() { + return path; + } + + void operator=(const char *path) { + this->path = path; + } +}; + #endif -- cgit v1.2.3 From 7aedf6d986f15d31d9597c139d91c761baf98f73 Mon Sep 17 00:00:00 2001 From: Julian Andres Klode Date: Tue, 8 Oct 2013 17:45:32 +0200 Subject: PyApt_Filename: Add return value to assignment operator --- python/generic.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'python/generic.h') diff --git a/python/generic.h b/python/generic.h index 97806e17..7dc89ca6 100644 --- a/python/generic.h +++ b/python/generic.h @@ -306,8 +306,8 @@ public: return path; } - void operator=(const char *path) { - this->path = path; + const char *operator=(const char *path) { + return this->path = path; } }; -- cgit v1.2.3 From 9305bd3ff845eb61649767b86688bb36b54d9437 Mon Sep 17 00:00:00 2001 From: Julian Andres Klode Date: Tue, 8 Oct 2013 18:46:37 +0200 Subject: python/generic.cc: Move PyApt_Filename::init here --- python/generic.cc | 23 +++++++++++++++++++++++ python/generic.h | 22 +--------------------- 2 files changed, 24 insertions(+), 21 deletions(-) (limited to 'python/generic.h') diff --git a/python/generic.cc b/python/generic.cc index 8e65c4f3..5e712899 100644 --- a/python/generic.cc +++ b/python/generic.cc @@ -93,3 +93,26 @@ PyObject *CharCharToList(const char **List,unsigned long Size) return PList; } /*}}}*/ + +int PyApt_Filename::init(PyObject *object) +{ + this->object = NULL; + this->path = NULL; + +#if PY_MAJOR_VERSION < 3 || (PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION < 2) + this->path = PyObject_AsString(object); + return this->path ? 1 : 0; +#else + if (PyUnicode_Check(object)) { + object = PyUnicode_EncodeFSDefault(object); + } else if (PyBytes_Check(object)) { + Py_INCREF(object); + } else { + return 0; + } + + this->object = object; + this->path = PyBytes_AS_STRING(this->object); + return 1; +#endif +} diff --git a/python/generic.h b/python/generic.h index 7dc89ca6..c9db916c 100644 --- a/python/generic.h +++ b/python/generic.h @@ -269,27 +269,7 @@ public: path = NULL; } - int init(PyObject *object) { - this->object = NULL; - this->path = NULL; - -#if PY_MAJOR_VERSION < 3 || (PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION < 2) - this->path = PyObject_AsString(object); - return this->path ? 1 : 0; -#else - if (PyUnicode_Check(object)) { - object = PyUnicode_EncodeFSDefault(object); - } else if (PyBytes_Check(object)) { - Py_INCREF(object); - } else { - return 0; - } - - this->object = object; - this->path = PyBytes_AS_STRING(this->object); - return 1; -#endif - } + int init(PyObject *object); ~PyApt_Filename() { Py_XDECREF(object); -- cgit v1.2.3 From ea815d93dda86504518bdf69488b1f336f2439a3 Mon Sep 17 00:00:00 2001 From: Julian Andres Klode Date: Wed, 23 Oct 2013 19:38:48 +0200 Subject: python/{depcache.cc,string.cc}: Ignore deprecation warnings for some calls Those functions are deprecated in APT, but we still need to provide them for backwards compatibility. --- python/depcache.cc | 2 ++ python/generic.h | 5 +++++ python/string.cc | 2 ++ 3 files changed, 9 insertions(+) (limited to 'python/generic.h') diff --git a/python/depcache.cc b/python/depcache.cc index 9f4b91b4..b257e2c9 100644 --- a/python/depcache.cc +++ b/python/depcache.cc @@ -895,7 +895,9 @@ static PyObject *PkgProblemResolverInstallProtect(PyObject *Self,PyObject *Args) pkgProblemResolver *fixer = GetCpp(Self); if (PyArg_ParseTuple(Args,"") == 0) return 0; + PY_APT_BEGIN_DEPRECATED; fixer->InstallProtect(); + PY_APT_END_DEPRECATED; Py_INCREF(Py_None); return HandleErrors(Py_None); } diff --git a/python/generic.h b/python/generic.h index c9db916c..26736f1a 100644 --- a/python/generic.h +++ b/python/generic.h @@ -75,6 +75,11 @@ typedef int Py_ssize_t; #define PyErr_WarnEx(cat,msg,stacklevel) PyErr_Warn(cat,msg) #endif +#define PY_APT_BEGIN_DEPRECATED { \ + _Pragma("GCC diagnostic push"); \ + _Pragma("GCC diagnostic ignored \"-Wdeprecated-declarations\""); } +#define PY_APT_END_DEPRECATED _Pragma("GCC diagnostic pop") + static inline const char *PyUnicode_AsString(PyObject *op) { // Convert to bytes object, using the default encoding. diff --git a/python/string.cc b/python/string.cc index 62aa34e7..9b02c933 100644 --- a/python/string.cc +++ b/python/string.cc @@ -113,8 +113,10 @@ PyObject *StrStrToTime(PyObject *Self,PyObject *Args) return 0; time_t Result; + PY_APT_BEGIN_DEPRECATED; if (StrToTime(Str,Result) == false) { + PY_APT_END_DEPRECATED; Py_INCREF(Py_None); return Py_None; } -- cgit v1.2.3 From c5ff65130e3c4353bfcdf1dcf3ab1d912597601d Mon Sep 17 00:00:00 2001 From: Julian Andres Klode Date: Mon, 27 Jan 2014 20:27:26 +0100 Subject: python/generic.h: Fix MkPyNumber to work if char is unsigned. We currently consider two cases "unsigned char" and "char". This works as long as "char" is signed, but this is not guaranteed. Change "char" to "signed char" instead. --- python/generic.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'python/generic.h') diff --git a/python/generic.h b/python/generic.h index 26736f1a..bcc4a5e4 100644 --- a/python/generic.h +++ b/python/generic.h @@ -250,7 +250,7 @@ inline PyObject *MkPyNumber(long long o) { return PyLong_FromLongLong(o); } inline PyObject *MkPyNumber(long o) { return PyInt_FromLong(o); } inline PyObject *MkPyNumber(int o) { return PyInt_FromLong(o); } inline PyObject *MkPyNumber(short o) { return PyInt_FromLong(o); } -inline PyObject *MkPyNumber(char o) { return PyInt_FromLong(o); } +inline PyObject *MkPyNumber(signed char o) { return PyInt_FromLong(o); } inline PyObject *MkPyNumber(double o) { return PyFloat_FromDouble(o); } -- cgit v1.2.3