Thanks to visit codestin.com
Credit goes to github.com

Skip to content

pypy support in pythonnet #330

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

Closed
den-run-ai opened this issue Jan 22, 2017 · 43 comments
Closed

pypy support in pythonnet #330

den-run-ai opened this issue Jan 22, 2017 · 43 comments
Assignees

Comments

@den-run-ai
Copy link
Contributor

den-run-ai commented Jan 22, 2017

cpyext is mature enough (numpy, cython, scipy, pandas soon) to support pythonnet in pypy. In fact pypy used to have a branch which ran on top of CLR (similar to IronPython), but this branch is not maintained.

The use of pypy in .NET is quite popular request:

http://stackoverflow.com/questions/6084697/is-it-possible-to-embed-pypy-into-a-net-application

The only limitation of pypy is 32-bit on Windows and poor support for Python 3+, although the latter is improving with funding from Mozilla.

Also this might be quite nice collaboration with pypy developers due to their level of expertise in python C-API and cffi.

@den-run-ai
Copy link
Contributor Author

@vmuriart
Copy link
Contributor

vmuriart commented Feb 9, 2017

src/monoclr/pynetinit.c: In functionmain_thread_handler’:
src/monoclr/pynetinit.c:106:5: error: unknown type nameDl_infosrc/monoclr/pynetinit.c:107:5: warning: implicit declaration of functiondladdr’ [-Wimplicit-function-declaration]
src/monoclr/pynetinit.c:107:22: error: ‘Py_Initializeundeclared (first use in this function)
src/monoclr/pynetinit.c:107:22: note: each undeclared identifier is reported only once for each function it appears in
src/monoclr/pynetinit.c:109:23: error: request for memberdli_fnamein something not a structure or union
src/monoclr/pynetinit.c:110:29: error: request for memberdli_fnamein something not a structure or union

above is the traceback from building on pypi/travis

@den-run-ai
Copy link
Contributor Author

den-run-ai commented Feb 10, 2017

I tried on pypy with pythonnet on Win10. It builds fine, but then fails to find clr during import.

C:\Python\pypy2-v5.6.0-win32>bin\pip install pythonnet
Collecting pythonnet
  Downloading pythonnet-2.2.2.tar.gz (1.5MB)
    100% |################################| 1.5MB 480kB/s
Building wheels for collected packages: pythonnet
  Running setup.py bdist_wheel for pythonnet ... done
  Stored in directory: C:\Users\denis\AppData\Local\pip\Cache\wheels\14\da\65\1042e21167805ad08c6319a87c9cd6b2e6e97ff828aa37eb3d
Successfully built pythonnet
Installing collected packages: pythonnet
Successfully installed pythonnet-2.2.2

C:\Python\pypy2-v5.6.0-win32>pypy
Python 2.7.12 (aff251e54385, Nov 09 2016, 17:54:55)
[PyPy 5.6.0 with MSC v.1500 32 bit] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>> import clr
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named clr
>>>>

@vmuriart
Copy link
Contributor

interesting. you tried building using the debug configuration? Im curious if the clrmodule is the one failing to find python.runtime.dll or if its not even finding the clrmodule.

I've don't know if the error is the same or not in either case.

@den-run-ai
Copy link
Contributor Author

can I pass debug flag to pip/setuptools (not from VS)?

@vmuriart
Copy link
Contributor

its on my todo list to add a cli option. currently you need to update setup.py and it will build on debug

@den-run-ai
Copy link
Contributor Author

still the same error, here is the full output from the build:

buildoutput.txt

@den-run-ai
Copy link
Contributor Author

@vmuriart
Copy link
Contributor

@den-run-ai
Copy link
Contributor Author

@vmuriart yes, that's possible that we are using some unsupported C-API features in cpyext. But it fails silently. I'm trying with nightly pypy builds:

http://buildbot.pypy.org/nightly/trunk/

@den-run-ai
Copy link
Contributor Author

@den-run-ai
Copy link
Contributor Author

same issue on nightly pypy build:

c:\Python\pypy
λ bin\pip2 install pythonnet
Collecting pythonnet
  Using cached pythonnet-2.2.2.tar.gz
Building wheels for collected packages: pythonnet
  Running setup.py bdist_wheel for pythonnet ... done
  Stored in directory: C:\Users\denis\AppData\Local\pip\Cache\wheels\14\da\65\1042e21167805ad08c6319a87c9cd6b2e6e97ff828aa37eb3d
Successfully built pythonnet
Installing collected packages: pythonnet
Successfully installed pythonnet-2.2.2

c:\Python\pypy
λ pypy
Python 2.7.13 (4a7ae8e412f5, Feb 01 2017, 22:52:31)
[PyPy 5.7.0-alpha0 with MSC v.1500 32 bit] on win32
Type "help", "copyright", "credits" or "license" for more information.
And now for something completely different: ``poststructuralism etc.''
>>>> import clr
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named clr

@arigo
Copy link

arigo commented Feb 10, 2017

I see that it creates and installs clr.pyd even on PyPy. This file name is wrong: it should be called clr.pypy-41.pyd. The normal distutils creates files of the correct name, but I guess a custom setup.py could hard-code clr.pyd.

After renaming it manually, it fails differently, which is probably another issue.

@den-run-ai
Copy link
Contributor Author

@arigo thanks, so now clr is looking for python27.dll, which I guess is libpypy-c.dll:

>>>> import clr

Unhandled Exception: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.DllNotFoundException: Unable to load DLL 'python27': The specified module could not be found. (Exception from HRESULT: 0x8007007E)
   at Python.Runtime.Runtime.Py_IsInitialized()
   at Python.Runtime.Runtime.Initialize()
   at Python.Runtime.PythonEngine.Initialize(IEnumerable`1 args)
   at Python.Runtime.PythonEngine.Initialize()
   at Python.Runtime.PythonEngine.InitExt()
   --- End of inner exception stack trace ---
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
   at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at System.RuntimeType.InvokeMember(String name, BindingFlags bindingFlags, Binder binder, Object target, Object[] providedArgs, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParams)
   at clrModule.initclr()

Then I created a "soft" symlink mklink Python27.dll libpypy-c.dll, but now clr cannot find Py_IsInitialized from Python C-API in libpypy-c.dll:

Python 2.7.12 (aff251e54385, Nov 09 2016, 17:54:55)
[PyPy 5.6.0 with MSC v.1500 32 bit] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>> import clr

Unhandled Exception: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.EntryPointNotFoundException: Unable to find an entry point named 'Py_IsInitialized' in DLL 'python27'.
   at Python.Runtime.Runtime.Py_IsInitialized()
   at Python.Runtime.Runtime.Initialize()
   at Python.Runtime.PythonEngine.Initialize(IEnumerable`1 args)
   at Python.Runtime.PythonEngine.Initialize()
   at Python.Runtime.PythonEngine.InitExt()
   --- End of inner exception stack trace ---
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
   at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at System.RuntimeType.InvokeMember(String name, BindingFlags bindingFlags, Binder binder, Object target, Object[] providedArgs, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParams)
   at clrModule.initclr()

the loaded libraries look like expected:

image

@arigo
Copy link

arigo commented Feb 10, 2017

libpypy-c.dll exposes the symbols under names PyPy_Xxx instead of Py_Xxx, to avoid conflicts in other situations. The normal header Python.h distributed with PyPy contains #define to do the renaming transparently. It seems that the CLR-compiled Python.Runtime.dll looks up the original names anyway, probably because it was not compiled with Python.h but instead with its own headers (rewritten in .NET maybe?).

@den-run-ai
Copy link
Contributor Author

den-run-ai commented Feb 10, 2017

OMG, now we need two sets of names for few hundred C-API pinvokes in C# code:

