-
Notifications
You must be signed in to change notification settings - Fork 205
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Long/Int confusion in Python 2.x #97
Comments
In the same vein, the codebase currently has a mix of the A good example is this: Lines 200 to 214 in 3239755
Assuming we use if (PyInt_Check(val)) {
buf[ii] = (__u8)PyInt_AS_LONG(val);
} else {
snprintf(wrmsg_text, sizeof (wrmsg_text) - 1, wrmsg_val, val);
PyErr_SetString(PyExc_TypeError, wrmsg_text);
return NULL;
} This would drop implicit support of the Since currently you can mix ints/longs in Python 2 and have the code behave in unexpected ways, failing with an exception when a Long is passed to any of the library methods or properties is probably a good idea. Although it may, conceivably, "break" existing (misbehaving) user code, at least it will be breaking it more explicitly. |
In think in #93 I've introduced a potential error or strangeness in Python 2.x by using
PyLong_FromLong
in lieu ofPyInt_FromLong
. Since we have a macro to smooth over this difference, I should probably use the latter:py-spidev/spidev_module.c
Line 58 in 3239755
Using the following code in Python 2.7:
results in:
IE: It's given a list of integers, and returns a list of longs.
This means that we get new long objects for every byte in the result, rather than references to the internal integer array. (Python contains an array of integer objects for all known integers between -5 and 256)
See:
Looking back at #34 suggests that things can potentially go awry the other way, too. The current check uses
PyLong_Check
whereas we have a define to smoothPyInt_Check
toPyLong_Check
. This means that under Python 2.x the library will happily accept LongsSince we can't accept and return longs in Python 2.x- and because #34 suggests passing in longs has unexpected results in Python 2.x- it should probably fail with a TypeError if any long type numbers are passed in.
The text was updated successfully, but these errors were encountered: