Skip to content

Commit b97d086

Browse files
author
tim_one
committed
More for SF bug [#460020] bug or feature: unicode() and subclasses
Repair float constructor to return a true float when passed a subclass instance. New PyFloat_CheckExact macro. git-svn-id: http://svn.python.org/projects/python/trunk@23094 6015fed2-1504-0410-9fe1-9d1591cc4771
1 parent 8909c5f commit b97d086

File tree

3 files changed

+7
-2
lines changed

3 files changed

+7
-2
lines changed

Include/floatobject.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ typedef struct {
1919
extern DL_IMPORT(PyTypeObject) PyFloat_Type;
2020

2121
#define PyFloat_Check(op) PyObject_TypeCheck(op, &PyFloat_Type)
22+
#define PyFloat_CheckExact(op) ((op)->ob_type == &PyFloat_Type)
2223

2324
/* Return Python float from string PyObject. Second argument ignored on
2425
input, and, if non-NULL, NULL is stored into *junk (this tried to serve a

Lib/test/test_descr.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1372,7 +1372,7 @@ def __repr__(self):
13721372
verify(repr(precfloat(1.1)) == "1.1")
13731373
a = precfloat(12345)
13741374
#XXX verify(float(a) == 12345.0)
1375-
#XXX verify(float(a).__class__ is float)
1375+
verify(float(a).__class__ is float)
13761376

13771377
class madtuple(tuple):
13781378
_rev = None

Objects/abstract.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -909,10 +909,14 @@ PyNumber_Float(PyObject *o)
909909

910910
if (o == NULL)
911911
return null_error();
912-
if (PyFloat_Check(o)) {
912+
if (PyFloat_CheckExact(o)) {
913913
Py_INCREF(o);
914914
return o;
915915
}
916+
if (PyFloat_Check(o)) {
917+
PyFloatObject *po = (PyFloatObject *)o;
918+
return PyFloat_FromDouble(po->ob_fval);
919+
}
916920
if (!PyString_Check(o)) {
917921
m = o->ob_type->tp_as_number;
918922
if (m && m->nb_float)

0 commit comments

Comments
 (0)