Skip to content

Commit

Permalink
Example RedHatOfficial#7: Python interpreter interop, better variant
Browse files Browse the repository at this point in the history
  • Loading branch information
tisnik committed Nov 28, 2019
1 parent b2db1c2 commit 46cc81b
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions testing/gexpect/07_python.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package main

import (
"log"
"time"

"github.com/ThomasRooney/gexpect"
)

func expectOutput(child *gexpect.ExpectSubprocess, output string) {
err := child.ExpectTimeout(output, time.Second)
if err != nil {
log.Fatal(err)
}
}

func expectPrompt(child *gexpect.ExpectSubprocess) {
expectOutput(child, ">>> ")
}

func sendCommand(child *gexpect.ExpectSubprocess, command string) {
err := child.SendLine(command)
if err != nil {
log.Fatal(err)
}
}

func main() {
child, err := gexpect.Spawn("python")
if err != nil {
log.Fatal(err)
}

strs, err := child.ExpectRegexFind("Python [23]")
if err != nil {
log.Fatal(err)
}

if strs[0] == "Python 2" {
log.Println("Python 2")
expectPrompt(child)
sendCommand(child, "print 1,2,3")
expectOutput(child, "1 2 3")
} else {
log.Println("Python 3")
expectPrompt(child)
sendCommand(child, "print(1,2,3)")
expectOutput(child, "1 2 3")
}

expectPrompt(child)
sendCommand(child, "quit()")

child.Wait()
}

0 comments on commit 46cc81b

Please sign in to comment.