-
Notifications
You must be signed in to change notification settings - Fork 65
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
Cast libusb pollfd callbacks from null pointer to proper type #35
Conversation
Looks good to me. I would like just one tiny change:
About why it works, I suspect Otherwise, it comes from the actual callbacks, and here I wonder if there would not be another issue in the code using python-libusb1 here: interpreter shutdown is extremely hard to get right (especially, In any case this change, modulo the |
Yes, I have indeed discovered this recently, which I don't think is visibly mentioned in the docs. I consider this a set of python-libusb1 bugs, especially since I can't easily restructure my code to look like you suggest. |
Patch updated. |
I tried to document it in the docstring of
I know, I was very much annoyed when I was told unordered garbage collection is just how it works and not a bug, and that context managers were the only reliable solution (and their There may be an alternative solution in rewriting python-libusb1 in C so that it can control garbage collection one level below the interpreter. But I have no intention of doing so, as then it would likely not work with pypy. As an alternative to context management, note that you can also call
You need to attach an instance of c_void_p to the class, again because of interpreter shutdown issues: module globals are deleted (replaced by Side note: in ctypes, |
This avoids an exception (sometimes a segfault) during Python process exit with a signature like the following: Exception ignored in: <bound method USBPoller.__del__ of <usb1.USBPoller object at 0x7f7700e4eda0>> Traceback (most recent call last): File ".../site-packages/usb1/__init__.py", line 1081, in __del__ File ".../site-packages/usb1/__init__.py", line 2127, in wrapper File ".../site-packages/usb1/__init__.py", line 2387, in setPollFDNotifiers ctypes.ArgumentError: argument 2: <class 'TypeError'>: expected CFunctionType instance instead of _ctypes.PyCSimpleType
Right, I agree this would be undesirable. I'll try to fix these bugs when I hit them without major changes.
Thanks, I think this will work.
Oh wow, I see now why GC is such a pain in this case, Python's del methods aren't like finalizers at all. This explains many things. Fixed. |
Looks perfect, thanks for contributing ! |
I noticed only after the merge that |
Thanks! |
Incidentally, you were right; on Windows, unless I do this, my application reliably crashes with an access violation on every exit. |
This avoids an exception (sometimes a segfault) during Python process exit
with a signature like the following:
I'm not sure how this ever worked--a ctypes change, perhaps? In any case, the
POINTER(None)
needs to be called before use, and cast to the callback type.