@@ -48,74 +48,51 @@ enum {
4848 CLOSEPOLY = 0x4f
4949};
5050
51- inline int prepare_and_add_type (PyTypeObject *type, PyObject *module )
52- {
53- if (PyType_Ready (type)) {
54- return -1 ;
55- }
56- char const * ptr = strrchr (type->tp_name , ' .' );
57- if (!ptr) {
58- PyErr_SetString (PyExc_ValueError, " tp_name should be a qualified name" );
59- return -1 ;
60- }
61- if (PyModule_AddObject (module , ptr + 1 , (PyObject *)type)) {
62- return -1 ;
63- }
64- return 0 ;
65- }
66-
6751#ifdef __cplusplus // not for macosx.m
6852// Check that array has shape (N, d1) or (N, d1, d2). We cast d1, d2 to longs
6953// so that we don't need to access the NPY_INTP_FMT macro here.
7054#include < pybind11/pybind11.h>
7155#include < pybind11/numpy.h>
7256
7357namespace py = pybind11;
58+ using namespace pybind11 ::literals;
7459
7560template <typename T>
76- inline bool check_trailing_shape (T array, char const * name, long d1)
61+ inline void check_trailing_shape (T array, char const * name, long d1)
7762{
7863 if (array.ndim () != 2 ) {
79- PyErr_Format (PyExc_ValueError,
80- " Expected 2-dimensional array, got %ld" ,
81- array.ndim ());
82- return false ;
64+ throw py::value_error (
65+ " Expected 2-dimensional array, got %d" _s.format (array.ndim ()));
8366 }
8467 if (array.size () == 0 ) {
8568 // Sometimes things come through as atleast_2d, etc., but they're empty, so
8669 // don't bother enforcing the trailing shape.
87- return true ;
70+ return ;
8871 }
8972 if (array.shape (1 ) != d1) {
90- PyErr_Format (PyExc_ValueError,
91- " %s must have shape (N, %ld), got (%ld, %ld)" ,
92- name, d1, array.shape (0 ), array.shape (1 ));
93- return false ;
73+ throw py::value_error (
74+ " %s must have shape (N, %d), got (%d, %d)" _s.format (
75+ name, d1, array.shape (0 ), array.shape (1 )));
9476 }
95- return true ;
9677}
9778
9879template <typename T>
99- inline bool check_trailing_shape (T array, char const * name, long d1, long d2)
80+ inline void check_trailing_shape (T array, char const * name, long d1, long d2)
10081{
10182 if (array.ndim () != 3 ) {
102- PyErr_Format (PyExc_ValueError,
103- " Expected 3-dimensional array, got %ld" ,
104- array.ndim ());
105- return false ;
83+ throw py::value_error (
84+ " Expected 3-dimensional array, got %d" _s.format (array.ndim ()));
10685 }
10786 if (array.size () == 0 ) {
10887 // Sometimes things come through as atleast_3d, etc., but they're empty, so
10988 // don't bother enforcing the trailing shape.
110- return true ;
89+ return ;
11190 }
11291 if (array.shape (1 ) != d1 || array.shape (2 ) != d2) {
113- PyErr_Format (PyExc_ValueError,
114- " %s must have shape (N, %ld, %ld), got (%ld, %ld, %ld)" ,
115- name, d1, d2, array.shape (0 ), array.shape (1 ), array.shape (2 ));
116- return false ;
92+ throw py::value_error (
93+ " %s must have shape (N, %d, %d), got (%d, %d, %d)" _s.format (
94+ name, d1, d2, array.shape (0 ), array.shape (1 ), array.shape (2 )));
11795 }
118- return true ;
11996}
12097
12198/* In most cases, code should use safe_first_shape(obj) instead of obj.shape(0), since
0 commit comments