Skip to content

Commit 3453696

Browse files
committed
fix: accessing a non-existent property on a Python bytes in JS land should return undefined instead of throwing a Python error
1 parent 7ea7a4c commit 3453696

File tree

4 files changed

+14
-8
lines changed

4 files changed

+14
-8
lines changed

src/PyBytesProxyHandler.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,9 @@ bool PyBytesProxyHandler::getOwnPropertyDescriptor(
415415
PyObject *attrName = idToKey(cx, id);
416416
PyObject *self = JS::GetMaybePtrFromReservedSlot<PyObject>(proxy, PyObjectSlot);
417417
PyObject *item = PyObject_GetAttr(self, attrName);
418+
if (!item && PyErr_ExceptionMatches(PyExc_AttributeError)) {
419+
PyErr_Clear(); // clear error, we will be returning undefined in this case
420+
}
418421

419422
return handleGetOwnPropertyDescriptor(cx, id, desc, item);
420423
}

src/PyDictProxyHandler.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ bool PyDictProxyHandler::getOwnPropertyDescriptor(
5656
) const {
5757
PyObject *attrName = idToKey(cx, id);
5858
PyObject *self = JS::GetMaybePtrFromReservedSlot<PyObject>(proxy, PyObjectSlot);
59-
PyObject *item = PyDict_GetItemWithError(self, attrName);
59+
PyObject *item = PyDict_GetItemWithError(self, attrName); // returns NULL without an exception set if the key wasn’t present.
6060

6161
return handleGetOwnPropertyDescriptor(cx, id, desc, item);
6262
}

src/PyIterableProxyHandler.cc

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ static bool toPrimitive(JSContext *cx, unsigned argc, JS::Value *vp) {
7979
_PyUnicodeWriter writer;
8080

8181
_PyUnicodeWriter_Init(&writer);
82-
82+
8383
PyObject *s = PyObject_Repr(self);
8484

8585
if (s == nullptr) {
@@ -95,8 +95,8 @@ static bool toPrimitive(JSContext *cx, unsigned argc, JS::Value *vp) {
9595
return true;
9696
}
9797

98-
PyObject* repr = _PyUnicodeWriter_Finish(&writer);
99-
98+
PyObject *repr = _PyUnicodeWriter_Finish(&writer);
99+
100100
args.rval().set(jsTypeFactory(cx, repr));
101101
return true;
102102
}
@@ -262,7 +262,7 @@ bool PyIterableProxyHandler::getOwnPropertyDescriptor(
262262
// symbol property
263263
if (id.isSymbol()) {
264264
JS::RootedSymbol rootedSymbol(cx, id.toSymbol());
265-
JS::SymbolCode symbolCode = JS::GetSymbolCode(rootedSymbol);
265+
JS::SymbolCode symbolCode = JS::GetSymbolCode(rootedSymbol);
266266

267267
if (symbolCode == JS::SymbolCode::iterator) {
268268
JSFunction *newFunction = JS_NewFunction(cx, iterable_values, 0, 0, NULL);
@@ -275,7 +275,7 @@ bool PyIterableProxyHandler::getOwnPropertyDescriptor(
275275
)
276276
));
277277
return true;
278-
}
278+
}
279279
else if (symbolCode == JS::SymbolCode::toPrimitive) {
280280
JSFunction *newFunction = JS_NewFunction(cx, toPrimitive, 0, 0, nullptr);
281281
if (!newFunction) return false;
@@ -293,6 +293,9 @@ bool PyIterableProxyHandler::getOwnPropertyDescriptor(
293293
PyObject *attrName = idToKey(cx, id);
294294
PyObject *self = JS::GetMaybePtrFromReservedSlot<PyObject>(proxy, PyObjectSlot);
295295
PyObject *item = PyObject_GetAttr(self, attrName);
296+
if (!item && PyErr_ExceptionMatches(PyExc_AttributeError)) {
297+
PyErr_Clear(); // clear error, we will be returning undefined in this case
298+
}
296299

297300
return handleGetOwnPropertyDescriptor(cx, id, desc, item);
298301
}

src/PyObjectProxyHandler.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,8 @@ bool PyObjectProxyHandler::getOwnPropertyDescriptor(
143143
PyObject *attrName = idToKey(cx, id);
144144
PyObject *self = JS::GetMaybePtrFromReservedSlot<PyObject>(proxy, PyObjectSlot);
145145
PyObject *item = PyObject_GetAttr(self, attrName);
146-
if (!item) { // clear error, we will be returning undefined in this case
147-
PyErr_Clear();
146+
if (!item && PyErr_ExceptionMatches(PyExc_AttributeError)) {
147+
PyErr_Clear(); // clear error, we will be returning undefined in this case
148148
}
149149

150150
return handleGetOwnPropertyDescriptor(cx, id, desc, item);

0 commit comments

Comments
 (0)