Skip to content
This repository was archived by the owner on Jan 20, 2025. It is now read-only.

Commit 3952ed1

Browse files
committed
simplify red() with PyArg_ParseTuple_s
1 parent 919184b commit 3952ed1

File tree

2 files changed

+8
-11
lines changed

2 files changed

+8
-11
lines changed

red_proj/red.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
PyObject* red(PyObject*);
55

66
/* To shim go's missing variadic function support */
7-
int PyArg_ParseTuple_U(PyObject* args, PyObject** obj) {
8-
return PyArg_ParseTuple(args, "U", obj);
7+
int PyArg_ParseTuple_s(PyObject* args, char** obj) {
8+
return PyArg_ParseTuple(args, "s", obj);
99
}
1010

1111
static struct PyMethodDef methods[] = {

red_proj/red.go

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,22 @@ package main
22

33
// #include <stdlib.h>
44
// #include <Python.h>
5-
// int PyArg_ParseTuple_U(PyObject*, PyObject**);
5+
// int PyArg_ParseTuple_s(PyObject*, char**);
66
import "C"
77
import "unsafe"
88
import "github.com/mgutz/ansi"
99

1010
//export red
1111
func red(self *C.PyObject, args *C.PyObject) *C.PyObject {
12-
var obj *C.PyObject
13-
if C.PyArg_ParseTuple_U(args, &obj) == 0 {
12+
var cstr *C.char
13+
if C.PyArg_ParseTuple_s(args, &cstr) == 0 {
1414
return nil
1515
}
16-
bytes := C.PyUnicode_AsUTF8String(obj)
17-
cstr := C.PyBytes_AsString(bytes)
1816
red := ansi.Color(C.GoString(cstr), "red")
19-
cstr = C.CString(red)
20-
ret := C.PyUnicode_FromString(cstr)
2117

22-
C.free(unsafe.Pointer(cstr))
23-
C.Py_DecRef(bytes)
18+
gocstr := C.CString(red)
19+
ret := C.PyUnicode_FromString(gocstr)
20+
C.free(unsafe.Pointer(gocstr))
2421

2522
return ret
2623
}

0 commit comments

Comments
 (0)