Skip to content

Commit 53fcefb

Browse files
committed
docs(JSObjectProxy): explain the .keys() method on a dict
1 parent 8f609c8 commit 53fcefb

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

src/JSObjectProxy.cc

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,13 @@ Py_ssize_t JSObjectProxyMethodDefinitions::JSObjectProxy_length(JSObjectProxy *s
7373

7474
PyObject *JSObjectProxyMethodDefinitions::JSObjectProxy_get(JSObjectProxy *self, PyObject *key)
7575
{
76+
// // XXX: If we ban access of the `.keys()` method from Python internals, we can force to-dict conversion by iterating over the sequence of key-value pairs
77+
// // see also: https://docs.python.org/3/c-api/dict.html#c.PyDict_Update
78+
// if (PyUnicode_CompareWithASCIIString(key, "keys") == 0) {
79+
// PyErr_SetString(PyExc_AttributeError, "Use for-in iterator instead.");
80+
// return NULL;
81+
// }
82+
7683
JS::RootedId id(GLOBAL_CX);
7784
if (!keyToId(key, &id)) {
7885
// TODO (Caleb Aikens): raise exception here
@@ -230,7 +237,7 @@ PyObject *JSObjectProxyMethodDefinitions::JSObjectProxy_repr(JSObjectProxy *self
230237
PyObject *objPtr = PyLong_FromVoidPtr(self->jsObject.get());
231238
// For `Py_ReprEnter`, we must get a same PyObject when visiting the same JSObject.
232239
// We cannot simply use the object returned by `PyLong_FromVoidPtr` because it won't reuse the PyLongObjects for ints not between -5 and 256.
233-
// Instead, we store this PyLongObject in a global dict, using itself as the hashable key.
240+
// Instead, we store this PyLongObject in a global dict, using itself as the hashable key, effectively interning the PyLongObject.
234241
PyObject *tsDict = PyThreadState_GetDict();
235242
PyObject *cyclicKey = PyDict_SetDefault(tsDict, /*key*/ objPtr, /*value*/ objPtr); // cyclicKey = (tsDict[objPtr] ??= objPtr)
236243
int status = Py_ReprEnter(cyclicKey);

0 commit comments

Comments
 (0)