Safer `GetAttr(name, default)` by lostmsu · Pull Request #1578 · pythonnet/pythonnet

What does this implement/fix? Explain your changes.

Prior to this change PyObject.GetAttr(name, default) would ignore any exceptions, including ones unrelated to missing attribute. For example (hypothetically) PyObject is a file, and you call file.GetAttr("len", 0), it would return 0 on I/O error even though file object actually has len attribute.

Remarks

I marked the method obsolete, as even now when we restrict ignored errors to AttributeError and derived types, the behavior of this method still going to be misleading in some scenarios. For example,

class RaisesAttrErr:
  @property
  def prop(self): raise AttributeError

class OK:
  def __init__(self, impl):
    self._impl = impl

  @property
  def existing_prop(self): return self._impl.prop
PythonEngine.Eval("OK(RaisesAttrErr())").GetAttr("existing_prop", 0)
// returns 0 despite existing_prop actually existing -^ in OK

Does this close any currently open issues?

fixes #1036

Checklist

Check all those that are applicable and complete.

  • Make sure to include one or more tests for your change
  • Updated the CHANGELOG