Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Draft] Support for multiple return values from go function to python #282

Draft
wants to merge 21 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
1fe3270
Merge pull request #1 from rudderlabs/nisshar/VariadicFunctions
nishantsharma Jun 19, 2022
399dd4b
Go module path updated so that it can be installed.
nishant-rudderstack Jun 20, 2022
f6c26ce
Replacing gopy URL so that it works in forks too.
nishant-rudderstack Jun 20, 2022
4ea0c07
go package to test is at rudderlabs/gopy
nishant-rudderstack Jun 20, 2022
3e19314
Added test code for multireturn.
nishant-rudderstack Jun 12, 2022
d0d0d65
First stage changes made. Next step is to change the loop to reflect …
nishant-rudderstack Jun 12, 2022
b5b07fe
Python code being generated sensibly.
nishant-rudderstack Jun 12, 2022
c57ef41
Correcting npyres computation.
nishant-rudderstack Jun 13, 2022
b3c7b5d
Verfied generated python code, atleast for smaller ret-counts.
nishant-rudderstack Jun 13, 2022
d93d069
Code to handle multiple return values as tuples now logically complet…
nishant-rudderstack Jun 14, 2022
1ae7696
Both go and python code is now compiling. Automatic Test are failing.
nishant-rudderstack Jun 14, 2022
05d1874
Working quite a bit as expected. But, some memory corruption is causi…
nishant-rudderstack Jun 15, 2022
1e11675
Tests passing once we let memory leak. Fix for memory leaks to be car…
nishant-rudderstack Jun 19, 2022
14d81b4
Ignoring functions returning functions.
nishant-rudderstack Jun 23, 2022
2cb06f8
__err was getting reused in a way which declarations difficult. Fixed.
nishant-rudderstack Jun 23, 2022
1a54a50
Fixed a few code generation error for methods.
nishant-rudderstack Jun 23, 2022
9350594
Python None now mapped to GoLang nil.
nishant-rudderstack Jun 23, 2022
ea0d913
TestGovet and TestGofmt now passing.
nishant-rudderstack Jun 28, 2022
9020e61
Crash in gen command fixed.
nishant-rudderstack Jun 28, 2022
a44f1e5
gopyerrors is not applicable any more.
nishant-rudderstack Jun 28, 2022
35cda9d
Test failure bug fixed.
nishant-rudderstack Jun 28, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ on:

env:
TAGS: "-tags=ci"
COVERAGE: "-coverpkg=github.com/go-python/gopy/..."
COVERAGE: "-coverpkg=github.com/rudderlabs/gopy/..."
# Init() in main_test will make sure all backends are available if
# GOPY_TRAVIS_CI is set
GOPY_TRAVIS_CI: 1
Expand Down
2 changes: 2 additions & 0 deletions SUPPORT_MATRIX.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ _examples/hi | no | yes
_examples/iface | no | yes
_examples/lot | yes | yes
_examples/maps | yes | yes
_examples/multireturn | no | yes
_examples/named | yes | yes
_examples/osfile | yes | yes
_examples/pkgconflict | yes | yes
Expand All @@ -29,4 +30,5 @@ _examples/sliceptr | yes | yes
_examples/slices | yes | yes
_examples/structs | yes | yes
_examples/unicode | no | yes
_examples/variadic | no | yes
_examples/vars | yes | yes
3 changes: 2 additions & 1 deletion _examples/cpkg/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

//go:build ignore
// +build ignore

package main

import (
"fmt"

"github.com/go-python/gopy/_examples/cpkg"
"github.com/rudderlabs/gopy/_examples/cpkg"
)

