lcompilers / lpython

Python compiler
https://lpython.org/
Other
1.5k stars 157 forks source link

Difficulty running bindc_03.py in regular CPython #2485

Open rebcabin opened 7 months ago

rebcabin commented 7 months ago

I thought I had the PYTHONPATH figured out, but CType can't be found.

PYTHONPATH=/Users/brian/Dropbox/Mac/Documents/GitHub/lpython/integration_tests/../src/runtime/lpython:/Users/brian/Dropbox/Mac/Documents/GitHub/lpython/integration_tests/.. python bindc_03.py
Traceback (most recent call last):
  File "/Users/brian/Dropbox/Mac/Documents/GitHub/lpython/integration_tests/bindc_03.py", line 12, in <module>
    def g(a: CPtr, value: i32, offset_value: bool) -> None:
  File "/Users/brian/Dropbox/Mac/Documents/GitHub/lpython/src/runtime/lpython/lpython.py", line 477, in wrap
    func = CTypes(func, c_shared_lib, c_shared_lib_path)
  File "/Users/brian/Dropbox/Mac/Documents/GitHub/lpython/src/runtime/lpython/lpython.py", line 369, in __init__
    self.cf = CTypes.emulations[self.name]
AttributeError: type object 'CTypes' has no attribute 'emulations'
certik commented 7 months ago

The above way works for most cases, but does not work if you call into C, as you need to tell the emulation layer where to find it.

$ PYTHONPATH=../src/runtime/lpython python bindc_03.py 
Traceback (most recent call last):
  File "/Users/ondrej/repos/lpython/integration_tests/bindc_03.py", line 12, in <module>
    def g(a: CPtr, value: i32, offset_value: bool) -> None:
  File "/Users/ondrej/repos/lpython/src/runtime/lpython/lpython.py", line 477, in wrap
    func = CTypes(func, c_shared_lib, c_shared_lib_path)
  File "/Users/ondrej/repos/lpython/src/runtime/lpython/lpython.py", line 369, in __init__
    self.cf = CTypes.emulations[self.name]
AttributeError: type object 'CTypes' has no attribute 'emulations'

For that you also have to set LPYTHON_PY_MOD_NAME and LPYTHON_PY_MOD_PATH. It's not user friendly currently, but it does work. You need consult CMakeLists.txt for the details how it does it.

rebcabin commented 7 months ago

got it

rebcabin commented 7 months ago
└─(12:33:29 on vector-backend)──> PYTHONPATH=".:../src/runtime/lpython:.." LPYTHON_PY_MOD_NAME="" LPYTHON_PY_MOD_PATH="_lpython-tmp-test-cpython" python bindc_03.py
Traceback (most recent call last):
  File "/Users/brian/Dropbox/Mac/Documents/GitHub/lpython/integration_tests/bindc_03.py", line 12, in <module>
    def g(a: CPtr, value: i32, offset_value: bool) -> None:
  File "/Users/brian/Dropbox/Mac/Documents/GitHub/lpython/src/runtime/lpython/lpython.py", line 477, in wrap
    func = CTypes(func, c_shared_lib, c_shared_lib_path)
  File "/Users/brian/Dropbox/Mac/Documents/GitHub/lpython/src/runtime/lpython/lpython.py", line 367, in __init__
    self.cf = self.library[self.name]
  File "/Users/brian/miniforge3/envs/lp/lib/python3.10/ctypes/__init__.py", line 392, in __getitem__
    func = self._FuncPtr((name_or_ordinal, self))
AttributeError: dlsym(0x87869030, g): symbol not found
(lp) ┌─(...uments/GitHub/lpython/integration_tests)───(brian@MacBook-Pro:s001)─┐
└─(12:35:23 on vector-backend)──>
rebcabin commented 7 months ago

I'm leaving this open. bindc_03.py doesn't work in CPython, but most of the other bindc programs do work with the ENVIRONMENT variables set as above. I verified by hand

    Start 22: bindc_01
1/8 Test #22: bindc_01 .........................   Passed    0.03 sec
    Start 23: bindc_02
2/8 Test #23: bindc_02 .........................   Passed    0.06 sec
    Start 24: bindc_07
3/8 Test #24: bindc_07 .........................   Passed    0.07 sec
    Start 25: bindc_08
4/8 Test #25: bindc_08 .........................   Passed    0.06 sec
    Start 26: bindc_09
5/8 Test #26: bindc_09 .........................   Passed    0.06 sec
    Start 27: bindc_09b
6/8 Test #27: bindc_09b ........................   Passed    0.06 sec
    Start 28: bindc_10
7/8 Test #28: bindc_10 .........................   Passed    0.06 sec
    Start 29: bindc_11
8/8 Test #29: bindc_11 .........................   Passed    0.06 sec
certik commented 7 months ago

Yes, it's a bug: it works in LPython, but fails in CPython. So either we need to disable it in LPython with an error message, or get it working with CPython. It's low priority and for now let's keep the LPython test, since at least LPython will keep working for this, and we can get the CPython version working later. We can always later decide to just give an error message in LPython for this case if we do not want to support it.