Click to expand the search results ```

Find all "internal.\n.Py._", Regular expressions, Find Results 1, Current Document
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(341): internal static IntPtr Py_single_input = (IntPtr)256;
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(342): internal static IntPtr Py_file_input = (IntPtr)257;
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(366): internal static IntPtr PyBytesType;
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(370): internal static IntPtr PyNotImplemented;
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(371): internal const int Py_LT = 0;
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(372): internal const int Py_LE = 1;
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(373): internal const int Py_EQ = 2;
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(374): internal const int Py_NE = 3;
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(375): internal const int Py_GT = 4;
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(595): internal unsafe static extern void
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(600): internal unsafe static extern int
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(605): internal unsafe static extern void
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(610): internal unsafe static extern IntPtr
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(615): internal unsafe static extern void
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(620): internal unsafe static extern IntPtr
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(625): internal unsafe static extern IntPtr
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(630): internal unsafe static extern IntPtr
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(635): internal unsafe static extern int
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(640): internal unsafe static extern int
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(645): internal unsafe static extern IntPtr
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(651): internal unsafe static extern IntPtr
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(656): internal unsafe static extern void
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(662): internal unsafe static extern IntPtr
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(679): internal unsafe static extern void
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(684): internal unsafe static extern int
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(689): internal unsafe static extern void
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(694): internal unsafe static extern void
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(699): internal unsafe static extern void
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(704): internal unsafe static extern void
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(709): internal unsafe static extern IntPtr
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(714): internal unsafe static extern void
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(719): internal unsafe static extern IntPtr
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(724): internal unsafe static extern IntPtr
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(729): internal unsafe static extern IntPtr
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(737): internal unsafe static extern string
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(742): internal unsafe static extern void
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(748): internal unsafe static extern string
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(753): internal unsafe static extern void
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(759): internal unsafe static extern string
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(764): internal unsafe static extern void
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(769): internal unsafe static extern string
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(774): internal unsafe static extern void
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(779): internal unsafe static extern string
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(784): internal unsafe static extern void
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(789): internal unsafe static extern string
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(794): internal unsafe static extern void
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(800): internal unsafe static extern string
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(805): internal unsafe static extern string
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(810): internal unsafe static extern string
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(815): internal unsafe static extern string
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(820): internal unsafe static extern string
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(825): internal unsafe static extern int
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(830): internal unsafe static extern IntPtr
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(835): internal unsafe static extern IntPtr
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(840): internal unsafe static extern IntPtr
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(845): internal unsafe static extern IntPtr
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(850): internal unsafe static extern IntPtr
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(855): internal unsafe static extern IntPtr
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(860): internal unsafe static extern IntPtr
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(865): internal unsafe static extern IntPtr
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(870): internal unsafe static extern IntPtr
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(883): internal unsafe static IntPtr
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(911): internal unsafe static IntPtr
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(928): internal unsafe static extern int
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(933): internal unsafe static extern IntPtr
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(938): internal unsafe static extern int
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(943): internal unsafe static extern int
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(948): internal unsafe static extern IntPtr
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(953): internal unsafe static extern int
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(958): internal unsafe static extern IntPtr
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(963): internal unsafe static extern int
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(968): internal unsafe static extern int
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(973): internal unsafe static extern IntPtr
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(978): internal unsafe static extern IntPtr
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(983): internal unsafe static extern IntPtr
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(989): internal unsafe static extern int
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(1019): internal unsafe static extern int
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(1026): internal unsafe static extern int
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(1031): internal unsafe static extern int
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(1036): internal unsafe static extern int
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(1041): internal unsafe static extern int
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(1046): internal unsafe static extern int
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(1051): internal unsafe static extern int
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(1056): internal unsafe static extern IntPtr
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(1061): internal unsafe static extern IntPtr
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(1066): internal unsafe static extern IntPtr
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(1073): internal unsafe static extern IntPtr
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(1078): internal unsafe static extern IntPtr
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(1084): internal unsafe static extern IntPtr
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(1096): internal unsafe static extern IntPtr
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(1102): internal unsafe static extern IntPtr
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(1108): internal unsafe static extern IntPtr
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(1113): internal unsafe static extern IntPtr
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(1119): internal unsafe static extern bool
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(1155): internal unsafe static extern int
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(1161): internal unsafe static extern IntPtr
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(1167): internal unsafe static extern int
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(1178): internal unsafe static extern int
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(1183): internal unsafe static extern IntPtr
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(1188): internal unsafe static extern int
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(1199): internal unsafe static extern IntPtr
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(1204): internal unsafe static extern IntPtr
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(1209): internal unsafe static extern IntPtr
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(1214): internal unsafe static extern IntPtr
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(1219): internal unsafe static extern IntPtr
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(1224): internal unsafe static extern IntPtr
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(1229): internal unsafe static extern int
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(1234): internal unsafe static extern uint
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(1239): internal unsafe static extern long
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(1244): internal unsafe static extern ulong
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(1255): internal unsafe static extern IntPtr
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(1260): internal unsafe static extern IntPtr
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(1265): internal unsafe static extern double
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(1270): internal unsafe static extern IntPtr
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(1275): internal unsafe static extern IntPtr
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(1280): internal unsafe static extern IntPtr
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(1285): internal unsafe static extern IntPtr
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(1290): internal unsafe static extern IntPtr
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(1295): internal unsafe static extern IntPtr
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(1300): internal unsafe static extern IntPtr
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(1305): internal unsafe static extern IntPtr
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(1310): internal unsafe static extern IntPtr
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(1315): internal unsafe static extern IntPtr
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(1320): internal unsafe static extern IntPtr
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(1325): internal unsafe static extern IntPtr
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(1330): internal unsafe static extern IntPtr
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(1335): internal unsafe static extern IntPtr
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(1340): internal unsafe static extern IntPtr
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(1345): internal unsafe static extern IntPtr
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(1350): internal unsafe static extern IntPtr
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(1355): internal unsafe static extern IntPtr
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(1360): internal unsafe static extern IntPtr
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(1365): internal unsafe static extern IntPtr
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(1370): internal unsafe static extern IntPtr
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(1375): internal unsafe static extern IntPtr
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(1380): internal unsafe static extern IntPtr
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(1385): internal unsafe static extern IntPtr
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(1390): internal unsafe static extern IntPtr
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(1399): internal unsafe static extern bool
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(1404): internal unsafe static extern IntPtr
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(1409): internal unsafe static extern int
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(1414): internal unsafe static extern int
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(1419): internal unsafe static extern IntPtr
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(1424): internal unsafe static extern int
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(1429): internal unsafe static extern int
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(1434): internal unsafe static extern int
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(1439): internal unsafe static extern int
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(1444): internal unsafe static extern IntPtr
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(1449): internal unsafe static extern IntPtr
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(1454): internal unsafe static extern int
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(1459): internal unsafe static extern int
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(1464): internal unsafe static extern IntPtr
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(1469): internal unsafe static extern IntPtr
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(1496): internal unsafe static extern IntPtr
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(1501): internal unsafe static extern int
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(1529): internal unsafe static extern IntPtr
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(1534): internal unsafe static extern IntPtr
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(1540): internal unsafe static extern IntPtr
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(1545): internal unsafe static extern int
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(1557): internal unsafe static extern IntPtr
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(1562): internal unsafe static extern IntPtr
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(1569): internal unsafe static extern IntPtr
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(1579): internal unsafe static extern int
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(1584): internal unsafe static extern char

C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(1590): internal unsafe static extern IntPtr
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(1595): internal unsafe static extern IntPtr
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(1601): internal unsafe static extern IntPtr
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(1607): internal unsafe static extern IntPtr
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(1613): internal unsafe static extern IntPtr
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(1619): internal unsafe static extern int
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(1625): internal unsafe static extern char*
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(1631): internal unsafe static extern IntPtr
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(1637): internal unsafe static extern IntPtr
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(1642): internal unsafe static extern IntPtr
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(1647): internal unsafe static extern IntPtr
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(1653): internal unsafe static extern IntPtr
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(1688): internal unsafe static extern int
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(1693): internal unsafe static extern IntPtr
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(1699): internal unsafe static extern IntPtr
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(1704): internal unsafe static extern IntPtr
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(1710): internal unsafe static extern IntPtr
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(1716): internal unsafe static extern IntPtr
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(1722): internal unsafe static extern IntPtr
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(1749): internal unsafe static extern int
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(1755): internal unsafe static extern IntPtr
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(1761): internal unsafe static extern IntPtr
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(1767): internal unsafe static extern IntPtr
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(1820): internal unsafe static extern IntPtr
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(1825): internal unsafe static extern IntPtr
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(1830): internal unsafe static extern IntPtr
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(1835): internal unsafe static extern IntPtr
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(1840): internal unsafe static extern int
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(1845): internal unsafe static extern int
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(1850): internal unsafe static extern int
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(1855): internal unsafe static extern int
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(1860): internal unsafe static extern int
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(1865): internal unsafe static extern IntPtr
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(1870): internal unsafe static extern IntPtr
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(1875): internal unsafe static extern IntPtr
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(1880): internal unsafe static extern IntPtr
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(1885): internal unsafe static extern int
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(1890): internal unsafe static extern void
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(1895): internal unsafe static extern int
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(1910): internal unsafe static extern IntPtr
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(1915): internal unsafe static extern IntPtr
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(1920): internal unsafe static extern IntPtr
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(1925): internal unsafe static extern int
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(1930): internal unsafe static extern int
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(1935): internal unsafe static extern int
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(1940): internal unsafe static extern int
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(1945): internal unsafe static extern int
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(1950): internal unsafe static extern IntPtr
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(1955): internal unsafe static extern int
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(1960): internal unsafe static extern int
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(1975): internal unsafe static extern IntPtr
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(1980): internal unsafe static extern IntPtr
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(1985): internal unsafe static extern int
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(1990): internal unsafe static extern IntPtr
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(1995): internal unsafe static extern int
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(2006): internal unsafe static extern bool
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(2009): internal static bool
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(2020): internal unsafe static extern IntPtr
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(2029): internal unsafe static extern IntPtr
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(2034): internal unsafe static extern string
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(2039): internal unsafe static extern IntPtr
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(2044): internal unsafe static extern string
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(2050): internal unsafe static extern IntPtr
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(2056): internal unsafe static extern IntPtr
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(2061): internal unsafe static extern IntPtr
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(2066): internal unsafe static extern IntPtr
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(2071): internal unsafe static extern IntPtr
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(2076): internal unsafe static extern IntPtr
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(2082): internal unsafe static extern void
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(2091): internal unsafe static extern void
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(2101): internal unsafe static extern IntPtr
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(2106): internal unsafe static extern int
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(2121): internal unsafe static extern void
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(2126): internal unsafe static extern bool
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(2137): internal unsafe static extern IntPtr
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(2142): internal unsafe static extern IntPtr
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(2147): internal unsafe static extern int
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(2152): internal unsafe static extern IntPtr
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(2157): internal unsafe static extern IntPtr
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(2162): internal unsafe static extern int
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(2167): internal unsafe static extern IntPtr
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(2172): internal unsafe static extern IntPtr
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(2177): internal unsafe static extern void
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(2182): internal unsafe static extern void
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(2187): internal unsafe static extern void
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(2197): internal unsafe static extern IntPtr
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(2202): internal unsafe static extern IntPtr
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(2207): internal unsafe static extern void
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(2217): internal unsafe static extern void
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(2222): internal unsafe static extern void
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(2227): internal unsafe static extern IntPtr
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(2232): internal unsafe static extern void
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(2237): internal unsafe static extern int
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(2242): internal unsafe static extern int
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(2247): internal unsafe static extern void
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(2252): internal unsafe static extern int
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(2257): internal unsafe static extern void
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(2262): internal unsafe static extern void
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(2267): internal unsafe static extern void
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(2272): internal unsafe static extern void
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(2282): internal unsafe static extern IntPtr
C:\Python\pypy2-v5.6.0-win32\pythonnet\src\runtime\runtime.cs(2287): internal unsafe static extern IntPtr
Matching lines: 266


</details>

@filmor
Copy link
Member

filmor commented Feb 10, 2017

Yeah, it's a bit of a pain :/. We could have two runtime.cs modules, though, switching them at runtime depending on the Python we run against.

@tonyroberts
Copy link
Contributor

Maybe a subset of runtime.cs could be auto-generated by parsing Python.h, similar to how the interopXX.cs files are generated?

@arigo
Copy link

arigo commented Feb 10, 2017

Note, before going to lengths in that direction, that PyPy's emulation of the CPython C extension API is not 100% complete. For example, Py_Initialize() is not implemented (it does not support "embedding", only "extending"). I just noticed it inside one of the error messages pasted above.

@den-run-ai
Copy link
Contributor Author

den-run-ai commented Feb 10, 2017

using this regex I replaced all Py*_ calls to PyPy*_ calls.

image

I had to remove UCS2/UCS4 calling convention from Unicode objects as well.

I ignored the call to Py_Initialize() for extending case with try/catch (this works in CPython), but is there any alternative method to initialize PyPy runtime when embedding it? I guess in the worst case we could resort to cffi embedding API?

http://cffi.readthedocs.io/en/latest/embedding.html

Anyway with extending (import clr) I'm now getting this error:

C:\Python\pypy2-v5.6.0-win32>pypy
Python 2.7.12 (aff251e54385, Nov 09 2016, 17:54:55)
[PyPy 5.6.0 with MSC v.1500 32 bit] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>> import clr
Attempting to load 'Python.Runtime' using standard binding rules.
'Python.Runtime' not found using standard binding rules.
Attempting to load Python.Runtime from: 'C:\Python\pypy2-v5.6.0-win32\site-packages\Python.Runtime.dll'.
Success loading 'Python.Runtime' from: 'C:\Python\pypy2-v5.6.0-win32\site-packages\Python.Runtime.dll'.

Unhandled Exception: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.NullReferenceException: Object reference not set to an instance of an object.
   at Python.Runtime.Runtime.XDecref(IntPtr op)
   at Python.Runtime.Runtime.Initialize()
   at Python.Runtime.PythonEngine.Initialize(IEnumerable`1 args)
   at Python.Runtime.PythonEngine.Initialize()
   at Python.Runtime.PythonEngine.InitExt()
   --- End of inner exception stack trace ---
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
   at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at System.RuntimeType.InvokeMember(String name, BindingFlags bindingFlags, Binder binder, Object target, Object[] providedArgs, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParams)
   at System.Type.InvokeMember(String name, BindingFlags invokeAttr, Binder binder, Object target, Object[] args)
   at clrModule.initclr()

