@@ -1258,37 +1258,30 @@ PyCPointerType_SetProto(ctypes_state *st, PyObject *self, StgInfo *stginfo, PyOb
12581258 return -1 ;
12591259 }
12601260 Py_XSETREF (stginfo -> proto , Py_NewRef (proto ));
1261+
1262+ // Set the format string for the pointer type based on element type.
1263+ // If info->format is NULL, this is a pointer to an incomplete type.
1264+ // We create a generic format string 'pointer to bytes' in this case.
1265+ char * new_format = NULL ;
12611266 STGINFO_LOCK (info );
12621267 if (info -> pointer_type == NULL ) {
12631268 Py_XSETREF (info -> pointer_type , Py_NewRef (self ));
12641269 }
1265- STGINFO_UNLOCK ();
1266- return 0 ;
1267- }
1268-
1269- // Set the format string for a pointer type based on its element type.
1270- static int
1271- PyCPointerType_SetFormat (ctypes_state * st , StgInfo * stginfo , PyObject * proto )
1272- {
1273- StgInfo * iteminfo ;
1274- if (PyStgInfo_FromType (st , proto , & iteminfo ) < 0 ) {
1275- return -1 ;
1276- }
1277- assert (iteminfo ); // PyCPointerType_SetProto already verified this
1278-
1279- const char * current_format = iteminfo -> format ? iteminfo -> format : "B" ;
1280- char * new_format ;
1281- if (iteminfo -> shape != NULL ) {
1270+ const char * current_format = info -> format ? info -> format : "B" ;
1271+ if (info -> shape != NULL ) {
1272+ // pointer to an array: the shape needs to be prefixed
12821273 new_format = _ctypes_alloc_format_string_with_shape (
1283- iteminfo -> ndim , iteminfo -> shape , "&" , current_format );
1274+ info -> ndim , info -> shape , "&" , current_format );
12841275 } else {
12851276 new_format = _ctypes_alloc_format_string ("&" , current_format );
12861277 }
1278+ PyMem_Free (stginfo -> format );
1279+ stginfo -> format = new_format ;
1280+ STGINFO_UNLOCK ();
1281+
12871282 if (new_format == NULL ) {
12881283 return -1 ;
12891284 }
1290- PyMem_Free (stginfo -> format );
1291- stginfo -> format = new_format ;
12921285 return 0 ;
12931286}
12941287
@@ -1344,10 +1337,6 @@ PyCPointerType_init(PyObject *self, PyObject *args, PyObject *kwds)
13441337 Py_DECREF (proto );
13451338 return -1 ;
13461339 }
1347- if (PyCPointerType_SetFormat (st , stginfo , proto ) < 0 ) {
1348- Py_DECREF (proto );
1349- return -1 ;
1350- }
13511340 Py_DECREF (proto );
13521341 }
13531342
@@ -1382,9 +1371,6 @@ PyCPointerType_set_type_impl(PyTypeObject *self, PyTypeObject *cls,
13821371 if (PyCPointerType_SetProto (st , (PyObject * )self , info , type ) < 0 ) {
13831372 return NULL ;
13841373 }
1385- if (PyCPointerType_SetFormat (st , info , type ) < 0 ) {
1386- return NULL ;
1387- }
13881374 if (PyObject_SetAttr ((PyObject * )self , & _Py_ID (_type_ ), type ) < 0 ) {
13891375 return NULL ;
13901376 }
0 commit comments