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

オープン
#285 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
30/100
issue の種類
バグ
明瞭さ
おおむね明確
活発さ
停滞
技術スタック
cpp, numpy, python
領域
backend

調査の方向性

報告されている BOOST_PYTHON_MODULE エントリポイントと np::initialize() の呼び出しから始め、numpy が存在しない状態でインポートを再現します。拡張機能のインポート中に、Boost.Python NumPy の初期化が ModuleNotFoundError または ImportError をどのように伝播させるかを調べます。Python で失敗を捕捉でき、インタープリターが終了しなければ完了です。

索引モデルが issue の本文から書いたものです。

説明

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.

主要言語
C++
スター
537
フォーク
223
平均マージ
11時間 22分
マージ済み PR(30日)
2

コントリビューションガイド

このリポジトリのコントリビューションガイドは索引されていません

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

boostorg/python のほかの issue

boostorg/python の issue をすべて見る

似ている issue

C++ の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。