Skip to content

Commit b596a71

Browse files
committed
Some more recursion-depth-checking fixes
- Fix a test to do the correct number of iterations - Catch more exceptions in PyDict_GetItem/createModule (now that more things can throw)
1 parent 7e2cbb5 commit b596a71

File tree

6 files changed

+28
-7
lines changed

6 files changed

+28
-7
lines changed

src/capi/modsupport.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,13 @@ extern "C" PyObject* Py_InitModule4(const char* name, PyMethodDef* methods, cons
434434
}
435435
}
436436

437-
BoxedModule* module = createModule(autoDecref(boxString(name)), NULL, doc);
437+
BoxedModule* module;
438+
try {
439+
module = createModule(autoDecref(boxString(name)), NULL, doc);
440+
} catch (ExcInfo e) {
441+
setCAPIException(e);
442+
return NULL;
443+
}
438444

439445
// Pass self as is, even if NULL we are not allowed to change it to None
440446
Box* passthrough = static_cast<Box*>(self);

src/core/types.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -975,7 +975,7 @@ class BoxedClass;
975975
// TODO these shouldn't be here
976976
void setupRuntime();
977977
Box* createAndRunModule(BoxedString* name, const std::string& fn);
978-
BORROWED(BoxedModule*) createModule(BoxedString* name, const char* fn = NULL, const char* doc = NULL) noexcept;
978+
BORROWED(BoxedModule*) createModule(BoxedString* name, const char* fn = NULL, const char* doc = NULL);
979979
Box* moduleInit(BoxedModule* self, Box* name, Box* doc = NULL);
980980

981981
// TODO where to put this

src/runtime/dict.cpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,10 +360,23 @@ extern "C" BORROWED(PyObject*) PyDict_GetItem(PyObject* dict, PyObject* key) noe
360360
if (PyDict_Check(dict)) {
361361
BoxedDict* d = static_cast<BoxedDict*>(dict);
362362

363+
BoxAndHash h;
364+
try {
365+
h = BoxAndHash(key);
366+
} catch (ExcInfo e) {
367+
e.clear();
368+
return NULL;
369+
}
370+
363371
/* preserve the existing exception */
364372
PyObject* err_type, *err_value, *err_tb;
365373
PyErr_Fetch(&err_type, &err_value, &err_tb);
366-
Box* b = d->getOrNull(key);
374+
Box* b = NULL;
375+
try {
376+
b = d->getOrNull(h);
377+
} catch (ExcInfo e) {
378+
e.clear();
379+
}
367380
/* ignore errors */
368381
PyErr_Restore(err_type, err_value, err_tb);
369382
return b;

src/runtime/types.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4725,7 +4725,7 @@ void setupRuntime() {
47254725
TRACK_ALLOCATIONS = true;
47264726
}
47274727

4728-
BORROWED(BoxedModule*) createModule(BoxedString* name, const char* fn, const char* doc) noexcept {
4728+
BORROWED(BoxedModule*) createModule(BoxedString* name, const char* fn, const char* doc) {
47294729
assert((!fn || strlen(fn)) && "probably wanted to set the fn to <stdin>?");
47304730

47314731
BoxedDict* d = getSysModulesDict();

src/runtime/types.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1035,13 +1035,15 @@ class BoxedDict : public Box {
10351035

10361036
DEFAULT_CLASS_SIMPLE(dict_cls, true);
10371037

1038-
BORROWED(Box*) getOrNull(Box* k) {
1039-
const auto& p = d.find(BoxAndHash(k));
1038+
BORROWED(Box*) getOrNull(BoxAndHash k) {
1039+
const auto& p = d.find(k);
10401040
if (p != d.end())
10411041
return p->second;
10421042
return NULL;
10431043
}
10441044

1045+
BORROWED(Box*) getOrNull(Box* k) { return getOrNull(BoxAndHash(k)); }
1046+
10451047
class iterator {
10461048
private:
10471049
DictMap::iterator it;

test/tests/generator_recursion_checking.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ def test(n):
77
g.next()
88
l.append(g)
99

10-
for i in xrange(1000):
10+
for i in xrange(3):
1111
test(3500)

0 commit comments

Comments
 (0)