go-python / gopy

gopy generates a CPython extension module from a go package.
BSD 3-Clause "New" or "Revised" License
2.05k stars 113 forks source link

Unable to import: zsh: segmentation fault python3 #314

Closed trueToastedCode closed 1 year ago

trueToastedCode commented 1 year ago

Was following the Examples from README

$ go mod init dummy.com/dum
$ go get github.com/go-python/gopy/_examples/hi
$ gopy build -output=out -vm=python3 github.com/go-python/gopy/_examples/hi
$ ls out
Makefile  __init__.py  __pycache__/  _hi.so*  build.py  go.py  hi.c  hi.go  hi.py  hi_go.h  hi_go.so

It compiles... however the import doesn't work

Python 3.10.8 (main, Nov 24 2022, 08:08:27) [Clang 14.0.6 ] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from out import hi
zsh: segmentation fault  python3

Fatal Python error: Segmentation fault
Current thread 0x00000001ec143a80 (most recent call first):
  File "<frozen importlib._bootstrap>", line 241 in _call_with_frames_removed
  File "<frozen importlib._bootstrap_external>", line 1176 in create_module
  File "<frozen importlib._bootstrap>", line 571 in module_from_spec
  File "<frozen importlib._bootstrap>", line 674 in _load_unlocked
  File "<frozen importlib._bootstrap>", line 1006 in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 1027 in _find_and_load
  File "<frozen importlib._bootstrap>", line 241 in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 1078 in _handle_fromlist
  File "/Users/lennard/GoProjects/dum/out/hi.py", line 20 in <module>
  File "<frozen importlib._bootstrap>", line 241 in _call_with_frames_removed
  File "<frozen importlib._bootstrap_external>", line 883 in exec_module
  File "<frozen importlib._bootstrap>", line 688 in _load_unlocked
  File "<frozen importlib._bootstrap>", line 1006 in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 1027 in _find_and_load
  File "<frozen importlib._bootstrap>", line 241 in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 1078 in _handle_fromlist
  File "/Users/lennard/GoProjects/dum/test.py", line 1 in <module>
tals commented 1 year ago

It works for me when using the macOS bundled Python (/usr/local/bin/python3), but fails when attempting to use one provisioned by Conda.

$ gopy build -output=out -vm=python3 github.com/go-python/gopy/_examples/hi

--- Processing package: github.com/go-python/gopy/_examples/hi ---

--- building package ---
gopy build -output=out -vm=python3 github.com/go-python/gopy/_examples/hi
goimports -w hi.go
go build -mod=mod -buildmode=c-shared -o hi_go.so .
/Users/tshiri/miniconda3/envs/py38/bin/python3 build.py
CGO_CFLAGS=-I/Users/tshiri/miniconda3/envs/py38/include/python3.8 -fPIC -Ofast
CGO_LDFLAGS=-L/Users/tshiri/miniconda3/envs/py38/lib -lpython3.8 -ldl -framework CoreFoundation
go build -mod=mod -buildmode=c-shared -o _hi.cpython-38-darwin.so .
$ which python
/Users/tshiri/miniconda3/envs/py38/bin/python
$ python
>>> import out.hi
Fatal Python error: _PyInterpreterState_Get(): no current thread state
Python runtime state: unknown

Process 58564 stopped
* thread #1, queue = 'com.apple.main-thread', stop reason = signal SIGABRT
    frame #0: 0x00000001a0e2ed98 libsystem_kernel.dylib`__pthread_kill + 8
libsystem_kernel.dylib`:
->  0x1a0e2ed98 <+8>:  b.lo   0x1a0e2edb8               ; <+40>
    0x1a0e2ed9c <+12>: pacibsp
    0x1a0e2eda0 <+16>: stp    x29, x30, [sp, #-0x10]!
    0x1a0e2eda4 <+20>: mov    x29, sp
Target 1: (python) stopped.
(lldb) bt
* thread #1, queue = 'com.apple.main-thread', stop reason = signal SIGABRT
  * frame #0: 0x00000001a0e2ed98 libsystem_kernel.dylib`__pthread_kill + 8
    frame #1: 0x00000001a0e63ee0 libsystem_pthread.dylib`pthread_kill + 288
    frame #2: 0x00000001a0d9e340 libsystem_c.dylib`abort + 168
    frame #3: 0x0000000105127028 libpython3.8.dylib`fatal_error + 684
    frame #4: 0x00000001051271a4 libpython3.8.dylib`Py_FatalError + 24
    frame #5: 0x000000010504a670 libpython3.8.dylib`PyModule_Create2 + 56
    frame #6: 0x000000010013ee4c python`_PyImport_LoadDynamicModuleWithSpec + 1276
    frame #7: 0x000000010013dd24 python`_imp_create_dynamic + 456
    frame #8: 0x00000001000755f0 python`cfunction_vectorcall_FASTCALL + 284
    frame #9: 0x0000000100028008 python`PyVectorcall_Call + 120
    frame #10: 0x0000000100110ce4 python`_PyEval_EvalFrameDefault + 28320
    frame #11: 0x000000010010982c python`_PyEval_EvalCodeWithName + 3008
    frame #12: 0x0000000100028948 python`_PyFunction_Vectorcall + 208
