From d65b88bc9d28aa3a0b309b7864ab5528ef88a442 Mon Sep 17 00:00:00 2001 From: M_noAria <46508354+M-Quadra@users.noreply.github.com> Date: Sun, 17 Oct 2021 15:27:44 +0800 Subject: [PATCH 1/9] Fix PyModule_GetState --- module.go | 2 +- module_test.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/module.go b/module.go index 749d487..0c62e65 100644 --- a/module.go +++ b/module.go @@ -58,7 +58,7 @@ func PyModule_GetName(module *PyObject) string { //PyModule_GetState : https://docs.python.org/3/c-api/module.html#c.PyModule_GetState func PyModule_GetState(module *PyObject) unsafe.Pointer { - return unsafe.Pointer(C.PyModule_GetNameObject(toc(module))) + return unsafe.Pointer(C.PyModule_GetState(toc(module))) } //PyModule_GetFilenameObject : https://docs.python.org/3/c-api/module.html#c.PyModule_GetFilenameObject diff --git a/module_test.go b/module_test.go index 9a253fc..67aef4e 100644 --- a/module_test.go +++ b/module_test.go @@ -93,7 +93,7 @@ func TestModuleGetState(t *testing.T) { defer sys.DecRef() state := PyModule_GetState(sys) - assert.NotNil(t, state) + assert.True(t, state == nil) } func TestModuleGetFilenameObject(t *testing.T) { From 8eaf2611eb657da766bbd1d8ab40b924bb0d9a2e Mon Sep 17 00:00:00 2001 From: Christian Korneck Date: Sun, 24 Oct 2021 13:55:07 +0200 Subject: [PATCH 2/9] add .vscode to gitignore --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.gitignore b/.gitignore index 6e85044..9880b30 100644 --- a/.gitignore +++ b/.gitignore @@ -11,3 +11,6 @@ # Output of the go coverage tool, specifically when used with LiteIDE *.out vendor + +# IDE config +.vscode From 374f36bb22a6593c61be011ee90bda41e8c7feed Mon Sep 17 00:00:00 2001 From: Christian Korneck Date: Sun, 24 Oct 2021 14:08:10 +0200 Subject: [PATCH 3/9] rename project to github.com/go-python/cpy3 --- .circleci/config.yml | 2 +- CONTRIBUTING.md | 4 +++- LICENSE | 6 ++++-- README.md | 25 +++++++++++++++++-------- examples/list/main.go | 2 +- examples/python3/main.go | 2 +- script/variadic.go | 2 +- 7 files changed, 28 insertions(+), 15 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 10fad98..580bf49 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -2,7 +2,7 @@ version: 2 templates: job_template: &job_template - working_directory: /go/src/github.com/DataDog/go-python3 + working_directory: /go/src/github.com/go-python/cpy3 docker: - image: golang:1.11.2 diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index ab36fe3..8c70f4e 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -1,3 +1,5 @@ # Contributing -Please, open issues and submit PRs on https://github.com/DataDog/go-python3 using the provided templates. \ No newline at end of file +Please, open issues and submit PRs on https://github.com/go-python/cpy3 using the provided templates. + +Backward compatibility is a priority. Please, avoid introducing breaking changes. (Exceptions need a good reason). \ No newline at end of file diff --git a/LICENSE b/LICENSE index f2bc383..ebbe328 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,8 @@ MIT License -Copyright (c) 2018 Datadog, Inc. +Copyright (c) 2018 - 2021 Datadog, Inc. +Copyright (c) 2018 - 2021 Contributors to the DataDog/go-python3 project +Copyright (c) 2021 Contributors to the go-python/cpy3 project Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal @@ -18,4 +20,4 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. +SOFTWARE. \ No newline at end of file diff --git a/README.md b/README.md index 4c95eba..b7f6057 100644 --- a/README.md +++ b/README.md @@ -1,18 +1,22 @@ -**This repo will be archived on December 1st 2021. While still readable it will no longer be maintained.** - -# go-python3 +# Go bindings for the CPython-3 C-API **Currently supports python-3.7 only.** -Golang bindings for the C-API of CPython-3. - This package provides a ``go`` package named "python" under which most of the ``PyXYZ`` functions and macros of the public C-API of CPython have been exposed. Theoretically, you should be able use https://docs.python.org/3/c-api and know what to type in your ``go`` program. +## relation to `DataDog/go-python3` + +This project is a community maintained successor to [`DataDog/go-python3`](https://github.com/DataDog/go-python3), which will get archived in December 2021. + +- If you use the Go package `github.com/DataDog/go-python3` in your code, you can use `github.com/go-python/cpy3` as a drop-in replacement. We intend to not introduce breaking changes. +- If you have unmerged PRs or open issues on `DataDog/go-python3`, please re-submit them here. -This project was inspired by https://github.com/sbinet/go-python. Go and take a look if we need something for python-2.7! +## relation to `sbinet/go-python` + +This project was inspired by [`sbinet/go-python`](https://github.com/sbinet/go-python) (Go bindings for the CPython-2 C-API). # Install @@ -28,7 +32,7 @@ the `PKG_CONFIG_PATH` environment variable. ## Go get -Then simply `go get github.com/DataDog/go-python3` +Then simply `go get github.com/go-python/cpy3` # API @@ -47,4 +51,9 @@ and error will be set if we failed to open `filename`. If an error is raise before calling th CPython function `int` default to `-1`. -Take a look at some [examples](examples) +Take a look at some [examples](examples) and this [tutorial blogpost](https://poweruser.blog/embedding-python-in-go-338c0399f3d5). + +# Contributing + +Contributions are welcome! See [details](CONTRIBUTING.md). +This project follows the [Go Community Code of Conduct](https://golang.org/conduct). \ No newline at end of file diff --git a/examples/list/main.go b/examples/list/main.go index 5caae40..45a9a15 100644 --- a/examples/list/main.go +++ b/examples/list/main.go @@ -11,7 +11,7 @@ import ( "fmt" "os" - "github.com/DataDog/go-python3" + python3 "github.com/go-python/cpy3" ) func main() { diff --git a/examples/python3/main.go b/examples/python3/main.go index 493256c..dccc4df 100644 --- a/examples/python3/main.go +++ b/examples/python3/main.go @@ -11,7 +11,7 @@ import ( "fmt" "os" - "github.com/DataDog/go-python3" + python3 "github.com/go-python/cpy3" ) func main() { diff --git a/script/variadic.go b/script/variadic.go index 11f4a5f..4c06d34 100644 --- a/script/variadic.go +++ b/script/variadic.go @@ -12,7 +12,7 @@ import ( "fmt" "os" - python "github.com/DataDog/go-python3" + python "github.com/go-python/cpy3" ) var ( From 8c7eedeae77e6dfec1010d375f2a9c5ff3c750df Mon Sep 17 00:00:00 2001 From: Christian Korneck Date: Mon, 25 Oct 2021 23:40:23 +0200 Subject: [PATCH 4/9] add Slack link --- README.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/README.md b/README.md index b7f6057..da933c4 100644 --- a/README.md +++ b/README.md @@ -56,4 +56,9 @@ Take a look at some [examples](examples) and this [tutorial blogpost](https://po # Contributing Contributions are welcome! See [details](CONTRIBUTING.md). + + +# Community +Find us in [`#go-python`](https://gophers.slack.com/archives/C4FDJLLET) on [Gophers Slack](https://gophers.slack.com). ([infos](https://blog.gopheracademy.com/gophers-slack-community/) | [invite](https://invite.slack.golangbridge.org/)) + This project follows the [Go Community Code of Conduct](https://golang.org/conduct). \ No newline at end of file From 621d33a4e42bc068583713509e9121075fa3ba2f Mon Sep 17 00:00:00 2001 From: M_noAria <46508354+M-Quadra@users.noreply.github.com> Date: Sun, 17 Oct 2021 15:27:44 +0800 Subject: [PATCH 5/9] Fix PyModule_GetState --- module.go | 2 +- module_test.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/module.go b/module.go index 749d487..0c62e65 100644 --- a/module.go +++ b/module.go @@ -58,7 +58,7 @@ func PyModule_GetName(module *PyObject) string { //PyModule_GetState : https://docs.python.org/3/c-api/module.html#c.PyModule_GetState func PyModule_GetState(module *PyObject) unsafe.Pointer { - return unsafe.Pointer(C.PyModule_GetNameObject(toc(module))) + return unsafe.Pointer(C.PyModule_GetState(toc(module))) } //PyModule_GetFilenameObject : https://docs.python.org/3/c-api/module.html#c.PyModule_GetFilenameObject diff --git a/module_test.go b/module_test.go index 9a253fc..67aef4e 100644 --- a/module_test.go +++ b/module_test.go @@ -93,7 +93,7 @@ func TestModuleGetState(t *testing.T) { defer sys.DecRef() state := PyModule_GetState(sys) - assert.NotNil(t, state) + assert.True(t, state == nil) } func TestModuleGetFilenameObject(t *testing.T) { From 407b83a936e2c7834b672b5aa3381b155e515c9f Mon Sep 17 00:00:00 2001 From: M_noAria <46508354+M-Quadra@users.noreply.github.com> Date: Tue, 11 Jan 2022 23:57:14 +0800 Subject: [PATCH 6/9] Fix import module name in unit test --- import_test.go | 6 +++--- module_test.go | 13 +++++-------- 2 files changed, 8 insertions(+), 11 deletions(-) diff --git a/import_test.go b/import_test.go index 5da3846..ed2976c 100644 --- a/import_test.go +++ b/import_test.go @@ -24,9 +24,9 @@ func TestImportModule(t *testing.T) { func TestImportModuleEx(t *testing.T) { Py_Initialize() - test := PyImport_ImportModuleEx("test", nil, nil, nil) - assert.NotNil(t, test) - test.DecRef() + queue := PyImport_ImportModuleEx("queue", nil, nil, nil) + assert.NotNil(t, queue) + queue.DecRef() } func TestImportModuleLevelObject(t *testing.T) { diff --git a/module_test.go b/module_test.go index 67aef4e..0023d89 100644 --- a/module_test.go +++ b/module_test.go @@ -99,16 +99,13 @@ func TestModuleGetState(t *testing.T) { func TestModuleGetFilenameObject(t *testing.T) { Py_Initialize() - name := "test" - pyName := PyUnicode_FromString(name) - defer pyName.DecRef() - - test := PyImport_ImportModule(name) - defer test.DecRef() + name := "queue" + queue := PyImport_ImportModule(name) + defer queue.DecRef() - pyFilename := PyModule_GetFilenameObject(test) + pyFilename := PyModule_GetFilenameObject(queue) assert.NotNil(t, pyFilename) filename := PyUnicode_AsUTF8(pyFilename) - assert.True(t, strings.Contains(filename, "/test/__init__.py")) + assert.True(t, strings.HasSuffix(filename, "/queue.py")) } From 98d3175d5fe18af977e7c6b96a8d6863b981187b Mon Sep 17 00:00:00 2001 From: M_noAria <46508354+M-Quadra@users.noreply.github.com> Date: Sat, 11 Jun 2022 21:09:52 +0800 Subject: [PATCH 7/9] Add Go module (#13) --- Gopkg.lock | 33 --------------------------------- Gopkg.toml | 7 ------- go.mod | 10 ++++++++++ go.sum | 6 ++++++ 4 files changed, 16 insertions(+), 40 deletions(-) delete mode 100644 Gopkg.lock delete mode 100644 Gopkg.toml create mode 100644 go.mod create mode 100644 go.sum diff --git a/Gopkg.lock b/Gopkg.lock deleted file mode 100644 index 06b518b..0000000 --- a/Gopkg.lock +++ /dev/null @@ -1,33 +0,0 @@ -# This file is autogenerated, do not edit; changes may be undone by the next 'dep ensure'. - - -[[projects]] - digest = "1:ffe9824d294da03b391f44e1ae8281281b4afc1bdaa9588c9097785e3af10cec" - name = "github.com/davecgh/go-spew" - packages = ["spew"] - pruneopts = "UT" - revision = "8991bc29aa16c548c550c7ff78260e27b9ab7c73" - version = "v1.1.1" - -[[projects]] - digest = "1:0028cb19b2e4c3112225cd871870f2d9cf49b9b4276531f03438a88e94be86fe" - name = "github.com/pmezard/go-difflib" - packages = ["difflib"] - pruneopts = "UT" - revision = "792786c7400a136282c1664665ae0a8db921c6c2" - version = "v1.0.0" - -[[projects]] - digest = "1:18752d0b95816a1b777505a97f71c7467a8445b8ffb55631a7bf779f6ba4fa83" - name = "github.com/stretchr/testify" - packages = ["assert"] - pruneopts = "UT" - revision = "f35b8ab0b5a2cef36673838d662e249dd9c94686" - version = "v1.2.2" - -[solve-meta] - analyzer-name = "dep" - analyzer-version = 1 - input-imports = ["github.com/stretchr/testify/assert"] - solver-name = "gps-cdcl" - solver-version = 1 diff --git a/Gopkg.toml b/Gopkg.toml deleted file mode 100644 index 844c8e7..0000000 --- a/Gopkg.toml +++ /dev/null @@ -1,7 +0,0 @@ -[prune] - go-tests = true - unused-packages = true - -[[constraint]] - name = "github.com/stretchr/testify" - version = "1.2.2" diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..f45baec --- /dev/null +++ b/go.mod @@ -0,0 +1,10 @@ +module github.com/go-python/cpy3 + +go 1.18 + +require github.com/stretchr/testify v1.2.2 + +require ( + github.com/davecgh/go-spew v1.1.1 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..e03ee77 --- /dev/null +++ b/go.sum @@ -0,0 +1,6 @@ +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w= +github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= From 8f74340f20d8e1fd67f3820a9732a0fe5bc41859 Mon Sep 17 00:00:00 2001 From: Christian Korneck Date: Tue, 14 Jun 2022 21:28:23 +0200 Subject: [PATCH 8/9] guide users to GH discussions for usage questions I hope this allows us to: - focus issues to bug, enhancement, question discussions specific to this project (i.e. bugs in this Go library, not the CPython C-API) - allow any kind of broad discussion how to achieve specific things using this library alongside the CPython C-API in Github Discussions --- .github/ISSUE_TEMPLATE.md | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/.github/ISSUE_TEMPLATE.md b/.github/ISSUE_TEMPLATE.md index 220844c..308f44c 100644 --- a/.github/ISSUE_TEMPLATE.md +++ b/.github/ISSUE_TEMPLATE.md @@ -1,9 +1,17 @@ -**Describe what happened:** +------ -**Describe what you expected:** +Note: If you have a **usage question** (i.e. how to achieve something specific using this library + the CPython C-API) consider posting it to [this project's **GitHub Discussions** board](https://github.com/go-python/cpy3/discussions) instead of creating an issue. Any question is welcome! +------ -**Steps to reproduce the issue:** - +**Describe what happened:** + + +**Describe what you expected:** + + +**Steps to reproduce the issue:** + + From ee677121842bb5a02528833c671e0781bf2414f3 Mon Sep 17 00:00:00 2001 From: Adam Bouqdib Date: Mon, 8 Aug 2022 18:56:33 +0100 Subject: [PATCH 9/9] ci: move to github actions --- .circleci/config.yml | 51 -------------------------------------- .github/workflows/test.yml | 44 ++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 51 deletions(-) delete mode 100644 .circleci/config.yml create mode 100644 .github/workflows/test.yml diff --git a/.circleci/config.yml b/.circleci/config.yml deleted file mode 100644 index 580bf49..0000000 --- a/.circleci/config.yml +++ /dev/null @@ -1,51 +0,0 @@ -version: 2 - -templates: - job_template: &job_template - working_directory: /go/src/github.com/go-python/cpy3 - docker: - - image: golang:1.11.2 - -jobs: - # python3.5: - # <<: *job_template - # steps: - # - checkout - # - run: curl https://raw.githubusercontent.com/golang/dep/master/install.sh | sh - # - run: dep ensure - # - run: echo "deb http://deb.debian.org/debian unstable main" >> /etc/apt/sources.list - # - run: apt-get update && apt-get install -y python3.5-dev - # - run: cp /usr/lib/x86_64-linux-gnu/pkgconfig/python-3.5.pc /usr/lib/x86_64-linux-gnu/pkgconfig/python3.pc - # - run: go test -cover - - # python3.6: - # <<: *job_template - # steps: - # - checkout - # - run: curl https://raw.githubusercontent.com/golang/dep/master/install.sh | sh - # - run: dep ensure - # - run: echo "deb http://deb.debian.org/debian unstable main" >> /etc/apt/sources.list - # - run: apt-get update && apt-get install -y python3.6-dev - # - run: cp /usr/lib/x86_64-linux-gnu/pkgconfig/python-3.6.pc /usr/lib/x86_64-linux-gnu/pkgconfig/python3.pc - # - run: go test -cover - - python3.7: - <<: *job_template - steps: - - checkout - - run: curl https://raw.githubusercontent.com/golang/dep/master/install.sh | sh - - run: dep ensure - - run: echo "deb http://deb.debian.org/debian unstable main" >> /etc/apt/sources.list - - run: apt-get update && apt-get install -y python3.7-dev - - run: cp /usr/lib/x86_64-linux-gnu/pkgconfig/python-3.7.pc /usr/lib/x86_64-linux-gnu/pkgconfig/python3.pc - - run: go test -cover - -workflows: - version: 2 - build_test_deploy: - jobs: - # Currently we inconditionally bind some newer functions making 3.5 and 3.6 nott working. - # We could later check for python version and readd the tests - # - python3.5 - # - python3.6 - - python3.7 diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..e460b9f --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,44 @@ +name: Test + +on: + pull_request: + branches: + - master + push: + branches: + - master + +jobs: + test: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + + - uses: actions/setup-go@v3 + with: + go-version: '1.18' + + - uses: actions/setup-python@v4 + with: + python-version: '3.7' + + - id: go-cache-paths + run: | + echo "::set-output name=go-build::$(go env GOCACHE)" + echo "::set-output name=go-mod::$(go env GOMODCACHE)" + + - name: Go Build Cache + uses: actions/cache@v3 + with: + path: ${{ steps.go-cache-paths.outputs.go-build }} + key: ${{ runner.os }}-go-build-${{ github.sha }} + restore-keys: ${{ runner.os }}-go-build- + + - name: Go Mod Cache + uses: actions/cache@v3 + with: + path: ${{ steps.go-cache-paths.outputs.go-mod }} + key: ${{ runner.os }}-go-mod-${{ hashFiles('**/go.sum') }} + restore-keys: ${{ runner.os }}-go-mod- + + - run: go test ./... -cover