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

pypy support in pythonnet #330

Open
denfromufa opened this issue Jan 22, 2017 · 38 comments
Open

pypy support in pythonnet #330

denfromufa opened this issue Jan 22, 2017 · 38 comments
Assignees
Labels

Comments

@denfromufa
Copy link
Contributor

@denfromufa denfromufa 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.

@denfromufa
Copy link
Contributor Author

@denfromufa denfromufa commented Jan 23, 2017

@vmuriart
Copy link
Contributor

@vmuriart 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

@denfromufa
Copy link
Contributor Author

@denfromufa denfromufa 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

@vmuriart vmuriart commented Feb 10, 2017

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.

@denfromufa
Copy link
Contributor Author

@denfromufa denfromufa commented Feb 10, 2017

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

@vmuriart
Copy link
Contributor

@vmuriart vmuriart commented Feb 10, 2017

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

@denfromufa
Copy link
Contributor Author

@denfromufa denfromufa commented Feb 10, 2017

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

buildoutput.txt

@denfromufa
Copy link
Contributor Author

@denfromufa denfromufa commented Feb 10, 2017

@vmuriart
Copy link
Contributor

@vmuriart vmuriart commented Feb 10, 2017

@denfromufa
Copy link
Contributor Author

@denfromufa denfromufa commented Feb 10, 2017

@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/

@denfromufa
Copy link
Contributor Author

@denfromufa denfromufa commented Feb 10, 2017

@denfromufa
Copy link
Contributor Author

@denfromufa denfromufa commented Feb 10, 2017

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 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.

@denfromufa
Copy link
Contributor Author

@denfromufa denfromufa commented Feb 10, 2017

@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 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?).

@denfromufa
Copy link
Contributor Author

@denfromufa denfromufa 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 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

@tonyroberts tonyroberts commented Feb 10, 2017

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 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.

@denfromufa
Copy link
Contributor Author

@denfromufa denfromufa 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()

@denfromufa
Copy link
Contributor Author

@denfromufa denfromufa commented Feb 10, 2017

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

@vmuriart
Copy link
Contributor

@vmuriart vmuriart commented Feb 10, 2017

@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 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.

@denfromufa
Copy link
Contributor Author

@denfromufa denfromufa commented Feb 10, 2017

@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.

@denfromufa
Copy link
Contributor Author

@denfromufa denfromufa commented Feb 10, 2017

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

@denfromufa
Copy link
Contributor Author

@denfromufa denfromufa commented Feb 13, 2017

@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()

@denfromufa
Copy link
Contributor Author

@denfromufa denfromufa commented Feb 13, 2017

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

@denfromufa
Copy link
Contributor Author

@denfromufa denfromufa commented Feb 13, 2017

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.

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

@denfromufa denfromufa commented Feb 14, 2017

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.

@denfromufa
Copy link
Contributor Author

@denfromufa denfromufa commented Feb 18, 2017

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

@denfromufa
Copy link
Contributor Author

@denfromufa denfromufa commented Feb 18, 2017

the structure of CLR Metatype looks fine:

image

@denfromufa
Copy link
Contributor Author

@denfromufa denfromufa commented Feb 26, 2017

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.

@denfromufa
Copy link
Contributor Author

@denfromufa denfromufa commented Mar 19, 2017

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 :)

@denfromufa
Copy link
Contributor Author

@denfromufa denfromufa commented Mar 20, 2017

@stonebig
Copy link
Contributor

@stonebig stonebig commented Jun 12, 2021

any hope ?

@filmor
Copy link
Member

@filmor 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 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 filmor commented Jul 3, 2021

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

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

No branches or pull requests

7 participants