Skip to content

Commit c3643c1

Browse files
authored
Filter undefined property getters/setters (#1417)
* Filter undefined property getters/setters * Add test * Fix filtering
1 parent 72c2b5a commit c3643c1

File tree

3 files changed

+88
-8
lines changed

3 files changed

+88
-8
lines changed

Src/IronPython/Runtime/Types/PythonTypeInfo.cs

Lines changed: 60 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -206,15 +206,19 @@ private class StandardResolver : MemberResolver {
206206
MemberGroup res;
207207

208208
foreach (Type curType in binder.GetContributingTypes(type)) {
209-
res = FilterObjectMembers(FilterSpecialNames(binder.GetMember(curType, name), name, action));
209+
res = binder.GetMember(curType, name);
210+
res = FilterSpecialNames(res, name, action);
211+
res = FilterProperties(res, action);
212+
res = FilterObjectMembers(res);
210213
if (res.Count > 0) {
211214
return res;
212215
}
213216
}
214217

215218
if (type.IsInterface) {
216219
foreach (Type t in type.GetInterfaces()) {
217-
res = FilterSpecialNames(binder.GetMember(t, name), name, action);
220+
res = binder.GetMember(t, name);
221+
res = FilterSpecialNames(res, name, action);
218222
if (res.Count > 0) {
219223
return res;
220224
}
@@ -265,7 +269,8 @@ private class ObjectResolver : StandardResolver {
265269
continue;
266270
}
267271

268-
res = FilterSpecialNames(binder.GetMember(curType, name), name, action);
272+
res = binder.GetMember(curType, name);
273+
res = FilterSpecialNames(res, name, action);
269274
if (res.Count > 0) {
270275
return res;
271276
}
@@ -1869,21 +1874,68 @@ private static void EnsureOperatorTable() {
18691874
}
18701875
}
18711876

1877+
#nullable enable
1878+
1879+
private static MemberGroup FilterProperties(MemberGroup group, MemberRequestKind action) {
1880+
List<MemberTracker>? mts = null;
1881+
for (int i = 0; i < group.Count; i++) {
1882+
MemberTracker mt = group[i];
1883+
1884+
if (mt.MemberType == TrackerTypes.Property && mt is ReflectedPropertyTracker rpt) {
1885+
if (FilterProperty(action, rpt.Property)) {
1886+
mts ??= MakeListWithPreviousMembers(group, mts, i);
1887+
continue;
1888+
}
1889+
}
1890+
mts?.Add(mt);
1891+
}
1892+
1893+
if (mts != null) {
1894+
if (mts.Count == 0) {
1895+
return MemberGroup.EmptyGroup;
1896+
}
1897+
return new MemberGroup(mts.ToArray());
1898+
}
1899+
return group;
1900+
1901+
static bool FilterProperty(MemberRequestKind action, PropertyInfo property) {
1902+
if (action == MemberRequestKind.Get) {
1903+
if (property.GetMethod is null && property.SetMethod is not null) {
1904+
return property.SetMethod != property.SetMethod.GetBaseDefinition();
1905+
}
1906+
} else if (action == MemberRequestKind.Set) {
1907+
if (property.SetMethod is null && property.GetMethod is not null) {
1908+
return property.GetMethod != property.GetMethod.GetBaseDefinition();
1909+
}
1910+
}
1911+
return false;
1912+
}
1913+
}
1914+
18721915
private static MemberGroup/*!*/ FilterObjectMembers(MemberGroup/*!*/ group) {
18731916
Assert.NotNull(group);
1874-
List<MemberTracker> mts = new List<MemberTracker>();
1917+
List<MemberTracker>? mts = null;
18751918
for (int i = 0; i < group.Count; i++) {
18761919
MemberTracker mt = group[i];
18771920

18781921
if (mt.DeclaringType == typeof(object) || mt.DeclaringType == typeof(ObjectOps)) {
1879-
continue;
1922+
mts ??= MakeListWithPreviousMembers(group, mts, i);
1923+
continue;
18801924
}
1881-
mts.Add(mt);
1925+
mts?.Add(mt);
18821926
}
1883-
if (mts.Count == 0) return MemberGroup.EmptyGroup;
1884-
return new MemberGroup(mts.ToArray());
1927+
1928+
if (mts != null) {
1929+
if (mts.Count == 0) {
1930+
return MemberGroup.EmptyGroup;
1931+
}
1932+
return new MemberGroup(mts.ToArray());
1933+
}
1934+
return group;
18851935
}
18861936

1937+
#nullable restore
1938+
18871939
private static MemberGroup/*!*/ FilterSpecialNames(MemberGroup/*!*/ group, string/*!*/ name, MemberRequestKind/*!*/ action) {
18881940
Assert.NotNull(group, name, action);
18891941

Src/IronPythonTest/InheritTest.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,20 @@ public NewClass(int width, int height) : base(-1, -1) {
208208
}
209209
}
210210

211+
public class PartialPropertyOverrideClass : BaseClass {
212+
public override int Height {
213+
set {
214+
size.height = 2 * value;
215+
}
216+
}
217+
218+
public override int Width {
219+
get {
220+
return size.width * 2;
221+
}
222+
}
223+
}
224+
211225
public class UnaryClass {
212226
public int value;
213227

Tests/test_inheritance.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1391,4 +1391,18 @@ def __init__(self):
13911391
self.assertEqual(a.GetFoo(), 42)
13921392
self.assertEqual(a.GetBar(), 23)
13931393

1394+
@skipUnlessIronPython()
1395+
def test_partial_property_override(self):
1396+
# https://github.com/IronLanguages/ironpython3/issues/1375
1397+
from IronPythonTest import PartialPropertyOverrideClass
1398+
c = PartialPropertyOverrideClass()
1399+
1400+
# only the setter is overridden on Height
1401+
c.Height = 4
1402+
self.assertEqual(c.Height, 8)
1403+
1404+
# only the getter is overridden on Width
1405+
c.Width = 4
1406+
self.assertEqual(c.Width, 8)
1407+
13941408
run_test(__name__)

0 commit comments

Comments
 (0)