Skip to content

BUG: Fix PyType_FastSubclass() check for numpy.int_ and Py3k inheritance - #3526

Merged
charris merged 3 commits into
numpy:masterfrom
zyv:fix_int_inheritance
Aug 11, 2013
Merged

charris merged 3 commits into
numpy:masterfrom
zyv:fix_int_inheritance

Conversation

@zyv

@zyv zyv commented Jul 16, 2013

Copy link
Copy Markdown
Contributor

Quote from the documentation at http://docs.scipy.org/doc/numpy/reference/arrays.scalars.html :

Five of the scalar types are essentially equivalent to fundamental Python types and therefore inherit from them as well as from the generic array scalar type <...>

However, presently, the code deviates from this document in two ways:

  1. For numpy.int_ type, the correct tp_flags are not set, which means that if one uses specialized fast-path Python C-API macro PyInt_Check on its instances to check whether the object is a subclass of a Python builtin, the result will be different (False) as compared to PyObject_TypeCheck (True).

This results in a different behavior of seemingly the same logic run through the Python interpreter using its MRO facilities and hand-written or generated modules, using optimized C-API calls directly.

  1. If one is using Py3k, then the code is expanded only to contain SINGLE_INHERIT macros, which means that numpy.int_ is not a subclass of int under Py3k at all.

My patch fixes these two issues by (a) introducing new macro DUAL_INHERIT_INT, which expands to DUAL_INHERIT(XXX, Long, XXX) under Py3k and DUAL_INHERIT(XXX, Int, XXX) otherwise and (b) setting tp_flags correctly from within this macro.

In order to test my changes, you will need the following minimal Cython program that generates code using optimized C-API calls:

def inspect(x):
    print("My MRO: {0}".format(type.mro(type(x))))
    if isinstance(x, int):
        print("I'm an normal integer number")
    elif isinstance(x, long):
        print("I'm an < Py3k long integer number")
    elif isinstance(x, float):
        print("I'm a floating point number")
    else:
        print("I don't know who I am")

Test output on the x86_64 machine:

Python 2.7, without patch
In [1]: import bt

In [2]: import numpy as np

In [3]: type.mro(type(np.int_(0)))
Out[3]: [numpy.int64, numpy.signedinteger, numpy.integer, numpy.number, numpy.generic, int, object]

In [4]: type.mro(type(np.int32(0)))
Out[4]: [numpy.int32, numpy.signedinteger, numpy.integer, numpy.number, numpy.generic, object]

In [5]: type.mro(type(np.int64(0)))
Out[5]: [numpy.int64, numpy.signedinteger, numpy.integer, numpy.number, numpy.generic, int, object]

In [6]: bt.inspect(np.int_(0))
My MRO: [<type 'numpy.int64'>, <type 'numpy.signedinteger'>, <type 'numpy.integer'>, <type 'numpy.number'>, <type 'numpy.generic'>, <type 'int'>, <type 'object'>]
I don't know who I am <--- WRONG!

In [7]: bt.inspect(np.int32(0))
My MRO: [<type 'numpy.int32'>, <type 'numpy.signedinteger'>, <type 'numpy.integer'>, <type 'numpy.number'>, <type 'numpy.generic'>, <type 'object'>]
I don't know who I am

In [8]: bt.inspect(np.int64(0))
My MRO: [<type 'numpy.int64'>, <type 'numpy.signedinteger'>, <type 'numpy.integer'>, <type 'numpy.number'>, <type 'numpy.generic'>, <type 'int'>, <type 'object'>]
I don't know who I am <--- WRONG!
Python 2.7, with patch
In [1]: import bt

In [2]: import numpy as np

In [3]: type.mro(type(np.int_(0)))
Out[3]: [numpy.int64, numpy.signedinteger, numpy.integer, numpy.number, numpy.generic, int, object]

In [4]: type.mro(type(np.int32(0)))
Out[4]: [numpy.int32, numpy.signedinteger, numpy.integer, numpy.number, numpy.generic, object]

In [5]: type.mro(type(np.int64(0)))
Out[5]: [numpy.int64, numpy.signedinteger, numpy.integer, numpy.number, numpy.generic, int, object]

