Skip to content

Commit 744a428

Browse files
Working quite a bit as expected. But, some memory corruption is causing un-expected memory freeing and crashes.
1 parent 41b9303 commit 744a428

File tree

4 files changed

+48
-18
lines changed

4 files changed

+48
-18
lines changed

_examples/multireturn/multireturn.go

+9-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ func SingleWithoutErrorFunc() int {
1313
return 100
1414
}
1515

16+
/////////////// Single Str WithoutError Return //////////////
17+
func SingleStrWithoutErrorFunc(vargs ...int) string {
18+
return "150"
19+
}
20+
1621
/////////////// Single WithError Return //////////////
1722
func SingleWithErrorFunc(throwError bool) error {
1823
if throwError {
@@ -37,7 +42,10 @@ func DoubleWithErrorFunc(throwError bool) (string, error) {
3742
}
3843

3944
/////////////// Triple Returns Without Error //////////////
40-
func TripleWithoutErrorFunc(vargs ...int) (int, string, int) {
45+
func TripleWithoutErrorFunc1(vargs ...int) (int, int, int) {
46+
return 600, 700, 800
47+
}
48+
func TripleWithoutErrorFunc2(vargs ...int) (int, string, int) {
4149
return 600, "700", 800
4250
}
4351

_examples/multireturn/test.py

+28-13
Original file line numberDiff line numberDiff line change
@@ -11,34 +11,49 @@
1111
oneResult = multireturn.SingleWithoutErrorFunc()
1212
print("Single WithoutError Return %r" % oneResult)
1313

14+
############### Single Str WithoutError Return ##############
15+
oneStrResult = multireturn.SingleStrWithoutErrorFunc()
16+
print("Single Str WithoutError Return %r" % oneStrResult)
17+
1418
############### Single WithError Return ##############
1519
errorFalseResult = multireturn.SingleWithErrorFunc(False)
1620
print("Single WithError(False) Return %r" % errorFalseResult)
1721

18-
errorTrueResult = multireturn.SingleWithErrorFunc(True)
19-
print("Single WithError(True) Return %r" % errorTrueResult)
22+
try:
23+
errorTrueResult = multireturn.SingleWithErrorFunc(True)
24+
print("Failed to throw an exception")
25+
except RuntimeError as ex:
26+
print("Single WithError(True). Exception:%r" % ex)
2027

2128
############### Double WithoutError Return ##############
2229
twoResults = multireturn.DoubleWithoutErrorFunc()
23-
print("Double WithoutError Return %r" % twoResults)
30+
print("Double WithoutError Return (%r, %r)" % twoResults)
2431

2532
############### Double WithError Return ##############
26-
(value400, errorTrueResult) = multireturn.DoubleeWithErrorFunc(True)
27-
print("Double WithError(True) Return (%r, %r)" % (value400, errorTrueResult))
33+
try:
34+
value400 = multireturn.DoubleWithErrorFunc(True)
35+
print("Failed to throw an exception. Return (%r, %r)." % value400)
36+
except RuntimeError as ex:
37+
print("Double WithError(True). exception Return %r" % ex)
2838

29-
(value500, errorFalseResult) = multireturn.DoubleWithErrorFunc(False)
30-
print("Double WithError(False) Return (%r, %r)" % (value500, errorFalseResult))
39+
value500 = multireturn.DoubleWithErrorFunc(False)
40+
print("Double WithError(False) Return %r" % value500)
3141

3242
############### Triple Without Error Return ##############
33-
threeResults = multireturn.TripleWithoutErrorFunc()
34-
print("Triple WithoutError Return %r" % threeResult)
43+
threeResults = multireturn.TripleWithoutErrorFunc1()
44+
print("Triple WithoutError(Without String) Return (%r, %r, %r)" % threeResults)
45+
46+
threeResults = multireturn.TripleWithoutErrorFunc2()
47+
print("Triple WithoutError(With String) Return (%r, %r, %r)" % threeResults)
3548

3649
############### Triple With Error Return ##############
37-
(value900, value1000, errorTrueResult) = multireturn.TripleWithErrorFunc(True)
38-
print("Triple WithError(True) Return (%r, %r, %r)" % (value900, value1000, errorFalseResult))
50+
try:
51+
(value900, value1000, errorTrueResult) = multireturn.TripleWithErrorFunc(True)
52+
except RuntimeError as ex:
53+
print("Triple WithError(True). exception Return %r" % ex)
3954

40-
(value1100, value1200, errorFalseResult) = multireturn.TripleWithErrorFunc(False)
41-
print("Triple WithError(False) Return (%r, %r, %r)" % (value1100, value1200, errorFalseResult))
55+
(value1100, value1200) = multireturn.TripleWithErrorFunc(False)
56+
print("Triple WithError(False) Return (%r, %r)" % (value1100, value1200))
4257

4358
############### Triple Struct Return With Error ##############
4459
(ptr1300, struct1400, errorTrueResult) = multireturn.TripleWithStructWithErrorFunc(True)

bind/gen_func.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -379,18 +379,23 @@ if __err != nil {
379379
}
380380

381381
pyFuncCall := fmt.Sprintf("_%s.%s(%s)", pkgname, mnm, strings.Join(wrapArgs, ", "))
382-
if nres > 0 {
382+
if npyres > 0 {
383383
retvars := make([]string, nres, nres)
384384
for i := 0; i < npyres; i++ {
385385
retvars[i] = "ret" + strconv.Itoa(i)
386386
if res[i].sym.hasHandle() {
387387
retvars[i] = "_" + retvars[i]
388388
}
389389
}
390+
390391
if fsym.haserr {
392+
// Only used if npyres == nres.
391393
retvars[nres-1] = "__err"
392394
}
393395

396+
// May drop the error var, if it is not supposed to be returned.
397+
retvars = retvars[0:npyres]
398+
394399
// Call upstream method and collect returns.
395400
g.pywrap.Printf(fmt.Sprintf("%s = %s\n", strings.Join(retvars, ", "), pyFuncCall))
396401

main_test.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -840,12 +840,14 @@ func TestBindMultiReturn(t *testing.T) {
840840
extras: nil,
841841
want: []byte(`No Return None
842842
Single WithoutError Return 100
843+
Single Str WithoutError Return 150
843844
Single WithError(False) Return nil
844-
Single WithError(True) Return 'Error'
845+
Single WithError(True). Exception: 'Error'
845846
Double WithoutError Return (200,300)
846-
Double WithError(True) Return (400, Error)
847+
Double WithError(True). Return (400, Error)
847848
Double WithError(False) Return (500, nil)
848-
Triple WithoutError Return (600, 700, 800)
849+
Triple WithoutError(Without String) Return (600, 700, 800)
850+
Triple WithoutError(With String) Return (600, 700, 800)
849851
Triple WithError(True) Return (900, 1000, Error)
850852
Triple WithError(False) Return (1100, 1200, nil)
851853
Triple WithError(True) Return (1300, 1400, Error)

0 commit comments

Comments
 (0)