@@ -503,16 +503,15 @@ R"(#include <stdio.h>
503503    }
504504
505505    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 );
508506
509507        std::string arg_conv = R"( 
510508    pArgs = PyTuple_New()"   + std::to_string (x.n_args ) + R"( );
511509)" 
512510        for  (size_t  i = 0 ; i < x.n_args ; ++i) {
511+             ASR::Variable_t *arg = ASRUtils::EXPR2VAR (x.m_args [i]);
513512            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"( );
516515    if (!pValue) { 
517516        Py_DECREF(pArgs); 
518517        Py_DECREF(pModule); 
@@ -526,6 +525,13 @@ R"(#include <stdio.h>
526525        return  arg_conv;
527526    }
528527
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+ 
529535    std::string get_func_body_bind_python (const  ASR::Function_t &x) {
530536        user_headers.insert (" Python.h" 
531537        std::string indent (indentation_level*indentation_spaces, '  ' 
@@ -540,8 +546,13 @@ R"(#include <stdio.h>
540546    wchar_t* argv1 = Py_DecodeLocale("", NULL); 
541547    wchar_t** argv = {&argv1}; 
542548    PySys_SetArgv(1, argv); 
549+ 
543550    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+     } 
545556
546557    pModule = PyImport_Import(pName); 
547558    Py_DECREF(pName); 
@@ -559,9 +570,7 @@ R"(#include <stdio.h>
559570        Py_DECREF(pModule); 
560571        exit(1); 
561572    } 
562- 
563-     )"   + get_arg_conv_bind_python (x) + R"( 
564- 
573+ )" get_arg_conv_bind_python (x) + R"( 
565574    pValue = PyObject_CallObject(pFunc, pArgs); 
566575    Py_DECREF(pArgs); 
567576    if (pValue == NULL) { 
@@ -571,14 +580,18 @@ R"(#include <stdio.h>
571580        fprintf(stderr,"Call failed\n"); 
572581        exit(1); 
573582    } 
574-     // TODO: handle/convert the return value here 
583+ )"  +  get_return_value_conv_bind_python (x) +  R"( 
575584    Py_DECREF(pValue); 
576585
577586    if (Py_FinalizeEx() < 0) { 
578587        fprintf(stderr,"BindPython: Unknown Error\n"); 
579588        exit(1); 
580589    } 
581590)" 
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+         }
582595        return  " {\n " " }\n " 
583596    }
584597
0 commit comments