@@ -3236,31 +3236,20 @@ nonstructured_to_structured_get_loop(
32363236 return 0 ;
32373237}
32383238
3239+ // these are filled in during module initialization
3240+ // we do not include these in the global data struct
3241+ // to avoid the need to #include array_method.h for
3242+ // all users of the global data struct
3243+ static PyArrayMethodObject * VoidToGenericMethod = NULL ;
3244+ static PyArrayMethodObject * GenericToVoidMethod = NULL ;
3245+ static PyArrayMethodObject * ObjectToGenericMethod = NULL ;
3246+ static PyArrayMethodObject * GenericToObjectMethod = NULL ;
32393247
32403248static PyObject *
32413249PyArray_GetGenericToVoidCastingImpl (void )
32423250{
3243- static PyArrayMethodObject * method = NULL ;
3244-
3245- if (method != NULL ) {
3246- Py_INCREF (method );
3247- return (PyObject * )method ;
3248- }
3249-
3250- method = PyObject_New (PyArrayMethodObject , & PyArrayMethod_Type );
3251- if (method == NULL ) {
3252- return PyErr_NoMemory ();
3253- }
3254-
3255- method -> name = "any_to_void_cast" ;
3256- method -> flags = NPY_METH_SUPPORTS_UNALIGNED | NPY_METH_REQUIRES_PYAPI ;
3257- method -> casting = -1 ;
3258- method -> resolve_descriptors = & nonstructured_to_structured_resolve_descriptors ;
3259- method -> get_strided_loop = & nonstructured_to_structured_get_loop ;
3260- method -> nin = 1 ;
3261- method -> nout = 1 ;
3262-
3263- return (PyObject * )method ;
3251+ Py_INCREF (GenericToVoidMethod );
3252+ return (PyObject * )GenericToVoidMethod ;
32643253}
32653254
32663255
@@ -3397,27 +3386,8 @@ structured_to_nonstructured_get_loop(
33973386static PyObject *
33983387PyArray_GetVoidToGenericCastingImpl (void )
33993388{
3400- static PyArrayMethodObject * method = NULL ;
3401-
3402- if (method != NULL ) {
3403- Py_INCREF (method );
3404- return (PyObject * )method ;
3405- }
3406-
3407- method = PyObject_New (PyArrayMethodObject , & PyArrayMethod_Type );
3408- if (method == NULL ) {
3409- return PyErr_NoMemory ();
3410- }
3411-
3412- method -> name = "void_to_any_cast" ;
3413- method -> flags = NPY_METH_SUPPORTS_UNALIGNED | NPY_METH_REQUIRES_PYAPI ;
3414- method -> casting = -1 ;
3415- method -> resolve_descriptors = & structured_to_nonstructured_resolve_descriptors ;
3416- method -> get_strided_loop = & structured_to_nonstructured_get_loop ;
3417- method -> nin = 1 ;
3418- method -> nout = 1 ;
3419-
3420- return (PyObject * )method ;
3389+ Py_INCREF (VoidToGenericMethod );
3390+ return (PyObject * )VoidToGenericMethod ;
34213391}
34223392
34233393
@@ -3781,31 +3751,11 @@ object_to_any_resolve_descriptors(
37813751static PyObject *
37823752PyArray_GetObjectToGenericCastingImpl (void )
37833753{
3784- static PyArrayMethodObject * method = NULL ;
3785-
3786- if (method != NULL ) {
3787- Py_INCREF (method );
3788- return (PyObject * )method ;
3789- }
3790-
3791- method = PyObject_New (PyArrayMethodObject , & PyArrayMethod_Type );
3792- if (method == NULL ) {
3793- return PyErr_NoMemory ();
3794- }
3795-
3796- method -> nin = 1 ;
3797- method -> nout = 1 ;
3798- method -> name = "object_to_any_cast" ;
3799- method -> flags = NPY_METH_SUPPORTS_UNALIGNED | NPY_METH_REQUIRES_PYAPI ;
3800- method -> casting = NPY_UNSAFE_CASTING ;
3801- method -> resolve_descriptors = & object_to_any_resolve_descriptors ;
3802- method -> get_strided_loop = & object_to_any_get_loop ;
3803-
3804- return (PyObject * )method ;
3754+ Py_INCREF (ObjectToGenericMethod );
3755+ return (PyObject * )ObjectToGenericMethod ;
38053756}
38063757
38073758
3808-
38093759/* Any object is simple (could even use the default) */
38103760static NPY_CASTING
38113761any_to_object_resolve_descriptors (
@@ -3838,27 +3788,8 @@ any_to_object_resolve_descriptors(
38383788static PyObject *
38393789PyArray_GetGenericToObjectCastingImpl (void )
38403790{
3841- static PyArrayMethodObject * method = NULL ;
3842-
3843- if (method != NULL ) {
3844- Py_INCREF (method );
3845- return (PyObject * )method ;
3846- }
3847-
3848- method = PyObject_New (PyArrayMethodObject , & PyArrayMethod_Type );
3849- if (method == NULL ) {
3850- return PyErr_NoMemory ();
3851- }
3852-
3853- method -> nin = 1 ;
3854- method -> nout = 1 ;
3855- method -> name = "any_to_object_cast" ;
3856- method -> flags = NPY_METH_SUPPORTS_UNALIGNED | NPY_METH_REQUIRES_PYAPI ;
3857- method -> casting = NPY_SAFE_CASTING ;
3858- method -> resolve_descriptors = & any_to_object_resolve_descriptors ;
3859- method -> get_strided_loop = & any_to_object_get_loop ;
3860-
3861- return (PyObject * )method ;
3791+ Py_INCREF (GenericToObjectMethod );
3792+ return (PyObject * )GenericToObjectMethod ;
38623793}
38633794
38643795
@@ -3910,6 +3841,68 @@ PyArray_InitializeObjectToObjectCast(void)
39103841 return res ;
39113842}
39123843
3844+ static int
3845+ initialize_void_and_object_globals (void ) {
3846+ VoidToGenericMethod = PyObject_New (PyArrayMethodObject , & PyArrayMethod_Type );
3847+
3848+ if (VoidToGenericMethod == NULL ) {
3849+ PyErr_NoMemory ();
3850+ return -1 ;
3851+ }
3852+
3853+ VoidToGenericMethod -> name = "void_to_any_cast" ;
3854+ VoidToGenericMethod -> flags = NPY_METH_SUPPORTS_UNALIGNED | NPY_METH_REQUIRES_PYAPI ;
3855+ VoidToGenericMethod -> casting = -1 ;
3856+ VoidToGenericMethod -> resolve_descriptors = & structured_to_nonstructured_resolve_descriptors ;
3857+ VoidToGenericMethod -> get_strided_loop = & structured_to_nonstructured_get_loop ;
3858+ VoidToGenericMethod -> nin = 1 ;
3859+ VoidToGenericMethod -> nout = 1 ;
3860+
3861+ GenericToVoidMethod = PyObject_New (PyArrayMethodObject , & PyArrayMethod_Type );
3862+ if (GenericToVoidMethod == NULL ) {
3863+ PyErr_NoMemory ();
3864+ return -1 ;
3865+ }
3866+
3867+ GenericToVoidMethod -> name = "any_to_void_cast" ;
3868+ GenericToVoidMethod -> flags = NPY_METH_SUPPORTS_UNALIGNED | NPY_METH_REQUIRES_PYAPI ;
3869+ GenericToVoidMethod -> casting = -1 ;
3870+ GenericToVoidMethod -> resolve_descriptors = & nonstructured_to_structured_resolve_descriptors ;
3871+ GenericToVoidMethod -> get_strided_loop = & nonstructured_to_structured_get_loop ;
3872+ GenericToVoidMethod -> nin = 1 ;
3873+ GenericToVoidMethod -> nout = 1 ;
3874+
3875+ ObjectToGenericMethod = PyObject_New (PyArrayMethodObject , & PyArrayMethod_Type );
3876+ if (ObjectToGenericMethod == NULL ) {
3877+ PyErr_NoMemory ();
3878+ return -1 ;
3879+ }
3880+
3881+ ObjectToGenericMethod -> nin = 1 ;
3882+ ObjectToGenericMethod -> nout = 1 ;
3883+ ObjectToGenericMethod -> name = "object_to_any_cast" ;
3884+ ObjectToGenericMethod -> flags = NPY_METH_SUPPORTS_UNALIGNED | NPY_METH_REQUIRES_PYAPI ;
3885+ ObjectToGenericMethod -> casting = NPY_UNSAFE_CASTING ;
3886+ ObjectToGenericMethod -> resolve_descriptors = & object_to_any_resolve_descriptors ;
3887+ ObjectToGenericMethod -> get_strided_loop = & object_to_any_get_loop ;
3888+
3889+ GenericToObjectMethod = PyObject_New (PyArrayMethodObject , & PyArrayMethod_Type );
3890+ if (GenericToObjectMethod == NULL ) {
3891+ PyErr_NoMemory ();
3892+ return -1 ;
3893+ }
3894+
3895+ GenericToObjectMethod -> nin = 1 ;
3896+ GenericToObjectMethod -> nout = 1 ;
3897+ GenericToObjectMethod -> name = "any_to_object_cast" ;
3898+ GenericToObjectMethod -> flags = NPY_METH_SUPPORTS_UNALIGNED | NPY_METH_REQUIRES_PYAPI ;
3899+ GenericToObjectMethod -> casting = NPY_SAFE_CASTING ;
3900+ GenericToObjectMethod -> resolve_descriptors = & any_to_object_resolve_descriptors ;
3901+ GenericToObjectMethod -> get_strided_loop = & any_to_object_get_loop ;
3902+
3903+ return 0 ;
3904+ }
3905+
39133906
39143907NPY_NO_EXPORT int
39153908PyArray_InitializeCasts ()
@@ -3930,5 +3923,10 @@ PyArray_InitializeCasts()
39303923 if (PyArray_InitializeDatetimeCasts () < 0 ) {
39313924 return -1 ;
39323925 }
3926+
3927+ if (initialize_void_and_object_globals () < 0 ) {
3928+ return -1 ;
3929+ }
3930+
39333931 return 0 ;
39343932}
0 commit comments