Skip to content
This repository was archived by the owner on Jul 24, 2023. It is now read-only.

Commit 2665c25

Browse files
committed
object: handle nil arguments for topy to match CPython semantics
Fixes #61
1 parent 2163105 commit 2665c25

File tree

4 files changed

+41
-2
lines changed

4 files changed

+41
-2
lines changed

object.go

+3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ func (self *PyObject) topy() *C.PyObject {
2020
}
2121

2222
func topy(self *PyObject) *C.PyObject {
23+
if self == nil {
24+
return nil
25+
}
2326
return self.ptr
2427
}
2528

python_test.go

+10
Original file line numberDiff line numberDiff line change
@@ -130,3 +130,13 @@ ival=1666
130130
`),
131131
})
132132
}
133+
134+
func TestIssue61(t *testing.T) {
135+
t.Parallel()
136+
testPkg(t, pkg{
137+
path: "tests/issue61",
138+
want: []byte(`['i want this gone']
139+
[]
140+
`),
141+
})
142+
}

sequence.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -323,8 +323,9 @@ func PyList_Append(self, item *PyObject) error {
323323
//
324324
// Changed in version 2.5: This function used an int for low and high. This might require changes in your code for properly supporting 64-bit systems.
325325
func PyList_SetSlice(self *PyObject, low, high int, itemlist *PyObject) error {
326-
err := C.PyList_SetSlice(topy(self), C.Py_ssize_t(low), C.Py_ssize_t(high),
327-
topy(itemlist))
326+
err := C.PyList_SetSlice(
327+
topy(self), C.Py_ssize_t(low), C.Py_ssize_t(high), topy(itemlist),
328+
)
328329
return int2err(err)
329330
}
330331

tests/issue61/main.go

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"log"
6+
7+
"github.com/sbinet/go-python"
8+
)
9+
10+
func init() {
11+
err := python.Initialize()
12+
if err != nil {
13+
log.Panic(err)
14+
}
15+
}
16+
17+
func main() {
18+
19+
origList := python.PyList_New(0)
20+
python.PyList_Append(origList, python.PyString_FromString("i want this gone"))
21+
fmt.Println(python.PyString_AsString(origList.Str()))
22+
23+
python.PyList_SetSlice(origList, 0, 1, nil)
24+
fmt.Println(python.PyString_AsString(origList.Str()))
25+
}

0 commit comments

Comments
 (0)