[Python-3000] Strange method resolution problem with __trunc__, __round__ on floats

Guido van Rossum guido at python.org
Fri Aug 24 04:02:23 CEST 2007


I figured it out by stepping through builtin_trunc and into
_PyType_Lookup for a bit. The type is not completely initialized;
apparently fundamental types like float get initialized lazily
*really* late. Inserting this block of code before the _PyType_Lookup
call fixes things:

        if (Py_Type(number)->tp_dict == NULL) {
                if (PyType_Ready(Py_Type(number)) < 0)
                        return NULL;
        }

I'll check in a change ASAP.

(Eric: this applies to the code I mailed you for format() earlier too!)

--Guido

On 8/23/07, Keir Mierle <mierle at gmail.com> wrote:
> The newly introduced trunc() and round() have the following odd behavior:
>
> $ ./python
> Python 3.0x (py3k, Aug 23 2007, 17:15:22)
> [GCC 4.0.3 (Ubuntu 4.0.3-1ubuntu5)] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
> >>> trunc(3.14)
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> TypeError: type float doesn't define __trunc__ method
> [36040 refs]
> >>> 3.14.__trunc__
> <built-in method __trunc__ of float object at 0x8255244>
> [36230 refs]
> >>> trunc(3.14)
> 3
> [36230 refs]
> >>>
>
> It looks like builtin_trunc() is failing at the call to
> _PyType_Lookup(), which must be returning NULL to get the above
> behavior. I'm not sure what's causing this; perhaps someone more
> experienced than me has an idea?
>
> Keir
> _______________________________________________
> Python-3000 mailing list
> Python-3000 at python.org
> http://mail.python.org/mailman/listinfo/python-3000
> Unsubscribe: http://mail.python.org/mailman/options/python-3000/guido%40python.org
>


-- 
--Guido van Rossum (home page: http://www.python.org/~guido/)


More information about the Python-3000 mailing list