[Numpy-discussion] Determine if numpy is installed from an extension

Peter Notebaert peno@telenet...
Wed Feb 3 09:57:35 CST 2010


Ah, that is maybe the idea:

if (_import_array() < 0) {
   /* Clear the error state since we are handling the error. */
   PyErr_Clear();
   /* ... set up for the sans-numpy case. */
}
else {
   /* ... set up for the with-numpy case. */
}

I did not call PyErr_Clear() when _import_array() < 0 and the error is
probably still hanging and then given later.

I will try this this evening.

Thank you for the hints.

Peter

On Wed, Feb 3, 2010 at 4:22 PM, Robert Kern <robert.kern@gmail.com> wrote:

> On Wed, Feb 3, 2010 at 03:41, David Cournapeau <cournape@gmail.com> wrote:
> > On Wed, Feb 3, 2010 at 5:38 PM, Peter Notebaert <peno@telenet.be> wrote:
> >> >From an extension? How to import numpy from there and then test if that
> >> succeeded and that without any annoying message if possible...
> >
> > One obvious solution would be to simply call PyImport_Import, something
> like:
> >
> > #include <Python.h>
> >
> > PyMODINIT_FUNC initfoo(void)
> > {
> >        PyObject *m, *mod;
> >
> >        m = Py_InitModule("foo", NULL);
> >        if (m == NULL) {
> >                return;
> >        }
> >
> >        mod = PyImport_ImportModule("numpy");
> >        if (mod == NULL) {
> >                return;
> >        }
> >        Py_DECREF(mod);
>
> Or rather, to recover from the failed import as the OP wants to do:
>
> mod = PyImport_ImportModule("numpy");
> if (mod == NULL) {
>     /* Clear the error state since we are handling the error. */
>    PyErr_Clear();
>    /* ... set up for the sans-numpy case. */
> }
> else {
>    Py_DECREF(mod);
>    import_array();
>    /* ... set up for the with-numpy case. */
> }
>
> --
> Robert Kern
>
> "I have come to believe that the whole world is an enigma, a harmless
> enigma that is made terrible by our own mad attempt to interpret it as
> though it had an underlying truth."
>  -- Umberto Eco
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/numpy-discussion/attachments/20100203/ae4b738e/attachment-0001.html 


More information about the NumPy-Discussion mailing list