-
-
Notifications
You must be signed in to change notification settings - Fork 9.5k
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
MAINT: Fix typo in PyArray_RegisterDataType error #18303
Conversation
Thanks, to be clear, are you actually running into this error? |
The reason I ask is, that I do not really expect anyone hitting this. So if you do and it isn't completely obviously just an oversight I would like to know (even if you trivially work around or so). |
Yes, I am, while debugging lscsoft/lalsuite!1526. What is the correct way in C to declare a new PyArray_Descr to wrap a specific kind of Python object? |
Some people have used a trick by making structured descriptor instead. I am not sure how you managed to wrap a specific python object. As far as I could tell, you should never be able to delete such an array correctly, because you cannot implement reference counting for your custom dtype's items! I really want API to wrap a custom Python kind of python object as a dtype, but I am slightly shocked you managed to do it; and it wasn't super high on my agenda. I have to dig in and see what exactly you are doing and what is working correctly and what may not be... So there is the hack I have seen, which I hope will continue to work is something I created a test for here: #17320 |
@lpsinger I tried to understand what the code does to see if there is some clear different solution, or this is a problem I need to dig into. But I have to admit, the whole SWIG logic is making it hard for me to really follow things. The dtype doesn't really seem to do a whole lot? For example, your |
I don't know the answers to those questions. @kwwette would. Meanwhile, though, I'm trying the workaround that you suggested: // Numpy workaround for registering user-defined object types. See
// https://github.com/numpy/numpy/pull/18303,
// https://github.com/numpy/numpy/pull/17320
(*pdescr)->names = Py_BuildValue("(s)", "swigobj");
(*pdescr)->fields = Py_BuildValue("{s:(s,i)}", "swigobj", "O", 0); It's working so far on my own machine; now I'm waiting to see the results of our CI pipeline. |
OK, to be clear: I am happy to discuss and look into this more; it just may be useful to have a brief chat to jump into whats going on. I added the error thinking that nobody can (usefully) do this. I am still not sure whether you managed or not, but I never had the intention of breaking working code. The idea was to warn new users and remove one worry of strange corner cases that might be hard or impossible to keep supporting unmodified. |
@kwwette, @duncanmmacleod, would you care to take over from here and follow up with @seberg? |
@seberg Thanks for helping us with this. LALSuite is a large scientific library written in C, for which we generate Python wrappings using SWIG plus some custom code. LALSuite contains a number of data structures of the following form:
We want to be able to view the To do this we've been using NumPy arrays as a view of the This has worked well so far. We've not noticed any problems with memory leaks with e.g. reference counting. In the statement It seems so far that for Numpy 1.20 we just need to work out a more portable way of creating the NumPy array descriptor that we've done previously. We'd appreciate though whether you're aware of any other changes to NumPy that might mean we can no longer using the NumPy array views as described above. |
@kwwette OK, I honestly expect you can probably just delete those flags. The point here is that
or alternatively:
which for example is a use case that users who want to implement strings run into. To implement those correctly you do need Just be sure, if |
Oh, one thing. If you want |
@seberg Thanks for the pointers. We'll try dropping the flags you mentioned and see how that goes. I don't think we'd be able to translate the C structs into NumPy structured types, at least not easily. LALSuite is a library of thousands of functions, structs, etc. so the point of using SWIG is to automatically generate the Python interface from the library C headers, without having to manually maintain/sync separate C and Python interface descriptions. So we'd have to find some way of automating the generation of NumPy structured types. That may be possible but would require a lot more work, and might break existing code. There are also some array element types in LALSuite (i.e. |
OK, so the point of those flags (internally to NumPy) is dynamic memory (really we only use Python Objects, which are also a type of dynamic memory). But the problem is that the additional functionality to handle dynamic memory correctly is not accessible for user dtypes. The problem I see is this:
At that point If this is a larger headache for you, I can also undo the change. Right now, all the flag probably achieves is disabling a few things like: I would like to get to a point where user DTypes can contain references (i.e. have their own memory management if necessary), but it is on the backburner list, simply because it is something that can be achieved later on. |
@seberg I'm not sure I've ever tried to copy one of our NumPy array views, as in your Is there a way to disable |
I honestly do not think there is currently a way to do that. It might be possible to add one. The only way I would have thought of is keeping Let me know if I can help you out now or in the future. DTypes will be revamped hopefully by the 1.21 release, which should give you cleaner solutions in the future (if you are still using this approach), but I guess that would be a bit of a long haul. For now: We are planning on a 1.20.1 release probably this weekend already to fix some simple, but annoying issues. If it will help you, I don't need much prodding to be convinced to just remove this error message again! :) |
@seberg We now have a fix for our issue; see the diff here. Basically we didn't need the I don't remember where The dtypes are static in that they're stored in a lookup table against a static list of type information generated by SWIG. So when a user wants to view an array of elements Thanks again for your help! |
OK, glad it seems to work for you. I do think that |
No description provided.