@@ -309,21 +309,36 @@ public PyObject GetAttr(string name)
309
309
310
310
311
311
/// <summary>
312
- /// GetAttr Method. Returns fallback value if getting attribute fails for any reason.
312
+ /// Returns the named attribute of the Python object, or the given
313
+ /// default object if the attribute access throws AttributeError.
313
314
/// </summary>
314
315
/// <remarks>
315
- /// Returns the named attribute of the Python object, or the given
316
- /// default object if the attribute access fails.
316
+ /// This method ignores any AttrubiteError(s), even ones
317
+ /// not raised due to missing requested attribute.
318
+ ///
319
+ /// For example, if attribute getter calls other Python code, and
320
+ /// that code happens to cause AttributeError elsewhere, it will be ignored
321
+ /// and <paramref name="_default"/> value will be returned instead.
317
322
/// </remarks>
323
+ /// <param name="name">Name of the attribute.</param>
324
+ /// <param name="_default">The object to return on AttributeError.</param>
325
+ [ Obsolete ( "See remarks" ) ]
318
326
public PyObject GetAttr ( string name , PyObject _default )
319
327
{
320
328
if ( name == null ) throw new ArgumentNullException ( nameof ( name ) ) ;
321
329
322
330
IntPtr op = Runtime . PyObject_GetAttrString ( obj , name ) ;
323
331
if ( op == IntPtr . Zero )
324
332
{
325
- Runtime . PyErr_Clear ( ) ;
326
- return _default ;
333
+ if ( Exceptions . ExceptionMatches ( Exceptions . AttributeError ) )
334
+ {
335
+ Runtime . PyErr_Clear ( ) ;
336
+ return _default ;
337
+ }
338
+ else
339
+ {
340
+ throw PythonException . ThrowLastAsClrException ( ) ;
341
+ }
327
342
}
328
343
return new PyObject ( op ) ;
329
344
}
@@ -351,22 +366,36 @@ public PyObject GetAttr(PyObject name)
351
366
352
367
353
368
/// <summary>
354
- /// GetAttr Method
369
+ /// Returns the named attribute of the Python object, or the given
370
+ /// default object if the attribute access throws AttributeError.
355
371
/// </summary>
356
372
/// <remarks>
357
- /// Returns the named attribute of the Python object, or the given
358
- /// default object if the attribute access fails. The name argument
359
- /// is a PyObject wrapping a Python string or unicode object.
373
+ /// This method ignores any AttrubiteError(s), even ones
374
+ /// not raised due to missing requested attribute.
375
+ ///
376
+ /// For example, if attribute getter calls other Python code, and
377
+ /// that code happens to cause AttributeError elsewhere, it will be ignored
378
+ /// and <paramref name="_default"/> value will be returned instead.
360
379
/// </remarks>
380
+ /// <param name="name">Name of the attribute. Must be of Python type 'str'.</param>
381
+ /// <param name="_default">The object to return on AttributeError.</param>
382
+ [ Obsolete ( "See remarks" ) ]
361
383
public PyObject GetAttr ( PyObject name , PyObject _default )
362
384
{
363
385
if ( name == null ) throw new ArgumentNullException ( nameof ( name ) ) ;
364
386
365
387
IntPtr op = Runtime . PyObject_GetAttr ( obj , name . obj ) ;
366
388
if ( op == IntPtr . Zero )
367
389
{
368
- Runtime . PyErr_Clear ( ) ;
369
- return _default ;
390
+ if ( Exceptions . ExceptionMatches ( Exceptions . AttributeError ) )
391
+ {
392
+ Runtime . PyErr_Clear ( ) ;
393
+ return _default ;
394
+ }
395
+ else
396
+ {
397
+ throw PythonException . ThrowLastAsClrException ( ) ;
398
+ }
370
399
}
371
400
return new PyObject ( op ) ;
372
401
}
0 commit comments