Skip to content

Commit 12c5539

Browse files
committed
don't try to call generic getattr before calling user-supplied override
1 parent ad4ad72 commit 12c5539

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

src/embed_tests/TestInstanceWrapping.cs

+6
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,12 @@ public void IntOrStr(string arg) =>
123123
public void BaseOrDerived(Derived _) => this.Value = Derived;
124124

125125
public bool TryGetAttr(string name, out PyObject value) {
126+
using (var self = this.ToPython()) {
127+
if (GetAttr.TryGetBaseAttr(self, name, out value)
128+
|| GetAttr.GenericGetAttr(self, name, out value))
129+
return true;
130+
}
131+
126132
value = GetAttrFallbackValue.ToPython();
127133
return true;
128134
}

src/runtime/slots.cs

+23-3
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ public interface IGetAttr {
1212

1313
static class SlotOverrides {
1414
public static IntPtr tp_getattro(IntPtr ob, IntPtr key) {
15-
IntPtr genericResult = Runtime.PyObject_GenericGetAttr(ob, key);
16-
if (genericResult != IntPtr.Zero || !Runtime.PyString_Check(key)) {
15+
if (!Runtime.PyString_Check(key)) {
16+
IntPtr genericResult = Runtime.PyObject_GenericGetAttr(ob, key);
1717
return genericResult;
1818
}
1919

@@ -43,7 +43,7 @@ public static bool TryGetBaseAttr(PyObject self, string name, out PyObject resul
4343
if (name == null) throw new ArgumentNullException(nameof(name));
4444

4545
using (var super = new PyObject(Runtime.PySuper))
46-
using (var @class = self.GetAttr("__class__"))
46+
using (var @class = self.GetPythonType())
4747
using (var @base = super.Invoke(@class, self)) {
4848
if (!@base.HasAttr(getAttr)) {
4949
result = null;
@@ -56,5 +56,25 @@ public static bool TryGetBaseAttr(PyObject self, string name, out PyObject resul
5656
}
5757
}
5858
}
59+
60+
public static bool GenericGetAttr(PyObject self, string name, out PyObject result) {
61+
if (self == null) throw new ArgumentNullException(nameof(self));
62+
if (name == null) throw new ArgumentNullException(nameof(name));
63+
64+
using (var pyName = name.ToPython()) {
65+
IntPtr pyResult = Runtime.PyObject_GenericGetAttr(self.Handle, pyName.Handle);
66+
if (pyResult == IntPtr.Zero) {
67+
result = null;
68+
if (!PythonException.Matches(Exceptions.AttributeError)) {
69+
throw PythonException.FromPyErr();
70+
}
71+
72+
Exceptions.Clear();
73+
return false;
74+
}
75+
result = new PyObject(Runtime.SelfIncRef(pyResult));
76+
return true;
77+
}
78+
}
5979
}
6080
}

0 commit comments

Comments
 (0)