4
4
5
5
package py
6
6
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
+
7
16
// Bool is called to implement truth value testing and the built-in
8
17
// operation bool(); should return False or True. When this method is
9
18
// not defined, __len__() is called, if it is defined, and the object
@@ -142,10 +151,10 @@ func DelItem(self Object, key Object) Object {
142
151
panic (ExceptionNewf (TypeError , "'%s' object does not support item deletion" , self .Type ().Name ))
143
152
}
144
153
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
146
155
//
147
156
// 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 ) {
149
158
defer func () {
150
159
if r := recover (); r != nil {
151
160
if IsException (AttributeError , r ) {
@@ -201,9 +210,16 @@ func GetAttrErr(self Object, key string) (res Object, err error) {
201
210
return
202
211
}
203
212
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
+
204
220
// GetAttrString gets the attribute, raising an error if not found
205
221
func GetAttrString (self Object , key string ) Object {
206
- res , err := GetAttrErr (self , key )
222
+ res , err := GetAttrStringErr (self , key )
207
223
if err != nil {
208
224
panic (err )
209
225
}
@@ -213,10 +229,7 @@ func GetAttrString(self Object, key string) Object {
213
229
// GetAttr gets the attribute rasing an error if key isn't a string or
214
230
// attribute not found
215
231
func 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 ))
220
233
}
221
234
222
235
// SetAttrString
@@ -255,10 +268,7 @@ func SetAttrString(self Object, key string, value Object) Object {
255
268
256
269
// SetAttr
257
270
func 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 ))
262
272
}
263
273
264
274
// DeleteAttrString
@@ -301,9 +311,5 @@ func DeleteAttrString(self Object, key string) {
301
311
302
312
// DeleteAttr
303
313
func 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 ))
309
315
}
0 commit comments