summaryrefslogtreecommitdiff
path: root/python/generic.cc
diff options
context:
space:
mode:
authorMichael Vogt <michael.vogt@ubuntu.com>2010-03-12 11:42:37 +0100
committerMichael Vogt <michael.vogt@ubuntu.com>2010-03-12 11:42:37 +0100
commit85238ea03cd35b48a90a2fc1f63f2cf05d5b83b4 (patch)
treebb7fcce7e80cc45e807eab19a3c36f628c888bd3 /python/generic.cc
parent62a7342edb16c38e3d646cc731a4a50ad6657b4f (diff)
parentc657b7a2a59e15a0c415ba94021c4de547a78e60 (diff)
downloadpython-apt-85238ea03cd35b48a90a2fc1f63f2cf05d5b83b4.tar.gz
merged from debian-sid
Diffstat (limited to 'python/generic.cc')
-rw-r--r--python/generic.cc63
1 files changed, 63 insertions, 0 deletions
diff --git a/python/generic.cc b/python/generic.cc
index b0770e02..5db1e490 100644
--- a/python/generic.cc
+++ b/python/generic.cc
@@ -48,6 +48,69 @@ PyObject *HandleErrors(PyObject *Res)
PyErr_SetString(PyExc_SystemError,Err.c_str());
return 0;
}
+
+# ifdef COMPAT_0_7
+// Helpers for deprecation.
+
+// Given the name of the old attribute, return the name of the new attribute
+// in a PyObject.
+static PyObject *_PyApt_NewNameForAttribute(const char *attr) {
+ // Some exceptions from the standard algorithm.
+ if (strcasecmp(attr, "FileName") == 0) return PyString_FromString("filename");
+ if (strcasecmp(attr, "DestFile") == 0) return PyString_FromString("destfile");
+ if (strcasecmp(attr, "FileSize") == 0) return PyString_FromString("filesize");
+ if (strcasecmp(attr, "SubTree") == 0) return PyString_FromString("subtree");
+ if (strcasecmp(attr, "ReadPinFile") == 0) return PyString_FromString("read_pinfile");
+ if (strcasecmp(attr, "SetReInstall") == 0) return PyString_FromString("set_reinstall");
+ if (strcasecmp(attr, "URI") == 0) return PyString_FromString("uri");
+ if (strcasecmp(attr, "MD5Hash") == 0) return PyString_FromString("md5_hash");
+ if (strcasecmp(attr, "SHA1Hash") == 0) return PyString_FromString("sha1_hash");
+ if (strcasecmp(attr, "SHA256Hash") == 0) return PyString_FromString("sha256_hash");
+ if (strcasecmp(attr, "UntranslatedDepType") == 0) return PyString_FromString("dep_type_untranslated");
+ size_t attrlen = strlen(attr);
+ // Reserve the old name + 5, this should reduce resize to a minimum.
+ string new_name;
+ new_name.reserve(attrlen + 5);
+ for(unsigned int i=0; i < attrlen; i++) {
+ // Replace all uppercase ASCII characters with their lower-case ones.
+ if (attr[i] > 64 && attr[i] < 91) {
+ if (i > 0)
+ new_name += "_";
+ new_name += attr[i] + 32;
+ } else {
+ new_name += attr[i];
+ }
+ }
+ return CppPyString(new_name);
+}
+
+// Handle deprecated attributes by setting a warning and returning the new
+// attribute.
+PyObject *_PyAptObject_getattro(PyObject *self, PyObject *attr) {
+ PyObject *value = PyObject_GenericGetAttr(self, attr);
+ if (value == NULL) {
+ PyObject *ptype, *pvalue, *ptraceback;
+ PyErr_Fetch(&ptype, &pvalue, &ptraceback);
+ const char *attrname = PyObject_AsString(attr);
+ PyObject *newattr = _PyApt_NewNameForAttribute(attrname);
+ value = PyObject_GenericGetAttr(self, newattr);
+ if (value != NULL) {
+ const char *newattrname = PyString_AsString(newattr);
+ const char *cls = self->ob_type->tp_name;
+ char *warning_string = new char[strlen(newattrname) + strlen(cls) +
+ strlen(attrname) + 66];
+ sprintf(warning_string, "Attribute '%s' of the '%s' object is "
+ "deprecated, use '%s' instead.", attrname, cls, newattrname);
+ PyErr_WarnEx(PyExc_DeprecationWarning, warning_string, 1);
+ delete[] warning_string;
+ } else {
+ PyErr_Restore(ptype, pvalue, ptraceback);
+ }
+ Py_DECREF(newattr);
+ }
+ return value;
+}
+# endif //COMPAT_0_7
/*}}}*/
// ListToCharChar - Convert a list to an array of char char /*{{{*/
// ---------------------------------------------------------------------