@@ -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,13 @@ 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
+ return " \n " + CUtils::get_c_type_from_ttype_t (r_v->m_type ) + " " + std::string (r_v->m_name ) + " = " +
532
+ CUtils::get_py_obj_return_type_conv_func_from_ttype_t (r_v->m_type ) + " (pValue);" ;
533
+ }
534
+
529
535
std::string get_func_body_bind_python (const ASR::Function_t &x) {
530
536
user_headers.insert (" Python.h" );
531
537
std::string indent (indentation_level*indentation_spaces, ' ' );
@@ -540,8 +546,13 @@ R"(#include <stdio.h>
540
546
wchar_t* argv1 = Py_DecodeLocale("", NULL);
541
547
wchar_t** argv = {&argv1};
542
548
PySys_SetArgv(1, argv);
549
+
543
550
pName = PyUnicode_FromString(")" + std::string (x.m_module_file ) + R"( ");
544
- // TODO: check for error in pName
551
+ if (pName == NULL) {
552
+ PyErr_Print();
553
+ fprintf(stderr, "Failed to convert to unicode string )" + std::string (x.m_module_file ) + R"( \n");
554
+ exit(1);
555
+ }
545
556
546
557
pModule = PyImport_Import(pName);
547
558
Py_DECREF(pName);
@@ -559,9 +570,7 @@ R"(#include <stdio.h>
559
570
Py_DECREF(pModule);
560
571
exit(1);
561
572
}
562
-
563
- )" + get_arg_conv_bind_python (x) + R"(
564
-
573
+ )" + get_arg_conv_bind_python (x) + R"(
565
574
pValue = PyObject_CallObject(pFunc, pArgs);
566
575
Py_DECREF(pArgs);
567
576
if (pValue == NULL) {
@@ -571,14 +580,18 @@ R"(#include <stdio.h>
571
580
fprintf(stderr,"Call failed\n");
572
581
exit(1);
573
582
}
574
- // TODO: handle/convert the return value here
583
+ )" + get_return_value_conv_bind_python (x) + R"(
575
584
Py_DECREF(pValue);
576
585
577
586
if (Py_FinalizeEx() < 0) {
578
587
fprintf(stderr,"BindPython: Unknown Error\n");
579
588
exit(1);
580
589
}
581
590
)" ;
591
+ if (x.m_return_var ) {
592
+ auto r_v = ASRUtils::EXPR2VAR (x.m_return_var );
593
+ func_body += " \n return " + std::string (r_v->m_name ) + " ;\n " ;
594
+ }
582
595
return " {\n " + indent + var_decls + func_body + " }\n " ;
583
596
}
584
597
0 commit comments