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

Minor refcounting fixes

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