Hacktoberfest 2026: the issues maintainers tagged for October, open and beginner-friendly. Browse Hacktoberfest issues

boost::python::numpy causing python crash when numpy not installed

Open
#285 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
30/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
cpp, numpy, python
Domain
backend

Research direction

Start with the reported BOOST_PYTHON_MODULE entry point and the np::initialize() call, then reproduce the import with numpy absent. Inspect how Boost.Python NumPy initialization propagates ModuleNotFoundError or ImportError during extension import. Done means the failure is catchable by Python instead of terminating the interpreter.

Written by the indexing model from the issue text.

Description

The following code causes python to exit (crash?) upon import when numpy is not installed. It looks like a ModuleNotFoundError does get thrown but it's not catchable and python simply exits. It's hard to tell if this is exit() being called or an actual crash. I would expect a catchable ModuleNotFoundError or ImportError to be thrown and python not to exit.

#define BOOST_PYTHON_STATIC_LIB
#define BOOST_NUMPY_STATIC_LIB
#include <boost/python.hpp>

#include <boost/python.hpp>
#include <boost/python/suite/indexing/vector_indexing_suite.hpp>
#include <boost/python/numpy.hpp>

namespace py = boost::python;
namespace np = boost::python::numpy;

template<class T>
np::ndarray to_numpy(const std::vector<T> &vec)
{
	Py_intptr_t shape[1] = { static_cast<Py_intptr_t>(vec.size()) };
	np::ndarray result = np::zeros(1, shape, np::dtype::get_builtin<T>());
	std::copy(vec.begin(), vec.end(), reinterpret_cast<T*>(result.get_data()));
	return result;
}

BOOST_PYTHON_MODULE(HelloBoostPython)
{
	np::initialize();

	py::class_<std::vector<double>>("DoubleVector")
		.def(py::init<size_t>())
		.def(py::init<size_t, const double&>())
		.def(py::vector_indexing_suite<std::vector<double>>())
		.def("to_numpy", to_numpy<double>)
		;
}

prompt:

(base) C:\Development\HelloBoostPython\Debug>python
Python 3.7.4 (default, Aug  9 2019, 18:22:51) [MSC v.1915 32 bit (Intel)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import HelloBoostPython
ModuleNotFoundError: No module named 'numpy'
ImportError: numpy.core._multiarray_umath failed to import

(base) C:\Development\HelloBoostPython\Debug>

If it matters, this is with boost 1.70 using visual studio 2017 to build.

Dominant language
C++
Stars
537
Forks
223
Avg merge
11h 22m
Merged PRs (30d)
2

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from boostorg/python

All issues in boostorg/python

Similar issues

More C++ issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.