summaryrefslogtreecommitdiff
path: root/python/string.cc
diff options
context:
space:
mode:
authorMichael Vogt <michael.vogt@ubuntu.com>2012-10-02 11:34:51 +0200
committerMichael Vogt <michael.vogt@ubuntu.com>2012-10-02 11:34:51 +0200
commit957603e4d012d29e69fefa4737a7d640e514875b (patch)
treeb16b77d4720d98f918d868811d75f51a4a188317 /python/string.cc
parent176e587073b3e529e1494175dc0b250fd2fb79b1 (diff)
parent8d1fae9e2bb03225c81ac1f0719408618d86eb52 (diff)
downloadpython-apt-957603e4d012d29e69fefa4737a7d640e514875b.tar.gz
merge upload from Barry
Diffstat (limited to 'python/string.cc')
-rw-r--r--python/string.cc28
1 files changed, 20 insertions, 8 deletions
diff --git a/python/string.cc b/python/string.cc
index 7abe2d17..62aa34e7 100644
--- a/python/string.cc
+++ b/python/string.cc
@@ -64,17 +64,29 @@ MkInt(StrTimeRFC1123,TimeRFC1123, long long, "L");
PyObject *StrSizeToStr(PyObject *Self,PyObject *Args)
{
PyObject *Obj;
+ double value;
+
if (PyArg_ParseTuple(Args,"O",&Obj) == 0)
return 0;
- if (PyInt_Check(Obj))
- return CppPyString(SizeToStr(PyInt_AsLong(Obj)));
+ // In Python 3, PyInt_Check is aliased to PyLong_Check and PyInt_AsLong is
+ // aliased to PyLong_AsLong. Therefore we do the actual long checks first
+ // so that if it is a long in Python 3, the value will be converted to a
+ // double rather than a long. This avoids OverflowError regressions in
+ // Python 3. LP: #1030278
if (PyLong_Check(Obj))
- return CppPyString(SizeToStr(PyLong_AsDouble(Obj)));
- if (PyFloat_Check(Obj))
- return CppPyString(SizeToStr(PyFloat_AsDouble(Obj)));
-
- PyErr_SetString(PyExc_TypeError,"Only understand integers and floats");
- return 0;
+ value = PyLong_AsDouble(Obj);
+ else if (PyInt_Check(Obj))
+ value = PyInt_AsLong(Obj);
+ else if (PyFloat_Check(Obj))
+ value = PyFloat_AsDouble(Obj);
+ else {
+ PyErr_SetString(PyExc_TypeError,"Only understand integers and floats");
+ return 0;
+ }
+ // Check for OverflowErrors or other exceptions during conversion.
+ if (PyErr_Occurred())
+ return 0;
+ return CppPyString(SizeToStr(value));
}
PyObject *StrQuoteString(PyObject *Self,PyObject *Args)