@den-run-ai
Copy link
Contributor Author

@arigo are there debug builds of pypy with PDB symbols anywhere? i read it takes hours to compile pypy locally.

@vmuriart
Copy link
Contributor

@filmor @denfromufa this looks like a good example to use @dmitriyse 's implementation of separating interop from runtime. I have a WIP branch of his work.

Since it uses interfaces for defining runtime, we can easily create a implementation for pypy

@arigo
Copy link

arigo commented Feb 10, 2017

Let me guess, if you don't read Python.h, then the .NET code also missed the fact that the base PyObject structure is different (and so, all others too). PyPy is really not meant to be an ABI-compatible replacement for CPython---only an API-compatible one. Another guess: for performance reasons the .NET code contains explicit versions of various CPython macros rewritten as .NET code. Sorry, that can't work either: either the macros are different, or they are implemented as full function calls.

@den-run-ai
Copy link
Contributor Author

@arigo you are right, but pythonnet does not use too many internal CPython structures. I believe we can port all of those structures either manually or using bundled geninterop.py that uses clang and pycparser. The only macro that was ported to C# which I know is Decref/Incref.

@den-run-ai
Copy link
Contributor Author

here is the pypy + pythonnet with my edits if anyone wants to play with or start a new branch:

https://www.dropbox.com/s/zevjlsfe9mk1pdg/pypy2-v5.6.0-win32.zip?dl=0

