From d7850c2d876828900f1ebc7ed047c0aeba65abc2 Mon Sep 17 00:00:00 2001 From: chai2010 Date: Fri, 12 Jan 2018 01:25:38 +0800 Subject: [PATCH] =?UTF-8?q?ch2-07:=20=E6=94=B9=E8=BF=9B=E4=BE=8B=E5=AD=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- examples/ch2-07/hello-py/Makefile | 12 ++--- examples/ch2-07/hello-py/gopkg.h | 41 +++++++---------- examples/ch2-07/hello-py/main.go | 62 ++++++++++---------------- examples/ch2-07/hello-py/py3-config.go | 34 ++++++++++++++ 4 files changed, 76 insertions(+), 73 deletions(-) create mode 100644 examples/ch2-07/hello-py/py3-config.go diff --git a/examples/ch2-07/hello-py/Makefile b/examples/ch2-07/hello-py/Makefile index bb54aa2..d224e8d 100644 --- a/examples/ch2-07/hello-py/Makefile +++ b/examples/ch2-07/hello-py/Makefile @@ -2,14 +2,10 @@ # License: https://creativecommons.org/licenses/by-nc-sa/4.0/ default: - go build -buildmode=c-shared -o gopkg.so main.go - python3 -c 'import gopkg; print(gopkg.system("time"))' - python3 -c 'import gopkg; print(gopkg.sum(2, 3))' + go build -o py3-config.out py3-config.go + PKG_CONFIG=./py3-config.out go build -buildmode=c-shared -o gopkg.so main.go + -rm py3-config.out + python3 -c 'import gopkg; print(gopkg.sum(1, 2))' clean: -rm *.so - -# PKG_CONFIG=python3-config go build -buildmode=c-shared -o say-hello.so main.go -# python3-config --ldflags -# python3-config --include - diff --git a/examples/ch2-07/hello-py/gopkg.h b/examples/ch2-07/hello-py/gopkg.h index a5f4a5a..68ac6e6 100644 --- a/examples/ch2-07/hello-py/gopkg.h +++ b/examples/ch2-07/hello-py/gopkg.h @@ -5,12 +5,9 @@ /* Start of preamble from import "C" comments. */ -#line 3 "/Users/chai/go/src/github.com/chai2010/advanced-go-programming-book/examples/ch2-07/hello-py/main.go" +#line 6 "/Users/chai/go/src/github.com/chai2010/advanced-go-programming-book/examples/ch2-07/hello-py/main.go" // macOS: -// python3-config --cflags -// python3-config --ldflags - // linux @@ -22,30 +19,22 @@ #define Py_LIMITED_API #include -static PyObject * -spam_system(PyObject *self, PyObject *args) { - const char *command; - if (!PyArg_ParseTuple(args, "s", &command)) { - return NULL; - } +extern PyObject* PyInit_gopkg(); +extern PyObject* Py_gopkg_sum(PyObject *, PyObject *); - int status = system(command); - return PyLong_FromLong(status); +static int cgo_PyArg_ParseTuple_ii(PyObject *arg, int *a, int *b) { + return PyArg_ParseTuple(arg, "ii", a, b); } -extern PyObject *sum(PyObject *self, PyObject *args); - -static PyMethodDef modMethods[] = { - {"system", spam_system, METH_VARARGS, "Execute a shell command."}, - {"sum", sum, METH_VARARGS, "Execute a shell command."}, - {NULL, NULL, 0, NULL} -}; - -static PyObject* PyInit_gopkg_(void) { - static struct PyModuleDef module = { - PyModuleDef_HEAD_INIT, "gopkg", NULL, -1, modMethods, +static PyObject* cgo_PyInit_gopkg(void) { + static PyMethodDef methods[] = { + {"sum", Py_gopkg_sum, METH_VARARGS, "Add two numbers."}, + {NULL, NULL, 0, NULL}, }; - return (void*)PyModule_Create(&module); + static struct PyModuleDef module = { + PyModuleDef_HEAD_INIT, "gopkg", NULL, -1, methods, + }; + return PyModule_Create(&module); } #line 1 "cgo-generated-wrapper" @@ -97,10 +86,10 @@ extern "C" { #endif -extern PyObject* sum(PyObject* p0, PyObject* p1); - extern PyObject* PyInit_gopkg(); +extern PyObject* Py_gopkg_sum(PyObject* p0, PyObject* p1); + #ifdef __cplusplus } #endif diff --git a/examples/ch2-07/hello-py/main.go b/examples/ch2-07/hello-py/main.go index 81c3d3a..9262704 100644 --- a/examples/ch2-07/hello-py/main.go +++ b/examples/ch2-07/hello-py/main.go @@ -5,10 +5,7 @@ package main /* // macOS: -// python3-config --cflags -// python3-config --ldflags -#cgo darwin CFLAGS: -I/Library/Frameworks/Python.framework/Versions/3.6/include/python3.6m -I/Library/Frameworks/Python.framework/Versions/3.6/include/python3.6m -fno-strict-aliasing -Wsign-compare -fno-common -dynamic -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -g -#cgo darwin LDFLAGS: -L/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/config-3.6m-darwin -lpython3.6m -ldl -framework CoreFoundation +#cgo darwin pkg-config: python3 // linux #cgo linux pkg-config: python3 @@ -19,51 +16,38 @@ package main #define Py_LIMITED_API #include -static PyObject * -spam_system(PyObject *self, PyObject *args) { - const char *command; - if (!PyArg_ParseTuple(args, "s", &command)) { - return NULL; - } +extern PyObject* PyInit_gopkg(); +extern PyObject* Py_gopkg_sum(PyObject *, PyObject *); - int status = system(command); - return PyLong_FromLong(status); +static int cgo_PyArg_ParseTuple_ii(PyObject *arg, int *a, int *b) { + return PyArg_ParseTuple(arg, "ii", a, b); } -extern PyObject *sum(PyObject *self, PyObject *args); - -static PyMethodDef modMethods[] = { - {"system", spam_system, METH_VARARGS, "Execute a shell command."}, - {"sum", sum, METH_VARARGS, "Execute a shell command."}, - {NULL, NULL, 0, NULL} -}; - -static PyObject* PyInit_gopkg_(void) { - static struct PyModuleDef module = { - PyModuleDef_HEAD_INIT, "gopkg", NULL, -1, modMethods, +static PyObject* cgo_PyInit_gopkg(void) { + static PyMethodDef methods[] = { + {"sum", Py_gopkg_sum, METH_VARARGS, "Add two numbers."}, + {NULL, NULL, 0, NULL}, }; - return (void*)PyModule_Create(&module); + static struct PyModuleDef module = { + PyModuleDef_HEAD_INIT, "gopkg", NULL, -1, methods, + }; + return PyModule_Create(&module); } */ import "C" -import ( - "fmt" -) - func main() {} -// export SayHello -func SayHello(name *C.char) { - fmt.Printf("hello %s!\n", C.GoString(name)) -} - -//export sum -func sum(self, args *C.PyObject) *C.PyObject { - return C.PyLong_FromLongLong(9527) // TODO -} - //export PyInit_gopkg func PyInit_gopkg() *C.PyObject { - return C.PyInit_gopkg_() + return C.cgo_PyInit_gopkg() +} + +//export Py_gopkg_sum +func Py_gopkg_sum(self, args *C.PyObject) *C.PyObject { + var a, b C.int + if C.cgo_PyArg_ParseTuple_ii(args, &a, &b) == 0 { + return nil + } + return C.PyLong_FromLong(C.long(a + b)) } diff --git a/examples/ch2-07/hello-py/py3-config.go b/examples/ch2-07/hello-py/py3-config.go new file mode 100644 index 0000000..64cdbbe --- /dev/null +++ b/examples/ch2-07/hello-py/py3-config.go @@ -0,0 +1,34 @@ +// Copyright © 2017 ChaiShushan . +// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ + +// +build ignore +// +build darwin + +// fix python3-config for cgo build + +package main + +import ( + "bytes" + "fmt" + "os" + "os/exec" +) + +func main() { + for _, s := range os.Args { + if s == "--cflags" { + out, _ := exec.Command("python3-config", "--cflags").CombinedOutput() + out = bytes.Replace(out, []byte("-arch"), []byte{}, -1) + out = bytes.Replace(out, []byte("i386"), []byte{}, -1) + out = bytes.Replace(out, []byte("x86_64"), []byte{}, -1) + fmt.Print(string(out)) + return + } + if s == "--libs" { + out, _ := exec.Command("python3-config", "--ldflags").CombinedOutput() + fmt.Print(string(out)) + return + } + } +}