diff --git a/.circleci/config.yml b/.circleci/config.yml deleted file mode 100644 index 10fad98..0000000 --- a/.circleci/config.yml +++ /dev/null @@ -1,51 +0,0 @@ -version: 2 - -templates: - job_template: &job_template - working_directory: /go/src/github.com/DataDog/go-python3 - 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/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:** + + 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 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 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/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/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..da933c4 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. + +## relation to `sbinet/go-python` -This project was inspired by https://github.com/sbinet/go-python. Go and take a look if we need something for python-2.7! +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,14 @@ 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). + + +# 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 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/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= 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.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..0023d89 100644 --- a/module_test.go +++ b/module_test.go @@ -93,22 +93,19 @@ 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) { 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")) } 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 (