Commit 4376696b authored by Kevin Modzelewski's avatar Kevin Modzelewski

Minor refcounting fixes

parent d7490eda
......@@ -9,7 +9,7 @@ extern "C" {
#endif
// Pyston change: changed most of these to const char*
PyAPI_FUNC(PyObject *) PySys_GetObject(const char *) PYSTON_NOEXCEPT;
PyAPI_FUNC(BORROWED(PyObject *)) PySys_GetObject(const char *) PYSTON_NOEXCEPT;
PyAPI_FUNC(int) PySys_SetObject(const char *, PyObject *) PYSTON_NOEXCEPT;
PyAPI_FUNC(FILE *) PySys_GetFile(char *, FILE *) PYSTON_NOEXCEPT;
PyAPI_FUNC(void) PySys_SetArgv(int, char **) PYSTON_NOEXCEPT;
......
......@@ -785,6 +785,7 @@ Box* getGlobals() {
auto it = getTopPythonFrame();
if (!it)
return NULL;
RELEASE_ASSERT(0, "check refcounting");
return it->getGlobals();
}
......
......@@ -87,7 +87,9 @@ BoxedDict* getSysModulesDict() {
BoxedList* getSysPath() {
// Unlike sys.modules, CPython handles sys.path by fetching it each time:
Box* _sys_path = sys_module->getattr(internStringMortal("path"));
auto path_str = getStaticString("path");
Box* _sys_path = sys_module->getattr(path_str);
assert(_sys_path);
if (_sys_path->cls != list_cls) {
......@@ -99,7 +101,8 @@ BoxedList* getSysPath() {
}
Box* getSysStdout() {
Box* sys_stdout = sys_module->getattr(internStringMortal("stdout"));
auto stdout_str = getStaticString("stdout");
Box* sys_stdout = sys_module->getattr(stdout_str);
RELEASE_ASSERT(sys_stdout, "lost sys.stdout??");
return sys_stdout;
}
......@@ -137,10 +140,10 @@ Box* sysGetRecursionLimit() {
extern "C" int PySys_SetObject(const char* name, PyObject* v) noexcept {
try {
if (!v) {
if (sys_module->getattr(internStringMortal(name)))
sys_module->delattr(internStringMortal(name), NULL);
if (sys_module->getattr(autoDecref(internStringMortal(name))))
sys_module->delattr(autoDecref(internStringMortal(name)), NULL);
} else
sys_module->setattr(internStringMortal(name), v, NULL);
sys_module->setattr(autoDecref(internStringMortal(name)), v, NULL);
} catch (ExcInfo e) {
abort();
}
......@@ -148,7 +151,7 @@ extern "C" int PySys_SetObject(const char* name, PyObject* v) noexcept {
}
extern "C" PyObject* PySys_GetObject(const char* name) noexcept {
return sys_module->getattr(internStringMortal(name));
return sys_module->getattr(autoDecref(internStringMortal(name)));
}
extern "C" FILE* PySys_GetFile(char* name, FILE* def) noexcept {
......
......@@ -232,6 +232,7 @@ void raiseExcHelper(BoxedClass* cls, const char* msg, ...) {
BoxedString* message = boxString(buf);
Box* exc_obj = runtimeCall(cls, ArgPassSpec(1), message, NULL, NULL, NULL, NULL);
Py_DECREF(message);
raiseExc(exc_obj);
} else {
Box* exc_obj = runtimeCall(cls, ArgPassSpec(0), NULL, NULL, NULL, NULL, NULL);
......
......@@ -428,7 +428,7 @@ static Box* importSub(const std::string& name, BoxedString* full_name, Box* pare
return module;
}
return None;
return incref(None);
}
static void markMiss(std::string& name) {
......@@ -474,6 +474,7 @@ static bool loadNext(Box* mod, Box* altmod, std::string& name, std::string& buf,
result = importSub(subname, autoDecref(boxString(buf)), mod);
if (result == None && altmod != mod) {
Py_DECREF(result);
/* Here, altmod must be None and mod must not be None */
result = importSub(subname, autoDecref(boxString(subname)), altmod);
if (result != NULL && result != None) {
......@@ -485,8 +486,10 @@ static bool loadNext(Box* mod, Box* altmod, std::string& name, std::string& buf,
if (result == NULL)
throwCAPIException();
if (result == None)
if (result == None) {
Py_DECREF(result);
raiseExcHelper(ImportError, "No module named %.200s", local_name.c_str());
}
*rtn = result;
return call_again;
......@@ -517,12 +520,16 @@ Box* importModuleLevel(llvm::StringRef name, Box* globals, Box* from_imports, in
while (again) {
Box* next;
again = loadNext(tail, tail, _name, buf, &next);
Py_DECREF(tail);
if (next == NULL) {
Py_DECREF(head);
return NULL;
}
tail = next;
}
if (tail == None) {
Py_DECREF(tail);
Py_DECREF(head);
/* If tail is Py_None, both get_parent and load_next found
an empty module name: someone called __import__("") or
doctored faulty bytecode */
......@@ -707,7 +714,7 @@ Box* nullImporterFindModule(Box* self, Box* fullname, Box* path) {
}
extern "C" Box* import(int level, Box* from_imports, llvm::StringRef module_name) {
return importModuleLevel(module_name, getGlobals(), from_imports, level);
return importModuleLevel(module_name, autoDecref<Box, true>(getGlobals()), from_imports, level);
}
Box* impFindModule(Box* _name, BoxedList* path) {
......
......@@ -3700,7 +3700,7 @@ void rearrangeArgumentsInternal(ParamReceiveSpec paramspec, const ParamNames* pa
(paramspec.num_args == 1 ? "" : "s"), argspec.num_args + argspec.num_keywords + varargs_size);
}
Py_DECREF(varargs);
Py_XDECREF(varargs);
////
// Second, apply any keywords:
......
......@@ -69,7 +69,7 @@ void setupAST();
void setupSysEnd();
BORROWED(BoxedDict*) getSysModulesDict();
BoxedList* getSysPath();
BORROWED(BoxedList*) getSysPath();
extern "C" Box* getSysStdout();
extern "C" BoxedTuple* EmptyTuple;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment