8000 Add custom AttributeError messages · numpy/numpy@0d4887d · GitHub
[go: up one dir, main page]

Skip to content

Commit 0d4887d

Browse files
committed
Add custom AttributeError messages
1 parent 1b24963 commit 0d4887d

File tree

4 files changed

+76
-2
lines changed

4 files changed

+76
-2
lines changed

doc/source/reference/arrays.ndarray.rst

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,6 @@ be performed.
412412
ndarray.argmax
413413
ndarray.min
414414
ndarray.argmin
415-
ndarray.ptp
416415
ndarray.clip
417416
ndarray.conj
418417
ndarray.round

numpy/core/src/multiarray/getset.c

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -938,6 +938,33 @@ array_matrix_transpose_get(PyArrayObject *self, void *NPY_UNUSED(ignored))
938938
return PyArray_MatrixTranspose(self);
939939
}
940940

941+
static PyObject *
942+
array_ptp(PyArrayObject *self, void *NPY_UNUSED(ignored))
943+
{
944+
PyErr_SetString(PyExc_AttributeError,
945+
"`ptp` has been removed from ndarray class. "
946+
"Use np.ptp(arr, ...) instead");
947+
return NULL;
948+
}
949+
950+
static PyObject *
951+
array_newbyteorder(PyArrayObject *self, PyObject *args)
952+
{
953+
PyErr_SetString(PyExc_AttributeError,
954+
"`newbyteorder` has been removed from ndarray class. "
955+
"Use `arr.view(arr.dtype.newbyteorder(order))` instead");
956+
return NULL;
957+
}
958+
959+
static PyObject *
960+
array_itemset(PyArrayObject *self, PyObject *args)
961+
{
962+
PyErr_SetString(PyExc_AttributeError,
963+
"`itemset` has been removed from ndarray class. "
964+
"Use `arr[index] = value` instead");
965+
return NULL;
966+
}
967+
941968
NPY_NO_EXPORT PyGetSetDef array_getsetlist[] = {
942969
{"ndim",
943970
(getter)array_ndim_get,
@@ -1003,6 +1030,18 @@ NPY_NO_EXPORT PyGetSetDef array_getsetlist[] = {
10031030
(getter)array_matrix_transpose_get,
10041031
NULL,
10051032
NULL, NULL},
1033+
{"ptp",
1034+
(getter)array_ptp,
1035+
NULL,
1036+
NULL, NULL},
1037+
{"newbyteorder",
1038+
(getter)array_newbyteorder,
1039+
NULL,
1040+
NULL, NULL},
1041+
{"itemset",
1042+
(getter)array_itemset,
1043+
NULL,
1044+
NULL, NULL},
10061045
{"__array_interface__",
10071046
(getter)array_interface_get,
10081047
NULL,

numpy/core/src/multiarray/scalartypes.c.src

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1594,6 +1594,33 @@ gentype_transpose_get(PyObject *self, void *NPY_UNUSED(ignored))
15941594
return self;
15951595
}
15961596

1597+
static PyObject *
1598+
gentype_newbyteorder(PyObject *NPY_UNUSED(self), void *NPY_UNUSED(ignored))
1599+
{
1600+
PyErr_SetString(PyExc_AttributeError,
1601+
"`newbyteorder` has been removed from scalar types. "
1602+
"Use `sc.view(sc.dtype.newbyteorder(order))` instead.");
1603+
return NULL;
1604+
}
1605+
1606+
static PyObject *
1607+
gentype_itemset(PyObject *NPY_UNUSED(self), void *NPY_UNUSED(ignored))
1608+
{
1609+
PyErr_SetString(PyExc_AttributeError,
1610+
"`itemset` has been removed from scalar types "
1611+
"because scalars are immutable.");
1612+
return NULL;
1613+
}
1614+
1615+
static PyObject *
1616+
gentype_ptp(PyObject *NPY_UNUSED(self), void *NPY_UNUSED(ignored))
1617+
{
1618+
PyErr_SetString(PyExc_AttributeError,
1619+
"`ptp` has been removed from scalar types. "
1620+
"For a scalar, the range of values always equals 0.");
1621+
return NULL;
1622+
}
1623+
15971624

15981625
static PyGetSetDef gentype_getsets[] = {
15991626
{"ndim",
@@ -1638,6 +1665,15 @@ static PyGetSetDef gentype_getsets[] = {
16381665
{"T",
16391666
(getter)gentype_transpose_get,
16401667
(setter)0, NULL, NULL},
1668+
{"newbyteorder",
1669+
(getter)gentype_newbyteorder,
1670+
(setter)0, NULL, NULL},
1671+
{"itemset",
1672+
(getter)gentype_itemset,
1673+
(setter)0, NULL, NULL},
1674+
{"ptp",
1675+
(getter)gentype_ptp,
1676+
(setter)0, NULL, NULL},
16411677
{"__array_interface__",
16421678
(getter)gentype_interface_get,
16431679
NULL,

numpy/matrixlib/tests/test_defmatrix.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ def test_instance_methods(self):
283283
'argmin', 'choose', 'dump', 'dumps', 'fill', 'getfield',
284284
'getA', 'getA1', 'item', 'nonzero', 'put', 'putmask', 'resize',
285285
'searchsorted', 'setflags', 'setfield', 'sort',
286-
'partition', 'argpartition',
286+
'partition', 'argpartition', 'newbyteorder',
287287
'take', 'tofile', 'tolist', 'tostring', 'tobytes', 'all', 'any',
288288
'sum', 'argmax', 'argmin', 'min', 'max', 'mean', 'var', 'ptp',
289289
'prod', 'std', 'ctypes', 'itemset',

0 commit comments

Comments
 (0)
0