44
55package py
66
7+ // AttributeName converts an Object to a string, raising a TypeError
8+ // if it wasn't a String
9+ func AttributeName (keyObj Object ) string {
10+ if key , ok := keyObj .(String ); ok {
11+ return string (key )
12+ }
13+ panic (ExceptionNewf (TypeError , "attribute name must be string, not '%s'" , keyObj .Type ().Name ))
14+ }
15+
716// Bool is called to implement truth value testing and the built-in
817// operation bool(); should return False or True. When this method is
918// not defined, __len__() is called, if it is defined, and the object
@@ -142,10 +151,10 @@ func DelItem(self Object, key Object) Object {
142151 panic (ExceptionNewf (TypeError , "'%s' object does not support item deletion" , self .Type ().Name ))
143152}
144153
145- // GetAttrErr - returns the result or an err to be raised if not found
154+ // GetAttrStringErr - returns the result or an err to be raised if not found
146155//
147156// Only AttributeErrors will be returned in err, everything else will be raised
148- func GetAttrErr (self Object , key string ) (res Object , err error ) {
157+ func GetAttrStringErr (self Object , key string ) (res Object , err error ) {
149158 defer func () {
150159 if r := recover (); r != nil {
151160 if IsException (AttributeError , r ) {
@@ -201,9 +210,16 @@ func GetAttrErr(self Object, key string) (res Object, err error) {
201210 return
202211}
203212
213+ // GetAttrErr - returns the result or an err to be raised if not found
214+ //
215+ // Only AttributeErrors will be returned in err, everything else will be raised
216+ func GetAttrErr (self Object , keyObj Object ) (res Object , err error ) {
217+ return GetAttrStringErr (self , AttributeName (keyObj ))
218+ }
219+
204220// GetAttrString gets the attribute, raising an error if not found
205221func GetAttrString (self Object , key string ) Object {
206- res , err := GetAttrErr (self , key )
222+ res , err := GetAttrStringErr (self , key )
207223 if err != nil {
208224 panic (err )
209225 }
@@ -213,10 +229,7 @@ func GetAttrString(self Object, key string) Object {
213229// GetAttr gets the attribute rasing an error if key isn't a string or
214230// attribute not found
215231func GetAttr (self Object , keyObj Object ) Object {
216- if key , ok := keyObj .(String ); ok {
217- return GetAttrString (self , string (key ))
218- }
219- panic (ExceptionNewf (TypeError , "attribute name must be string, not '%s'" , self .Type ().Name ))
232+ return GetAttrString (self , AttributeName (keyObj ))
220233}
221234
222235// SetAttrString
@@ -255,10 +268,7 @@ func SetAttrString(self Object, key string, value Object) Object {
255268
256269// SetAttr
257270func SetAttr (self Object , keyObj Object , value Object ) Object {
258- if key , ok := keyObj .(String ); ok {
259- return GetAttrString (self , string (key ))
260- }
261- panic (ExceptionNewf (TypeError , "attribute name must be string, not '%s'" , self .Type ().Name ))
271+ return GetAttrString (self , AttributeName (keyObj ))
262272}
263273
264274// DeleteAttrString
@@ -301,9 +311,5 @@ func DeleteAttrString(self Object, key string) {
301311
302312// DeleteAttr
303313func DeleteAttr (self Object , keyObj Object ) {
304- if key , ok := keyObj .(String ); ok {
305- DeleteAttrString (self , string (key ))
306- return
307- }
308- panic (ExceptionNewf (TypeError , "attribute name must be string, not '%s'" , self .Type ().Name ))
314+ DeleteAttrString (self , AttributeName (keyObj ))
309315}
0 commit comments