Added support for multiple source files for py_loader
@@ -33,6 +33,7 @@ typedef struct loader_impl_py_type
PyObject * inspect_module;
PyObject * inspect_signature;
PyObject * builtins_module;
+ PyObject * main_module;
} *loader_impl_py;
@@ -446,8 +447,20 @@ loader_impl_data py_loader_impl_initialize(loader_impl impl)
if (py_loader_impl_initialize_inspect(impl, py_impl) == 0)
{
+ PyModuleDef module_def = { 0 };
+
+ module_def.m_name = "__metacall__";
+
+ PyObject* py_module_def = PyModuleDef_Init(&module_def);
+
+ py_impl->main_module = PyModule_Create(py_module_def);
+
+ Py_IncRef(py_impl->main_module);
+
+ if (py_impl->main_module != NULL) {
return py_impl;
}
+ }
free(py_impl);
}
@@ -476,15 +489,37 @@ int py_loader_impl_execution_path(loader_impl impl, const loader_naming_path pat
loader_handle py_loader_impl_load_from_files(loader_impl impl, const loader_naming_name name, const loader_naming_path path[], size_t size)
{
- PyObject * module_name = PyUnicode_DecodeFSDefault(name);
+ loader_impl_py py_impl = loader_impl_get(impl);
- PyObject * module = PyImport_Import(module_name);
+ PyObject* main_dict = PyModule_GetDict(py_impl->main_module);
- Py_DECREF(module_name);
+ loader_naming_name module_name;
- log_write("metacall", LOG_LEVEL_DEBUG, "Python loader (%p) importing %s from (%s) module at (%p)", (void *)impl, path[0], name, (void *)module);
+ char location_path[512] = { 0 };
- return (loader_handle)module;
+ for (size_t i = 0; i < size; i++)
+ {
+ loader_path_get_name(path[i], module_name);
+ PyObject* py_module_name = PyUnicode_DecodeFSDefault(module_name);
+
+ strncpy(location_path, path[i], strlen(path[i]) - (strlen(module_name) + 3));
+
+ /* ugly wave*/
+
+ PyObject * system_path = PySys_GetObject("path");
+ PyObject * current_path = PyUnicode_DecodeFSDefault(location_path);
+
+ PyList_Append(system_path, current_path);
+
+ Py_DECREF(current_path);
+
+ PyObject * module = PyImport_Import(py_module_name);
+ PyObject * module_dict = PyModule_GetDict(module);
+ PyDict_Merge(main_dict, module_dict, 0);
+ }
+
+
+ return (loader_handle)py_impl->main_module;
}
loader_handle py_loader_impl_load_from_memory(loader_impl impl, const loader_naming_name name, const loader_naming_extension extension, const char * buffer, size_t size)
@@ -592,7 +627,7 @@ int py_loader_impl_discover_func(loader_impl impl, PyObject * func, function f)
if (args != NULL)
{
PyTuple_SetItem(args, 0, func);
-
+ PyErr_Print();
result = PyObject_CallObject(py_impl->inspect_signature, args);
}
@@ -730,6 +765,8 @@ int py_loader_impl_destroy(loader_impl impl)
Py_DECREF(py_impl->builtins_module);
+ Py_DECREF(py_impl->main_module);
+
Py_Finalize();
free(py_impl);