In [6]: bt.inspect(np.int_(0))
My MRO: [<type 'numpy.int64'>, <type 'numpy.signedinteger'>, <type 'numpy.integer'>, <type 'numpy.number'>, <type 'numpy.generic'>, <type 'int'>, <type 'object'>]
I'm an normal integer number
^^^^ FIXED!
In [7]: bt.inspect(np.int32(0))
My MRO: [<type 'numpy.int32'>, <type 'numpy.signedinteger'>, <type 'numpy.integer'>, <type 'numpy.number'>, <type 'numpy.generic'>, <type 'object'>]
I don't know who I am

In [8]: bt.inspect(np.int64(0))
My MRO: [<type 'numpy.int64'>, <type 'numpy.signedinteger'>, <type 'numpy.integer'>, <type 'numpy.number'>, <type 'numpy.generic'>, <type 'int'>, <type 'object'>]
I'm an normal integer number
^^^^ FIXED!

Thank you for considering my bugfix for inclusion in NumPy,

--Yury.

@njsmith

njsmith commented Jul 16, 2013

Copy link
Copy Markdown
Member

The tp_flags change sounds correct, but the py3 issue is a doc bug --
np.int_ is not intended to inherit from int on py3, because on py3 int
is not longer a fixed-width integer type.

On Tue, Jul 16, 2013 at 2:40 PM, Yury V. Zaytsev
notifications@github.com wrote:

Quote from the documentation at
http://docs.scipy.org/doc/numpy/reference/arrays.scalars.html :

Five of the scalar types are essentially equivalent to fundamental Python
types and therefore inherit from them as well as from the generic array
scalar type <...>

However, presently, the code deviates from this document in two ways:

  1. For numpy.int_ type, the correct tp_flags are not set, which means that
    if one uses specialized fast-path Python C-API macro PyInt_Check on its
    instances to check whether the object is a subclass of a Python builtin, the
    result will be different (False) as compared to PyObject_TypeCheck (True).

This results in a different behavior of seemingly the same logic run through
the Python interpreter using its MRO facilities and hand-written or
generated modules, using optimized C-API calls directly.

  1. If one is using Py3k, then the code is expanded only to contain
    SINGLE_INHERIT macros, which means that numpy.int_ is not a subclass of int
    under Py3k at all.

My patch fixes these two issues by (a) introducing new macro
DUAL_INHERIT_INT, which expands to DUAL_INHERIT(XXX, Long, XXX) under Py3k
and DUAL_INHERIT(XXX, Int, XXX) otherwise and (b) setting tp_flags correctly
from within this macro.

In order to test my changes, you will need the following minimal Cython
program that generates code using optimized C-API calls:

def inspect(x):
print("My MRO: {0}".format(type.mro(type(x))))
if isinstance(x, int):
print("I'm an normal integer number")
elif isinstance(x, long):
print("I'm an < Py3k long integer number")
elif isinstance(x, float):
print("I'm a floating point number")
else:
print("I don't know who I am")

Test output on the x86_64 machine:

Python 2.7, without patch

In [1]: import bt

In [2]: import numpy as np

In [3]: type.mro(type(np.int_(0)))
Out[3]: [numpy.int64, numpy.signedinteger, numpy.integer, numpy.number,
numpy.generic, int, object]

In [4]: type.mro(type(np.int32(0)))
Out[4]: [numpy.int32, numpy.signedinteger, numpy.integer, numpy.number,
numpy.generic, object]

In [5]: type.mro(type(np.int64(0)))
Out[5]: [numpy.int64, numpy.signedinteger, numpy.integer, numpy.number,
numpy.generic, int, object]

In [6]: bt.inspect(np.int_(0))
My MRO: [<type 'numpy.int64'>, <type 'numpy.signedinteger'>, <type
'numpy.integer'>, <type 'numpy.number'>, <type 'numpy.generic'>, <type
'int'>, <type 'object'>]
I don't know who I am <--- WRONG!

In [7]: bt.inspect(np.int32(0))
My MRO: [<type 'numpy.int32'>, <type 'numpy.signedinteger'>, <type
'numpy.integer'>, <type 'numpy.number'>, <type 'numpy.generic'>, <type
'object'>]
I don't know who I am

In [8]: bt.inspect(np.int64(0))
My MRO: [<type 'numpy.int64'>, <type 'numpy.signedinteger'>, <type
'numpy.integer'>, <type 'numpy.number'>, <type 'numpy.generic'>, <type
'int'>, <type 'object'>]
I don't know who I am <--- WRONG!

Python 3.2, without patch

In [1]: import bt

