Skip to content

Commit

Permalink
Perfect the code
Browse files Browse the repository at this point in the history
  • Loading branch information
chungshien-chai committed Sep 25, 2024
1 parent 5783f94 commit 0b2c9d3
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 10 deletions.
16 changes: 8 additions & 8 deletions src/Configuration/CFGCommon/CFGCommon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -944,15 +944,14 @@ CFG_Python_MGR::CFG_Python_MGR(const std::string& filepath,
CFG_ASSERT(module_objs.find(main_module) != module_objs.end());
dict_ptr = PyModule_GetDict((PyObject*)(module_objs[main_module]));
} else {
CFG_ASSERT(main_module.empty());
PyObject* dict = PyDict_New();
dict_ptr = dict;
delete_dict = true;
}
}

CFG_Python_MGR::~CFG_Python_MGR() {
main_module = "";
if (delete_dict && dict_ptr != nullptr) {
if (main_module.empty() && dict_ptr != nullptr) {
PyObject* dict = static_cast<PyObject*>(dict_ptr);
Py_DECREF(dict);
}
Expand All @@ -961,6 +960,7 @@ CFG_Python_MGR::~CFG_Python_MGR() {
Py_XDECREF((PyObject*)(iter.second));
}
Py_Finalize();
main_module = "";
}

std::string CFG_Python_MGR::get_main_module() { return main_module; }
Expand All @@ -975,11 +975,11 @@ std::string CFG_Python_MGR::set_file(const std::string& file,
std::filesystem::path dir = fullpath.parent_path();
std::filesystem::path filename = fullpath.filename();
std::string standard_dir = CFG_change_directory_to_linux_format(dir.string());
PyRun_SimpleString(
CFG_print(
"import sys\nif '%s' not in sys.path:\n sys.path.insert(0, '%s')\n",
standard_dir.c_str(), standard_dir.c_str())
.c_str());
PyRun_SimpleString(CFG_print("import sys\n"
"if '%s' not in sys.path:\n"
" sys.path.insert(0, '%s')\n",
standard_dir.c_str(), standard_dir.c_str())
.c_str());
std::string module =
filename.string().substr(0, filename.string().size() - 3);
CFG_ASSERT(module_objs.find(module) == module_objs.end());
Expand Down
3 changes: 1 addition & 2 deletions src/Configuration/CFGCommon/CFGCommon.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,8 @@ class CFG_Python_MGR {
std::vector<std::string> result_strs(const std::string& result);

private:
bool delete_dict = false;
void* dict_ptr = nullptr;
std::string main_module = "";
void* dict_ptr = nullptr;
std::map<std::string, CFG_Python_OBJ> result_objs;
std::map<std::string, void*> module_objs;
};
Expand Down

0 comments on commit 0b2c9d3

Please sign in to comment.