@@ -233,6 +233,7 @@ public static IntPtr tp_str(IntPtr ob)
233
233
}
234
234
try
235
235
{
236
+ Console . WriteLine ( "classbase tp_str" ) ;
236
237
//As per python doc:
237
238
//The return value must be a string object. If a class defines __repr__() but not __str__(),
238
239
//then __repr__() is also used when an “informal” string representation of instances of that
@@ -245,25 +246,32 @@ public static IntPtr tp_str(IntPtr ob)
245
246
var instType = co . inst . GetType ( ) ;
246
247
foreach ( var method in instType . GetMethods ( ) )
247
248
{
248
-
249
+ Console . WriteLine ( "classbase tp_str in loop" ) ;
249
250
//TODO this could probably be done more cleanly with Linq
250
251
if ( ! method . IsPublic ) continue ; //skip private/protected methods
251
252
if ( method . Name != "ToString" ) continue ; //only look for ToString
252
253
if ( method . DeclaringType == typeof ( object ) ) continue ; //ignore default from object
253
254
if ( method . GetParameters ( ) . Length != 0 ) continue ; //ignore Formatter overload of ToString
254
255
256
+ Console . WriteLine ( "classbase tp_str in loop end" ) ;
257
+
255
258
//match! something other than object provides a parameter-less overload of ToString
256
259
return Runtime . PyString_FromString ( co . inst . ToString ( ) ) ;
257
260
}
258
261
262
+ Console . WriteLine ( "classbase tp_str after loop" ) ;
263
+
259
264
//If the object defines __repr__, call it.
260
265
System . Reflection . MethodInfo reprMethodInfo = instType . GetMethod ( "__repr__" ) ;
261
266
if ( reprMethodInfo != null && reprMethodInfo . IsPublic )
262
267
{
268
+ Console . WriteLine ( "classbase tp_str has __repr__" ) ;
263
269
var reprString = reprMethodInfo . Invoke ( co . inst , null ) as string ;
264
270
return Runtime . PyString_FromString ( reprString ) ;
265
271
}
266
272
273
+ Console . WriteLine ( "classbase tp_str fallback" ) ;
274
+
267
275
//otherwise fallback to object's ToString() implementation
268
276
return Runtime . PyString_FromString ( co . inst . ToString ( ) ) ;
269
277
@@ -288,6 +296,7 @@ public static IntPtr tp_repr(IntPtr ob)
288
296
}
289
297
try
290
298
{
299
+ Console . WriteLine ( "classbase tp_repr" ) ;
291
300
//if __repr__ is defined, use it
292
301
var instType = co . inst . GetType ( ) ;
293
302
System . Reflection . MethodInfo methodInfo = instType . GetMethod ( "__repr__" ) ;
@@ -297,6 +306,8 @@ public static IntPtr tp_repr(IntPtr ob)
297
306
return Runtime . PyString_FromString ( reprString ) ;
298
307
}
299
308
309
+ Console . WriteLine ( "classbase tp_repr fallback" ) ;
310
+
300
311
//otherwise use the standard object.__repr__(inst)
301
312
IntPtr args = Runtime . PyTuple_New ( 1 ) ;
302
313
Runtime . PyTuple_SetItem ( args , 0 , ob ) ;
0 commit comments