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

Commit 46d882b

Browse files
icholysbinet
authored andcommitted
object: always return the same value for Py_None, Py_True, and Py_False
* tests: add failing test for None check * object: always return the same value for Py_None, Py_True, and Py_False
1 parent 092ab27 commit 46d882b

File tree

6 files changed

+59
-5
lines changed

6 files changed

+59
-5
lines changed

none.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ import "C"
66
// The Python None object, denoting lack of value. This object has no methods.
77
// It needs to be treated just like any other object with respect to reference
88
// counts.
9-
var Py_None = togo(C._gopy_pynone())
9+
var Py_None = &PyObject{ptr: C._gopy_pynone()}
1010

1111
// EOF

numeric.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -332,13 +332,13 @@ func PyBool_Check(self *PyObject) bool {
332332
// The Python False object. This object has no methods.
333333
// It needs to be treated just like any other object with respect to
334334
// reference counts.
335-
var Py_False = togo(C._gopy_pyfalse())
335+
var Py_False = &PyObject{ptr: C._gopy_pyfalse()}
336336

337337
// PyObject* Py_True
338338
// The Python True object. This object has no methods.
339339
// It needs to be treated just like any other object with respect to
340340
// reference counts.
341-
var Py_True = togo(C._gopy_pytrue())
341+
var Py_True = &PyObject{ptr: C._gopy_pytrue()}
342342

343343
/*
344344
Py_RETURN_FALSE

object.go

+10-2
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,18 @@ func topy(self *PyObject) *C.PyObject {
3434
}
3535

3636
func togo(obj *C.PyObject) *PyObject {
37-
if obj == nil {
37+
switch obj {
38+
case nil:
3839
return nil
40+
case Py_None.ptr:
41+
return Py_None
42+
case Py_True.ptr:
43+
return Py_True
44+
case Py_False.ptr:
45+
return Py_False
46+
default:
47+
return &PyObject{ptr: obj}
3948
}
40-
return &PyObject{ptr: obj}
4149
}
4250

4351
// PyObject_FromVoidPtr converts a PyObject from an unsafe.Pointer

python_test.go

+9
Original file line numberDiff line numberDiff line change
@@ -140,3 +140,12 @@ func TestIssue61(t *testing.T) {
140140
`),
141141
})
142142
}
143+
144+
func TestCheckNone(t *testing.T) {
145+
t.Parallel()
146+
testPkg(t, pkg{
147+
path: "tests/none-check",
148+
want: []byte(`type=<type 'NoneType'>, str=None, eq_none=true
149+
`),
150+
})
151+
}

tests/none-check/get_none.py

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/usr/bin/env python2
2+
3+
def get_none():
4+
return None
5+

tests/none-check/main.go

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
module := python.PyImport_ImportModule("get_none")
19+
if module == nil {
20+
log.Fatal("could not import 'get_none' module")
21+
}
22+
get_none := module.GetAttrString("get_none")
23+
if get_none == nil {
24+
log.Fatal("could not import 'get_none' function")
25+
}
26+
none := get_none.CallFunction()
27+
fmt.Printf("type=%s, str=%s, eq_none=%t\n",
28+
python.PyString_AsString(none.Type().Str()),
29+
python.PyString_AsString(none.Str()),
30+
none == python.Py_None,
31+
)
32+
}

0 commit comments

Comments
 (0)