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.k...@gmail.com> wrote: > On Wed, Feb 3, 2010 at 03:41, David Cournapeau <courn...@gmail.com> wrote: > > On Wed, Feb 3, 2010 at 5:38 PM, Peter Notebaert <p...@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 >
_______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion