Skip to content
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

Export of Python function to a .NET assembly file #503

Closed
vadimkantorov opened this issue May 25, 2023 · 5 comments
Closed

Export of Python function to a .NET assembly file #503

vadimkantorov opened this issue May 25, 2023 · 5 comments

Comments

@vadimkantorov
Copy link

Hy! My scenario is to use a rather simple Python function from .NET.

As far as I understood, this is what Pyjion does anyway: produces IL and saves a .NET assembly. Is it possible to obtain this assembly / clean way of calling it from C# / .NET? Does Pyjion provide an explicit API of exporting a Python function to a .NET assembly?

Thanks!

@vadimkantorov vadimkantorov changed the title Export of Python function to .NET assembly Export of Python function to a .NET assembly file May 25, 2023
@tonybaloney
Copy link
Owner

Hey

Unfortunately no, this isn't possible using Pyjion. But you can do it using this other project

https://pythonnet.github.io/pythonnet/dotnet.html

@vadimkantorov
Copy link
Author

vadimkantorov commented May 25, 2023

It seems that PythonNet is actually embedding CPython, right?

I was wondering whether the CIL / Pyjion-generated assemblies can be used from .NET as well (as it seems a cleaner solution if the original Python code is sufficiently simple)

Is it not possible because the interfacing with the generated assemblies is hard / needs extra runtime?

Because it seemed that if you generate assemblies anyway, some marshalling and function calling these assemblies from C#/.NET should be possible, no?

@vadimkantorov
Copy link
Author

vadimkantorov commented May 25, 2023

Should we just be able to dump m_il byte code to an assembly?

or maybe first create MethodInfo and then create an assembly just with one method?

a question: will pyjion jit methods called from the currently-jitted method? (including the python standard library)

An example of creating a minimal assembly from a MethodInfo seems here in the JitPacker/Unpacker: project https://github.com/LJP-TW/JITHook and slides deck: https://jsac.jpcert.or.jp/archive/2023/pdf/JSAC2023_1_4_chang_en.pdf

@tonybaloney
Copy link
Owner

I spent some time looking at the reverse. There are lots of issues to overcome.

Firstly, and most importantly, Pyjion only uses RyuJIT. We never initialise the managed .NET runtime. That in itself is a huge undertaking.
Secondly the object models are not compatible. Pyjion uses low level types for integers, floats, bool. Beyond that it doesn't implement any managed types/classes in CIL because they aren't compatible with Pythons objects, nor would there be any advantage to doing so.
Third, as you pointed out you need export more information into an assembly that can be imported and run from .NET. I started investigating it but decided it was too much work.

@vadimkantorov
Copy link
Author

I see, understand it better now. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants