@@ -601,12 +601,48 @@ pycall_libpython_helpers_m_unicode_literals_p(VALUE mod)
601
601
return python_is_unicode_literals ? Qtrue : Qfalse ;
602
602
}
603
603
604
+ VALUE
605
+ pycall_import_module (char const * name )
606
+ {
607
+ PyObject * pymod = Py_API (PyImport_ImportModule )(name );
608
+ if (!pymod ) {
609
+ pycall_pyerror_fetch_and_raise ("PyImport_ImportModule in pycall_libpython_helpers_m_import_module" );
610
+ }
611
+ return pycall_pyobject_to_ruby (pymod );
612
+ }
613
+
614
+ VALUE
615
+ pycall_import_module_level (char const * name , VALUE globals , VALUE locals , VALUE fromlist , int level )
616
+ {
617
+ PyObject * pyglobals = NULL , * pylocals = NULL , * pyfromlist = NULL , * pymod ;
618
+
619
+ if (!NIL_P (globals )) {
620
+ pyglobals = check_get_pyobj_ptr (globals , Py_API (PyDict_Type ));
621
+ }
622
+ if (!NIL_P (locals )) {
623
+ pylocals = check_get_pyobj_ptr (locals , Py_API (PyDict_Type ));
624
+ }
625
+ if (!NIL_P (fromlist )) {
626
+ fromlist = rb_convert_type (fromlist , T_ARRAY , "Array" , "to_ary" );
627
+ pyfromlist = pycall_pyobject_from_ruby (fromlist );
628
+ }
629
+ else {
630
+ /* TODO: set the default fromlist to ['*'] */
631
+ }
632
+
633
+ pymod = Py_API (PyImport_ImportModuleLevel )(name , pyglobals , pylocals , pyfromlist , level );
634
+ if (!pymod ) {
635
+ pycall_pyerror_fetch_and_raise ("PyImport_ImportModuleLevel in pycall_libpython_helpers_m_import_module" );
636
+ }
637
+
638
+ return pycall_pyobject_to_ruby (pymod );
639
+ }
640
+
604
641
static VALUE
605
642
pycall_libpython_helpers_m_import_module (int argc , VALUE * argv , VALUE mod )
606
643
{
607
- VALUE name , globals , locals , fromlist , level , rbmod ;
644
+ VALUE name , globals , locals , fromlist , level ;
608
645
char const * name_cstr ;
609
- PyObject * pymod ;
610
646
611
647
rb_scan_args (argc , argv , "14" , & name , & globals , & locals , & fromlist , & level );
612
648
@@ -617,42 +653,17 @@ pycall_libpython_helpers_m_import_module(int argc, VALUE *argv, VALUE mod)
617
653
name_cstr = StringValueCStr (name );
618
654
619
655
if (argc == 1 ) {
620
- pymod = Py_API (PyImport_ImportModule )(name_cstr );
621
- if (!pymod ) {
622
- pycall_pyerror_fetch_and_raise ("PyImport_ImportModule in pycall_libpython_helpers_m_import_module" );
623
- }
656
+ return pycall_import_module (name_cstr );
624
657
}
625
- else {
626
- PyObject * pyglobals = NULL , * pylocals = NULL , * pyfromlist = NULL ;
627
-
628
- if (argc >= 2 && !NIL_P (globals )) {
629
- pyglobals = check_get_pyobj_ptr (globals , Py_API (PyDict_Type ));
630
- }
631
- if (argc >= 3 && !NIL_P (locals )) {
632
- pylocals = check_get_pyobj_ptr (locals , Py_API (PyDict_Type ));
633
- }
634
- if (argc >= 4 ) {
635
- fromlist = rb_convert_type (fromlist , T_ARRAY , "Array" , "to_ary" );
636
- pyfromlist = pycall_pyobject_from_ruby (fromlist );
637
- }
638
- else {
639
- /* TODO: set the default fromlist to ['*'] */
640
- }
641
- if (argc == 5 ) {
642
- level = rb_check_to_integer (level , "to_int" );
643
- }
644
- else {
645
- /* TODO: set the default level to 0 */
646
- }
647
658
648
- pymod = Py_API (PyImport_ImportModuleLevel )(name_cstr , pyglobals , pylocals , pyfromlist , NUM2INT (level ));
649
- if (!pymod ) {
650
- pycall_pyerror_fetch_and_raise ("PyImport_ImportModuleLevel in pycall_libpython_helpers_m_import_module" );
651
- }
659
+ if (argc == 5 ) {
660
+ level = rb_check_to_integer (level , "to_int" );
661
+ }
662
+ else {
663
+ /* TODO: set the default level to 0 */
652
664
}
653
665
654
- rbmod = pycall_pyobject_to_ruby (pymod );
655
- return rbmod ;
666
+ return pycall_import_module_level (name_cstr , globals , locals , fromlist , NUM2INT (level ));
656
667
}
657
668
658
669
static int
0 commit comments