grumpyhome / grumpy

Grumpy is a Python to Go source code transcompiler and runtime.
Apache License 2.0
420 stars 18 forks source link

how to use externs or interop for go code? #148

Closed xukku closed 4 years ago

xukku commented 4 years ago

if i want to use standart go libs with this grympy code - how i can do this?

alanjds commented 4 years ago

Hi, 6ump.

You can import from '__go__' to get stuff from Golang libs.

Please take a look on the math.py implementation, that uses from '__go__/math' import: https://github.com/grumpyhome/grumpy/blob/7370a936adc980d998f769efc56db0d820cce053/grumpy-runtime-src/lib/math.py#L15

xukku commented 4 years ago

from 'go/math' import Pi print Pi

this works and output

grumpy run test.py 3.14159265359

but this not works


 import "__go__/encoding/hex"
 print hex.Dump('foobar')

output error grumpy run test.py

/tmp/tmp8Y9Chnpycache/gopath/src/python/main/module.go:4:2: cannot find package "python/go/encoding/hex" in any of: /usr/lib/go/src/python/go/encoding/hex (from $GOROOT) /tmp/tmp8Y9Chnpycache/gopath/src/python/go/encoding/hex (from $GOPATH) /home/user/.local/lib/python2.7/site-packages/grumpy_runtime/data/gopath/src/python/go/encoding/hex

alanjds commented 4 years ago

Can you post the output of grumpy -v debug run test.py instead?

xukku commented 4 years ago
grumpy -v debug fail.py                                                                                                                                            :(
info: GOPATH: /home/user/.local/lib/python2.7/site-packages/grumpy_runtime/data/gopath
info: __main__ pycache folder: /tmp/tmphOSzQ___pycache__
debug: Should transpile '__main__'
debug: Dependencies file regenerated
info: `go run` GOPATH=/tmp/tmphOSzQ___pycache__/gopath:/home/user/.local/lib/python2.7/site-packages/grumpy_runtime/data/gopath
debug: Starting subprocess: `go run /tmp/tmphOSzQ___pycache__/gopath/src/__python__/main.go`
/tmp/tmphOSzQ___pycache__/gopath/src/__python__/__main__/module.go:4:2: cannot find package "__python__/__go__/encoding/hex" in any of:
    /usr/lib/go/src/__python__/__go__/encoding/hex (from $GOROOT)
    /tmp/tmphOSzQ___pycache__/gopath/src/__python__/__go__/encoding/hex (from $GOPATH)
    /home/user/.local/lib/python2.7/site-packages/grumpy_runtime/data/gopath/src/__python__/__go__/encoding/hex
xukku commented 4 years ago

i found one folder with __python__/__go__

and here is this files - some go lib wrapers? but not all are implemented /home/user/.local/lib/python2.7/site-packages/grumpy_runtime/data/gopath/pkg/linux_amd64/__python__/__go__


.
./math
./math/rand.a
./io
./io/ioutil.a
./reflect.a
./grumpy.a
./time.a
./math.a
./syscall.a
./unicode.a
./os.a
./runtime.a
./path
./path/filepath.a
./sync.a

find . | grep '__python__/__go__'
./data/gopath/src/__python__/__go__
./data/gopath/src/__python__/__go__/math
./data/gopath/src/__python__/__go__/math/rand
./data/gopath/src/__python__/__go__/math/rand/module.go
./data/gopath/src/__python__/__go__/math/module.go
./data/gopath/src/__python__/__go__/io
./data/gopath/src/__python__/__go__/io/ioutil
./data/gopath/src/__python__/__go__/io/ioutil/module.go
./data/gopath/src/__python__/__go__/runtime
./data/gopath/src/__python__/__go__/runtime/module.go
./data/gopath/src/__python__/__go__/time
./data/gopath/src/__python__/__go__/time/module.go
./data/gopath/src/__python__/__go__/unicode
./data/gopath/src/__python__/__go__/unicode/module.go
./data/gopath/src/__python__/__go__/reflect
./data/gopath/src/__python__/__go__/reflect/module.go
./data/gopath/src/__python__/__go__/syscall
./data/gopath/src/__python__/__go__/syscall/module.go
./data/gopath/src/__python__/__go__/grumpy
./data/gopath/src/__python__/__go__/grumpy/module.go
./data/gopath/src/__python__/__go__/sync
./data/gopath/src/__python__/__go__/sync/module.go
./data/gopath/src/__python__/__go__/os
./data/gopath/src/__python__/__go__/os/module.go
./data/gopath/src/__python__/__go__/path
./data/gopath/src/__python__/__go__/path/filepath
./data/gopath/src/__python__/__go__/path/filepath/module.go
./data/gopath/pkg/linux_amd64/__python__/__go__
./data/gopath/pkg/linux_amd64/__python__/__go__/math
./data/gopath/pkg/linux_amd64/__python__/__go__/math/rand.a
./data/gopath/pkg/linux_amd64/__python__/__go__/io
./data/gopath/pkg/linux_amd64/__python__/__go__/io/ioutil.a
./data/gopath/pkg/linux_amd64/__python__/__go__/reflect.a
./data/gopath/pkg/linux_amd64/__python__/__go__/grumpy.a
./data/gopath/pkg/linux_amd64/__python__/__go__/time.a
./data/gopath/pkg/linux_amd64/__python__/__go__/math.a
./data/gopath/pkg/linux_amd64/__python__/__go__/syscall.a
./data/gopath/pkg/linux_amd64/__python__/__go__/unicode.a
./data/gopath/pkg/linux_amd64/__python__/__go__/os.a
./data/gopath/pkg/linux_amd64/__python__/__go__/runtime.a
./data/gopath/pkg/linux_amd64/__python__/__go__/path
./data/gopath/pkg/linux_amd64/__python__/__go__/path/filepath.a
./data/gopath/pkg/linux_amd64/__python__/__go__/sync.a

`/home/user/.local/lib/python2.7/site-packages/grumpy_runtime
`

so i should write some go file wrapers  for other go standart libs manually ?
or is there some script for automating this task?