In [2]: import numpy as np

In [3]: type.mro(type(np.int_(0)))
Out[3]: [numpy.int64, numpy.signedinteger, numpy.integer, numpy.number,
numpy.generic, builtins.object]
^^^^ WRONG!
In [4]: type.mro(type(np.int32(0)))
Out[4]: [numpy.int32, numpy.signedinteger, numpy.integer, numpy.number,
numpy.generic, builtins.object]

In [5]: type.mro(type(np.int64(0)))
Out[5]: [numpy.int64, numpy.signedinteger, numpy.integer, numpy.number,
numpy.generic, builtins.object]
^^^^ WRONG!
In [6]: bt.inspect(np.int_(0))
My MRO: [<class 'numpy.int64'>, <class 'numpy.signedinteger'>, <class
'numpy.integer'>, <class 'numpy.number'>, <class 'numpy.generic'>, <class
'object'>]
I don't know who I am
^^^^ WRONG!
In [7]: bt.inspect(np.int32(0))
My MRO: [<class 'numpy.int32'>, <class 'numpy.signedinteger'>, <class
'numpy.integer'>, <class 'numpy.number'>, <class 'numpy.generic'>, <class
'object'>]
I don't know who I am

In [8]: bt.inspect(np.int64(0))
My MRO: [<class 'numpy.int64'>, <class 'numpy.signedinteger'>, <class
'numpy.integer'>, <class 'numpy.number'>, <class 'numpy.generic'>, <class
'object'>]
I don't know who I am
^^^^ WRONG!

Python 2.7, with patch

In [1]: import bt

In [2]: import numpy as np

In [3]: type.mro(type(np.int_(0)))
Out[3]: [numpy.int64, numpy.signedinteger, numpy.integer, numpy.number,
numpy.generic, int, object]

In [4]: type.mro(type(np.int32(0)))
Out[4]: [numpy.int32, numpy.signedinteger, numpy.integer, numpy.number,
numpy.generic, object]

In [5]: type.mro(type(np.int64(0)))
Out[5]: [numpy.int64, numpy.signedinteger, numpy.integer, numpy.number,
numpy.generic, int, object]

In [6]: bt.inspect(np.int_(0))
My MRO: [<type 'numpy.int64'>, <type 'numpy.signedinteger'>, <type
'numpy.integer'>, <type 'numpy.number'>, <type 'numpy.generic'>, <type
'int'>, <type 'object'>]
I'm an normal integer number
^^^^ FIXED!
In [7]: bt.inspect(np.int32(0))
My MRO: [<type 'numpy.int32'>, <type 'numpy.signedinteger'>, <type
'numpy.integer'>, <type 'numpy.number'>, <type 'numpy.generic'>, <type
'object'>]
I don't know who I am

In [8]: bt.inspect(np.int64(0))
My MRO: [<type 'numpy.int64'>, <type 'numpy.signedinteger'>, <type
'numpy.integer'>, <type 'numpy.number'>, <type 'numpy.generic'>, <type
'int'>, <type 'object'>]
I'm an normal integer number
^^^^ FIXED!

Python 3.2, with patch

In [1]: import bt

In [2]: import numpy as np

In [3]: type.mro(type(np.int_(0)))
Out[3]: [numpy.int64, numpy.signedinteger, numpy.integer, numpy.number,
numpy.generic, builtins.int, builtins.object]
^^^^ FIXED!
In [4]: type.mro(type(np.int32(0)))
Out[4]: [numpy.int32, numpy.signedinteger, numpy.integer, numpy.number,
numpy.generic, builtins.object]

In [5]: type.mro(type(np.int64(0)))
Out[5]: [numpy.int64, numpy.signedinteger, numpy.integer, numpy.number,
numpy.generic, builtins.int, builtins.object]
^^^^ FIXED!
In [6]: bt.inspect(np.int_(0))
My MRO: [<class 'numpy.int64'>, <class 'numpy.signedinteger'>, <class
'numpy.integer'>, <class 'numpy.number'>, <class 'numpy.generic'>, <class
'int'>, <class 'object'>]
I'm an normal integer number
^^^^ FIXED!
In [7]: bt.inspect(np.int32(0))
My MRO: [<class 'numpy.int32'>, <class 'numpy.signedinteger'>, <class
'numpy.integer'>, <class 'numpy.number'>, <class 'numpy.generic'>, <class
'object'>]
I don't know who I am

