diff --git a/src/libasr/pass/python_bind.cpp b/src/libasr/pass/python_bind.cpp index c3dc6fba5d..a2c4b341ee 100644 --- a/src/libasr/pass/python_bind.cpp +++ b/src/libasr/pass/python_bind.cpp @@ -187,19 +187,25 @@ namespace LCompilers { args_PyLong_FromUnsignedLongLong.push_back(al, {f->base.base.loc, ASRUtils::EXPR(ASR::make_Cast_t(al, f->base.base.loc, f->m_args[i], ASR::cast_kindType::UnsignedIntegerToUnsignedInteger, u8_type, nullptr))}); conv_result = ASRUtils::EXPR(ASRUtils::make_FunctionCall_t_util(al, f->base.base.loc, sym_PyLong_FromUnsignedLongLong, nullptr, args_PyLong_FromUnsignedLongLong.p, args_PyLong_FromUnsignedLongLong.n, ptr_t, nullptr, nullptr)); } else if (arg_type->type == ASR::ttypeType::Logical) { - LCOMPILERS_ASSERT_MSG(false, "Not Implemented"); + ASR::symbol_t *sym_PyBool_FromLong = module->m_symtab->get_symbol("PyBool_FromLong"); + Vec args_PyBool_FromLong; + args_PyBool_FromLong.reserve(al, 1); + args_PyBool_FromLong.push_back(al, {f->base.base.loc, ASRUtils::EXPR(ASR::make_Cast_t(al, f->base.base.loc, f->m_args[i], ASR::cast_kindType::LogicalToInteger, i4_type, nullptr))}); + conv_result = ASRUtils::EXPR(ASRUtils::make_FunctionCall_t_util(al, f->base.base.loc, sym_PyBool_FromLong, nullptr, args_PyBool_FromLong.p, args_PyBool_FromLong.n, ptr_t, nullptr, nullptr)); } else if (arg_type->type == ASR::ttypeType::Real) { ASR::symbol_t *sym_PyFloat_FromDouble = module->m_symtab->get_symbol("PyFloat_FromDouble"); Vec args_PyFloat_FromDouble; args_PyFloat_FromDouble.reserve(al, 1); args_PyFloat_FromDouble.push_back(al, {f->base.base.loc, ASRUtils::EXPR(ASR::make_Cast_t(al, f->base.base.loc, f->m_args[i], ASR::cast_kindType::RealToReal, f8_type, nullptr))}); conv_result = ASRUtils::EXPR(ASRUtils::make_FunctionCall_t_util(al, f->base.base.loc, sym_PyFloat_FromDouble, nullptr, args_PyFloat_FromDouble.p, args_PyFloat_FromDouble.n, ptr_t, nullptr, nullptr)); - } else if (arg_type->type == ASR::ttypeType::Complex) { - LCOMPILERS_ASSERT_MSG(false, "Not Implemented"); } else if (arg_type->type == ASR::ttypeType::Character) { - LCOMPILERS_ASSERT_MSG(false, "Not Implemented"); + ASR::symbol_t *sym_PyUnicode_FromString = module->m_symtab->get_symbol("PyUnicode_FromString"); + Vec args_PyUnicode_FromString; + args_PyUnicode_FromString.reserve(al, 1); + args_PyUnicode_FromString.push_back(al, {f->base.base.loc, f->m_args[i]}); + conv_result = ASRUtils::EXPR(ASRUtils::make_FunctionCall_t_util(al, f->base.base.loc, sym_PyUnicode_FromString, nullptr, args_PyUnicode_FromString.p, args_PyUnicode_FromString.n, ptr_t, nullptr, nullptr)); } else { - LCOMPILERS_ASSERT_MSG(false, "Not Implemented"); + throw LCompilersException("Calling CPython with " + ASRUtils::get_type_code(ASRUtils::expr_type(f->m_args[i])) + " type not supported"); } LCOMPILERS_ASSERT(conv_result); args_PyTuple_SetItem.push_back(al, {f->base.base.loc, conv_result}); diff --git a/src/lpython/semantics/python_ast_to_asr.cpp b/src/lpython/semantics/python_ast_to_asr.cpp index ae895c19d7..29ad70b52f 100644 --- a/src/lpython/semantics/python_ast_to_asr.cpp +++ b/src/lpython/semantics/python_ast_to_asr.cpp @@ -4513,6 +4513,7 @@ class SymbolTableVisitor : public CommonVisitor { "PyLong_FromUnsignedLongLong", "PyFloat_FromDouble", "PyFloat_AsDouble", + "PyBool_FromLong", }; Str s; AST::alias_t *module_symbols = diff --git a/src/runtime/cpython_bindings.py b/src/runtime/cpython_bindings.py index 7b6fde2693..a5cb7e1485 100644 --- a/src/runtime/cpython_bindings.py +++ b/src/runtime/cpython_bindings.py @@ -79,3 +79,7 @@ def PyFloat_FromDouble(a: f64) -> CPtr: @ccall(header="Python.h") def PyFloat_AsDouble(a: CPtr) -> f64: pass + +@ccall(header="Python.h") +def PyBool_FromLong(a: i32) -> CPtr: + pass \ No newline at end of file