mirror of
https://github.com/python/cpython.git
synced 2025-10-10 00:43:41 +00:00
Invert the checks in get_[u]long and get_[u]longlong. The intent was
to not accept float types; the result was that integer-like objects were not accepted.
This commit is contained in:
parent
e105f98046
commit
fe528ebf68
3 changed files with 35 additions and 20 deletions
|
@ -90,15 +90,31 @@ class NumberTestCase(unittest.TestCase):
|
||||||
def test_floats(self):
|
def test_floats(self):
|
||||||
# c_float and c_double can be created from
|
# c_float and c_double can be created from
|
||||||
# Python int, long and float
|
# Python int, long and float
|
||||||
|
class FloatLike(object):
|
||||||
|
def __float__(self):
|
||||||
|
return 2.0
|
||||||
|
f = FloatLike()
|
||||||
for t in float_types:
|
for t in float_types:
|
||||||
self.failUnlessEqual(t(2.0).value, 2.0)
|
self.failUnlessEqual(t(2.0).value, 2.0)
|
||||||
self.failUnlessEqual(t(2).value, 2.0)
|
self.failUnlessEqual(t(2).value, 2.0)
|
||||||
self.failUnlessEqual(t(2L).value, 2.0)
|
self.failUnlessEqual(t(2L).value, 2.0)
|
||||||
|
self.failUnlessEqual(t(f).value, 2.0)
|
||||||
|
|
||||||
def test_integers(self):
|
def test_integers(self):
|
||||||
# integers cannot be constructed from floats
|
class FloatLike(object):
|
||||||
|
def __float__(self):
|
||||||
|
return 2.0
|
||||||
|
f = FloatLike()
|
||||||
|
class IntLike(object):
|
||||||
|
def __int__(self):
|
||||||
|
return 2
|
||||||
|
i = IntLike()
|
||||||
|
# integers cannot be constructed from floats,
|
||||||
|
# but from integer-like objects
|
||||||
for t in signed_types + unsigned_types:
|
for t in signed_types + unsigned_types:
|
||||||
self.assertRaises(TypeError, t, 3.14)
|
self.assertRaises(TypeError, t, 3.14)
|
||||||
|
self.assertRaises(TypeError, t, f)
|
||||||
|
self.failUnlessEqual(t(i).value, 2)
|
||||||
|
|
||||||
def test_sizes(self):
|
def test_sizes(self):
|
||||||
for t in signed_types + unsigned_types + float_types:
|
for t in signed_types + unsigned_types + float_types:
|
||||||
|
|
|
@ -67,6 +67,9 @@ Core and builtins
|
||||||
Library
|
Library
|
||||||
-------
|
-------
|
||||||
|
|
||||||
|
- The ctypes int types did not accept objects implementing
|
||||||
|
__int__() in the constructor.
|
||||||
|
|
||||||
- #1189216: Fix the zipfile module to work on archives with headers
|
- #1189216: Fix the zipfile module to work on archives with headers
|
||||||
past the 2**31 byte boundary.
|
past the 2**31 byte boundary.
|
||||||
|
|
||||||
|
|
|
@ -355,10 +355,9 @@ static int
|
||||||
get_long(PyObject *v, long *p)
|
get_long(PyObject *v, long *p)
|
||||||
{
|
{
|
||||||
long x;
|
long x;
|
||||||
if (!PyInt_Check(v) && !PyLong_Check(v)) {
|
if (PyFloat_Check(v)) {
|
||||||
PyErr_Format(PyExc_TypeError,
|
PyErr_SetString(PyExc_TypeError,
|
||||||
"int expected instead of %s instance",
|
"int expected instead of float");
|
||||||
v->ob_type->tp_name);
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
x = PyInt_AsUnsignedLongMask(v);
|
x = PyInt_AsUnsignedLongMask(v);
|
||||||
|
@ -374,10 +373,9 @@ static int
|
||||||
get_ulong(PyObject *v, unsigned long *p)
|
get_ulong(PyObject *v, unsigned long *p)
|
||||||
{
|
{
|
||||||
unsigned long x;
|
unsigned long x;
|
||||||
if (!PyInt_Check(v) && !PyLong_Check(v)) {
|
if (PyFloat_Check(v)) {
|
||||||
PyErr_Format(PyExc_TypeError,
|
PyErr_SetString(PyExc_TypeError,
|
||||||
"int expected instead of %s instance",
|
"int expected instead of float");
|
||||||
v->ob_type->tp_name);
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
x = PyInt_AsUnsignedLongMask(v);
|
x = PyInt_AsUnsignedLongMask(v);
|
||||||
|
@ -395,10 +393,9 @@ static int
|
||||||
get_longlong(PyObject *v, PY_LONG_LONG *p)
|
get_longlong(PyObject *v, PY_LONG_LONG *p)
|
||||||
{
|
{
|
||||||
PY_LONG_LONG x;
|
PY_LONG_LONG x;
|
||||||
if (!PyInt_Check(v) && !PyLong_Check(v)) {
|
if (PyFloat_Check(v)) {
|
||||||
PyErr_Format(PyExc_TypeError,
|
PyErr_SetString(PyExc_TypeError,
|
||||||
"int expected instead of %s instance",
|
"int expected instead of float");
|
||||||
v->ob_type->tp_name);
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
x = PyInt_AsUnsignedLongLongMask(v);
|
x = PyInt_AsUnsignedLongLongMask(v);
|
||||||
|
@ -414,10 +411,9 @@ static int
|
||||||
get_ulonglong(PyObject *v, unsigned PY_LONG_LONG *p)
|
get_ulonglong(PyObject *v, unsigned PY_LONG_LONG *p)
|
||||||
{
|
{
|
||||||
unsigned PY_LONG_LONG x;
|
unsigned PY_LONG_LONG x;
|
||||||
if (!PyInt_Check(v) && !PyLong_Check(v)) {
|
if (PyFloat_Check(v)) {
|
||||||
PyErr_Format(PyExc_TypeError,
|
PyErr_SetString(PyExc_TypeError,
|
||||||
"int expected instead of %s instance",
|
"int expected instead of float");
|
||||||
v->ob_type->tp_name);
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
x = PyInt_AsUnsignedLongLongMask(v);
|
x = PyInt_AsUnsignedLongLongMask(v);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue