@@ -503,16 +503,15 @@ R"(#include <stdio.h>
503
503
}
504
504
505
505
std::string get_arg_conv_bind_python (const ASR::Function_t &x) {
506
- // args for bind python not yet supported
507
- LCOMPILERS_ASSERT (x.n_args == 0 );
508
506
509
507
std::string arg_conv = R"(
510
508
pArgs = PyTuple_New()" + std::to_string (x.n_args ) + R"( );
511
509
)" ;
512
510
for (size_t i = 0 ; i < x.n_args ; ++i) {
511
+ ASR::Variable_t *arg = ASRUtils::EXPR2VAR (x.m_args [i]);
513
512
arg_conv += R"(
514
- // Use appropriate Py* Function for x.m_arg[i] conversion
515
- pValue = PyLong_FromLong(x.m_arg[i] );
513
+ pValue = )" + CUtils::get_py_obj_type_conv_func_from_ttype_t (arg-> m_type ) + " ( "
514
+ + std::string (arg-> m_name ) + R"( );
516
515
if (!pValue) {
517
516
Py_DECREF(pArgs);
518
517
Py_DECREF(pModule);
@@ -526,6 +525,20 @@ R"(#include <stdio.h>
526
525
return arg_conv;
527
526
}
528
527
528
+ std::string get_return_value_conv_bind_python (const ASR::Function_t &x) {
529
+ if (!x.m_return_var ) return " " ;
530
+ ASR::Variable_t* r_v = ASRUtils::EXPR2VAR (x.m_return_var );
531
+ std::string indent = " \n " ;
532
+ std::string py_val_cnvrt = CUtils::get_py_obj_return_type_conv_func_from_ttype_t (r_v->m_type ) + " (pValue)" ;
533
+ std::string ret_var_decl = indent + CUtils::get_c_type_from_ttype_t (r_v->m_type ) + " " + std::string (r_v->m_name ) + " ;" ;
534
+ std::string ret_stmt = indent + std::string (r_v->m_name ) + " = " + py_val_cnvrt + " ;" ;
535
+ std::string clear_pValue = " " ;
536
+ if (!ASRUtils::is_aggregate_type (r_v->m_type )) {
537
+ clear_pValue = indent + " Py_DECREF(pValue);" ;
538
+ }
539
+ return ret_var_decl + ret_stmt + clear_pValue + " \n " ;
540
+ }
541
+
529
542
std::string get_func_body_bind_python (const ASR::Function_t &x) {
530
543
user_headers.insert (" Python.h" );
531
544
std::string indent (indentation_level*indentation_spaces, ' ' );
@@ -540,8 +553,13 @@ R"(#include <stdio.h>
540
553
wchar_t* argv1 = Py_DecodeLocale("", NULL);
541
554
wchar_t** argv = {&argv1};
542
555
PySys_SetArgv(1, argv);
556
+
543
557
pName = PyUnicode_FromString(")" + std::string (x.m_module_file ) + R"( ");
544
- // TODO: check for error in pName
558
+ if (pName == NULL) {
559
+ PyErr_Print();
560
+ fprintf(stderr, "Failed to convert to unicode string )" + std::string (x.m_module_file ) + R"( \n");
561
+ exit(1);
562
+ }
545
563
546
564
pModule = PyImport_Import(pName);
547
565
Py_DECREF(pName);
@@ -559,9 +577,7 @@ R"(#include <stdio.h>
559
577
Py_DECREF(pModule);
560
578
exit(1);
561
579
}
562
-
563
- )" + get_arg_conv_bind_python (x) + R"(
564
-
580
+ )" + get_arg_conv_bind_python (x) + R"(
565
581
pValue = PyObject_CallObject(pFunc, pArgs);
566
582
Py_DECREF(pArgs);
567
583
if (pValue == NULL) {
@@ -571,14 +587,16 @@ R"(#include <stdio.h>
571
587
fprintf(stderr,"Call failed\n");
572
588
exit(1);
573
589
}
574
- // TODO: handle/convert the return value here
575
- Py_DECREF(pValue);
576
-
590
+ )" + get_return_value_conv_bind_python (x) + R"(
577
591
if (Py_FinalizeEx() < 0) {
578
592
fprintf(stderr,"BindPython: Unknown Error\n");
579
593
exit(1);
580
594
}
581
595
)" ;
596
+ if (x.m_return_var ) {
597
+ auto r_v = ASRUtils::EXPR2VAR (x.m_return_var );
598
+ func_body += " \n return " + std::string (r_v->m_name ) + " ;\n " ;
599
+ }
582
600
return " {\n " + indent + var_decls + func_body + " }\n " ;
583
601
}
584
602
0 commit comments