-
-
Notifications
You must be signed in to change notification settings - Fork 10.8k
ENH: Add inline C function to import and cache Python functions. #5940
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
Conversation
There are a lot of places this function should be used, and more in the offing. |
Why not do:
if there is an error? Use of this helper function would then typically look like:
That way you can handle errors more gracefully. |
I had that in there originally, but in most (all?) cases numpy is unusable when there is a failure. The intent is to load python implementations of multiarray functions, and later have explicit loads of ufuncs (n_ops currently set on loading the ufunc module) as implementation of the ndarray operators becomes set. I'd actually prefer to call abort on failure to avoid debug dependency, but assert is probably adequate. |
Note that cache will be NULL on failure as is. |
I'm open to argument on this point, however. |
I think on failure, if the You could also make it return an int, so that the standard use would be:
|
Yeah, I had that one too ;) I'm leaning towards your first solution. |
A new inline function 'npy_cache_pyfunc' is provided for the common operation of inporting and caching a Python function in static variables. The intended usage is as follows. int myfunc() { static PyObject *cache = NULL: npy_cache_pyfunc("the.module.name", "function", &cache); ... } Errors are not recoverable, so checked with assert for debugging.
c17bbb9
to
403d61d
Compare
OK, changed. Most of the current uses already check for NULL so that is an easy upgrade. |
ENH: Add inline C function to import and cache Python functions.
In it goes, thanks! |
If I use it i.e. for the VisibleDeprecationWarning, do you mind if I rename it to _pyobject (honestly don't care much)? ;) |
I'm thinking of renaming it |
A new inline function 'npy_cache_pyfunc' is provided for the common
operation of inporting and caching a Python function in static
variables. The intended usage is as follows.
int myfunc()
{
static PyObject *cache = NULL:
}
Errors are not recoverable, so checked with assert for debugging.