In [8]: bt.inspect(np.int64(0))
My MRO: [<class 'numpy.int64'>, <class 'numpy.signedinteger'>, <class
'numpy.integer'>, <class 'numpy.number'>, <class 'numpy.generic'>, <class
'int'>, <class 'object'>]
I'm an normal integer number
^^^^ FIXED!

Thank you for considering my bugfix for inclusion in NumPy,

--Yury.


You can merge this Pull Request by running

git pull https://github.com/zyv/numpy fix_int_inheritance

Or view, comment on, or merge it at:

#3526

Commit Summary

BUG: Fix PyType_FastSubclass() check for numpy.int_ and Py3k inheritance

File Changes

M numpy/core/src/multiarray/multiarraymodule.c (34)

Patch Links:

https://github.com/numpy/numpy/pull/3526.patch
https://github.com/numpy/numpy/pull/3526.diff

@zyv

zyv commented Jul 16, 2013

Copy link
Copy Markdown
Contributor Author

Hi @njsmith,

Thank you very much for your review! I will then drop the second part (Py3k support) and add a doc patch that contains your comment. This is great, because my changes trigger a testsuite segfault on Py3k which I'm not sure of how to approach :-/

Do you have any ideas on how to properly test that or it would be fine the way it is? I guess I could add a test which checks whether isinstance(np.int_, int) == True under Python 2, but this is kind of pointless, because it already works, and (a) I'm not sure of how to check for other types, and, (b) specifically, my C-API change...

--Yury.

@zyv

zyv commented Jul 16, 2013

Copy link
Copy Markdown
Contributor Author

Hi @njsmith,

I've updated my patch and Travis seems to be happy now. Python 3 behavior is unchanged, Python 2 is still as illustrated in the first post (i.e. now fast-path checks should work).

Thanks!

@zyv

zyv commented Jul 17, 2013

Copy link
Copy Markdown
Contributor Author

After a productive discussion with @seberg on the IRC, I have managed to come up with a test, which seems to work. Let me know if there is anything else that I can do.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of leaving the wrong information, and then putting in an addendum correcting it, can you just fix the text?

be_bold svg

Maybe change "Five of the scalar types..." to "Some of the scalar types...", and then in the table say

:class:`IntType` (Python 2 only)

?

@njsmith

njsmith commented Jul 17, 2013

Copy link
Copy Markdown
Member

Otherwise looks good to me.

@zyv

zyv commented Jul 17, 2013

Copy link
Copy Markdown
Contributor Author

Hi @njsmith,

Thank you for your valuable comments. I have changed the docstring to a comment in the test, and implemented your suggestions for the documentation patch. I have left the addendum, however, because I think that it's useful to have the reason explained under the table. Nevertheless, if you maintain that it should be removed altogether, I will do so.

Thanks again!

@zyv

zyv commented Aug 2, 2013

Copy link
Copy Markdown
Contributor Author

Ping!

@charris

charris commented Aug 2, 2013

Copy link
Copy Markdown
Member

@njsmith Any remaining problems with this?

@njsmith

njsmith commented Aug 3, 2013

Copy link
Copy Markdown
Member

Can't really check while traveling, but I think its probably good. If you
like it then go for it.
On 2 Aug 2013 21:47, "Charles Harris" notifications@github.com wrote:

@njsmith https://github.com/njsmith Any remaining problems with this?

—
Reply to this email directly or view it on GitHubhttps://github.com//pull/3526#issuecomment-22030496
.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

First line in multiline comment should be blank

/*
 * start here
 */

zyv added 3 commits August 10, 2013 21:28
Signed-off-by: Yury V. Zaytsev <yury@shurup.com>
Signed-off-by: Yury V. Zaytsev <yury@shurup.com>
Signed-off-by: Yury V. Zaytsev <yury@shurup.com>
@zyv

zyv commented Aug 10, 2013

Copy link
Copy Markdown
Contributor Author

Hi Charles,

Thanks, I've fixed it and rebased to latest master...

Z.

@charris

charris commented Aug 11, 2013

Copy link
Copy Markdown
Member

Let's give it a shot. Thanks.

charris added a commit that referenced this pull request Aug 11, 2013
BUG: Fix PyType_FastSubclass() check for numpy.int_ and Py3k inheritance
@charris
charris merged commit 3e34afc into numpy:master Aug 11, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants