Skip to content

Commit 09128f9

Browse files
authored
Bump to python 3.12 (#9)
* Bump to python-3.12 * Fix lint issue * Bump go version * Fix some minor issues * Fix lint issues * Fix lint issues * Try to fix test errors * Fix test errors * Fix lint issues * Try to fix tests * Fix tests
1 parent 380234f commit 09128f9

32 files changed

+235
-123
lines changed

.golangci.yaml

Lines changed: 53 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,77 @@
1-
# See https://github.com/golangci/golangci-lint/blob/master/.golangci.example.yml
1+
version: "2"
22
run:
33
tests: true
4-
timeout: 10m
5-
6-
linters-settings:
7-
errcheck:
8-
check-type-assertions: true
9-
check-blank: true
10-
gocyclo:
11-
min-complexity: 20
12-
dupl:
13-
threshold: 100
14-
misspell:
15-
locale: US
16-
revive:
17-
rules:
18-
- name: var-naming
19-
disabled: true
20-
stylecheck:
21-
checks: ["all", "-ST1003"]
22-
unused:
23-
check-exported: false
24-
unparam:
25-
check-exported: true
26-
274
linters:
28-
enable-all: true
5+
default: all
296
disable:
307
- copyloopvar
318
- depguard
329
- exhaustruct
33-
- exportloopref
3410
- forbidigo
3511
- forcetypeassert
36-
- gci
12+
- funcorder
3713
- gochecknoglobals
3814
- intrange
3915
- ireturn
4016
- lll
4117
- mnd
4218
- nlreturn
19+
- noinlineerr
20+
- nolintlint
4321
- nonamedreturns
44-
- nolintlint # https://github.com/golangci/golangci-lint/issues/3063
4522
- paralleltest
23+
- perfsprint
24+
- staticcheck
4625
- tagalign
4726
- tagliatelle
4827
- testpackage
4928
- varnamelen
5029
- wrapcheck
51-
30+
- wsl
31+
- wsl_v5
32+
settings:
33+
dupl:
34+
threshold: 100
35+
errcheck:
36+
check-type-assertions: true
37+
check-blank: true
38+
gocyclo:
39+
min-complexity: 20
40+
misspell:
41+
locale: US
42+
revive:
43+
rules:
44+
- name: var-naming
45+
disabled: true
46+
staticcheck:
47+
checks:
48+
- -ST1003
49+
- all
50+
exclusions:
51+
generated: lax
52+
rules:
53+
- linters:
54+
- dupl
55+
- err113
56+
- funlen
57+
- goconst
58+
- mnd
59+
- noctx
60+
path: _test.go
61+
paths:
62+
- third_party$
63+
- builtin$
64+
- examples$
5265
issues:
5366
max-same-issues: 50
54-
exclude-use-default: false
55-
exclude-rules:
56-
- linters:
57-
- dupl
58-
- funlen
59-
- goconst
60-
- err113
61-
- mnd
62-
- noctx
63-
path: "_test.go"
67+
formatters:
68+
enable:
69+
- gofmt
70+
- gofumpt
71+
- goimports
72+
exclusions:
73+
generated: lax
74+
paths:
75+
- third_party$
76+
- builtin$
77+
- examples$

.python-version

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
3.12.11

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ MODULE_NAME=cpy
22

33
VENDOR_DIR = vendor
44

5-
PYTHON_VERSION ?= 3.11
6-
GOLANGCI_LINT_VERSION ?= v1.63.4
5+
PYTHON_VERSION ?= 3.12
6+
GOLANGCI_LINT_VERSION ?= v2.2.1
77

88
GO ?= go
99
GOLANGCI_LINT ?= $(shell go env GOPATH)/bin/golangci-lint-$(GOLANGCI_LINT_VERSION)

README.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
# Go bindings for the CPython-3 C-API
22

33
> [!IMPORTANT]
4-
> **Currently supports python-3.11 only.**
4+
> **Currently supports python-3.12 only.**
55
66
This package provides a ``go`` package named "python" under which most of the ``PyXYZ`` functions and macros of the
77
public C-API of CPython have been exposed. Theoretically, you should be able use https://docs.python.org/3/c-api
88
and know what to type in your ``go`` program.
99

1010
## Prerequisites
1111

12-
- `go >= 1.21`
13-
- `python = 3.11.x`
12+
- `go >= 1.24`
13+
- `python = 3.12.x`
1414

1515
### MacOS
16-
- `brew install python@3.11`
16+
- `brew install python@3.12`
1717
- `brew install pkg-config`
1818

1919
### Linux
2020

21-
We will need `pkg-config` and a working `python3.11` environment to build these bindings. Make sure you have Python
22-
libraries and header files installed as well (`python3.11-dev` on Debian, `brew install python@3.11` on macOS, or
21+
We will need `pkg-config` and a working `python3.12` environment to build these bindings. Make sure you have Python
22+
libraries and header files installed as well (`python3.12-dev` on Debian, `brew install python@3.12` on macOS, or
2323
`python3-devel` on Centos for example).
2424

2525
By default `pkg-config` will look at the `python3` library so if you want to choose a specific version just symlink
@@ -50,10 +50,10 @@ Take a look at some [examples](examples) and this [tutorial blogpost](https://po
5050

5151
## Versioning
5252

53-
We follow the versioning of the CPython API. The version of this package is `3.11.x` which means it supports the CPython
54-
API version `3.11`. However, the patch version `x` is used to indicate the version of this package, not the CPython API.
55-
If you see a version `3.11.7`, it doesn't mean the module only supports CPython API version `3.11.7`, it's just the 7th
56-
time we patch to support the CPython API version `3.11`.
53+
We follow the versioning of the CPython API. The version of this package is `3.12.x` which means it supports the CPython
54+
API version `3.12`. However, the patch version `x` is used to indicate the version of this package, not the CPython API.
55+
If you see a version `3.12.7`, it doesn't mean the module only supports CPython API version `3.12.7`, it's just the 7th
56+
time we patch to support the CPython API version `3.12`.
5757

5858
## Contributing
5959

boolean_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,15 @@ import (
1010

1111
func TestBoolCheck(t *testing.T) {
1212
cpy.Py_Initialize()
13+
defer cpy.Py_FinalizeEx()
1314

1415
assert.True(t, cpy.PyBool_Check(cpy.Py_True))
1516
assert.True(t, cpy.PyBool_Check(cpy.Py_False))
1617
}
1718

1819
func TestBoolFromLong(t *testing.T) {
1920
cpy.Py_Initialize()
21+
defer cpy.Py_FinalizeEx()
2022

2123
assert.Equal(t, cpy.Py_True, cpy.PyBool_FromLong(1))
2224
assert.Equal(t, cpy.Py_False, cpy.PyBool_FromLong(0))

byte_array_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010

1111
func TestByteArrayCheck(t *testing.T) {
1212
cpy.Py_Initialize()
13+
defer cpy.Py_FinalizeEx()
1314

1415
s1 := "aaaaaaaa"
1516

@@ -22,6 +23,7 @@ func TestByteArrayCheck(t *testing.T) {
2223

2324
func TestByteArrayFromAsString(t *testing.T) {
2425
cpy.Py_Initialize()
26+
defer cpy.Py_FinalizeEx()
2527

2628
s1 := "aaaaaaaa"
2729

@@ -33,6 +35,7 @@ func TestByteArrayFromAsString(t *testing.T) {
3335

3436
func TestByteArrayConcat(t *testing.T) {
3537
cpy.Py_Initialize()
38+
defer cpy.Py_FinalizeEx()
3639

3740
s1 := "aaaaaaaa"
3841
s2 := "bbbbbbbb"
@@ -58,6 +61,7 @@ func TestByteArrayConcat(t *testing.T) {
5861

5962
func TestByteArrayResize(t *testing.T) {
6063
cpy.Py_Initialize()
64+
defer cpy.Py_FinalizeEx()
6165

6266
s1 := "aaaaaaaa"
6367

bytes_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010

1111
func TestBytesCheck(t *testing.T) {
1212
cpy.Py_Initialize()
13+
defer cpy.Py_FinalizeEx()
1314

1415
s1 := "aaaaaaaa"
1516

@@ -22,6 +23,7 @@ func TestBytesCheck(t *testing.T) {
2223

2324
func TestBytesFromAsString(t *testing.T) {
2425
cpy.Py_Initialize()
26+
defer cpy.Py_FinalizeEx()
2527

2628
s1 := "aaaaaaaa"
2729

@@ -33,6 +35,7 @@ func TestBytesFromAsString(t *testing.T) {
3335

3436
func TestBytesSize(t *testing.T) {
3537
cpy.Py_Initialize()
38+
defer cpy.Py_FinalizeEx()
3639

3740
s1 := "aaaaaaaa"
3841

@@ -44,6 +47,7 @@ func TestBytesSize(t *testing.T) {
4447

4548
func TestBytesConcat(t *testing.T) {
4649
cpy.Py_Initialize()
50+
defer cpy.Py_FinalizeEx()
4751

4852
s1 := "aaaaaaaa"
4953
s2 := "bbbbbbbb"
@@ -67,6 +71,7 @@ func TestBytesConcat(t *testing.T) {
6771

6872
func TestBytesConcatAndDel(t *testing.T) {
6973
cpy.Py_Initialize()
74+
defer cpy.Py_FinalizeEx()
7075

7176
s1 := "aaaaaaaa"
7277
s2 := "bbbbbbbb"
@@ -86,6 +91,7 @@ func TestBytesConcatAndDel(t *testing.T) {
8691

8792
func TestByteSlices(t *testing.T) {
8893
cpy.Py_Initialize()
94+
defer cpy.Py_FinalizeEx()
8995

9096
s1 := []byte("aaaaaaaa")
9197
s2 := []byte("bbbbbbbb")

complex_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010

1111
func TestComplex(t *testing.T) {
1212
cpy.Py_Initialize()
13+
defer cpy.Py_FinalizeEx()
1314

1415
nReal := 2.
1516
nImaginary := 5.

dict_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010

1111
func TestDict(t *testing.T) {
1212
cpy.Py_Initialize()
13+
defer cpy.Py_FinalizeEx()
1314

1415
dict := cpy.PyDict_New()
1516
defer dict.DecRef()

errors.go

Lines changed: 12 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -176,44 +176,23 @@ func PyErr_ExceptionMatches(exc *PyObject) bool {
176176
return ret == 1
177177
}
178178

179-
// PyErr_Fetch retrieves the error indicator into three variables whose addresses are passed. If the error indicator is
180-
// not set, set all three variables to NULL. If it is set, it will be cleared and you own a reference to each object
181-
// retrieved. The value and traceback object may be NULL even when the type object is not.
179+
// PyErr_GetRaisedException Return the exception currently being raised, clearing the error indicator at the same time.
180+
// Return NULL if the error indicator is not set.
182181
//
183-
// Reference: https://docs.python.org/3/c-api/exceptions.html#c.PyErr_Fetch
184-
func PyErr_Fetch() (*PyObject, *PyObject, *PyObject) {
185-
var pyType, value, traceback *C.PyObject
186-
187-
C.PyErr_Fetch(&pyType, &value, &traceback) //nolint: gocritic
188-
189-
return togo(pyType), togo(value), togo(traceback)
190-
}
191-
192-
// PyErr_Restore sets the error indicator from the three objects, type, value, and traceback, clearing the existing
193-
// exception if one is set. If the objects are NULL, the error indicator is cleared. Do not pass a NULL type and
194-
// non-NULL value or traceback. The exception type should be a class. Do not pass an invalid exception type or value.
195-
// (Violating these rules will cause subtle problems later.) This call takes away a reference to each object: you must
196-
// own a reference to each object before the call and after the call you no longer own these references.
197-
// (If you don't understand this, don't use this function. I warned you.)
182+
// This function is used by code that needs to catch exceptions, or code that needs to save and restore the error
183+
// indicator temporarily.
198184
//
199-
// Reference: https://docs.python.org/3/c-api/exceptions.html#c.PyErr_Restore
200-
func PyErr_Restore(pyType *PyObject, value *PyObject, traceback *PyObject) {
201-
C.PyErr_Restore(toc(pyType), toc(value), toc(traceback))
185+
// Reference: https://docs.python.org/3/c-api/exceptions.html#c.PyErr_GetRaisedException
186+
func PyErr_GetRaisedException() *PyObject {
187+
return togo(C.PyErr_GetRaisedException())
202188
}
203189

204-
// PyErr_NormalizeException is used to instantiate the class in that the values returned by PyErr_Fetch() below can be
205-
// "unnormalized", meaning that *exc is a class object but *val is not an instance of the same class. If the values are
206-
// already normalized, nothing happens. The delayed normalization is implemented to improve performance.
190+
// PyErr_SetRaisedException Set exc as the exception currently being raised, clearing the existing exception if one is
191+
// set.
207192
//
208-
// Reference: https://docs.python.org/3/c-api/exceptions.html#c.PyErr_NormalizeException
209-
func PyErr_NormalizeException(exc, val, tb *PyObject) (*PyObject, *PyObject, *PyObject) {
210-
cexc := toc(exc)
211-
cval := toc(val)
212-
ctb := toc(tb)
213-
214-
C.PyErr_NormalizeException(&cexc, &cval, &ctb) //nolint: gocritic
215-
216-
return togo(cexc), togo(cval), togo(ctb)
193+
// Reference: https://docs.python.org/3/c-api/exceptions.html#c.PyErr_SetRaisedException
194+
func PyErr_SetRaisedException(exec *PyObject) {
195+
C.PyErr_SetRaisedException(toc(exec))
217196
}
218197

219198
// PyErr_GetExcInfo retrieves the old-style representation of the exception info, as known from sys.exc_info(). This

0 commit comments

Comments
 (0)