Repository navigation
Convert projects to SDK style #1209
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
f811e51
Convert projects to SDK style
filmor 5607205
Drop CLSCompliant attributes to get rid of warnings
filmor fabe401
Add pyparser dependency to pyproject.toml
filmor 6f97bdd
Adjust clr loading and merge Mono code into one file
filmor ee990b7
Adjust workflows
filmor 640b497
Ignore test failing on Mono for now
filmor d75cdcc
Always run configure step if dotnet modules are present
filmor 6cb3fd9
Add solution items back
filmor deaf97b
Remove outdated comment
filmor 916b26a
Run AppVeyor tests without coverage for now
filmor 3ae7026
Split build_dotnet into its own command
filmor e3f8c20
Do the clr import later to not interfere with other test imports
filmor File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Adjust clr loading and merge Mono code into one file
- Loading branch information
commit 6f97bdd928e6c4c0d46cd10727f298e9c2361415
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| mono/ | ||
| netfx/ | ||
| runtime/ | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| def get_assembly_path(): | ||
| import os | ||
| return os.path.dirname(__file__) + "/runtime/Python.Runtime.dll" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,70 +1,223 @@ | ||
| #include "pynetclr.h" | ||
| // #define Py_LIMITED_API 0x03050000 | ||
| #include <Python.h> | ||
|
|
||
| /* List of functions defined in the module */ | ||
| static PyMethodDef clr_methods[] = { | ||
| {NULL, NULL, 0, NULL} /* Sentinel */ | ||
| }; | ||
| #include "stdlib.h" | ||
|
|
||
| PyDoc_STRVAR(clr_module_doc, | ||
| "clr facade module to initialize the CLR. It's later " | ||
| "replaced by the real clr module. This module has a facade " | ||
| "attribute to make it distinguishable from the real clr module." | ||
| ); | ||
| #define MONO_VERSION "v4.0.30319.1" | ||
| #define MONO_DOMAIN "Python" | ||
|
|
||
| static PyNet_Args *pn_args; | ||
| char **environ = NULL; | ||
|
|
||
| #if PY_MAJOR_VERSION >= 3 | ||
| static struct PyModuleDef clrdef = { | ||
| PyModuleDef_HEAD_INIT, | ||
| "clr", /* m_name */ | ||
| clr_module_doc, /* m_doc */ | ||
| -1, /* m_size */ | ||
| clr_methods, /* m_methods */ | ||
| NULL, /* m_reload */ | ||
| NULL, /* m_traverse */ | ||
| NULL, /* m_clear */ | ||
| NULL, /* m_free */ | ||
| }; | ||
| #include <mono/jit/jit.h> | ||
| #include <mono/metadata/environment.h> | ||
| #include <mono/metadata/mono-config.h> | ||
| #include <mono/metadata/debug-helpers.h> | ||
| #include <mono/metadata/assembly.h> | ||
|
|
||
| #ifndef _WIN32 | ||
| #include "dirent.h" | ||
| #include "dlfcn.h" | ||
| #include "libgen.h" | ||
| #include "alloca.h" | ||
| #endif | ||
|
|
||
| static PyObject *_initclr() | ||
| typedef struct | ||
| { | ||
| PyObject *m; | ||
| MonoDomain *domain; | ||
| MonoAssembly *pr_assm; | ||
| MonoMethod *shutdown; | ||
| const char *pr_file; | ||
| char *error; | ||
| char *init_name; | ||
| char *shutdown_name; | ||
| PyObject *module; | ||
| } PyNet_Args; | ||
|
|
||
| /* Create the module and add the functions */ | ||
| #if PY_MAJOR_VERSION >= 3 | ||
| m = PyModule_Create(&clrdef); | ||
| #else | ||
| m = Py_InitModule3("clr", clr_methods, clr_module_doc); | ||
| #endif | ||
| if (m == NULL) | ||
| return NULL; | ||
| PyModule_AddObject(m, "facade", Py_True); | ||
| Py_INCREF(Py_True); | ||
| PyNet_Args *PyNet_Init(void); | ||
| static PyNet_Args *pn_args; | ||
|
|
||
| pn_args = PyNet_Init(1); | ||
| PyMODINIT_FUNC | ||
| PyInit_clr(void) | ||
| { | ||
| pn_args = PyNet_Init(); | ||
| if (pn_args->error) | ||
| { | ||
| return NULL; | ||
| } | ||
|
|
||
| if (NULL != pn_args->module) | ||
| return pn_args->module; | ||
| return pn_args->module; | ||
| } | ||
|
|
||
| void PyNet_Finalize(PyNet_Args *); | ||
| void main_thread_handler(PyNet_Args *user_data); | ||
|
|
||
| // initialize Mono and PythonNet | ||
| PyNet_Args *PyNet_Init() | ||
| { | ||
| PyObject *pn_module; | ||
| PyObject *pn_path; | ||
| PyNet_Args *pn_args; | ||
| pn_args = (PyNet_Args *)malloc(sizeof(PyNet_Args)); | ||
|
|
||
| pn_module = PyImport_ImportModule("pythonnet"); | ||
| if (pn_module == NULL) | ||
| { | ||
| pn_args->error = "Failed to import pythonnet"; | ||
| return pn_args; | ||
| } | ||
|
|
||
| pn_path = PyObject_CallMethod(pn_module, "get_assembly_path", NULL); | ||
| if (pn_path == NULL) | ||
| { | ||
| Py_DecRef(pn_module); | ||
| pn_args->error = "Failed to get assembly path"; | ||
| return pn_args; | ||
| } | ||
|
|
||
| return m; | ||
| pn_args->pr_file = PyUnicode_AsUTF8(pn_path); | ||
| pn_args->error = NULL; | ||
| pn_args->shutdown = NULL; | ||
| pn_args->module = NULL; | ||
|
|
||
| #ifdef __linux__ | ||
| // Force preload libmono-2.0 as global. Without this, on some systems | ||
| // symbols from libmono are not found by libmononative (which implements | ||
| // some of the System.* namespaces). Since the only happened on Linux so | ||
| // far, we hardcode the library name, load the symbols into the global | ||
| // namespace and leak the handle. | ||
| dlopen("libmono-2.0.so", RTLD_LAZY | RTLD_GLOBAL); | ||
| #endif | ||
|
|
||
| pn_args->init_name = "Python.Runtime:InitExt()"; | ||
| pn_args->shutdown_name = "Python.Runtime:Shutdown()"; | ||
|
|
||
| pn_args->domain = mono_jit_init_version(MONO_DOMAIN, MONO_VERSION); | ||
|
|
||
| // XXX: Skip setting config for now, should be derived from pr_file | ||
| // mono_domain_set_config(pn_args->domain, ".", "Python.Runtime.dll.config"); | ||
|
|
||
| /* | ||
| * Load the default Mono configuration file, this is needed | ||
| * if you are planning on using the dllmaps defined on the | ||
| * system configuration | ||
| */ | ||
| mono_config_parse(NULL); | ||
|
|
||
| /* I can't use this call to run the main_thread_handler. The function | ||
| * runs it in another thread but *this* thread holds the Python | ||
| * import lock -> DEAD LOCK. | ||
| * | ||
| * mono_runtime_exec_managed_code(pn_args->domain, main_thread_handler, | ||
| * pn_args); | ||
| */ | ||
|
filmor marked this conversation as resolved.
Resolved
|
||
|
|
||
| main_thread_handler(pn_args); | ||
|
|
||
| if (pn_args->error != NULL) | ||
| { | ||
| PyErr_SetString(PyExc_ImportError, pn_args->error); | ||
| } | ||
| return pn_args; | ||
| } | ||
|
|
||
| #if PY_MAJOR_VERSION >= 3 | ||
| PyMODINIT_FUNC | ||
| PyInit_clr(void) | ||
| char *PyNet_ExceptionToString(MonoObject *e); | ||
|
|
||
| // Shuts down PythonNet and cleans up Mono | ||
| void PyNet_Finalize(PyNet_Args *pn_args) | ||
| { | ||
| return _initclr(); | ||
| MonoObject *exception = NULL; | ||
|
|
||
| if (pn_args->shutdown) | ||
| { | ||
| mono_runtime_invoke(pn_args->shutdown, NULL, NULL, &exception); | ||
| if (exception) | ||
| { | ||
| pn_args->error = PyNet_ExceptionToString(exception); | ||
| } | ||
| pn_args->shutdown = NULL; | ||
| } | ||
|
|
||
| if (pn_args->domain) | ||
| { | ||
| mono_jit_cleanup(pn_args->domain); | ||
| pn_args->domain = NULL; | ||
| } | ||
| free(pn_args); | ||
| } | ||
| #else | ||
| PyMODINIT_FUNC | ||
| initclr(void) | ||
|
|
||
| MonoMethod *getMethodFromClass(MonoClass *cls, char *name) | ||
| { | ||
| _initclr(); | ||
| MonoMethodDesc *mdesc; | ||
| MonoMethod *method; | ||
|
|
||
| mdesc = mono_method_desc_new(name, 1); | ||
| method = mono_method_desc_search_in_class(mdesc, cls); | ||
| mono_method_desc_free(mdesc); | ||
|
|
||
| return method; | ||
| } | ||
|
|
||
| void main_thread_handler(PyNet_Args *user_data) | ||
| { | ||
| PyNet_Args *pn_args = user_data; | ||
| MonoMethod *init; | ||
| MonoImage *pr_image; | ||
| MonoClass *pythonengine; | ||
| MonoObject *exception = NULL; | ||
| MonoObject *init_result; | ||
|
|
||
| pn_args->pr_assm = mono_domain_assembly_open(pn_args->domain, pn_args->pr_file); | ||
| if (!pn_args->pr_assm) | ||
| { | ||
| pn_args->error = "Unable to load assembly"; | ||
| return; | ||
| } | ||
|
|
||
| pr_image = mono_assembly_get_image(pn_args->pr_assm); | ||
| if (!pr_image) | ||
| { | ||
| pn_args->error = "Unable to get image"; | ||
| return; | ||
| } | ||
|
|
||
| pythonengine = mono_class_from_name(pr_image, "Python.Runtime", "PythonEngine"); | ||
| if (!pythonengine) | ||
| { | ||
| pn_args->error = "Unable to load class PythonEngine from Python.Runtime"; | ||
| return; | ||
| } | ||
|
|
||
| init = getMethodFromClass(pythonengine, pn_args->init_name); | ||
| if (!init) | ||
| { | ||
| pn_args->error = "Unable to fetch Init method from PythonEngine"; | ||
| return; | ||
| } | ||
|
|
||
| pn_args->shutdown = getMethodFromClass(pythonengine, pn_args->shutdown_name); | ||
| if (!pn_args->shutdown) | ||
| { | ||
| pn_args->error = "Unable to fetch shutdown method from PythonEngine"; | ||
| return; | ||
| } | ||
|
|
||
| init_result = mono_runtime_invoke(init, NULL, NULL, &exception); | ||
| if (exception) | ||
| { | ||
| pn_args->error = PyNet_ExceptionToString(exception); | ||
| return; | ||
| } | ||
|
|
||
| pn_args->module = *(PyObject**)mono_object_unbox(init_result); | ||
| } | ||
|
|
||
| // Get string from a Mono exception | ||
| char *PyNet_ExceptionToString(MonoObject *e) | ||
| { | ||
| MonoMethodDesc *mdesc = mono_method_desc_new(":ToString()", 0 /*FALSE*/); | ||
| MonoMethod *mmethod = mono_method_desc_search_in_class(mdesc, mono_get_object_class()); | ||
| mono_method_desc_free(mdesc); | ||
|
|
||
| mmethod = mono_object_get_virtual_method(e, mmethod); | ||
| MonoString *monoString = (MonoString*) mono_runtime_invoke(mmethod, e, NULL, NULL); | ||
| mono_runtime_invoke(mmethod, e, NULL, NULL); | ||
| return mono_string_to_utf8(monoString); | ||
| } | ||
| #endif | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.