@den-run-ai
Copy link
Contributor Author

@arigo @tonyroberts I think Decref/Incref and PyPyObject_Type macros are resolved. Now I'm dealing with this issue in pycparser for parsing PyHeapTypeObject and all its sub-structs:

eliben/pycparser#162 (comment)

If this is not resolved, then will need to regenerate PyHeapTypeObject from Python.h manually.

Here is the current crash state in pypy with import clr:

Unhandled Exception: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
   at Python.Runtime.Runtime.PyPyType_Ready(IntPtr type)
   at Python.Runtime.TypeManager.CreateMetaType(Type impl)
   at Python.Runtime.MetaType.Initialize()
   at Python.Runtime.Runtime.Initialize()
   at Python.Runtime.PythonEngine.Initialize(IEnumerable`1 args)
   at Python.Runtime.PythonEngine.Initialize()
   at Python.Runtime.PythonEngine.InitExt()
   --- End of inner exception stack trace ---
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
   at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at System.RuntimeType.InvokeMember(String name, BindingFlags bindingFlags, Binder binder, Object target, Object[] providedArgs, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParams)
   at System.Type.InvokeMember(String name, BindingFlags invokeAttr, Binder binder, Object target, Object[] args)
   at clrModule.initclr()

@den-run-ai
Copy link
Contributor Author

PyObject_HEAD contains extra Py_ssize_t ob_pypy_link, which I added to PyHeapTypeObject:

image

Now, next error:

Unhandled Exception: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
   at Python.Runtime.Runtime.PyPyType_IsSubtype(IntPtr t1, IntPtr t2)
   at Python.Runtime.Runtime.PyPyObject_TypeCheck(IntPtr ob, IntPtr tp)
   at Python.Runtime.ObjectOffset.Size(IntPtr ob)
   at Python.Runtime.TypeManager.CreateType(Type impl)
   at Python.Runtime.TypeManager.GetTypeHandle(Type type)
   at Python.Runtime.ExtensionType..ctor()
   at Python.Runtime.ModuleObject..ctor(String name)
   at Python.Runtime.CLRModule..ctor()
   at Python.Runtime.ImportHook.Initialize()
   at Python.Runtime.Runtime.Initialize()
   at Python.Runtime.PythonEngine.Initialize(IEnumerable`1 args)
   at Python.Runtime.PythonEngine.Initialize()
   at Python.Runtime.PythonEngine.InitExt()
   --- End of inner exception stack trace ---
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
   at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at System.RuntimeType.InvokeMember(String name, BindingFlags bindingFlags, Binder binder, Object target, Object[] providedArgs, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParams)
   at System.Type.InvokeMember(String name, BindingFlags invokeAttr, Binder binder, Object target, Object[] args)
   at clrModule.initclr()

