Skip to content

Commit b118921

Browse files
committed
Extract pycall_import_module and pycall_import_module_level
1 parent a8c262e commit b118921

File tree

2 files changed

+48
-34
lines changed

2 files changed

+48
-34
lines changed

ext/pycall/pycall.c

Lines changed: 45 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -601,12 +601,48 @@ pycall_libpython_helpers_m_unicode_literals_p(VALUE mod)
601601
return python_is_unicode_literals ? Qtrue : Qfalse;
602602
}
603603

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+
604641
static VALUE
605642
pycall_libpython_helpers_m_import_module(int argc, VALUE *argv, VALUE mod)
606643
{
607-
VALUE name, globals, locals, fromlist, level, rbmod;
644+
VALUE name, globals, locals, fromlist, level;
608645
char const *name_cstr;
609-
PyObject *pymod;
610646

611647
rb_scan_args(argc, argv, "14", &name, &globals, &locals, &fromlist, &level);
612648

@@ -617,42 +653,17 @@ pycall_libpython_helpers_m_import_module(int argc, VALUE *argv, VALUE mod)
617653
name_cstr = StringValueCStr(name);
618654

619655
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);
624657
}
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-
}
647658

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 */
652664
}
653665

654-
rbmod = pycall_pyobject_to_ruby(pymod);
655-
return rbmod;
666+
return pycall_import_module_level(name_cstr, globals, locals, fromlist, NUM2INT(level));
656667
}
657668

658669
static int

ext/pycall/pycall_internal.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -629,6 +629,9 @@ RUBY_EXTERN const rb_data_type_t pycall_pyptr_data_type;
629629
size_t pycall_pyptr_memsize(void const *);
630630
void pycall_pyptr_free(void *);
631631

632+
VALUE pycall_import_module(char const *name);
633+
VALUE pycall_import_module_level(char const *name, VALUE globals, VALUE locals, VALUE fromlist, int level);
634+
632635
VALUE pycall_pyobject_to_ruby(PyObject *);
633636
VALUE pycall_pytype_to_ruby(PyObject *);
634637
VALUE pycall_pymodule_to_ruby(PyObject *);

0 commit comments

Comments
 (0)