From e4cb25c2e412f8aac7d783903c42f4550047625c Mon Sep 17 00:00:00 2001 From: Sebastien Binet Date: Mon, 10 Aug 2015 09:41:44 +0200 Subject: [PATCH] all: use cpkg to print via C-stdio so outputs are sync'd with python's i/o layer Change-Id: If1dfa6e3cbb74cf4cd4852ea6db850c3318b388b --- _examples/cpkg/cpkg.go | 21 ++++++++++++++++++--- _examples/hi/hi.go | 10 ++++++---- main_test.go | 18 +++++++++--------- 3 files changed, 33 insertions(+), 16 deletions(-) diff --git a/_examples/cpkg/cpkg.go b/_examples/cpkg/cpkg.go index aaab02d..98e8fe7 100644 --- a/_examples/cpkg/cpkg.go +++ b/_examples/cpkg/cpkg.go @@ -7,7 +7,7 @@ package cpkg //#include //#include //#include -//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) } diff --git a/_examples/hi/hi.go b/_examples/hi/hi.go index a89cd2d..348a2a5 100644 --- a/_examples/hi/hi.go +++ b/_examples/hi/hi.go @@ -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 } diff --git a/main_test.go b/main_test.go index d47b22d..9d93509 100644 --- a/main_test.go +++ b/main_test.go @@ -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: --- 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}