Here is the current state:

https://www.dropbox.com/s/zevjlsfe9mk1pdg/pypy2-v5.6.0-win32.zip?dl=0

@den-run-ai
Copy link
Contributor Author

Oh, now I reached RPython traceback:

pypy
Python 2.7.12 (aff251e54385, Nov 09 2016, 17:54:55)
[PyPy 5.6.0 with MSC v.1500 32 bit] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>> import clr
Attempting to load 'Python.Runtime' using standard binding rules.
'Python.Runtime' not found using standard binding rules.
Attempting to load Python.Runtime from: 'C:\Python\pypy2-v5.6.0-win32\site-packages\Python.Runtime.dll'.
Success loading 'Python.Runtime' from: 'C:\Python\pypy2-v5.6.0-win32\site-packages\Python.Runtime.dll'.
RPython traceback:
  File "pypy_module_cpyext_2.c", line 26114, in type_realize
  File "pypy_module_cpyext_2.c", line 31744, in _type_realize
Fatal RPython error: AssertionError

This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.

@den-run-ai den-run-ai self-assigned this Feb 14, 2017
@den-run-ai den-run-ai added this to the 3.0 milestone Feb 14, 2017
@den-run-ai
Copy link
Contributor Author

Here is the current pypy branch:

https://github.com/pythonnet/pythonnet/tree/pypy

I'm stuck with above error from rpython with this CLR traceback:

image

My plan is to try out with debug build of pypy and possibly attach with native debugger.

@den-run-ai
Copy link
Contributor Author

ok, I built pypy from source and now can debug the rpython translated to C. But I have no idea why the object became undefined:

image

@den-run-ai
Copy link
Contributor Author

the structure of CLR Metatype looks fine:

image

@den-run-ai
Copy link
Contributor Author

I have some progress on pypy debugging with pythonnet. The issue right now is setting tp_flags which crashes PyType_Ready:

https://bitbucket.org/pypy/pypy/issues/2486/pypytype_ready-with-tp_flags-crashes-pypy

some issues were resolved in pypy by @arigo, which enable parsing pyheaptypeobject without hacking the source:

https://bitbucket.org/pypy/pypy/issues/2484/pyheaptypeobject-difference-with-cpython

https://bitbucket.org/pypy/pypy/issues/2483/clang-complains-about-parsing-pypy-headers

https://bitbucket.org/pypy/pypy/issues/2478/sysconfigget_config_var-includepy-empty-on

The only difference in pyheaptypeobject is still ob_pypy_link like I pointed out above by using diff.

@den-run-ai
Copy link
Contributor Author

some progress in these issues:

https://bitbucket.org/pypy/pypy/issues/2486/pypytype_ready-with-tp_flags-crashes-pypy
https://bitbucket.org/pypy/pypy/issues/2504/pypy-pythonnet-call-fails-on-pypyclass_new

I can actually do import clr with pypy, but it can't do anything useful right now :)

@den-run-ai
Copy link
Contributor Author

One more issue to track in PyPy cpyext:

