@@ -21,6 +21,9 @@ new_stringdtype_instance(PyObject *na_object, int coerce)
21
21
npy_string_allocator * allocator = NULL ;
22
22
PyThread_type_lock * allocator_lock = NULL ;
23
23
24
+ char * default_string_buf = NULL ;
25
+ char * na_name_buf = NULL ;
26
+
24
27
allocator = NpyString_new_allocator (PyMem_RawMalloc , PyMem_RawFree ,
25
28
PyMem_RawRealloc );
26
29
if (allocator == NULL ) {
@@ -35,12 +38,8 @@ new_stringdtype_instance(PyObject *na_object, int coerce)
35
38
goto fail ;
36
39
}
37
40
38
- char packed_na_name [SIZEOF_NPY_PACKED_STATIC_STRING ] = {0 };
39
- NpyString_pack (allocator , (npy_packed_static_string * )& packed_na_name , "" ,
40
- 0 );
41
- char packed_default_string [SIZEOF_NPY_PACKED_STATIC_STRING ] = {0 };
42
- NpyString_pack (allocator ,
43
- (npy_packed_static_string * )& packed_default_string , "" , 0 );
41
+ npy_static_string default_string = {0 , NULL };
42
+ npy_static_string na_name = {0 , NULL };
44
43
45
44
Py_XINCREF (na_object );
46
45
((StringDTypeObject * )new )-> na_object = na_object ;
@@ -53,15 +52,9 @@ new_stringdtype_instance(PyObject *na_object, int coerce)
53
52
has_string_na = 1 ;
54
53
Py_ssize_t size = 0 ;
55
54
const char * buf = PyUnicode_AsUTF8AndSize (na_object , & size );
56
- if (NpyString_newsize (
57
- buf , (size_t )size ,
58
- (npy_packed_static_string * )& packed_default_string ,
59
- allocator ) < 0 ) {
60
- PyErr_SetString (PyExc_MemoryError ,
61
- "Failed to allocate string while creating "
62
- "StringDType instance." );
63
- goto fail ;
64
- }
55
+ default_string .buf = PyMem_RawMalloc (size );
56
+ memcpy ((char * )default_string .buf , buf , size );
57
+ default_string .size = size ;
65
58
}
66
59
else {
67
60
// treat as nan-like if != comparison returns a object whose truth
@@ -92,55 +85,20 @@ new_stringdtype_instance(PyObject *na_object, int coerce)
92
85
Py_DECREF (na_pystr );
93
86
goto fail ;
94
87
}
95
- if (NpyString_newsize (utf8_ptr , (size_t )size ,
96
- (npy_packed_static_string * )& packed_na_name ,
97
- allocator ) < 0 ) {
98
- PyErr_SetString (PyExc_MemoryError ,
99
- "Failed to allocate string while creating "
100
- "StringDType instance." );
101
- Py_DECREF (na_pystr );
102
- goto fail ;
103
- }
88
+ na_name .buf = PyMem_RawMalloc (size );
89
+ memcpy ((char * )na_name .buf , utf8_ptr , size );
90
+ na_name .size = size ;
104
91
Py_DECREF (na_pystr );
105
92
}
106
93
107
94
StringDTypeObject * snew = (StringDTypeObject * )new ;
108
95
109
96
snew -> has_nan_na = has_nan_na ;
110
97
snew -> has_string_na = has_string_na ;
111
- memcpy (snew -> packed_default_string , packed_default_string ,
112
- SIZEOF_NPY_PACKED_STATIC_STRING );
113
- memcpy (snew -> packed_na_name , packed_na_name ,
114
- SIZEOF_NPY_PACKED_STATIC_STRING );
115
98
snew -> coerce = coerce ;
116
99
snew -> allocator_lock = allocator_lock ;
117
100
snew -> allocator = allocator ;
118
101
snew -> array_owned = 0 ;
119
-
120
- npy_static_string default_string = {0 , NULL };
121
- if (NpyString_load (
122
- allocator ,
123
- (npy_packed_static_string * )& snew -> packed_default_string ,
124
- & default_string ) == -1 ) {
125
- PyErr_SetString (PyExc_MemoryError ,
126
- "Failed to load packed string while "
127
- "creating StringDType instance." );
128
- Py_DECREF (snew );
129
- return NULL ;
130
- }
131
-
132
- npy_static_string na_name = {0 , NULL };
133
- if (NpyString_load (allocator ,
134
- (npy_packed_static_string * )& snew -> packed_na_name ,
135
- & na_name ) == -1 ) {
136
- PyErr_SetString (PyExc_MemoryError ,
137
- "Failed to load packed string while "
138
- "creating StringDType instance." );
139
-
140
- Py_DECREF (snew );
141
- return NULL ;
142
- }
143
-
144
102
snew -> na_name = na_name ;
145
103
snew -> default_string = default_string ;
146
104
@@ -162,10 +120,13 @@ new_stringdtype_instance(PyObject *na_object, int coerce)
162
120
fail :
163
121
// this only makes sense if the allocator isn't attached to new yet
164
122
Py_DECREF (new );
123
+ if (default_string_buf != NULL ) {
124
+ PyMem_RawFree (default_string_buf );
125
+ }
126
+ if (na_name_buf != NULL ) {
127
+ PyMem_RawFree (na_name_buf );
128
+ }
165
129
if (allocator != NULL ) {
166
- NpyString_free ((npy_packed_static_string * )& packed_na_name , allocator );
167
- NpyString_free ((npy_packed_static_string * )& packed_default_string ,
168
- allocator );
169
130
NpyString_free_allocator (allocator );
170
131
}
171
132
if (allocator_lock != NULL ) {
@@ -713,14 +674,11 @@ stringdtype_dealloc(StringDTypeObject *self)
713
674
if (self -> allocator != NULL ) {
714
675
// can we assume the destructor for an instance will only get called
715
676
// inside of one C thread?
716
- NpyString_free (
717
- (npy_packed_static_string * )& self -> packed_default_string ,
718
- self -> allocator );
719
- NpyString_free ((npy_packed_static_string * )& self -> packed_na_name ,
720
- self -> allocator );
721
677
NpyString_free_allocator (self -> allocator );
722
678
PyThread_free_lock (self -> allocator_lock );
723
679
}
680
+ PyMem_RawFree ((char * )self -> na_name .buf );
681
+ PyMem_RawFree ((char * )self -> default_string .buf );
724
682
PyArrayDescr_Type .tp_dealloc ((PyObject * )self );
725
683
}
726
684
0 commit comments