func main() {
Expand Down
2 changes: 1 addition & 1 deletion _examples/funcs/funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ package funcs
import (
"fmt"

"github.com/go-python/gopy/_examples/cpkg"
"github.com/rudderlabs/gopy/_examples/cpkg"
)

type FunStruct struct {
Expand Down
29 changes: 0 additions & 29 deletions _examples/gopyerrors/gopyerrors.go

This file was deleted.

13 changes: 0 additions & 13 deletions _examples/gopyerrors/test.py

This file was deleted.

4 changes: 2 additions & 2 deletions _examples/hi/hi.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ package hi
import (
"fmt"

"github.com/go-python/gopy/_examples/cpkg"
"github.com/go-python/gopy/_examples/structs"
"github.com/rudderlabs/gopy/_examples/cpkg"
"github.com/rudderlabs/gopy/_examples/structs"
)

const (
Expand Down
2 changes: 1 addition & 1 deletion _examples/iface/iface.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
package iface

import (
"github.com/go-python/gopy/_examples/cpkg"
"github.com/rudderlabs/gopy/_examples/cpkg"
)

// Iface has a single F() method
Expand Down
111 changes: 111 additions & 0 deletions _examples/multireturn/multireturn.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package multireturn

import (
"fmt"
)

/////////////// No Return //////////////
func NoReturnFunc() {
}

/////////////// Single WithoutError Return //////////////
func SingleWithoutErrorFunc() int {
return 100
}

/////////////// Single Str WithoutError Return //////////////
func SingleStrWithoutErrorFunc(vargs ...int) string {
return "150"
}

/////////////// Single WithError Return //////////////
func SingleWithErrorFunc(throwError bool) error {
if throwError {
return fmt.Errorf("Error")
} else {
return nil
}
}

/////////////// Double WithoutError Return //////////////
func DoubleWithoutErrorFunc1() (int, int) {
return 200, 300
}
func DoubleWithoutErrorFunc2() (string, string) {
return "200", "300"
}

/////////////// Double WithError Return //////////////
func DoubleWithErrorFunc(throwError bool) (string, error) {
if throwError {
return "400", fmt.Errorf("Error")
} else {
return "500", nil
}
}

/////////////// Triple Returns Without Error //////////////
func TripleWithoutErrorFunc1(vargs ...int) (int, int, int) {
return 600, 700, 800
}
func TripleWithoutErrorFunc2(vargs ...int) (int, string, int) {
return 600, "700", 800
}

/////////////// Triple Returns With Error //////////////
func TripleWithErrorFunc(throwError bool) (int, int, error) {
if throwError {
return 900, 1000, fmt.Errorf("Error")
} else {
return 1100, 1200, nil
}
}

/////////////// Triple Struct Returns With Error //////////////
type IntStrUct struct {
P int
}

func NewIntStrUct(n int) IntStrUct {
return IntStrUct{
P: n,
}
}

func TripleWithStructWithErrorFunc(throwError bool) (*IntStrUct, IntStrUct, error) {
s1300 := IntStrUct{P: 1300}
s1400 := IntStrUct{P: 1400}
s1500 := IntStrUct{P: 1500}
s1600 := IntStrUct{P: 1600}
if throwError {
return &s1300, s1400, fmt.Errorf("Error")
} else {
return &s1500, s1600, nil
}
}

/////////////// Triple Interface Returns Without Error //////////////
type IntInterFace interface {
Number() int
}

func (is *IntStrUct) Number() int {
return is.P
}

func TripleWithInterfaceWithoutErrorFunc() (IntInterFace, IntStrUct, *IntStrUct) {
i1700 := IntStrUct{P: 1700}
s1800 := IntStrUct{P: 1800}
s1900 := IntStrUct{P: 1900}

return &i1700, s1800, &s1900
}

//// Function returning function /////
type FunctionType func(input int) int

func FunctionReturningFunction() FunctionType {
return func(input int) int {
return input
}
}
77 changes: 77 additions & 0 deletions _examples/multireturn/test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# Copyright 2018 The go-python Authors. All rights reserved.
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file.
import multireturn, go

############### No Return ##############
noResult = multireturn.NoReturnFunc()
print("No Return %r" % noResult)

############### Single WithoutError Return ##############
oneResult = multireturn.SingleWithoutErrorFunc()
print("Single WithoutError Return %r" % oneResult)

############### Single Str WithoutError Return ##############
oneStrResult = multireturn.SingleStrWithoutErrorFunc()
print("Single Str WithoutError Return %r" % oneStrResult)

############### Single WithError Return ##############
errorFalseResult1 = multireturn.SingleWithErrorFunc(False)
print("Single WithError(False) Return %r" % errorFalseResult1)

try:
errorTrueResult1 = multireturn.SingleWithErrorFunc(True)
print("Failed to throw an exception")
except RuntimeError as ex:
print("Single WithError(True). Exception: %r" % ex)

############### Double WithoutError Return ##############
twoResults = multireturn.DoubleWithoutErrorFunc1()
print("Double WithoutError(Without String) Return (%r, %r)" % twoResults)

twoResults = multireturn.DoubleWithoutErrorFunc2()
print("Double WithoutError(With String) Return (%r, %r)" % twoResults)

############### Double WithError Return ##############
try:
value400 = multireturn.DoubleWithErrorFunc(True)
print("Failed to throw an exception. Return (%r, %r)." % value400)
except RuntimeError as ex:
print("Double WithError(True). Exception: %r" % ex)

value500 = multireturn.DoubleWithErrorFunc(False)
print("Double WithError(False) Return %r" % value500)

############### Triple Without Error Return ##############
threeResults = multireturn.TripleWithoutErrorFunc1()
print("Triple WithoutError(Without String) Return (%r, %r, %r)" % threeResults)

threeResults = multireturn.TripleWithoutErrorFunc2()
print("Triple WithoutError(With String) Return (%r, %r, %r)" % threeResults)

############### Triple With Error Return ##############
try:
(value900, value1000) = multireturn.TripleWithErrorFunc(True)
print("Triple WithError(True) Return (%r, %r, %r)" % (value900, value1000))
except RuntimeError as ex:
print("Triple WithError(True) Exception: %r" % ex)

(value1100, value1200) = multireturn.TripleWithErrorFunc(False)
print("Triple WithError(False) Return (%r, %r)" % (value1100, value1200))

############### Triple Struct Return With Error ##############
try:
(ptr1300, struct1400) = multireturn.TripleWithStructWithErrorFunc(True)
print("Triple WithError(True) Return (%r, %r)" % (ptr1300.P, struct1400.P))
except RuntimeError as ex:
print("Triple WithError(True) Exception: %r" % ex)

(value1500, value1600) = multireturn.TripleWithStructWithErrorFunc(False)
print("Triple WithError(False) Return (%r, %r)" % (value1500.P, value1600.P))

############### Triple Interface Return Without Error ##############
(interface1700, struct1800, ptr1900) = multireturn.TripleWithInterfaceWithoutErrorFunc()
print("Triple WithoutError() Return (%r, %r, %r)" % (interface1700.Number(), struct1800.P, ptr1900.P))

############## Function Returning Functions ignored ##############
assert("FunctionReturningFunction" not in dir(multireturn))
16 changes: 8 additions & 8 deletions _examples/variadic/variadic.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package variadic

/////////////// Non Variadic //////////////
func NonVariFunc(arg1 int, arg2 []int, arg3 int) int{
func NonVariFunc(arg1 int, arg2 []int, arg3 int) int {
total := arg1
for _, num := range arg2 {
total += num
Expand All @@ -12,7 +12,7 @@ func NonVariFunc(arg1 int, arg2 []int, arg3 int) int{
}

/////////////// Variadic Over Int //////////////
func VariFunc(vargs ...int) int{
func VariFunc(vargs ...int) int {
total := 0
for _, num := range vargs {
total += num
Expand All @@ -26,15 +26,15 @@ type IntStrUct struct {
}

func NewIntStrUct(n int) IntStrUct {
return IntStrUct {
p:n,
return IntStrUct{
p: n,
}
}
}

func VariStructFunc(vargs ...IntStrUct) int{
func VariStructFunc(vargs ...IntStrUct) int {
total := 0
for _, inst := range vargs {
total += inst.p
total += inst.p
}
return total
}
Expand All @@ -48,7 +48,7 @@ func (is *IntStrUct) Number() int {
return is.p
}

func VariInterFaceFunc(vargs ...IntInterFace) int{
func VariInterFaceFunc(vargs ...IntInterFace) int {
total := 0
for _, inst := range vargs {
total += inst.Number()
Expand Down
2 changes: 1 addition & 1 deletion _examples/wrapper/pywrapper/wrapper_code.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ package pywrapper
import (
"fmt"

"github.com/go-python/gopy/_examples/wrapper"
"github.com/rudderlabs/gopy/_examples/wrapper"
)

type WrapperStruct struct {
Expand Down
16 changes: 16 additions & 0 deletions bind/bind.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,34 @@ import (
type BindCfg struct {
// output directory for bindings
OutputDir string

// name of output package (otherwise name of first package is used)
Name string

// code string to run in the go main() function in the cgo library
Main string

// the full command args as a string, without path to exe
Cmd string

// path to python interpreter
VM string

// package prefix used when generating python import statements
PkgPrefix string

// rename Go exported symbols to python PEP snake_case
RenameCase bool

// If set, python exceptions are not thrown.
NoPyExceptions bool

// Path to Go module, which is to be used to translate Go errors to Python exceptions.
ModPathGoErr2PyEx string

// If set, when a Go function returns a (value, err), python returns (value, ) tuple.
// By default, we return just value.
UsePyTuple4VE bool
}

// ErrorList is a list of errors
Expand Down
Loading