https://bitbucket.org/pypy/pypy/issues/2447/cpyext-_pyobject_getdictptr-is

@stonebig
Copy link
Contributor

any hope ?

@filmor
Copy link
Member

filmor commented Jun 12, 2021

I'm personally waiting for hpy to mature before working on this myself. If anyone wants to take another shot at cpyext (which should be really easy now!), I'll gladly help out.

@m-rossi
Copy link
Contributor

m-rossi commented Jul 3, 2021

Conda-forge recently started the support for PyPy on Windows and started a migration.

As I can read here I can close this Merge Request for now?

@filmor
Copy link
Member

filmor commented Jul 3, 2021

Yes. There is no support for Pypy in Python.NET.

@mzdk100
Copy link

mzdk100 commented Sep 16, 2023

Can it be achieved? I also need to use pythonnet in pypy now.
Python 3.10.12 (af44d0b8114cb82c40a07bb9ee9c1ca8a1b3688c, Jun 15 2023, 15:42:22)
[PyPy 7.3.12 with MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.

import clr
Failed to initialize pythonnet: System.TypeInitializationException: “Delegates”的类型初始值设定项引发异常。 ---> Pytho
n.Runtime.BadPythonDllException: Runtime.PythonDLL was not set or does not point to a supported Python runtime DLL. See
https://github.com/pythonnet/pythonnet#embedding-python-in-net ---> System.MissingMethodException: Failed to load symbol
Py_IncRef. ---> System.ComponentModel.Win32Exception: 找不到指定的程序。
--- 内部异常堆栈跟踪的结尾 ---
在 Python.Runtime.Platform.WindowsLoader.GetFunction(IntPtr hModule, String procedureName)
在 Python.Runtime.Runtime.Delegates.GetFunctionByName(String functionName, IntPtr libraryHandle)
--- 内部异常堆栈跟踪的结尾 ---
在 Python.Runtime.Runtime.Delegates.GetFunctionByName(String functionName, IntPtr libraryHandle)
在 Python.Runtime.Runtime.Delegates..cctor()
--- 内部异常堆栈跟踪的结尾 ---
在 Python.Runtime.Runtime.Delegates.get_PyGILState_Ensure()
在 Python.Runtime.Runtime.PyGILState_Ensure()
在 Python.Runtime.Py.GIL()
在 Python.Runtime.Loader.Initialize(IntPtr data, Int32 size)
在 Python.Runtime.Runtime.Delegates.get_PyGILState_Ensure()
在 Python.Runtime.Runtime.PyGILState_Ensure()
在 Python.Runtime.Py.GIL()
在 Python.Runtime.Loader.Initialize(IntPtr data, Int32 size)Traceback (most recent call last):
File "", line 1, in
File "D:\pypy3.10\lib\site-packages\clr.py", line 6, in
load()
File "D:\pypy3.10\lib\site-packages\pythonnet_init_.py", line 146, in load
raise RuntimeError("Failed to initialize Python.Runtime.dll")
RuntimeError: Failed to initialize Python.Runtime.dll

@ethindp
Copy link

ethindp commented Feb 16, 2024

I also would like to see this. Has there been any progress?

@mzdk100
Copy link

mzdk100 commented Feb 16, 2024

It seems like there's no way.

@arigo
Copy link

arigo commented Feb 17, 2024

I think the whole approach is doomed for PyPy or any non-CPython interpreter. A way forward might be to use CFFI embeddings, like mentioned above: you can build with CFFI a DLL that basically exports whatever (C-level) API you like, and then you can write C# code to call functions in that DLL, install callbacks, and so on. You don't get any of the pythonnet-like features, though; only a C API. Depending on your goal, it might be enough.

Alternatively, someone could then write a C API that gives Python some generic control over the .NET objects, with logic both on the C# and the Python side to give generic wrappers of C# objects in Python and/or vice-versa, using runtime introspection on both sides.

@mzdk100
Copy link

mzdk100 commented Feb 17, 2024

I think the most important reason for choosing Pypy is to consider runtime performance, but currently I think it may not be necessary because Python 3.13 has already removed GIL, so I may not prioritize choosing Pypy.
Even so, as some alternatives, the Mojo programming language may perform better than Pypy.
I have now shifted my energy to Rust, and many features are not slower to implement than Python.

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

9 participants