]> granicus.if.org Git - python/commitdiff
More for SF bug [#460020] bug or feature: unicode() and subclasses
authorTim Peters <tim.peters@gmail.com>
Mon, 10 Sep 2001 21:28:20 +0000 (21:28 +0000)
committerTim Peters <tim.peters@gmail.com>
Mon, 10 Sep 2001 21:28:20 +0000 (21:28 +0000)
Repair float constructor to return a true float when passed a subclass
instance.  New PyFloat_CheckExact macro.

Include/floatobject.h
Lib/test/test_descr.py
Objects/abstract.c

index 9f67bc107d63ee3a83ce8b867933dbdceee6d566..bd4a782846d58fc1e4931ff74728d8b08dc65f7f 100644 (file)
@@ -19,6 +19,7 @@ typedef struct {
 extern DL_IMPORT(PyTypeObject) PyFloat_Type;
 
 #define PyFloat_Check(op) PyObject_TypeCheck(op, &PyFloat_Type)
+#define PyFloat_CheckExact(op) ((op)->ob_type == &PyFloat_Type)
 
 /* Return Python float from string PyObject.  Second argument ignored on
    input, and, if non-NULL, NULL is stored into *junk (this tried to serve a
index 35544c6762270e4ef16bebc49fad2f347bce72ca..1ff90607a4bc698ef159a32456090c3f1a19582b 100644 (file)
@@ -1372,7 +1372,7 @@ def inherits():
     verify(repr(precfloat(1.1)) == "1.1")
     a = precfloat(12345)
     #XXX verify(float(a) == 12345.0)
-    #XXX verify(float(a).__class__ is float)
+    verify(float(a).__class__ is float)
 
     class madtuple(tuple):
         _rev = None
index 2bd0fcceba1618a5cf8ae0a97afdcba163181998..37f7eeab0528a052946312dbf756bc8326481afe 100644 (file)
@@ -909,10 +909,14 @@ PyNumber_Float(PyObject *o)
 
        if (o == NULL)
                return null_error();
-       if (PyFloat_Check(o)) {
+       if (PyFloat_CheckExact(o)) {
                Py_INCREF(o);
                return o;
        }
+       if (PyFloat_Check(o)) {
+               PyFloatObject *po = (PyFloatObject *)o;
+               return PyFloat_FromDouble(po->ob_fval);
+       }
        if (!PyString_Check(o)) {
                m = o->ob_type->tp_as_number;
                if (m && m->nb_float)