all: use cpkg to print via C-stdio so outputs are sync'd with python's i/o layer

Change-Id: If1dfa6e3cbb74cf4cd4852ea6db850c3318b388b
This commit is contained in:
Sebastien Binet 2015-08-10 09:41:44 +02:00
parent 85958438a3
commit e4cb25c2e4
3 changed files with 33 additions and 16 deletions

View File

@ -7,7 +7,7 @@ package cpkg
//#include <stdio.h>
//#include <string.h>
//#include <stdlib.h>
//void hello(const char *str) {
//void cpkg_printf(const char *str) {
// fprintf(stdout, str);
//}
import "C"
@ -17,12 +17,27 @@ import (
"unsafe"
)
// Hi prints hi from Go (via C's stdio)
func Hi() {
cstr := C.CString("hi from go\n")
defer C.free(unsafe.Pointer(cstr))
C.cpkg_printf(cstr)
}
// Hello prints a string via C's stdio
func Hello(s string) {
if s == "" {
s = "you"
}
cstr := C.CString(fmt.Sprintf("hello %s from C\n", s))
cstr := C.CString(fmt.Sprintf("hello %s from go\n", s))
defer C.free(unsafe.Pointer(cstr))
C.hello(cstr)
C.cpkg_printf(cstr)
}
// Printf prints a string via C's stdio
func Printf(format string, args ...interface{}) {
str := fmt.Sprintf(format, args...)
cstr := C.CString(str)
defer C.free(unsafe.Pointer(cstr))
C.cpkg_printf(cstr)
}

View File

@ -7,6 +7,8 @@ package hi
import (
"fmt"
"github.com/go-python/gopy/_examples/cpkg"
)
const (
@ -23,12 +25,12 @@ var (
// Hi prints hi from Go
func Hi() {
fmt.Printf("hi from go\n")
cpkg.Hi()
}
// Hello prints a greeting from Go
func Hello(s string) {
fmt.Printf("hello %s from go\n", s)
cpkg.Hello(s)
}
// Concat concatenates two strings together and returns the resulting string.
@ -86,11 +88,11 @@ func (p *Person) greet() string {
// Work makes a Person go to work for h hours
func (p *Person) Work(h int) error {
fmt.Printf("working...\n")
cpkg.Printf("working...\n")
if h > 7 {
return fmt.Errorf("can't work for %d hours!", h)
}
fmt.Printf("worked for %d hours\n", h)
cpkg.Printf("worked for %d hours\n", h)
return nil
}

View File

@ -49,14 +49,7 @@ func TestBind(t *testing.T) {
t.Fatalf("error copying 'test.py': %v\n", err)
}
want := []byte(`hi from go
hello you from go
working...
worked for 2 hours
working...
working...
worked for 4 hours
--- doc(hi)...
want := []byte(`--- doc(hi)...
package hi exposes a few Go functions to be wrapped and used from Python.
--- hi.GetUniverse(): 42
@ -76,12 +69,14 @@ Hi()
Hi prints hi from Go
--- hi.Hi()...
hi from go
--- doc(hi.Hello)...
Hello(str s)
Hello prints a greeting from Go
--- hi.Hello('you')...
hello you from go
--- doc(hi.Add)...
Add(int i, int j) int
@ -118,14 +113,19 @@ hi.Person{Name="foo", Age=42}
--- p.Age: 42
--- p.Name: foo
--- p.Work(2)...
working...
worked for 2 hours
--- p.Work(24)...
working...
caught: can't work for 24 hours!
--- p.Salary(2): 20
--- p.Salary(24): caught: can't work for 24 hours!
caught: Person.__init__ takes no argument | err-type: <type 'exceptions.TypeError'>
--- hi.NewPerson('me', 666): hi.Person{Name="me", Age=666}
--- hi.NewPersonWithAge(666): hi.Person{Name="stranger", Age=666}
--- hi.NewActivePerson(4): hi.Person{Name="", Age=0}
--- hi.NewActivePerson(4):working...
worked for 4 hours
hi.Person{Name="", Age=0}
--- c = hi.Couple()...
hi.Couple{P1=hi.Person{Name="", Age=0}, P2=hi.Person{Name="", Age=0}}
--- c.P1: hi.Person{Name="", Age=0}