Skip to content

Commit df32a3c

Browse files
allow object key to shadow method
1 parent 27019d2 commit df32a3c

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

src/JSObjectProxy.cc

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,14 @@ static inline PyObject *getKey(JSObjectProxy *self, PyObject *key, JS::HandleId
111111
}
112112
else {
113113
if (strcmp(methodName, PyUnicode_AsUTF8(key)) == 0) {
114-
return PyObject_GenericGetAttr((PyObject *)self, key);
114+
// just make sure no property is shadowing a method by name
115+
JS::RootedValue value(GLOBAL_CX);
116+
JS_GetPropertyById(GLOBAL_CX, *(self->jsObject), id, &value);
117+
if (!value.isUndefined()) {
118+
return pyTypeFactory(GLOBAL_CX, value)->getPyObject();
119+
} else {
120+
return PyObject_GenericGetAttr((PyObject *)self, key);
121+
}
115122
}
116123
}
117124
}

tests/python/test_dict_methods.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -448,4 +448,10 @@ def test_items_mapping():
448448
#get method
449449
def test_get_method():
450450
dishes = pm.eval("({'eggs': 2, 'sausage': 1, 'bacon': 1, 'spam': 500})")
451-
assert dishes.get('eggs') == 2
451+
assert dishes.get('eggs') == 2
452+
453+
#get method shadowing
454+
def test_method_shadowing():
455+
jsObj = pm.eval("({get: 'value'})")
456+
assert jsObj.get == 'value'
457+
assert jsObj['get'] == 'value'

0 commit comments

Comments
 (0)