@@ -94,6 +94,8 @@ static PyObject* NewTypeMethod_call(NewTypeMethodObject* self,
9494 self -> func_get , Py_None , self -> wrapped_cls , NULL );
9595 } else {
9696 DEBUG_PRINT ("`self->obj` is not NULL\n" );
97+ DEBUG_PRINT ("`self->wrapped_cls`: %s\n" ,
98+ PyUnicode_AsUTF8 (PyObject_Repr (self -> wrapped_cls )));
9799 func = PyObject_CallFunctionObjArgs (
98100 self -> func_get , self -> obj , self -> wrapped_cls , NULL );
99101 }
@@ -145,7 +147,9 @@ static PyObject* NewTypeMethod_call(NewTypeMethodObject* self,
145147 { // now we try to build an instance of the subtype
146148 DEBUG_PRINT ("`result` is an instance of `self->wrapped_cls`\n" );
147149 PyObject * init_args , * init_kwargs ;
148- PyObject * new_inst , * args_combined ;
150+ PyObject * new_inst ;
151+ PyObject * args_combined = NULL ;
152+ Py_ssize_t args_len = 0 ;
149153
150154 if (self -> obj == NULL ) {
151155 PyObject * first_elem ;
@@ -158,73 +162,136 @@ static PyObject* NewTypeMethod_call(NewTypeMethodObject* self,
158162 first_elem = PyTuple_GetItem (args , 0 );
159163 Py_XINCREF (
160164 first_elem ); // Increment reference count of the first element
161-
165+ DEBUG_PRINT ("`first_elem`: %s\n" ,
166+ PyUnicode_AsUTF8 (PyObject_Repr (first_elem )));
162167 } else { // `args` is empty here, then we are done actually
168+ DEBUG_PRINT ("`args` is empty\n" );
163169 goto done ;
164170 };
165171 if (PyObject_IsInstance (first_elem , (PyObject * )self -> cls )) {
166172 init_args = PyObject_GetAttrString (first_elem , NEWTYPE_INIT_ARGS_STR );
167173 init_kwargs =
168174 PyObject_GetAttrString (first_elem , NEWTYPE_INIT_KWARGS_STR );
175+ DEBUG_PRINT ("`init_args`: %s\n" ,
176+ PyUnicode_AsUTF8 (PyObject_Repr (init_args )));
177+ DEBUG_PRINT ("`init_kwargs`: %s\n" ,
178+ PyUnicode_AsUTF8 (PyObject_Repr (init_kwargs )));
169179 } else { // first element is not the subtype, so we are done also
180+ DEBUG_PRINT ("`first_elem` is not the subtype\n" );
170181 goto done ;
171182 }
172183 Py_XDECREF (first_elem );
173184 } else { // `self->obj` is not NULL
174185
186+ DEBUG_PRINT ("`self->obj` is not NULL\n" );
175187 init_args = PyObject_GetAttrString (self -> obj , NEWTYPE_INIT_ARGS_STR );
176188 init_kwargs = PyObject_GetAttrString (self -> obj , NEWTYPE_INIT_KWARGS_STR );
189+ DEBUG_PRINT ("`init_args`: %s\n" ,
190+ PyUnicode_AsUTF8 (PyObject_Repr (init_args )));
191+ DEBUG_PRINT ("`init_kwargs`: %s\n" ,
192+ PyUnicode_AsUTF8 (PyObject_Repr (init_kwargs )));
177193 }
178194
179- Py_ssize_t args_len = PyTuple_Size (init_args );
180- Py_ssize_t combined_args_len = 1 + args_len ;
181- args_combined = PyTuple_New (combined_args_len );
182- if (args_combined == NULL ) {
183- Py_XDECREF (init_args );
184- Py_XDECREF (init_kwargs );
185- Py_DECREF (result );
186- return NULL ; // Use return NULL instead of Py_RETURN_NONE
187- }
188-
189- // Set the first item of the new tuple to `result`
190- PyTuple_SET_ITEM (args_combined ,
191- 0 ,
192- result ); // `result` is now owned by `args_combined`
193-
194- // Copy items from `init_args` to `args_combined`
195- for (Py_ssize_t i = 0 ; i < args_len ; i ++ ) {
196- PyObject * item = PyTuple_GetItem (init_args , i ); // Borrowed reference
197- if (item == NULL ) {
198- Py_DECREF (args_combined );
195+ if (init_args != NULL ) {
196+ DEBUG_PRINT ("`init_args` is not NULL\n" );
197+ args_len = PyTuple_Size (init_args );
198+ DEBUG_PRINT ("`args_len`: %zd\n" , args_len );
199+ Py_ssize_t combined_args_len = 1 + args_len ;
200+ DEBUG_PRINT ("`combined_args_len`: %zd\n" , combined_args_len );
201+ args_combined = PyTuple_New (combined_args_len );
202+ DEBUG_PRINT ("`args_combined`: %s\n" ,
203+ PyUnicode_AsUTF8 (PyObject_Repr (args_combined )));
204+ if (args_combined == NULL ) {
199205 Py_XDECREF (init_args );
200206 Py_XDECREF (init_kwargs );
201- return NULL ;
207+ Py_DECREF (result );
208+ DEBUG_PRINT ("`args_combined` is NULL\n" );
209+ return NULL ; // Use return NULL instead of Py_RETURN_NONE
202210 }
203- Py_INCREF ( item ); // Increase reference count
211+ // Set the first item of the new tuple to `result`
204212 PyTuple_SET_ITEM (args_combined ,
205- i + 1 ,
206- item ); // `item` is now owned by `args_combined`
213+ 0 ,
214+ result ); // `result` is now owned by `args_combined`
215+
216+ // Copy items from `init_args` to `args_combined`
217+ for (Py_ssize_t i = 0 ; i < args_len ; i ++ ) {
218+ PyObject * item = PyTuple_GetItem (init_args , i ); // Borrowed reference
219+ if (item == NULL ) {
220+ DEBUG_PRINT ("`item` is NULL\n" );
221+ Py_DECREF (args_combined );
222+ Py_XDECREF (init_args );
223+ Py_XDECREF (init_kwargs );
224+ return NULL ;
225+ }
226+ DEBUG_PRINT ("`item`: %s\n" , PyUnicode_AsUTF8 (PyObject_Repr (item )));
227+ Py_INCREF (item ); // Increase reference count
228+ PyTuple_SET_ITEM (args_combined ,
229+ i + 1 ,
230+ item ); // `item` is now owned by `args_combined`
231+ }
232+ DEBUG_PRINT ("`args_combined`: %s\n" ,
233+ PyUnicode_AsUTF8 (PyObject_Repr (args_combined )));
207234 }
208- DEBUG_PRINT ("`args_combined`: %s\n" ,
209- PyUnicode_AsUTF8 (PyObject_Repr (args_combined )));
235+
236+ if (init_args == NULL || init_kwargs == NULL ) {
237+ DEBUG_PRINT ("`init_args` or `init_kwargs` is NULL\n" );
238+ };
210239
211240 if (init_kwargs != NULL ) {
212241 DEBUG_PRINT ("`init_kwargs`: %s\n" ,
213242 PyUnicode_AsUTF8 (PyObject_Repr (init_kwargs )));
214243 };
215244
216- // Call the function or constructor
245+ // If `args_combined` is NULL, create a new tuple with one item
246+ // and set `result` as the first item of the tuple
247+ if (init_args == NULL ) {
248+ DEBUG_PRINT ("`init_args` is NULL\n" );
249+
250+ if (PyObject_SetAttrString (
251+ self -> obj , NEWTYPE_INIT_ARGS_STR , PyTuple_New (0 ))
252+ < 0 )
253+ {
254+ result = NULL ;
255+ goto done ;
256+ }
257+ if (PyObject_SetAttrString (
258+ self -> obj , NEWTYPE_INIT_KWARGS_STR , PyDict_New ())
259+ < 0 )
260+ {
261+ result = NULL ;
262+ goto done ;
263+ }
264+
265+ args_combined = PyTuple_New (1 ); // Allocate tuple with one element
266+ Py_INCREF (result );
267+ PyTuple_SET_ITEM (args_combined , 0 , result );
268+ DEBUG_PRINT ("`args_combined`: %s\n" ,
269+ PyUnicode_AsUTF8 (PyObject_Repr (args_combined )));
270+ new_inst =
271+ PyObject_Call ((PyObject * )self -> cls , args_combined , init_kwargs );
272+ if (new_inst == NULL ) {
273+ DEBUG_PRINT ("`new_inst` is NULL\n" );
274+ Py_DECREF (result );
275+ Py_DECREF (self -> obj );
276+ Py_DECREF (args_combined );
277+ return NULL ;
278+ }
279+ Py_DECREF (result );
280+ Py_DECREF (self -> obj );
281+ Py_DECREF (args_combined );
282+ DEBUG_PRINT ("`new_inst`: %s\n" ,
283+ PyUnicode_AsUTF8 (PyObject_Repr (new_inst )));
284+ return new_inst ;
285+ }
286+
217287 new_inst = PyObject_Call ((PyObject * )self -> cls , args_combined , init_kwargs );
218288
219289 // Clean up
220- Py_DECREF (args_combined ); // Decrement reference count of `args_combined`
290+ Py_XDECREF (args_combined ); // Decrement reference count of `args_combined`
221291 Py_XDECREF (init_args );
222292 Py_XDECREF (init_kwargs );
223293
224- // Ensure proper error propagation
225- if (new_inst == NULL ) {
226- return NULL ;
227- }
294+ DEBUG_PRINT ("`new_inst`: %s\n" , PyUnicode_AsUTF8 (PyObject_Repr (new_inst )));
228295
229296 // Only proceed if we have all required objects and dictionaries
230297 if (self -> obj != NULL && result != NULL && new_inst != NULL
@@ -427,6 +494,7 @@ static PyObject* NewTypeMethod_call(NewTypeMethodObject* self,
427494
428495done :
429496 Py_XINCREF (result );
497+ DEBUG_PRINT ("DONE! `result`: %s\n" , PyUnicode_AsUTF8 (PyObject_Repr (result )));
430498 return result ;
431499}
432500
0 commit comments