I ran such code in C++11:
PyObject* aa = PyLong_FromLong(7L);
Then I checked the value of aa
, it's not NULL
, but aa->ob_type
was NULL
.
However, when I ran:
PyObject* aa = PyLong_FromLong(257L);
aa->ob_type
was not NULL
anymore. I read the document of PyLong_FromLong, and found this:
PyObject* PyLong_FromLong(long v)
Return value: New reference.
Return a new PyLongObject object from v, or NULL on failure.
The current implementation keeps an array of integer objects for all integers between -5 and 256, when you create an int in that range you actually just get back a reference to the existing object. So it should be possible to change the value of 1. I suspect the behaviour of Python in this case is undefined. :-)
Seems that when building PyLongObject
between -5 and 256 will meet this problem. But I don't understand the reason.
What's more, this problem doesn't appear in Python2. That's incredible!