mirror of
https://github.com/chai2010/advanced-go-programming-book.git
synced 2025-05-24 04:22:22 +00:00
ch2-07: 改进例子
This commit is contained in:
parent
23d419d3d2
commit
d7850c2d87
@ -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
|
||||
|
||||
|
@ -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 <Python.h>
|
||||
|
||||
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 *);
|
||||
|
||||
static int cgo_PyArg_ParseTuple_ii(PyObject *arg, int *a, int *b) {
|
||||
return PyArg_ParseTuple(arg, "ii", a, b);
|
||||
}
|
||||
|
||||
int status = system(command);
|
||||
return PyLong_FromLong(status);
|
||||
}
|
||||
|
||||
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* cgo_PyInit_gopkg(void) {
|
||||
static PyMethodDef methods[] = {
|
||||
{"sum", Py_gopkg_sum, METH_VARARGS, "Add two numbers."},
|
||||
{NULL, NULL, 0, NULL},
|
||||
};
|
||||
|
||||
static PyObject* PyInit_gopkg_(void) {
|
||||
static struct PyModuleDef module = {
|
||||
PyModuleDef_HEAD_INIT, "gopkg", NULL, -1, modMethods,
|
||||
PyModuleDef_HEAD_INIT, "gopkg", NULL, -1, methods,
|
||||
};
|
||||
return (void*)PyModule_Create(&module);
|
||||
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
|
||||
|
@ -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 <Python.h>
|
||||
|
||||
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 *);
|
||||
|
||||
static int cgo_PyArg_ParseTuple_ii(PyObject *arg, int *a, int *b) {
|
||||
return PyArg_ParseTuple(arg, "ii", a, b);
|
||||
}
|
||||
|
||||
int status = system(command);
|
||||
return PyLong_FromLong(status);
|
||||
}
|
||||
|
||||
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* cgo_PyInit_gopkg(void) {
|
||||
static PyMethodDef methods[] = {
|
||||
{"sum", Py_gopkg_sum, METH_VARARGS, "Add two numbers."},
|
||||
{NULL, NULL, 0, NULL},
|
||||
};
|
||||
|
||||
static PyObject* PyInit_gopkg_(void) {
|
||||
static struct PyModuleDef module = {
|
||||
PyModuleDef_HEAD_INIT, "gopkg", NULL, -1, modMethods,
|
||||
PyModuleDef_HEAD_INIT, "gopkg", NULL, -1, methods,
|
||||
};
|
||||
return (void*)PyModule_Create(&module);
|
||||
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))
|
||||
}
|
||||
|
34
examples/ch2-07/hello-py/py3-config.go
Normal file
34
examples/ch2-07/hello-py/py3-config.go
Normal file
@ -0,0 +1,34 @@
|
||||
// Copyright © 2017 ChaiShushan <chaishushan{AT}gmail.com>.
|
||||
// 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
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user