@@ -111,8 +111,8 @@ set_legacy_print_mode(PyObject *NPY_UNUSED(self), PyObject *args)
111111
112112NPY_NO_EXPORT PyTypeObject * _PyArray_Type_p = NULL ;
113113NPY_NO_EXPORT HPyContext * numpy_global_ctx = NULL ;
114- NPY_NO_EXPORT HPy HPyArray_Type ;
115- NPY_NO_EXPORT HPy HPyArrayDescr_Type ;
114+ NPY_NO_EXPORT HPyGlobal HPyArray_Type ;
115+ NPY_NO_EXPORT HPyGlobal HPyArrayDescr_Type ;
116116
117117/* Only here for API compatibility */
118118NPY_NO_EXPORT PyTypeObject PyBigArray_Type ;
@@ -1693,6 +1693,7 @@ _hpy_array_fromobject_generic(
16931693 HPyContext * ctx , HPy op , HPy type , _PyArray_CopyMode copy , NPY_ORDER order ,
16941694 npy_bool subok , int ndmin )
16951695{
1696+ HPy array_type = HPy_NULL ;
16961697 HPy ret = HPy_NULL ;
16971698 PyArrayObject * oparr ;
16981699 HPy oldtype = HPy_NULL ;
@@ -1704,14 +1705,15 @@ _hpy_array_fromobject_generic(
17041705 HPyErr_SetString (ctx , ctx -> h_ValueError ,
17051706 "ndmin bigger than allowable number of dimensions "
17061707 "NPY_MAXDIMS (=%d)" /*, HPY TODO: NPY_MAXDIMS*/ );
1707- return HPy_NULL ;
1708+ goto fail ;
17081709 }
17091710 /* fast exit if simple call */
17101711 // HPY TODO (API): original code uses "check exact"
17111712 // HPy version has to use two calls HPy_Type and HPy_Is
17121713 // It would be faster to check subok first and then exact or subclass check
1713- if (HPy_Is (ctx , HPy_Type (ctx , op ), HPyArray_Type ) ||
1714- (subok && HPy_TypeCheck (ctx , op , HPyArray_Type ))) {
1714+ array_type = HPyGlobal_Load (ctx , HPyArray_Type );
1715+ if (HPy_Is (ctx , HPy_Type (ctx , op ), array_type ) ||
1716+ (subok && HPy_TypeCheck (ctx , op , array_type ))) {
17151717 oparr = PyArrayObject_AsStruct (ctx , op );
17161718 if (HPy_IsNull (type )) {
17171719 if (copy != NPY_COPY_ALWAYS && STRIDING_OK (oparr , order )) {
@@ -1722,7 +1724,7 @@ _hpy_array_fromobject_generic(
17221724 if (copy == NPY_COPY_NEVER ) {
17231725 HPyErr_SetString (ctx , ctx -> h_ValueError ,
17241726 "Unable to avoid copy while creating a new array." );
1725- return HPy_NULL ;
1727+ goto fail ;
17261728 }
17271729 ret = HPyArray_NewCopy (ctx , op , order );
17281730 goto finish ;
@@ -1740,9 +1742,9 @@ _hpy_array_fromobject_generic(
17401742 }
17411743 else {
17421744 if (copy == NPY_COPY_NEVER ) {
1743- PyErr_SetString ( PyExc_ValueError ,
1745+ HPyErr_SetString ( ctx , ctx -> h_ValueError ,
17441746 "Unable to avoid copy while creating a new array." );
1745- return HPy_NULL ;
1747+ goto fail ;
17461748 }
17471749 capi_warn ("np.array: PyArray_NewCopy" );
17481750 PyObject * py_ret = (PyObject * ) PyArray_NewCopy (oparr , order );
@@ -1768,7 +1770,7 @@ _hpy_array_fromobject_generic(
17681770 }
17691771 else if ((order == NPY_FORTRANORDER )
17701772 /* order == NPY_ANYORDER && */
1771- || (HPy_TypeCheck (ctx , op , HPyArray_Type ) &&
1773+ || (HPy_TypeCheck (ctx , op , array_type ) &&
17721774 PyArray_ISFORTRAN (oparr ))) {
17731775 flags |= NPY_ARRAY_F_CONTIGUOUS ;
17741776 }
@@ -1783,6 +1785,8 @@ _hpy_array_fromobject_generic(
17831785 0 , 0 , flags , HPy_NULL );
17841786
17851787finish :
1788+ HPy_Close (ctx , array_type );
1789+
17861790 if (HPy_IsNull (ret )) {
17871791 return ret ;
17881792 }
@@ -1798,6 +1802,10 @@ _hpy_array_fromobject_generic(
17981802 // * steals a reference to ret
17991803 // */
18001804 // return _prepend_ones(ret, nd, ndmin, order);
1805+
1806+ fail :
1807+ HPy_Close (ctx , array_type );
1808+ return HPy_NULL ;
18011809}
18021810
18031811#undef STRIDING_OK
@@ -4921,7 +4929,12 @@ static HPyModuleDef moduledef = {
49214929 .doc = NULL ,
49224930 .size = -1 ,
49234931 .legacy_methods = array_module_methods ,
4924- .defines = array_module_hpy_methods
4932+ .defines = array_module_hpy_methods ,
4933+ .globals = {
4934+ & HPyArrayDescr_Type ,
4935+ & HPyArray_Type ,
4936+ NULL
4937+ }
49254938};
49264939
49274940/* Initialization function for the module */
@@ -5000,7 +5013,8 @@ static HPy init__multiarray_umath_impl(HPyContext *ctx) {
50005013 // HPY TODO: storing the types to globals to support legacy code, and HPy code w/o module state
50015014 _PyArrayDescr_Type_p = (PyTypeObject * ) HPy_AsPyObject (ctx , h_PyArrayDescr_Type );
50025015 PyArrayDTypeMeta_Type = (PyTypeObject * ) HPy_AsPyObject (ctx , h_PyArrayDTypeMeta_Type );
5003- HPyArrayDescr_Type = HPy_Dup (ctx , h_PyArrayDescr_Type );
5016+
5017+ HPyGlobal_Store (ctx , & HPyArrayDescr_Type , h_PyArrayDescr_Type );
50045018
50055019 HPy_Close (ctx , h_PyArrayDTypeMeta_Type );
50065020 HPy_Close (ctx , h_PyArrayDescr_Type );
@@ -5018,7 +5032,7 @@ static HPy init__multiarray_umath_impl(HPyContext *ctx) {
50185032 goto err ;
50195033 }
50205034 _PyArray_Type_p = (PyTypeObject * )HPy_AsPyObject (ctx , h_array_type );
5021- HPyArray_Type = HPy_Dup (ctx , h_array_type );
5035+ HPyGlobal_Store (ctx , & HPyArray_Type , h_array_type );
50225036 PyArray_Type .tp_weaklistoffset = offsetof(PyArrayObject_fields , weakreflist );
50235037 HPy_Close (ctx , h_array_type );
50245038
0 commit comments