cpython icon indicating copy to clipboard operation
cpython copied to clipboard

gh-112636: remove check for float subclasses without nb_float

Open skirpichev opened this issue 2 years ago • 2 comments

  • Issue: gh-112636

skirpichev avatar Dec 03 '23 02:12 skirpichev

Probably, this could skip news.

skirpichev avatar Dec 03 '23 02:12 skirpichev

@serhiy-storchaka, could you please comment what's wrong with the pr on your opinion? Apparently, if removed block should be kept - then we must restore similar block in PyNumber_Long(), removed in 31a655411.

skirpichev avatar Dec 04 '23 02:12 skirpichev

CC @vstinner, does it make sense for you?

skirpichev avatar Sep 27 '24 03:09 skirpichev

>>> a.break_it()
>>> float(a)  # and only NOW this branch will work...
XXX
3.14

I don't understand the example. float(a) does now return XXX? Or does it print XXX? Which part of the code prints XXX?

vstinner avatar Sep 27 '24 19:09 vstinner

I don't understand the example. float(a) does now return XXX?

Ah, that was just a debug print from the removed (by this pr) code. I've added a diff to description.

The point is that to trigger that code - you must explicitly set nb_float field in the derived class to NULL. But in this way we can break everything! Can't you set nb_add to NULL in a float subclass? Easy! But CPython has enough code, which assuming that float subclasses have working arithmetic dunder methods.

skirpichev avatar Sep 28 '24 02:09 skirpichev

What is the behavior on this nb_float=NULL type with your change? Does return PyFloat_FromString(o) fail?

vstinner avatar Sep 30 '24 16:09 vstinner

What is the behavior on this nb_float=NULL type with your change?

It fails with TypeError (after you run first break_it()).

Does return PyFloat_FromString(o) fail?

Yes, because o is not a subclass of str/bytes/etc.

skirpichev avatar Oct 01 '24 00:10 skirpichev

Sorry, but I now think that we should rather return a fallback for integers.

serhiy-storchaka avatar Oct 01 '24 12:10 serhiy-storchaka

This change is a backward incompatible change. You should document it in a NEWS entry.

Ok, I did. News entry was missed just as in 31a655411 (dropped similar check in PyNumber_Long).

I now think that we should rather return a fallback for integers.

But why?! Should we check on same ground that an int subtype has nb_add field set?

skirpichev avatar Oct 01 '24 12:10 skirpichev

Because PyFloat_AsDouble() never fails for instances of float subclasses. It does not use nb_float in that case.

serhiy-storchaka avatar Oct 01 '24 12:10 serhiy-storchaka

In same way we can recover in case when float subtype sets e.g. nb_negative to NULL. I think such subtypes are broken and it's better to fail quickly.

skirpichev avatar Oct 01 '24 12:10 skirpichev

now think that we should rather return a fallback for integers.

There is a difference, if nb_int is set to NULL, but nb_index is not broken (at least not NULL) - then we got an equivalent fallback with _PyLong_Copy, see: https://github.com/python/cpython/blob/31516c98dd7097047ba10da8dcf728c3d580f3d6/Objects/abstract.c#L1403-L1405 and https://github.com/python/cpython/blob/31516c98dd7097047ba10da8dcf728c3d580f3d6/Objects/abstract.c#L1446-L1448

I still think we shouldn't keep workarounds for broken subtypes. But if so, at least the nb_int workaround shouldn't be restored.

skirpichev avatar Oct 07 '24 06:10 skirpichev

Ok, I'm closing this.

skirpichev avatar Nov 02 '24 05:11 skirpichev