@@ -376,8 +376,23 @@ array_nbytes_get(PyArrayObject *self, void *NPY_UNUSED(ignored))
376
376
* will be adjusted in that case as well.
377
377
*/
378
378
static int
379
- array_descr_set_lock_held (PyArrayObject * self , PyObject * arg )
379
+ array_descr_set (PyArrayObject * self , PyObject * arg )
380
380
{
381
+ // to be replaced with PyUnstable_Object_IsUniquelyReferenced https://github.com/python/cpython/pull/133144
382
+ int unique_reference = (Py_REFCNT (self ) == 1 );
383
+
384
+ if (!unique_reference ) {
385
+ // this will not emit deprecation warnings for all cases, but for most it will
386
+ /* DEPRECATED 2025-05-04, NumPy 2.3 */
387
+ int ret = PyErr_WarnEx (PyExc_DeprecationWarning ,
388
+ "Setting the dtype on a Numpy array has been deprecated in Numpy 2.3.\n"
389
+ "Instead of changing the dtype on an array x, create a new array with x.view(new_dtype)" ,
390
+ 1 );
391
+ if (ret ) {
392
+ return -1 ;
393
+ }
394
+ }
395
+
381
396
PyArray_Descr * newtype = NULL ;
382
397
383
398
if (arg == NULL ) {
@@ -522,28 +537,6 @@ array_descr_set_lock_held(PyArrayObject *self, PyObject *arg)
522
537
return -1 ;
523
538
}
524
539
525
- static int
526
- array_descr_set (PyArrayObject * self , PyObject * arg , void * NPY_UNUSED (ignored ))
527
- {
528
- // to be replaced with PyUnstable_Object_IsUniquelyReferenced https://github.com/python/cpython/pull/133144
529
- int unique_reference = (Py_REFCNT (self ) == 1 );
530
-
531
- if (!unique_reference ) {
532
- // this will not emit deprecation warnings for all cases we want, but it gets is in the right direction
533
- /* DEPRECATED 2025-05-04, NumPy 2.3 */
534
- int ret = PyErr_WarnEx (PyExc_DeprecationWarning ,
535
- "Setting the dtype on a Numpy array has been deprecated in Numpy 2.3.\n"
536
- "Instead of changing the dtype on an array x, create a new array with x.view(new_dtype)" ,
537
- 1 );
538
- //printf("array_descr_set: %p %p: %ld, bail\n", self, arg, Py_REFCNT(self));
539
- if (ret ) {
540
- return -1 ;
541
- }
542
- }
543
- //printf("array_descr_set: %p %p: %ld\n", self, arg, Py_REFCNT(self));
544
- return array_descr_set_lock_held (self , arg );
545
- }
546
-
547
540
static PyObject *
548
541
array_struct_get (PyArrayObject * self , void * NPY_UNUSED (ignored ))
549
542
{
0 commit comments