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/
|
# License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||||
|
|
||||||
default:
|
default:
|
||||||
go build -buildmode=c-shared -o gopkg.so main.go
|
go build -o py3-config.out py3-config.go
|
||||||
python3 -c 'import gopkg; print(gopkg.system("time"))'
|
PKG_CONFIG=./py3-config.out go build -buildmode=c-shared -o gopkg.so main.go
|
||||||
python3 -c 'import gopkg; print(gopkg.sum(2, 3))'
|
-rm py3-config.out
|
||||||
|
python3 -c 'import gopkg; print(gopkg.sum(1, 2))'
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
-rm *.so
|
-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. */
|
/* 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:
|
// macOS:
|
||||||
// python3-config --cflags
|
|
||||||
// python3-config --ldflags
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// linux
|
// linux
|
||||||
@ -22,30 +19,22 @@
|
|||||||
#define Py_LIMITED_API
|
#define Py_LIMITED_API
|
||||||
#include <Python.h>
|
#include <Python.h>
|
||||||
|
|
||||||
static PyObject *
|
extern PyObject* PyInit_gopkg();
|
||||||
spam_system(PyObject *self, PyObject *args) {
|
extern PyObject* Py_gopkg_sum(PyObject *, PyObject *);
|
||||||
const char *command;
|
|
||||||
if (!PyArg_ParseTuple(args, "s", &command)) {
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
int status = system(command);
|
static int cgo_PyArg_ParseTuple_ii(PyObject *arg, int *a, int *b) {
|
||||||
return PyLong_FromLong(status);
|
return PyArg_ParseTuple(arg, "ii", a, b);
|
||||||
}
|
}
|
||||||
|
|
||||||
extern PyObject *sum(PyObject *self, PyObject *args);
|
static PyObject* cgo_PyInit_gopkg(void) {
|
||||||
|
static PyMethodDef methods[] = {
|
||||||
static PyMethodDef modMethods[] = {
|
{"sum", Py_gopkg_sum, METH_VARARGS, "Add two numbers."},
|
||||||
{"system", spam_system, METH_VARARGS, "Execute a shell command."},
|
{NULL, NULL, 0, NULL},
|
||||||
{"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,
|
|
||||||
};
|
};
|
||||||
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"
|
#line 1 "cgo-generated-wrapper"
|
||||||
@ -97,10 +86,10 @@ extern "C" {
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
extern PyObject* sum(PyObject* p0, PyObject* p1);
|
|
||||||
|
|
||||||
extern PyObject* PyInit_gopkg();
|
extern PyObject* PyInit_gopkg();
|
||||||
|
|
||||||
|
extern PyObject* Py_gopkg_sum(PyObject* p0, PyObject* p1);
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
@ -5,10 +5,7 @@ package main
|
|||||||
|
|
||||||
/*
|
/*
|
||||||
// macOS:
|
// macOS:
|
||||||
// python3-config --cflags
|
#cgo darwin pkg-config: python3
|
||||||
// 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
|
|
||||||
|
|
||||||
// linux
|
// linux
|
||||||
#cgo linux pkg-config: python3
|
#cgo linux pkg-config: python3
|
||||||
@ -19,51 +16,38 @@ package main
|
|||||||
#define Py_LIMITED_API
|
#define Py_LIMITED_API
|
||||||
#include <Python.h>
|
#include <Python.h>
|
||||||
|
|
||||||
static PyObject *
|
extern PyObject* PyInit_gopkg();
|
||||||
spam_system(PyObject *self, PyObject *args) {
|
extern PyObject* Py_gopkg_sum(PyObject *, PyObject *);
|
||||||
const char *command;
|
|
||||||
if (!PyArg_ParseTuple(args, "s", &command)) {
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
int status = system(command);
|
static int cgo_PyArg_ParseTuple_ii(PyObject *arg, int *a, int *b) {
|
||||||
return PyLong_FromLong(status);
|
return PyArg_ParseTuple(arg, "ii", a, b);
|
||||||
}
|
}
|
||||||
|
|
||||||
extern PyObject *sum(PyObject *self, PyObject *args);
|
static PyObject* cgo_PyInit_gopkg(void) {
|
||||||
|
static PyMethodDef methods[] = {
|
||||||
static PyMethodDef modMethods[] = {
|
{"sum", Py_gopkg_sum, METH_VARARGS, "Add two numbers."},
|
||||||
{"system", spam_system, METH_VARARGS, "Execute a shell command."},
|
{NULL, NULL, 0, NULL},
|
||||||
{"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,
|
|
||||||
};
|
};
|
||||||
return (void*)PyModule_Create(&module);
|
static struct PyModuleDef module = {
|
||||||
|
PyModuleDef_HEAD_INIT, "gopkg", NULL, -1, methods,
|
||||||
|
};
|
||||||
|
return PyModule_Create(&module);
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
import "C"
|
import "C"
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
)
|
|
||||||
|
|
||||||
func main() {}
|
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
|
//export PyInit_gopkg
|
||||||
func PyInit_gopkg() *C.PyObject {
|
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