@@ -716,7 +716,7 @@ StructUnionType_init(PyObject *self, PyObject *args, PyObject *kwds, int isStruc
716716 /* copy base info */
717717 ret = PyCStgInfo_clone (info , baseinfo );
718718 if (ret >= 0 ) {
719- stginfo_set_dict_final ( info );
719+ assert ( stginfo_get_dict_final ( info ) == 0 );
720720 stginfo_set_dict_final (baseinfo );
721721 }
722722 STGINFO_UNLOCK2 ();
@@ -2152,18 +2152,7 @@ static PyObject *CreateSwappedType(ctypes_state *st, PyTypeObject *type,
21522152 if (!swapped_args )
21532153 return NULL ;
21542154
2155- if (st -> swapped_suffix == NULL ) {
2156- #ifdef WORDS_BIGENDIAN
2157- st -> swapped_suffix = PyUnicode_InternFromString ("_le" );
2158- #else
2159- st -> swapped_suffix = PyUnicode_InternFromString ("_be" );
2160- #endif
2161- }
2162- if (st -> swapped_suffix == NULL ) {
2163- Py_DECREF (swapped_args );
2164- return NULL ;
2165- }
2166-
2155+ assert (st -> swapped_suffix != NULL );
21672156 newname = PyUnicode_Concat (name , st -> swapped_suffix );
21682157 if (newname == NULL ) {
21692158 Py_DECREF (swapped_args );
@@ -3137,6 +3126,7 @@ PyCData_MallocBuffer(CDataObject *obj, StgInfo *info)
31373126 * access.
31383127 */
31393128 assert (Py_REFCNT (obj ) == 1 );
3129+ assert (stginfo_get_dict_final (info ) == 1 );
31403130
31413131 if ((size_t )info -> size <= sizeof (obj -> b_value )) {
31423132 /* No need to call malloc, can use the default buffer */
@@ -3186,7 +3176,13 @@ PyCData_FromBaseObj(ctypes_state *st,
31863176 if (cmem == NULL ) {
31873177 return NULL ;
31883178 }
3189- STGINFO_LOCK (info );
3179+
3180+ if (stginfo_get_dict_final (info ) != 1 ) {
3181+ STGINFO_LOCK (info );
3182+ stginfo_set_dict_final (info );
3183+ STGINFO_UNLOCK ();
3184+ }
3185+
31903186 assert (CDataObject_Check (st , cmem ));
31913187 cmem -> b_length = info -> length ;
31923188 cmem -> b_size = info -> size ;
@@ -3198,15 +3194,12 @@ PyCData_FromBaseObj(ctypes_state *st,
31983194 cmem -> b_index = index ;
31993195 } else { /* copy contents of adr */
32003196 if (-1 == PyCData_MallocBuffer (cmem , info )) {
3201- Py_CLEAR (cmem );
3202- goto exit ;
3197+ Py_DECREF (cmem );
3198+ return NULL ;
32033199 }
32043200 memcpy (cmem -> b_ptr , adr , info -> size );
32053201 cmem -> b_index = index ;
32063202 }
3207- exit :;
3208- stginfo_set_dict_final (info );
3209- STGINFO_UNLOCK ();
32103203 return (PyObject * )cmem ;
32113204}
32123205
@@ -5126,12 +5119,8 @@ PyCArrayType_from_ctype(ctypes_state *st, PyObject *itemtype, Py_ssize_t length)
51265119 char name [256 ];
51275120 PyObject * len ;
51285121
5129- if (st -> array_cache == NULL ) {
5130- st -> array_cache = PyDict_New ();
5131- if (st -> array_cache == NULL ) {
5132- return NULL ;
5133- }
5134- }
5122+ assert (st -> array_cache != NULL );
5123+
51355124 len = PyLong_FromSsize_t (length );
51365125 if (len == NULL )
51375126 return NULL ;
@@ -6112,6 +6101,20 @@ _ctypes_mod_exec(PyObject *mod)
61126101 return -1 ;
61136102 }
61146103
6104+ st -> array_cache = PyDict_New ();
6105+ if (st -> array_cache == NULL ) {
6106+ return -1 ;
6107+ }
6108+
6109+ #ifdef WORDS_BIGENDIAN
6110+ st -> swapped_suffix = PyUnicode_InternFromString ("_le" );
6111+ #else
6112+ st -> swapped_suffix = PyUnicode_InternFromString ("_be" );
6113+ #endif
6114+ if (st -> swapped_suffix == NULL ) {
6115+ return -1 ;
6116+ }
6117+
61156118 if (_ctypes_add_types (mod ) < 0 ) {
61166119 return -1 ;
61176120 }
0 commit comments