trueToastedCode commented 1 year ago

It works for me when using the macOS bundled Python (/usr/local/bin/python3), but fails when attempting to use one provisioned by Conda.

$ gopy build -output=out -vm=python3 github.com/go-python/gopy/_examples/hi

--- Processing package: github.com/go-python/gopy/_examples/hi ---

--- building package ---
gopy build -output=out -vm=python3 github.com/go-python/gopy/_examples/hi
goimports -w hi.go
go build -mod=mod -buildmode=c-shared -o hi_go.so .
/Users/tshiri/miniconda3/envs/py38/bin/python3 build.py
CGO_CFLAGS=-I/Users/tshiri/miniconda3/envs/py38/include/python3.8 -fPIC -Ofast
CGO_LDFLAGS=-L/Users/tshiri/miniconda3/envs/py38/lib -lpython3.8 -ldl -framework CoreFoundation
go build -mod=mod -buildmode=c-shared -o _hi.cpython-38-darwin.so .
$ which python
/Users/tshiri/miniconda3/envs/py38/bin/python
$ python
>>> import out.hi
Fatal Python error: _PyInterpreterState_Get(): no current thread state
Python runtime state: unknown

Process 58564 stopped
* thread #1, queue = 'com.apple.main-thread', stop reason = signal SIGABRT
    frame #0: 0x00000001a0e2ed98 libsystem_kernel.dylib`__pthread_kill + 8
libsystem_kernel.dylib`:
->  0x1a0e2ed98 <+8>:  b.lo   0x1a0e2edb8               ; <+40>
    0x1a0e2ed9c <+12>: pacibsp
    0x1a0e2eda0 <+16>: stp    x29, x30, [sp, #-0x10]!
    0x1a0e2eda4 <+20>: mov    x29, sp
Target 1: (python) stopped.
(lldb) bt
* thread #1, queue = 'com.apple.main-thread', stop reason = signal SIGABRT
  * frame #0: 0x00000001a0e2ed98 libsystem_kernel.dylib`__pthread_kill + 8
    frame #1: 0x00000001a0e63ee0 libsystem_pthread.dylib`pthread_kill + 288
    frame #2: 0x00000001a0d9e340 libsystem_c.dylib`abort + 168
    frame #3: 0x0000000105127028 libpython3.8.dylib`fatal_error + 684
    frame #4: 0x00000001051271a4 libpython3.8.dylib`Py_FatalError + 24
    frame #5: 0x000000010504a670 libpython3.8.dylib`PyModule_Create2 + 56
    frame #6: 0x000000010013ee4c python`_PyImport_LoadDynamicModuleWithSpec + 1276
    frame #7: 0x000000010013dd24 python`_imp_create_dynamic + 456
    frame #8: 0x00000001000755f0 python`cfunction_vectorcall_FASTCALL + 284
    frame #9: 0x0000000100028008 python`PyVectorcall_Call + 120
    frame #10: 0x0000000100110ce4 python`_PyEval_EvalFrameDefault + 28320
    frame #11: 0x000000010010982c python`_PyEval_EvalCodeWithName + 3008
    frame #12: 0x0000000100028948 python`_PyFunction_Vectorcall + 208

Ok thx... succeeded to import using the Python from Homebrew

$ which python3
/opt/homebrew/bin/python3

But now there is the next problem:

>>> import out.hi
>>> dir(hi)
['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', '_collections_abc', '_hi', 'collections', 'currentdir', 'cwd', 'go', 'inspect', 'os', 'sys']

the package doesn't include any of the defined methods from hi.go... e.g. Hi

out.hi.Hi()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: module 'out.hi' has no attribute 'Hi'

also tried to make my own go module... same result... methods defined in go, won't be available in python :(

MOVED THIS TO: https://github.com/go-python/gopy/issues/315