From e6a14f8cacd5da0c8495e666ec5514bcd2dfe802 Mon Sep 17 00:00:00 2001 From: Noam Kleinburd Date: Sun, 19 Nov 2023 14:04:23 +0200 Subject: [PATCH 01/25] Implement efficient copies of bytes. --- _examples/gobytes/gobytes.go | 33 ++++++++++++++++++++++++++ _examples/gobytes/test.py | 18 ++++++++++++++ bind/gen_slice.go | 46 ++++++++++++++++++++++++++++++++++++ main_test.go | 20 ++++++++++++++++ 4 files changed, 117 insertions(+) create mode 100644 _examples/gobytes/gobytes.go create mode 100644 _examples/gobytes/test.py diff --git a/_examples/gobytes/gobytes.go b/_examples/gobytes/gobytes.go new file mode 100644 index 0000000..f7721e8 --- /dev/null +++ b/_examples/gobytes/gobytes.go @@ -0,0 +1,33 @@ +// Copyright 2017 The go-python Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package gobytes + +func HashBytes(b []byte) [4]byte { + result := [4]byte{0, 0, 0, 0} + full_blocks := len(b) / 4 + for i := 0; i < full_blocks; i++ { + for j := 0; j < 4; j++ { + result[j] ^= b[4*i+j] + } + } + if full_blocks*4 < len(b) { + for j := 0; j < 4; j++ { + if full_blocks*4+j < len(b) { + result[j] ^= b[full_blocks*4+j] + } else { + result[j] ^= 0x55 + } + } + } + return result +} + +func CreateBytes(len byte) []byte { + res := make([]byte, len) + for i := (byte)(0); i < len; i++ { + res[i] = i + } + return res +} diff --git a/_examples/gobytes/test.py b/_examples/gobytes/test.py new file mode 100644 index 0000000..09887bc --- /dev/null +++ b/_examples/gobytes/test.py @@ -0,0 +1,18 @@ +# Copyright 2017 The go-python Authors. All rights reserved. +# Use of this source code is governed by a BSD-style +# license that can be found in the LICENSE file. + +from __future__ import print_function +import gobytes, go + +a = bytes([0, 1, 2, 3]) +b = gobytes.CreateBytes(10) +print ("Python bytes:", a) +print ("Go slice: ", b) + +print ("gobytes.HashBytes from Go bytes:", gobytes.HashBytes(b)) + +print("Python bytes to Go: ", go.Slice_byte.from_bytes(a)) +print("Go bytes to Python: ", bytes(go.Slice_byte([3, 4, 5]))) + +print("OK") diff --git a/bind/gen_slice.go b/bind/gen_slice.go index bb33214..4ef47e4 100644 --- a/bind/gen_slice.go +++ b/bind/gen_slice.go @@ -277,6 +277,25 @@ otherwise parameter is a python list that we copy from g.pywrap.Outdent() g.pywrap.Outdent() } + + if slNm == "Slice_byte" { + g.pywrap.Printf("@staticmethod\n") + g.pywrap.Printf("def from_bytes(value):\n") + g.pywrap.Indent() + g.pywrap.Printf(`"""Create a Go []byte object from a Python bytes object""" +`) + g.pywrap.Printf("handle = _%s_from_bytes(value)\n", qNm) + g.pywrap.Printf("return Slice_byte(handle=handle)\n") + g.pywrap.Outdent() + g.pywrap.Printf("def __bytes__(self):\n") + g.pywrap.Indent() + g.pywrap.Printf(`"""Convert the slice to a bytes object.""" +`) + g.pywrap.Printf("return _%s_to_bytes(self.handle)\n", qNm) + g.pywrap.Outdent() + g.pywrap.Outdent() + + } } if !extTypes || !pyWrapOnly { @@ -367,6 +386,33 @@ otherwise parameter is a python list that we copy from g.pybuild.Printf("mod.add_function('%s_append', None, [param('%s', 'handle'), param('%s', 'value'%s)])\n", slNm, PyHandle, esym.cpyname, transfer_ownership) } + + if slNm == "Slice_byte" { + g.gofile.Printf("//export Slice_byte_from_bytes\n") + g.gofile.Printf("func Slice_byte_from_bytes(o *C.PyObject) CGoHandle {\n") + g.gofile.Indent() + g.gofile.Printf("size := C.PyBytes_Size(o)\n") + g.gofile.Printf("ptr := unsafe.Pointer(C.PyBytes_AsString(o))\n") + g.gofile.Printf("data := make([]byte, size)\n") + g.gofile.Printf("tmp := unsafe.Slice((*byte)(ptr), size)\n") + g.gofile.Printf("copy(data, tmp)\n") + g.gofile.Printf("return handleFromPtr_Slice_byte(&data)\n") + g.gofile.Outdent() + g.gofile.Printf("}\n\n") + + g.gofile.Printf("//export Slice_byte_to_bytes\n") + g.gofile.Printf("func Slice_byte_to_bytes(handle CGoHandle) *C.PyObject {\n") + g.gofile.Indent() + g.gofile.Printf("s := deptrFromHandle_Slice_byte(handle)\n") + g.gofile.Printf("ptr := unsafe.Pointer(&s[0])\n") + g.gofile.Printf("size := len(s)\n") + g.gofile.Printf("return C.PyBytes_FromStringAndSize((*C.char)(ptr), C.long(size))\n") + g.gofile.Outdent() + g.gofile.Printf("}\n\n") + + g.pybuild.Printf("mod.add_function('Slice_byte_from_bytes', retval('%s'%s), [param('PyObject*', 'o', transfer_ownership=False)])\n", PyHandle, caller_owns_ret) + g.pybuild.Printf("mod.add_function('Slice_byte_to_bytes', retval('PyObject*', caller_owns_return=True), [param('%s', 'handle')])\n", PyHandle) + } } } diff --git a/main_test.go b/main_test.go index 38c03e3..5592072 100644 --- a/main_test.go +++ b/main_test.go @@ -23,6 +23,7 @@ var ( testBackends = map[string]string{} features = map[string][]string{ "_examples/hi": []string{"py3"}, + "_examples/gobytes": []string{"py3"}, "_examples/funcs": []string{"py3"}, "_examples/sliceptr": []string{"py3"}, "_examples/simple": []string{"py3"}, @@ -267,6 +268,25 @@ OK } +func TestBytes(t *testing.T) { + // t.Parallel() + path := "_examples/gobytes" + testPkg(t, pkg{ + path: path, + lang: features[path], + cmd: "build", + extras: nil, + want: []byte(`Python bytes: b'\x00\x01\x02\x03' +Go slice: go.Slice_byte len: 10 handle: 1 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] +gobytes.HashBytes from Go bytes: gobytes.Array_4_byte len: 4 handle: 2 [12, 13, 81, 81] +Python bytes to Go: go.Slice_byte len: 4 handle: 3 [0, 1, 2, 3] +Go bytes to Python: b'\x03\x04\x05' +OK +`), + }) + +} + func TestBindFuncs(t *testing.T) { // t.Parallel() path := "_examples/funcs" From 036ed327ebb6312755f6f329514a0308f24986b4 Mon Sep 17 00:00:00 2001 From: Noam Kleinburd Date: Wed, 13 Dec 2023 11:19:20 +0200 Subject: [PATCH 02/25] Add gobytes example to SUPPORT_MATRIX.md. --- SUPPORT_MATRIX.md | 1 + 1 file changed, 1 insertion(+) diff --git a/SUPPORT_MATRIX.md b/SUPPORT_MATRIX.md index 9153644..8e73c1a 100644 --- a/SUPPORT_MATRIX.md +++ b/SUPPORT_MATRIX.md @@ -11,6 +11,7 @@ _examples/consts | yes _examples/cstrings | yes _examples/empty | yes _examples/funcs | yes +_examples/gobytes | yes _examples/gopygc | yes _examples/gostrings | yes _examples/hi | yes From 4f1a7129481fc3581fd3db17debd6fbf60cd44e1 Mon Sep 17 00:00:00 2001 From: Jorropo Date: Fri, 23 Feb 2024 03:45:34 +0100 Subject: [PATCH 03/25] fix panic when packaged go code relies on structural typing Fix this panic: ``` panic: interface conversion: types.Type is *types.Signature, not *types.Named goroutine 1 [running]: github.com/go-python/gopy/bind.(*Package).process(0xc01d5ee800) /home/hugo/k/gopy/bind/package.go:340 +0x2078 github.com/go-python/gopy/bind.NewPackage(0xc009d2e720, 0xc00b228120) /home/hugo/k/gopy/bind/package.go:68 +0x27c main.parsePackage(0xc0203ec180) /home/hugo/k/gopy/gen.go:159 +0x276 main.buildPkgRecurse({0xc0000ce640, 0x20}, {0xc0005d1ce0, 0x26}, {0x7ffd641ad938, 0x1b}, 0xc00d08bc68, {0x0, 0x0}) /home/hugo/k/gopy/cmd_pkg.go:162 +0x2b6 main.buildPkgRecurse({0xc0000ce640, 0x20}, {0xc01173e280, 0x20}, {0x7ffd641ad938, 0x1b}, 0xc00d08bc68, {0x0, 0x0}) /home/hugo/k/gopy/cmd_pkg.go:174 +0x428 main.buildPkgRecurse({0xc0000ce640, 0x20}, {0x7ffd641ad938, 0x1b}, {0x7ffd641ad938, 0x1b}, 0xc00d08bc68, {0x0, 0x0}) /home/hugo/k/gopy/cmd_pkg.go:174 +0x428 main.gopyRunCmdPkg(0xc00012cd20, {0xc00009e230, 0x1, 0x747468?}) /home/hugo/k/gopy/cmd_pkg.go:132 +0xd19 github.com/gonuts/commander.(*Command).Dispatch(0xc00012cd20, {0xc00009e230, 0x1, 0x1}) /home/hugo/go/pkg/mod/github.com/gonuts/commander@v0.1.0/commands.go:209 +0x170 github.com/gonuts/commander.(*Command).Dispatch(0xc00012cf00, {0xc00009e220, 0x2, 0x2}) /home/hugo/go/pkg/mod/github.com/gonuts/commander@v0.1.0/commands.go:175 +0x22f main.run({0xc00009e220, 0x2, 0x2}) /home/hugo/k/gopy/main.go:62 +0x25b main.main() /home/hugo/k/gopy/main.go:70 +0x49 ``` When packaing code like this ```go type Public = func() ``` --- bind/package.go | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/bind/package.go b/bind/package.go index 2136abb..cf346c6 100644 --- a/bind/package.go +++ b/bind/package.go @@ -337,8 +337,15 @@ func (p *Package) process() error { funcs[name] = fv case *types.TypeName: - named := obj.Type().(*types.Named) - switch typ := named.Underlying().(type) { + typ := obj.Type() + if named, ok := typ.(*types.Named); ok { + typ = named.Underlying() + } else { + // we are dealing with a type alias to a type literal. + // this is a cursed feature used to do structural typing. + // just pass it as-is. + } + switch typ := typ.(type) { case *types.Struct: sv, err := newStruct(p, obj) if err != nil { From 7e6f349c4462e1596102bdd3566e08533cbe5ca5 Mon Sep 17 00:00:00 2001 From: Evan Oman Date: Sat, 13 Apr 2024 21:43:18 -0500 Subject: [PATCH 04/25] Adds reference in gen_map elem fn --- bind/gen_map.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bind/gen_map.go b/bind/gen_map.go index 27c1d86..834121b 100644 --- a/bind/gen_map.go +++ b/bind/gen_map.go @@ -303,7 +303,7 @@ otherwise parameter is a python list that we copy from g.gofile.Outdent() g.gofile.Printf("}\n") if esym.go2py != "" { - g.gofile.Printf("return %s(v)%s\n", esym.go2py, esym.go2pyParenEx) + g.gofile.Printf("return %s(&v)%s\n", esym.go2py, esym.go2pyParenEx) } else { g.gofile.Printf("return v\n") } From 6079a4ceca502783b6a6cbcccb9b0458f0750b0d Mon Sep 17 00:00:00 2001 From: Evan Oman Date: Mon, 22 Apr 2024 12:22:44 -0500 Subject: [PATCH 05/25] Adds guard around pointer reference for basic types --- bind/gen_map.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/bind/gen_map.go b/bind/gen_map.go index 834121b..2803438 100644 --- a/bind/gen_map.go +++ b/bind/gen_map.go @@ -303,7 +303,14 @@ otherwise parameter is a python list that we copy from g.gofile.Outdent() g.gofile.Printf("}\n") if esym.go2py != "" { - g.gofile.Printf("return %s(&v)%s\n", esym.go2py, esym.go2pyParenEx) + // If the go2py starts with handleFromPtr_, use &v, otherwise just v + val_str := "" + if strings.HasPrefix(esym.go2py, "handleFromPtr_") { + val_str = "&v" + } else { + val_str = "v" + } + g.gofile.Printf("return %s(%s)%s\n", esym.go2py, val_str, esym.go2pyParenEx) } else { g.gofile.Printf("return v\n") } From 08d1f14ff9fe7812f17f08b3d24cccd3084acf91 Mon Sep 17 00:00:00 2001 From: "Randall C. O'Reilly" Date: Mon, 22 Apr 2024 15:31:34 -0700 Subject: [PATCH 06/25] quote the -I and -L CFLAGS, LDFLAGS paths. Fixes #349 --- bind/utils.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/bind/utils.go b/bind/utils.go index 3048445..cded682 100644 --- a/bind/utils.go +++ b/bind/utils.go @@ -208,11 +208,11 @@ else: cfg.Version = raw.Version cfg.ExtSuffix = raw.ExtSuffix cfg.CFlags = strings.Join([]string{ - "-I" + raw.IncDir, + `"-I` + raw.IncDir + `"`, }, " ") cfg.LdFlags = strings.Join([]string{ - "-L" + raw.LibDir, - "-l" + raw.LibPy, + `"-L` + raw.LibDir + `"`, + `"-l` + raw.LibPy + `"`, raw.ShLibs, raw.SysLibs, }, " ") From 4aba3809257d9de72193b1702402a5ac09ff43f1 Mon Sep 17 00:00:00 2001 From: "Randall C. O'Reilly" Date: Mon, 22 Apr 2024 15:48:31 -0700 Subject: [PATCH 07/25] automatically exclude internal packages. Fixes #343 --- cmd_pkg.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd_pkg.go b/cmd_pkg.go index c0d6cba..0f58480 100644 --- a/cmd_pkg.go +++ b/cmd_pkg.go @@ -167,7 +167,7 @@ func buildPkgRecurse(odir, path, rootpath string, exmap map[string]struct{}, bui drs := Dirs(dir) for _, dr := range drs { _, ex := exmap[dr] - if ex || dr[0] == '.' || dr[0] == '_' { + if ex || dr[0] == '.' || dr[0] == '_' || dr == "internal" { continue } sp := filepath.Join(path, dr) From 9dde3761d503cd869dd0a6e78e7aba1a142f0786 Mon Sep 17 00:00:00 2001 From: "Randall C. O'Reilly" Date: Mon, 22 Apr 2024 16:04:17 -0700 Subject: [PATCH 08/25] if renaming case, use lower-case string() method instead of String() -- fixes #337 --- bind/gen.go | 10 ++++++++++ bind/gen_map.go | 2 +- bind/gen_slice.go | 2 +- bind/gen_struct.go | 4 ++-- 4 files changed, 14 insertions(+), 4 deletions(-) diff --git a/bind/gen.go b/bind/gen.go index c8b6756..05fd70e 100644 --- a/bind/gen.go +++ b/bind/gen.go @@ -863,3 +863,13 @@ func (g *pyGen) genGoPkg() { g.genType(sym, false, false) // not exttypes } } + +// genStringerCall generates a call to either self.String() or self.string() +// depending on RenameCase option +func (g *pyGen) genStringerCall() { + if g.cfg.RenameCase { + g.pywrap.Printf("return self.string()\n") + } else { + g.pywrap.Printf("return self.String()\n") + } +} diff --git a/bind/gen_map.go b/bind/gen_map.go index 2803438..f65111f 100644 --- a/bind/gen_map.go +++ b/bind/gen_map.go @@ -134,7 +134,7 @@ otherwise parameter is a python list that we copy from if isStringer(m.obj) { g.pywrap.Printf("def __str__(self):\n") g.pywrap.Indent() - g.pywrap.Printf("return self.String()\n") + g.genStringerCall() g.pywrap.Outdent() g.pywrap.Printf("\n") } diff --git a/bind/gen_slice.go b/bind/gen_slice.go index 4ef47e4..fa0ab54 100644 --- a/bind/gen_slice.go +++ b/bind/gen_slice.go @@ -129,7 +129,7 @@ otherwise parameter is a python list that we copy from if isStringer(m.obj) { g.pywrap.Printf("def __str__(self):\n") g.pywrap.Indent() - g.pywrap.Printf("return self.String()\n") + g.genStringerCall() g.pywrap.Outdent() } } diff --git a/bind/gen_struct.go b/bind/gen_struct.go index 815c083..b50ddfc 100644 --- a/bind/gen_struct.go +++ b/bind/gen_struct.go @@ -101,7 +101,7 @@ in which case a new Go object is constructed first } g.pywrap.Printf("def __str__(self):\n") g.pywrap.Indent() - g.pywrap.Printf("return self.String()\n") + g.genStringerCall() g.pywrap.Outdent() g.pywrap.Printf("\n") } @@ -345,7 +345,7 @@ handle=A Go-side object is always initialized with an explicit handle=arg } g.pywrap.Printf("def __str__(self):\n") g.pywrap.Indent() - g.pywrap.Printf("return self.String()\n") + g.genStringerCall() g.pywrap.Outdent() g.pywrap.Printf("\n") } From cdad836f8ab0ae3f81b4e4ca64890b5c3f2695c6 Mon Sep 17 00:00:00 2001 From: "Randall C. O'Reilly" Date: Mon, 22 Apr 2024 17:02:23 -0700 Subject: [PATCH 09/25] v0.4.9 release --- Makefile | 2 +- version.go | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Makefile b/Makefile index 02e10c7..f8b2bf0 100644 --- a/Makefile +++ b/Makefile @@ -54,7 +54,7 @@ prereq: # NOTE: MUST update version number here prior to running 'make release' and edit this file! -VERS=v0.4.8 +VERS=v0.4.9 PACKAGE=main GIT_COMMIT=`git rev-parse --short HEAD` VERS_DATE=`date -u +%Y-%m-%d\ %H:%M` diff --git a/version.go b/version.go index 26b7c0c..3410661 100644 --- a/version.go +++ b/version.go @@ -3,7 +3,7 @@ package main const ( - Version = "v0.4.8" - GitCommit = "034c7db" // the commit JUST BEFORE the release - VersionDate = "2023-12-12 20:15" // UTC + Version = "v0.4.9" + GitCommit = "9dde376" // the commit JUST BEFORE the release + VersionDate = "2024-04-23 00:02" // UTC ) From f16e21bc0e4e36aff8ad31677d6700ed90a1c7ec Mon Sep 17 00:00:00 2001 From: Evan Oman Date: Mon, 22 Apr 2024 21:39:40 -0500 Subject: [PATCH 10/25] Adds missing pointer reference for slices along with test --- _examples/slices/slices.go | 13 +++++++++++++ _examples/slices/test.py | 7 +++++++ bind/gen_slice.go | 9 ++++++--- main_test.go | 1 + 4 files changed, 27 insertions(+), 3 deletions(-) diff --git a/_examples/slices/slices.go b/_examples/slices/slices.go index 6c6fb1c..4bd2d08 100644 --- a/_examples/slices/slices.go +++ b/_examples/slices/slices.go @@ -60,3 +60,16 @@ func CmplxSqrt(arr SliceComplex) SliceComplex { } return res } + +func GetEmptyMatrix(xSize int, ySize int) [][]bool { + result := [][]bool{} + + for i := 0; i < xSize; i++ { + result = append(result, []bool{}) + for j := 0; j < ySize; j++ { + result[i] = append(result[i], false) + } + } + + return result +} \ No newline at end of file diff --git a/_examples/slices/test.py b/_examples/slices/test.py index 6dc3f22..143f453 100644 --- a/_examples/slices/test.py +++ b/_examples/slices/test.py @@ -44,4 +44,11 @@ assert math.isclose(root_squared.real, orig.real) assert math.isclose(root_squared.imag, orig.imag) + +matrix = slices.GetEmptyMatrix(4,4) +for i in range(4): + for j in range(4): + assert not matrix[i][j] +print("[][]bool working as expected") + print("OK") diff --git a/bind/gen_slice.go b/bind/gen_slice.go index fa0ab54..c0ef885 100644 --- a/bind/gen_slice.go +++ b/bind/gen_slice.go @@ -325,11 +325,14 @@ otherwise parameter is a python list that we copy from g.gofile.Indent() g.gofile.Printf("s := deptrFromHandle_%s(handle)\n", slNm) if esym.go2py != "" { - if !esym.isPointer() && esym.isStruct() { - g.gofile.Printf("return %s(&(s[_idx]))%s\n", esym.go2py, esym.go2pyParenEx) + // If the go2py starts with handleFromPtr_, use reference &, otherwise just return the value + val_str := "" + if strings.HasPrefix(esym.go2py, "handleFromPtr_") { + val_str = "&(s[_idx])" } else { - g.gofile.Printf("return %s(s[_idx])%s\n", esym.go2py, esym.go2pyParenEx) + val_str = "s[_idx]" } + g.gofile.Printf("return %s(%s)%s\n", esym.go2py, val_str, esym.go2pyParenEx) } else { g.gofile.Printf("return s[_idx]\n") } diff --git a/main_test.go b/main_test.go index 5592072..e7d4292 100644 --- a/main_test.go +++ b/main_test.go @@ -616,6 +616,7 @@ struct slice: slices.Slice_Ptr_slices_S len: 3 handle: 11 [slices.S{Name=S0, ha struct slice[0]: slices.S{Name=S0, handle=15} struct slice[1]: slices.S{Name=S1, handle=16} struct slice[2].Name: S2 +[][]bool working as expected OK `), }) From 69ffdd7010551fb80779d549c01da3f30f7987bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=88=91=E4=B8=8D=E6=98=AFArt?= <101685021+Inotart@users.noreply.github.com> Date: Tue, 30 Apr 2024 12:14:44 +0800 Subject: [PATCH 11/25] Update gen_slice.go --- bind/gen_slice.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bind/gen_slice.go b/bind/gen_slice.go index fa0ab54..70a3e62 100644 --- a/bind/gen_slice.go +++ b/bind/gen_slice.go @@ -406,7 +406,7 @@ otherwise parameter is a python list that we copy from g.gofile.Printf("s := deptrFromHandle_Slice_byte(handle)\n") g.gofile.Printf("ptr := unsafe.Pointer(&s[0])\n") g.gofile.Printf("size := len(s)\n") - g.gofile.Printf("return C.PyBytes_FromStringAndSize((*C.char)(ptr), C.long(size))\n") + g.gofile.Printf("return C.PyBytes_FromStringAndSize((*C.char)(ptr), C.longlong(size))\n") g.gofile.Outdent() g.gofile.Printf("}\n\n") From 49f82061f646c5869a3515aa1c07683ac71b6646 Mon Sep 17 00:00:00 2001 From: "Randall C. O'Reilly" Date: Fri, 3 May 2024 14:30:03 -0700 Subject: [PATCH 12/25] need to run files through format or goimports.. --- _examples/slices/slices.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_examples/slices/slices.go b/_examples/slices/slices.go index 4bd2d08..baa5d7e 100644 --- a/_examples/slices/slices.go +++ b/_examples/slices/slices.go @@ -72,4 +72,4 @@ func GetEmptyMatrix(xSize int, ySize int) [][]bool { } return result -} \ No newline at end of file +} From 8e6808ae7495ac5d6c8acd91210ccd6d15846b9d Mon Sep 17 00:00:00 2001 From: "Randall C. O'Reilly" Date: Fri, 3 May 2024 14:50:32 -0700 Subject: [PATCH 13/25] revert #353 -- doesn't work on CI or on my mac, on python 3.11 at least. --- bind/gen_slice.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bind/gen_slice.go b/bind/gen_slice.go index bcf1f6c..c0ef885 100644 --- a/bind/gen_slice.go +++ b/bind/gen_slice.go @@ -409,7 +409,7 @@ otherwise parameter is a python list that we copy from g.gofile.Printf("s := deptrFromHandle_Slice_byte(handle)\n") g.gofile.Printf("ptr := unsafe.Pointer(&s[0])\n") g.gofile.Printf("size := len(s)\n") - g.gofile.Printf("return C.PyBytes_FromStringAndSize((*C.char)(ptr), C.longlong(size))\n") + g.gofile.Printf("return C.PyBytes_FromStringAndSize((*C.char)(ptr), C.long(size))\n") g.gofile.Outdent() g.gofile.Printf("}\n\n") From 3f3fd09c10635d0e3ff021a01f5e98eb0cb63f71 Mon Sep 17 00:00:00 2001 From: "Randall C. O'Reilly" Date: Fri, 3 May 2024 14:57:18 -0700 Subject: [PATCH 14/25] update ci to latest go versions --- .github/workflows/ci.yml | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 5be3246..29987c4 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -13,7 +13,6 @@ env: # GOPY_TRAVIS_CI is set GOPY_TRAVIS_CI: 1 GOTRACEBACK: crash - PYPYVERSION: "v7.1.1" GO111MODULE: auto jobs: @@ -22,7 +21,7 @@ jobs: name: Build strategy: matrix: - go-version: [1.20.x, 1.19.x] + go-version: [1.22.x, 1.21.x] platform: [ubuntu-latest] #platform: [ubuntu-latest, macos-latest, windows-latest] runs-on: ${{ matrix.platform }} @@ -53,18 +52,8 @@ jobs: run: | sudo apt-get update sudo apt-get install curl libffi-dev python3-cffi python3-pip - # pypy3 isn't packaged in ubuntu yet. - TEMPDIR=$(mktemp -d) - curl -L https://downloads.python.org/pypy/pypy3.6-${PYPYVERSION}-linux64.tar.bz2 --output $TEMPDIR/pypy3.tar.bz2 - tar xf $TEMPDIR/pypy3.tar.bz2 -C $TEMPDIR - sudo ln -s $TEMPDIR/pypy3.6-$PYPYVERSION-linux64/bin/pypy3 /usr/local/bin/pypy3 - # curl -L https://bootstrap.pypa.io/get-pip.py --output ${TEMPDIR}/get-pip.py - # pypy3 ${TEMPDIR}/get-pip.py - # install pybindgen python3 -m pip install --user -U pybindgen - # pypy3 -m pip install --user -U pybindgen - # install goimports go install golang.org/x/tools/cmd/goimports@latest From 581b1882a120ce29f1b283f12466171827c474ce Mon Sep 17 00:00:00 2001 From: "Randall C. O'Reilly" Date: Fri, 3 May 2024 15:02:18 -0700 Subject: [PATCH 15/25] try to update appveyor --- appveyor.yml | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/appveyor.yml b/appveyor.yml index 03aeeb6..cb5b45c 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -14,15 +14,13 @@ branches: environment: GOPATH: C:\gopath - GOROOT: C:\go119 + GOROOT: C:\go121 GOPY_APPVEYOR_CI: '1' GOTRACEBACK: 'crash' - #CPYTHON2DIR: "C:\\Python27-x64" - CPYTHON3DIR: "C:\\Python37-x64" - #PATH: '%GOPATH%\bin;%CPYTHON2DIR%;%CPYTHON2DIR%\\Scripts;%CPYTHON3DIR%;%CPYTHON3DIR%\\Scripts;C:\msys64\mingw64\bin;C:\msys64\usr\bin\;%PATH%' + CPYTHON3DIR: "C:\\Python311-x64" PATH: '%GOPATH%\bin;%GOROOT%\bin;%CPYTHON3DIR%;%CPYTHON3DIR%\\Scripts;C:\msys64\mingw64\bin;C:\msys64\usr\bin\;%PATH%' -stack: go 1.19 +stack: go 1.21 build_script: - python --version From ecc3b0766bb55f2af4a625183a2476d29becc985 Mon Sep 17 00:00:00 2001 From: "Randall C. O'Reilly" Date: Fri, 3 May 2024 15:07:20 -0700 Subject: [PATCH 16/25] install goimports --- appveyor.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/appveyor.yml b/appveyor.yml index cb5b45c..afa387b 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -31,6 +31,7 @@ build_script: - go version - go env - go get -v -t ./... + - go install github.com/goki/go-tools/cmd/goimports@latest test_script: - go test ./... From feb8731223cf73d2a21a72b16af2e6fd090cd713 Mon Sep 17 00:00:00 2001 From: "Randall C. O'Reilly" Date: Fri, 3 May 2024 15:08:08 -0700 Subject: [PATCH 17/25] install goimports --- appveyor.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/appveyor.yml b/appveyor.yml index afa387b..98b9cd9 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -31,7 +31,7 @@ build_script: - go version - go env - go get -v -t ./... - - go install github.com/goki/go-tools/cmd/goimports@latest + - go install golang.org/x/tools/cmd/goimports@latest test_script: - go test ./... From 5fb68318e0eb98bcc338a014940e092d4ccc4fb2 Mon Sep 17 00:00:00 2001 From: "Randall C. O'Reilly" Date: Fri, 3 May 2024 15:27:38 -0700 Subject: [PATCH 18/25] generate longlong for gen_slice on windows --- bind/gen_slice.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/bind/gen_slice.go b/bind/gen_slice.go index c0ef885..8df9ded 100644 --- a/bind/gen_slice.go +++ b/bind/gen_slice.go @@ -409,7 +409,11 @@ otherwise parameter is a python list that we copy from g.gofile.Printf("s := deptrFromHandle_Slice_byte(handle)\n") g.gofile.Printf("ptr := unsafe.Pointer(&s[0])\n") g.gofile.Printf("size := len(s)\n") - g.gofile.Printf("return C.PyBytes_FromStringAndSize((*C.char)(ptr), C.long(size))\n") + if WindowsOS { + g.gofile.Printf("return C.PyBytes_FromStringAndSize((*C.char)(ptr), C.longlong(size))\n") + } else { + g.gofile.Printf("return C.PyBytes_FromStringAndSize((*C.char)(ptr), C.long(size))\n") + } g.gofile.Outdent() g.gofile.Printf("}\n\n") From b735a58c6bee594e581610518268e91dce7f7e5b Mon Sep 17 00:00:00 2001 From: "Randall C. O'Reilly" Date: Fri, 3 May 2024 15:39:05 -0700 Subject: [PATCH 19/25] not finding pybindgen but looks like longlong worked. --- appveyor.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/appveyor.yml b/appveyor.yml index 98b9cd9..62425a6 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -27,7 +27,7 @@ build_script: - "%CPYTHON3DIR%\\python --version" - "%CPYTHON3DIR%\\python -m pip install --upgrade pip" - "%CPYTHON3DIR%\\python -m pip install cffi" - - "%CPYTHON3DIR%\\python -m pip install pybindgen" + - "%CPYTHON3DIR%\\python -m pip install --user -U pybindgen" - go version - go env - go get -v -t ./... From 1d7f3a2d29bd9f3921ff640f8e4e8908170ff3bf Mon Sep 17 00:00:00 2001 From: "Randall C. O'Reilly" Date: Fri, 3 May 2024 15:57:34 -0700 Subject: [PATCH 20/25] v0.4.10 release --- Makefile | 2 +- appveyor.yml | 2 +- version.go | 6 +++--- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/Makefile b/Makefile index f8b2bf0..604892a 100644 --- a/Makefile +++ b/Makefile @@ -54,7 +54,7 @@ prereq: # NOTE: MUST update version number here prior to running 'make release' and edit this file! -VERS=v0.4.9 +VERS=v0.4.10 PACKAGE=main GIT_COMMIT=`git rev-parse --short HEAD` VERS_DATE=`date -u +%Y-%m-%d\ %H:%M` diff --git a/appveyor.yml b/appveyor.yml index 62425a6..98b9cd9 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -27,7 +27,7 @@ build_script: - "%CPYTHON3DIR%\\python --version" - "%CPYTHON3DIR%\\python -m pip install --upgrade pip" - "%CPYTHON3DIR%\\python -m pip install cffi" - - "%CPYTHON3DIR%\\python -m pip install --user -U pybindgen" + - "%CPYTHON3DIR%\\python -m pip install pybindgen" - go version - go env - go get -v -t ./... diff --git a/version.go b/version.go index 3410661..55a0f48 100644 --- a/version.go +++ b/version.go @@ -3,7 +3,7 @@ package main const ( - Version = "v0.4.9" - GitCommit = "9dde376" // the commit JUST BEFORE the release - VersionDate = "2024-04-23 00:02" // UTC + Version = "v0.4.10" + GitCommit = "b735a58" // the commit JUST BEFORE the release + VersionDate = "2024-05-03 22:57" // UTC ) From 9fae0b244de4fc78db14a9486ebc46b38218514c Mon Sep 17 00:00:00 2001 From: guoguangwu Date: Sat, 11 May 2024 09:55:01 +0800 Subject: [PATCH 21/25] unconditional use strings.TrimSuffix Signed-off-by: guoguangwu --- bind/utils.go | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/bind/utils.go b/bind/utils.go index cded682..5c13c30 100644 --- a/bind/utils.go +++ b/bind/utils.go @@ -198,12 +198,8 @@ else: fmt.Printf("no LibPy -- set to: %s\n", raw.LibPy) } - if strings.HasSuffix(raw.LibPy, ".a") { - raw.LibPy = raw.LibPy[:len(raw.LibPy)-len(".a")] - } - if strings.HasPrefix(raw.LibPy, "lib") { - raw.LibPy = raw.LibPy[len("lib"):] - } + raw.LibPy = strings.TrimSuffix(raw.LibPy, ".a") + raw.LibPy = strings.TrimPrefix(raw.LibPy, "lib") cfg.Version = raw.Version cfg.ExtSuffix = raw.ExtSuffix From d709cba2c1014cac6fefeab17f02daef60ead761 Mon Sep 17 00:00:00 2001 From: Alex Palaistras Date: Thu, 5 Dec 2024 19:08:11 +0000 Subject: [PATCH 22/25] build: Append existing CFLAGS and LDFLAGS for CGO Underlying `go build` commands can pass custom `CFLAGS` and `LDFLAGS` to CGO invocations, which GoPy uses in order to pass some of its own command-line arguments to builds. However, there are cases where additional, build-specific parameters may need to be used (e.g. in the case where we're linking against a static library which itself links to other dynamic libraries, which aren't set as options in the packages themselves); this commit respects any existing uses of the `CGO_CFLAGS` and `CGO_LDFLAGS` environment variables, and appends their values to ones used internally by GoPy if needed. --- cmd_build.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/cmd_build.go b/cmd_build.go index 7b57a7e..d8fb85f 100644 --- a/cmd_build.go +++ b/cmd_build.go @@ -235,6 +235,9 @@ func runBuild(mode bind.BuildMode, cfg *BuildCfg) error { if include, exists := os.LookupEnv("GOPY_INCLUDE"); exists { cflags = append(cflags, "-I"+filepath.ToSlash(include)) } + if oldcflags, exists := os.LookupEnv("CGO_CFLAGS"); exists { + cflags = append(cflags, oldcflags) + } var ldflags []string if cfg.DynamicLinking { ldflags = strings.Fields(strings.TrimSpace(pycfg.LdDynamicFlags)) @@ -250,6 +253,9 @@ func runBuild(mode bind.BuildMode, cfg *BuildCfg) error { if libname, exists := os.LookupEnv("GOPY_PYLIB"); exists { ldflags = append(ldflags, "-l"+filepath.ToSlash(libname)) } + if oldldflags, exists := os.LookupEnv("CGO_LDFLAGS"); exists { + ldflags = append(ldflags, oldldflags) + } removeEmpty := func(src []string) []string { o := make([]string, 0, len(src)) From 51b165a675da26774cf51b2032a984306cc4784a Mon Sep 17 00:00:00 2001 From: b-long Date: Mon, 5 May 2025 23:47:06 -0400 Subject: [PATCH 23/25] link to GitHub discussions area --- CONTRIBUTE.md | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/CONTRIBUTE.md b/CONTRIBUTE.md index a647624..8b1d620 100644 --- a/CONTRIBUTE.md +++ b/CONTRIBUTE.md @@ -6,8 +6,9 @@ The `go-python` project (and `gopy`) eagerly accepts contributions from the comm The `go-python` project provides libraries and tools in Go for the Go community to better integrate with Python projects and libraries, and we would like you to join us in improving `go-python`'s quality and scope. -This document is for contributors or those interested in contributing. -Questions about `go-python` and the use of its libraries can be directed to the [go-python](mailto:go-python@googlegroups.com) mailing list. +This document is for contributors or those interested in contributing. + +Questions about `gopy` can be directed to the [the `gopy` discussions area in Github: https://github.com/go-python/gopy/discussions]. ## Contributing @@ -35,7 +36,7 @@ As a rule, we keep all tests OK and try to increase code coverage. ### Suggesting Enhancements If the scope of the enhancement is small, open an issue. -If it is large, such as suggesting a new repository, sub-repository, or interface refactoring, then please start a discussion on [the go-python list](https://groups.google.com/forum/#!forum/go-python). +If it is large, such as suggesting a new repository, sub-repository, or interface refactoring, then please start a discussion using [the `gopy` discussions area in Github: https://github.com/go-python/gopy/discussions]. ### Your First Code Contribution @@ -140,3 +141,5 @@ We use [Go style](https://github.com/golang/go/wiki/CodeReviewComments). This _"Contributing"_ guide has been extracted from the [Gonum](https://gonum.org) project. Its guide is [here](https://github.com/gonum/license/blob/master/CONTRIBUTING.md). + +[the `gopy` discussions area in Github: https://github.com/go-python/gopy/discussions]: https://github.com/go-python/gopy/discussions \ No newline at end of file From 8e04ede2007f06c9bd2617f98396f83bbe8bee5e Mon Sep 17 00:00:00 2001 From: Paul Wilde <31094984+pswilde@users.noreply.github.com> Date: Fri, 13 Jun 2025 21:31:02 +0100 Subject: [PATCH 24/25] Add freebsd to main_unix.go Add freebsd compatibility --- main_unix.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/main_unix.go b/main_unix.go index 1acf66f..bd84a69 100644 --- a/main_unix.go +++ b/main_unix.go @@ -2,8 +2,8 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -//go:build (linux && !android) || dragonfly || openbsd -// +build linux,!android dragonfly openbsd +//go:build (linux && !android) || dragonfly || openbsd || freebsd +// +build linux,!android dragonfly openbsd freebsd package main From 592ddf1501315904f9842c9a6fc11496296e6a18 Mon Sep 17 00:00:00 2001 From: coffemakingtoaster Date: Fri, 25 Jul 2025 12:21:21 +0200 Subject: [PATCH 25/25] update clang fastmath --- cmd_build.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd_build.go b/cmd_build.go index d8fb85f..6c63dff 100644 --- a/cmd_build.go +++ b/cmd_build.go @@ -231,7 +231,7 @@ func runBuild(mode bind.BuildMode, cfg *BuildCfg) error { } cflags := strings.Fields(strings.TrimSpace(pycfg.CFlags)) - cflags = append(cflags, "-fPIC", "-Ofast") + cflags = append(cflags, "-fPIC", "-O3", "-ffast-math") if include, exists := os.LookupEnv("GOPY_INCLUDE"); exists { cflags = append(cflags, "-I"+filepath.ToSlash(include)) }