@@ -326,52 +326,52 @@ func DeleteAttr(self Object, keyObj Object) error {
326
326
327
327
// Calls __str__ on the object
328
328
//
329
- // If no method was found, returns ok as false
330
- func str (self Object ) (res Object , ok bool , err error ) {
331
- if _ , ok = self .(String ); ok {
332
- return self , true , nil
329
+ // Calls __repr__ on the object or returns a sensible default
330
+ func Repr (self Object ) (Object , error ) {
331
+ if I , ok := self .(I__repr__ ); ok {
332
+ return I .M__repr__ ()
333
+ } else if res , ok , err := TypeCall0 (self , "__repr__" ); ok {
334
+ return res , err
333
335
}
336
+ return String (fmt .Sprintf ("<%s instance at %p>" , self .Type ().Name , self )), nil
337
+ }
338
+
339
+ // Calls __str__ on the object and if not found calls __repr__
340
+ func Str (self Object ) (Object , error ) {
334
341
if I , ok := self .(I__str__ ); ok {
335
- res , err = I .M__str__ ()
336
- return res , true , err
342
+ return I .M__str__ ()
337
343
} else if res , ok , err := TypeCall0 (self , "__str__" ); ok {
338
- return res , true , err
344
+ return res , err
339
345
}
340
- return nil , false , nil
346
+ return Repr ( self )
341
347
}
342
348
343
- // Calls __str__ on the object
344
- func Str (self Object ) (Object , error ) {
345
- res , ok , err := str (self )
349
+ // Returns object as a string
350
+ //
351
+ // Calls Str then makes sure the output is a string
352
+ func StrAsString (self Object ) (string , error ) {
353
+ res , err := Str (self )
346
354
if err != nil {
347
- return nil , err
355
+ return "" , err
348
356
}
357
+ str , ok := res .(String )
349
358
if ! ok {
350
- return nil , ExceptionNewf (TypeError , "object of type '%s' has no __str__()" , self .Type ().Name )
351
- }
352
- return res , err
353
- }
354
-
355
- // Calls __repr__ on the object or returns a sensible default
356
- func Repr (self Object ) (Object , error ) {
357
- if I , ok := self .(I__repr__ ); ok {
358
- return I .M__repr__ ()
359
- } else if res , ok , err := TypeCall0 (self , "__repr__" ); ok {
360
- return res , err
359
+ return "" , ExceptionNewf (TypeError , "result of __str__ must be string, not '%s'" , res .Type ().Name )
361
360
}
362
- return String ( fmt . Sprintf ( "<%s instance at %p>" , self . Type (). Name , self ) ), nil
361
+ return string ( str ), nil
363
362
}
364
363
365
364
// Returns object as a string
366
365
//
367
- // Calls __str__ then __repr__ on the object then makes something up
368
- func AsString (self Object ) (Object , error ) {
369
- res , ok , err := str (self )
366
+ // Calls Repr then makes sure the output is a string
367
+ func ReprAsString (self Object ) (string , error ) {
368
+ res , err := Repr (self )
370
369
if err != nil {
371
- return nil , err
370
+ return "" , err
372
371
}
373
- if ok {
374
- return res , err
372
+ str , ok := res .(String )
373
+ if ! ok {
374
+ return "" , ExceptionNewf (TypeError , "result of __repr__ must be string, not '%s'" , res .Type ().Name )
375
375
}
376
- return Repr ( self )
376
+ return string ( str ), nil
377
377
}
0 commit comments