Skip to content

Commit

Permalink
refactor: importing cpython_bindings separate out into function
Browse files Browse the repository at this point in the history
  • Loading branch information
Vipul-Cariappa committed Aug 4, 2024
1 parent 62ae095 commit 24f52fa
Showing 1 changed file with 50 additions and 45 deletions.
95 changes: 50 additions & 45 deletions src/lpython/semantics/python_ast_to_asr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4429,6 +4429,55 @@ class SymbolTableVisitor : public CommonVisitor<SymbolTableVisitor> {
//Implemented in BodyVisitor
}

void import_cpython(const AST::FunctionDef_t &x, SymbolTable *parent_scope) {
std::vector<std::string> pybind_funcs = {
"Py_Initialize",
"Py_IsInitialized",
"PyRun_SimpleString",
"Py_DecodeLocale",
"PySys_SetArgv",
"Py_FinalizeEx",
"PyUnicode_FromString",
"PyUnicode_AsUTF8AndSize",
"PyImport_Import",
"Py_DecRef",
"Py_IncRef",
"PyObject_GetAttrString",
"PyTuple_New",
"PyTuple_SetItem",
"PyObject_CallObject",
"PyLong_AsLongLong",
"PyLong_AsUnsignedLongLong",
"PyLong_FromLongLong",
"PyLong_FromUnsignedLongLong",
"PyFloat_FromDouble",
"PyFloat_AsDouble",
"PyBool_FromLong",
"PyObject_IsTrue",
};
Str s;
AST::alias_t *module_symbols =
al.allocate<AST::alias_t>(pybind_funcs.size());

for (size_t i = 0; i < pybind_funcs.size(); i++) {
s.from_str(al, pybind_funcs.at(i));
(module_symbols + i)->loc = x.base.base.loc;
(module_symbols + i)->m_name = s.c_str(al);
(module_symbols + i)->m_asname = nullptr;
}

AST::ImportFrom_t *imports =
(AST::ImportFrom_t*)AST::make_ImportFrom_t(
al, x.base.base.loc,
(char*)"cpython_bindings", module_symbols,
pybind_funcs.size(), 0);

SymbolTable *function_scope = current_scope;
current_scope = parent_scope;
visit_ImportFrom(*imports);
current_scope = function_scope;
}

void visit_FunctionDef(const AST::FunctionDef_t &x) {
dependencies.clear(al);
SymbolTable *parent_scope = current_scope;
Expand Down Expand Up @@ -4492,52 +4541,8 @@ class SymbolTableVisitor : public CommonVisitor<SymbolTableVisitor> {
module_file = extract_keyword_val_from_decorator(call_d, "module");

if (!contains_bindpython) {
std::vector<std::string> pybind_funcs = {
"Py_Initialize",
"Py_IsInitialized",
"PyRun_SimpleString",
"Py_DecodeLocale",
"PySys_SetArgv",
"Py_FinalizeEx",
"PyUnicode_FromString",
"PyUnicode_AsUTF8AndSize",
"PyImport_Import",
"Py_DecRef",
"Py_IncRef",
"PyObject_GetAttrString",
"PyTuple_New",
"PyTuple_SetItem",
"PyObject_CallObject",
"PyLong_AsLongLong",
"PyLong_AsUnsignedLongLong",
"PyLong_FromLongLong",
"PyLong_FromUnsignedLongLong",
"PyFloat_FromDouble",
"PyFloat_AsDouble",
"PyBool_FromLong",
"PyObject_IsTrue",
};
Str s;
AST::alias_t *module_symbols =
al.allocate<AST::alias_t>(pybind_funcs.size());

for (size_t i = 0; i < pybind_funcs.size(); i++) {
s.from_str(al, pybind_funcs.at(i));
(module_symbols + i)->loc = x.base.base.loc;
(module_symbols + i)->m_name = s.c_str(al);
(module_symbols + i)->m_asname = nullptr;
}

AST::ImportFrom_t *imports =
(AST::ImportFrom_t*)AST::make_ImportFrom_t(
al, x.base.base.loc,
(char*)"cpython_bindings", module_symbols,
pybind_funcs.size(), 0);

SymbolTable *function_scope = current_scope;
current_scope = parent_scope;
visit_ImportFrom(*imports);
current_scope = function_scope;
import_cpython(x, parent_scope);
}
contains_bindpython = true;

Expand Down

0 comments on commit 24f52fa

Please sign in to comment.