GithubHelp home page GithubHelp logo

py's Introduction

py - Golang bindings to the CPython C-API

NOTE: This project is unmaintained. Maybe https://github.com/go-python/cpy3 is a good replacement.

py is Golang bindings to the CPython C-API.

py project's homepage is: https://github.com/qiniu/py

Install

go get github.com/qiniu/py

Example

package main

import (
	"fmt"
	"github.com/qiniu/log"
	"github.com/qiniu/py"
)

// -------------------------------------------------------------------

type FooModule struct {
}

func (r *FooModule) Py_bar(args *py.Tuple) (ret *py.Base, err error) {
	var i int
	var s string
	err = py.Parse(args, &i, &s)
	if err != nil {
		return
	}
	fmt.Println("call foo.bar:", i, s)
	return py.IncNone(), nil
}

func (r *FooModule) Py_bar2(args *py.Tuple) (ret *py.Base, err error) {
	var i int
	var s []string
	err = py.ParseV(args, &i, &s)
	if err != nil {
		return
	}
	fmt.Println("call foo.bar2:", i, s)
	return py.IncNone(), nil
}

// -------------------------------------------------------------------

const pyCode = `

import foo
foo.bar(1, 'Hello')
foo.bar2(1, 'Hello', 'world!')
`

func main() {

	gomod, err := py.NewGoModule("foo", "", new(FooModule))
	if err != nil {
		log.Fatal("NewGoModule failed:", err)
	}
	defer gomod.Decref()

	code, err := py.Compile(pyCode, "", py.FileInput)
	if err != nil {
		log.Fatal("Compile failed:", err)
	}
	defer code.Decref()

	mod, err := py.ExecCodeModule("test", code.Obj())
	if err != nil {
		log.Fatal("ExecCodeModule failed:", err)
	}
	defer mod.Decref()
}

// -------------------------------------------------------------------

py's People

Contributors

xushiwei avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

py's Issues

python2.7.4下编译不通过

在python2.7.4环境下安装,出现以下错误:

# github.com/qiniu/py
In file included from /usr/include/python2.7/Python.h:126:0,
                 from class.go:3:
/usr/include/python2.7/modsupport.h:27:1: error: 'PyArg_ParseTuple' is an unrecognized format function type [-Werror=format=]
 PyAPI_FUNC(int) PyArg_ParseTuple(PyObject *, const char *, ...) Py_FORMAT_PARSETUPLE(PyArg_ParseTuple, 2, 3);
 ^
cc1: all warnings being treated as errors

Unable to go get package

executing command:
go get github.com/qiniu/py

results in error:
github.com/qiniu/py imports github.com/qiniu/errors: cannot find module providing package github.com/qiniu/errors github.com/qiniu/py imports github.com/qiniu/log: cannot find module providing package github.com/qiniu/log

This is because the imported packages "error" and "log" don't exist in the repo.
Thanks in advance.

win10安装错误

打算在windows下go调用python,但是在go get时发生错误

C:\Users\Administrator>go get github.com/qiniu/py
# pkg-config --cflags python-2.7
pkg-config: exec: "pkg-config": executable file not found in %PATH%

请问这应该如何解决啊

hi,i has a question when i install this,please help me!

$ go build

pkg-config --cflags python-2.7

Package python-2.7 was not found in the pkg-config search path.
Perhaps you should add the directory containing `python-2.7.pc'
to the PKG_CONFIG_PATH environment variable
No package 'python-2.7' found
exit status 1

Failed to download

$ go get github.com/qiniu/py/
cd .; git clone -- https://github.com/qiniu/errors /Users/user/go/src/github.com/qiniu/errors Cloning into '/Users/user/go/src/github.com/qiniu/errors'... fatal: could not read Username for 'https://github.com': terminal prompts disabled package github.com/qiniu/errors: exit status 128 cd .; git clone -- https://github.com/qiniu/log /Users/user/go/src/github.com/qiniu/log Cloning into '/Users/user/go/src/github.com/qiniu/log'... fatal: could not read Username for 'https://github.com': terminal prompts disabled package github.com/qiniu/log: exit status 128

go1.3编译不过

inconsistent definitions for C.decref

Linux ubuntu 3.2.0-23-generic #36-Ubuntu SMP Tue Apr 10 20:39:51 UTC 2012 x86_64 x86_64 x86_64 GNU/Linux

cgo argument has Go pointer to Go pointer

the example straight from README.md doesnt work.

$ ./derp 
panic: runtime error: cgo argument has Go pointer to Go pointer

goroutine 1 [running]:
github.com/qiniu/py.(*Closure).NewFunction.func2(0xc420020120, 0x2, 0x2645d50)
	/home/aep/ai/makepage/.workspace/src/github.com/qiniu/py/gofunction.go:25 +0x52
github.com/qiniu/py.(*Closure).NewFunction(0xc4200200f0, 0xc4200102a1, 0x7, 0x2, 0x0, 0x0, 0x0)
	/home/aep/ai/makepage/.workspace/src/github.com/qiniu/py/gofunction.go:25 +0xb7
github.com/qiniu/py.Register(0x7f4a8bddfd70, 0xc42004fe18, 0x4, 0x4f2380, 0x7c93c8, 0xc42004fe18, 0x4, 0xc4200102d0)
	/home/aep/ai/makepage/.workspace/src/github.com/qiniu/py/goregister.go:64 +0x4a5
github.com/qiniu/py.NewGoModule(0x50eaea, 0x3, 0x0, 0x0, 0x4f2380, 0x7c93c8, 0x7f4a8bde66e0, 0x0, 0x0, 0x0, ...)
	/home/aep/ai/makepage/.workspace/src/github.com/qiniu/py/gomodule.go:33 +0x1bf
main.main()
	/home/aep/ai/makepage/.workspace/src/derp/main.go:46 +0x83
$ go env
GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/aep/gocode"
GORACE=""
GOROOT="/usr/lib/go"
GOTOOLDIR="/usr/lib/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build102676572=/tmp/go-build -gno-record-gcc-switches"
CXX="g++"
CGO_ENABLED="1"
PKG_CONFIG="pkg-config"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"

error信息不完整

项目中有很多py文件,go中用CallMethodObjArgs调用py出错时,错误信息只有最后一行。能否有方法返回完整的Traceback? 附带文件名以及行号。现在的这种错误信息很难定位。

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.