@@ -18,6 +18,7 @@ import (
1818 "fmt"
1919 "math"
2020 "math/big"
21+ "strings"
2122 "unicode"
2223)
2324
@@ -110,6 +111,7 @@ var builtinTypes = map[*Type]*builtinTypeInfo{
110111 EllipsisType : {init : initEllipsisType , global : true },
111112 enumerateType : {init : initEnumerateType , global : true },
112113 EnvironmentErrorType : {global : true },
114+ EOFErrorType : {global : true },
113115 ExceptionType : {global : true },
114116 FileType : {init : initFileType , global : true },
115117 FloatType : {init : initFloatType , global : true },
@@ -559,6 +561,37 @@ func builtinRange(f *Frame, args Args, kwargs KWArgs) (*Object, *BaseException)
559561 return ListType .Call (f , []* Object {r }, nil )
560562}
561563
564+ func builtinRawInput (f * Frame , args Args , kwargs KWArgs ) (* Object , * BaseException ) {
565+ if len (args ) > 1 {
566+ msg := fmt .Sprintf ("[raw_]input expcted at most 1 arguments, got %d" , len (args ))
567+ return nil , f .RaiseType (TypeErrorType , msg )
568+ }
569+
570+ if Stdin == nil {
571+ msg := fmt .Sprintf ("[raw_]input: lost sys.stdin" )
572+ return nil , f .RaiseType (RuntimeErrorType , msg )
573+ }
574+
575+ if Stdout == nil {
576+ msg := fmt .Sprintf ("[raw_]input: lost sys.stdout" )
577+ return nil , f .RaiseType (RuntimeErrorType , msg )
578+ }
579+
580+ if len (args ) == 1 {
581+ err := pyPrint (f , args , "" , "" , Stdout )
582+ if err != nil {
583+ return nil , err
584+ }
585+ }
586+
587+ line , err := Stdin .reader .ReadString ('\n' )
588+ if err != nil {
589+ return nil , f .RaiseType (EOFErrorType , "EOF when reading a line" )
590+ }
591+ line = strings .TrimRight (line , "\n " )
592+ return NewStr (line ).ToObject (), nil
593+ }
594+
562595func builtinRepr (f * Frame , args Args , kwargs KWArgs ) (* Object , * BaseException ) {
563596 if raised := checkFunctionArgs (f , "repr" , args , ObjectType ); raised != nil {
564597 return nil , raised
@@ -693,6 +726,7 @@ func init() {
693726 "ord" : newBuiltinFunction ("ord" , builtinOrd ).ToObject (),
694727 "print" : newBuiltinFunction ("print" , builtinPrint ).ToObject (),
695728 "range" : newBuiltinFunction ("range" , builtinRange ).ToObject (),
729+ "raw_input" : newBuiltinFunction ("raw_input" , builtinRawInput ).ToObject (),
696730 "repr" : newBuiltinFunction ("repr" , builtinRepr ).ToObject (),
697731 "round" : newBuiltinFunction ("round" , builtinRound ).ToObject (),
698732 "setattr" : newBuiltinFunction ("setattr" , builtinSetAttr ).ToObject (),
0 commit comments