Code Monkey home page Code Monkey logo

Comments (7)

sbinet avatar sbinet commented on June 12, 2024

(took the liberty to reformat slightly your submission)

from go-python.

sbinet avatar sbinet commented on June 12, 2024

I haven't checked my hypothesis yet but I surmise this is because you haven't checked whether an exception was raised (possibly with PyErr_Occurred [1]) and then cleared it (with PyErr_Clear [2])

[1] https://godoc.org/github.com/sbinet/go-python#PyErr_Occurred
[2] https://godoc.org/github.com/sbinet/go-python#PyErr_Clear

from go-python.

shtripat avatar shtripat commented on June 12, 2024

Yes. Its not checked for exception in our code.
Will try this and update. Thanks for the quick response.

Regards
Shubhendu
On 13 Jan 2016 02:03, "Sebastien Binet" [email protected] wrote:

I haven't checked my hypothesis yet but I surmise this is because you
haven't checked whether an exception was raised (possibly with
PyErr_Occurred [1]) and then cleared it (with PyErr_Clear [2])

[1] https://godoc.org/github.com/sbinet/go-python#PyErr_Occurred
[2] https://godoc.org/github.com/sbinet/go-python#PyErr_Clear


Reply to this email directly or view it on GitHub
#33 (comment).

from go-python.

shtripat avatar shtripat commented on June 12, 2024

Thanks the solution works fine. ...
On 13 Jan 2016 10:36, "Shubhendu Tripathi" [email protected]
wrote:

Yes. Its not checked for exception in our code.
Will try this and update. Thanks for the quick response.

Regards
Shubhendu
On 13 Jan 2016 02:03, "Sebastien Binet" [email protected] wrote:

I haven't checked my hypothesis yet but I surmise this is because you
haven't checked whether an exception was raised (possibly with
PyErr_Occurred [1]) and then cleared it (with PyErr_Clear [2])

[1] https://godoc.org/github.com/sbinet/go-python#PyErr_Occurred
[2] https://godoc.org/github.com/sbinet/go-python#PyErr_Clear


Reply to this email directly or view it on GitHub
#33 (comment).

from go-python.

shtripat avatar shtripat commented on June 12, 2024

Hi Sebastien,

Thanks for the support last time.
We are facing another issue in our project while using go-python package.
It seems something related to python version and compatibility with
go-python package.

Issue Description

To simulate the issue I have python written as below

#!/bin/python

import sys

def PrintSysArgv():
    print (sys.argv)

if __name__ == "__main__":
    PrintSysArgv()

This python if I run from python CLI works fine as shown below

>python test.py
['test.py']
>

Now I have go program to invoke the same function PrintSysArgv as below

package main

import (
    "<package>/gopy"
    "fmt"
)

var funcNames = [...]string{
    "PrintSysArgv",
}

var pyFuncs map[string]*gopy.PyFunction

func main() {
    pyFuncs, err := gopy.Import("mymodule.test", funcNames[:]...)
    if err != nil {
        panic("Error importing python module")
    }

    _, err = pyFuncs["PrintSysArgv"].Call()
    if err != nil {
        panic(fmt.Sprintf("Error executing python function. error: %v",
err))
    }
    fmt.Println("Done")
}

Below is the snippet of gopy wrapper I am using in above go source

package gopy

import (
        "errors"
        "fmt"
        "github.com/sbinet/go-python"
        "reflect"
)

type PyFunction struct {
        *python.PyObject
}

func (f *PyFunction) Call(args ...interface{}) (r *python.PyObject, err
error) {
        var pyargs *python.PyObject

        if pyargs, err = ToPyObject(reflect.ValueOf(args)); err != nil {
                return
        }

        name := python.PyString_AsString(f.GetAttrString("__name__"))
        if r = f.CallObject(pyargs); r == nil {
                err = errors.New(fmt.Sprintf("%s(): function failed at
python side", name))
        }
        if pyobj := python.PyErr_Occurred(); pyobj != nil {
                err = errors.New(fmt.Sprintf("%s(): exception happened in
python side", name))
                python.PyErr_Clear()
        }
        return
}

var pyinit = false

func Init() (err error) {
        if !pyinit {
                if err = python.Initialize(); err == nil {
                        pyinit = true
                }
        }
        return
}

func Import(module string, functions ...string) (funcs
map[string]*PyFunction, err error) {
        if err = Init(); err != nil {
                return
        }

        if pymod := python.PyImport_ImportModuleNoBlock(module); pymod ==
nil {
                err = errors.New(fmt.Sprintf("gopy:%s: module import
failed", module))
        } else {
                funcs = make(map[string]*PyFunction)
                for _, name := range functions {
                        if pyfunc := pymod.GetAttrString(name); pyfunc ==
nil {
                                err = errors.New(fmt.Sprintf("gopy:%s:%s:
function not found", module, name))
                                return
                        } else {
                                funcs[name] = &PyFunction{pyfunc}
                        }
                }
        }
        return
}

The out of the execution of go program fails python side as shown below

go run test.go
panic: Error executing python function. error: PrintSysArgv(): exception
happened in python side

goroutine 1 [running]:
main.main()
    /root/go/bin/test.go:22 +0x291

goroutine 17 [syscall, locked to thread]:
runtime.goexit()
    /usr/local/go/src/runtime/asm_amd64.s:2232 +0x1
exit status 2

Just a wierd thought that go-python uses a trimmed version of python which
is not able to take care of "sys.argv" as such.

Can you please look into the same and suggest us accordingly.

If needed / suggested I can raise the same in wiki...

Thanks and Regards,
Shubhendu

On Thu, Jan 14, 2016 at 1:48 AM, Sebastien Binet [email protected]
wrote:

Closed #33 #33.


Reply to this email directly or view it on GitHub
#33 (comment).

from go-python.

sbinet avatar sbinet commented on June 12, 2024

hi,
please open a new issue,
thanks.

from go-python.

shtripat avatar shtripat commented on June 12, 2024

Thanks Sebastien.
Raised one at #36

Regards,
Shubhendu

On Tue, Feb 9, 2016 at 1:48 PM, Sebastien Binet [email protected]
wrote:

hi,
please open a new issue,
thanks.


Reply to this email directly or view it on GitHub
#33 (comment).

from go-python.

Related Issues (20)

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.