Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
grumpy
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
grumpy
Commits
b2da45fd
Commit
b2da45fd
authored
Feb 13, 2017
by
Dong-hee Na
Committed by
Dylan Trotter
Feb 13, 2017
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Switch pyPrint() into based on *grumpy.File (#247)
parent
332739d7
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
49 additions
and
25 deletions
+49
-25
lib/sys.py
lib/sys.py
+2
-7
runtime/builtin_types.go
runtime/builtin_types.go
+1
-2
runtime/builtin_types_test.go
runtime/builtin_types_test.go
+3
-3
runtime/core.go
runtime/core.go
+18
-6
runtime/core_test.go
runtime/core_test.go
+3
-7
runtime/file.go
runtime/file.go
+22
-0
No files found.
lib/sys.py
View file @
b2da45fd
...
...
@@ -14,8 +14,8 @@
"""System-specific parameters and functions."""
from
__go__.os
import
Args
,
Stdin
,
Stdout
,
Stderr
from
__go__.grumpy
import
SysModules
,
MaxInt
,
NewFileFromFD
# pylint: disable=g-multiple-import
from
__go__.os
import
Args
from
__go__.grumpy
import
SysModules
,
MaxInt
,
Stdin
as
stdin
,
Stdout
as
stdout
,
Stderr
as
stderr
# pylint: disable=g-multiple-import
from
__go__.runtime
import
Version
from
__go__.unicode
import
MaxRune
...
...
@@ -33,11 +33,6 @@ warnoptions = []
# TODO: Support actual byteorder
byteorder
=
'little'
stdin
=
NewFileFromFD
(
Stdin
.
Fd
())
stdout
=
NewFileFromFD
(
Stdout
.
Fd
())
stderr
=
NewFileFromFD
(
Stderr
.
Fd
())
class
_Flags
(
object
):
"""Container class for sys.flags."""
debug
=
0
...
...
runtime/builtin_types.go
View file @
b2da45fd
...
...
@@ -18,7 +18,6 @@ import (
"fmt"
"math"
"math/big"
"os"
"unicode"
)
...
...
@@ -528,7 +527,7 @@ func builtinOrd(f *Frame, args Args, _ KWArgs) (*Object, *BaseException) {
func
builtinPrint
(
f
*
Frame
,
args
Args
,
kwargs
KWArgs
)
(
*
Object
,
*
BaseException
)
{
sep
:=
" "
end
:=
"
\n
"
file
:=
os
.
Stdout
file
:=
Stdout
for
_
,
kwarg
:=
range
kwargs
{
switch
kwarg
.
Name
{
case
"sep"
:
...
...
runtime/builtin_types_test.go
View file @
b2da45fd
...
...
@@ -350,10 +350,10 @@ func captureStdout(f *Frame, fn func() *BaseException) (string, *BaseException)
if
err
!=
nil
{
return
""
,
f
.
RaiseType
(
RuntimeErrorType
,
fmt
.
Sprintf
(
"failed to open pipe: %v"
,
err
))
}
oldStdout
:=
os
.
Stdout
os
.
Stdout
=
w
oldStdout
:=
Stdout
Stdout
=
NewFileFromFD
(
w
.
Fd
())
defer
func
()
{
os
.
Stdout
=
oldStdout
Stdout
=
oldStdout
}()
done
:=
make
(
chan
struct
{})
var
raised
*
BaseException
...
...
runtime/core.go
View file @
b2da45fd
...
...
@@ -16,7 +16,6 @@ package grumpy
import
(
"fmt"
"io"
"log"
"os"
"reflect"
...
...
@@ -641,7 +640,7 @@ func Print(f *Frame, args Args, nl bool) *BaseException {
}
else
if
len
(
args
)
>
0
{
end
=
" "
}
return
pyPrint
(
f
,
args
,
" "
,
end
,
os
.
Stdout
)
return
pyPrint
(
f
,
args
,
" "
,
end
,
Stdout
)
}
// Repr returns a string containing a printable representation of o. This is
...
...
@@ -1216,17 +1215,30 @@ func hashNotImplemented(f *Frame, o *Object) (*Object, *BaseException) {
}
// pyPrint encapsulates the logic of the Python print function.
func
pyPrint
(
f
*
Frame
,
args
Args
,
sep
,
end
string
,
file
io
.
Writer
)
*
BaseException
{
func
pyPrint
(
f
*
Frame
,
args
Args
,
sep
,
end
string
,
file
*
File
)
*
BaseException
{
for
i
,
arg
:=
range
args
{
if
i
>
0
{
fmt
.
Fprint
(
file
,
sep
)
err
:=
file
.
writeString
(
sep
)
if
err
!=
nil
{
return
f
.
RaiseType
(
IOErrorType
,
err
.
Error
())
}
}
s
,
raised
:=
ToStr
(
f
,
arg
)
if
raised
!=
nil
{
return
raised
}
fmt
.
Fprint
(
file
,
s
.
Value
())
err
:=
file
.
writeString
(
s
.
Value
())
if
err
!=
nil
{
return
f
.
RaiseType
(
IOErrorType
,
err
.
Error
())
}
}
fmt
.
Fprint
(
file
,
end
)
err
:=
file
.
writeString
(
end
)
if
err
!=
nil
{
return
f
.
RaiseType
(
IOErrorType
,
err
.
Error
())
}
return
nil
}
runtime/core_test.go
View file @
b2da45fd
...
...
@@ -15,7 +15,6 @@
package
grumpy
import
(
"bytes"
"fmt"
"math/big"
"reflect"
...
...
@@ -790,12 +789,9 @@ func TestOct(t *testing.T) {
func
TestPyPrint
(
t
*
testing
.
T
)
{
fun
:=
wrapFuncForTest
(
func
(
f
*
Frame
,
args
*
Tuple
,
sep
,
end
string
)
(
string
,
*
BaseException
)
{
var
buf
bytes
.
Buffer
raised
:=
pyPrint
(
NewRootFrame
(),
args
.
elems
,
sep
,
end
,
&
buf
)
if
raised
!=
nil
{
return
""
,
raised
}
return
buf
.
String
(),
nil
return
captureStdout
(
f
,
func
()
*
BaseException
{
return
pyPrint
(
NewRootFrame
(),
args
.
elems
,
sep
,
end
,
Stdout
)
})
})
cases
:=
[]
invokeTestCase
{
{
args
:
wrapArgs
(
NewTuple
(),
""
,
"
\n
"
),
want
:
NewStr
(
"
\n
"
)
.
ToObject
()},
...
...
runtime/file.go
View file @
b2da45fd
...
...
@@ -89,6 +89,19 @@ func (f *File) readLine(maxBytes int) (string, error) {
return
buf
.
String
(),
nil
}
func
(
f
*
File
)
writeString
(
s
string
)
error
{
f
.
mutex
.
Lock
()
defer
f
.
mutex
.
Unlock
()
if
!
f
.
open
{
return
io
.
ErrClosedPipe
}
if
_
,
err
:=
f
.
file
.
Write
([]
byte
(
s
));
err
!=
nil
{
return
err
}
return
nil
}
// FileType is the object representing the Python 'file' type.
var
FileType
=
newBasisType
(
"file"
,
reflect
.
TypeOf
(
File
{}),
toFileUnsafe
,
ObjectType
)
...
...
@@ -342,3 +355,12 @@ func fileParseReadArgs(f *Frame, method string, args Args) (*File, int, *BaseExc
}
return
toFileUnsafe
(
args
[
0
]),
size
,
nil
}
var
(
// Stdin is an alias for sys.stdin.
Stdin
=
NewFileFromFD
(
os
.
Stdin
.
Fd
())
// Stdout is an alias for sys.stdout.
Stdout
=
NewFileFromFD
(
os
.
Stdout
.
Fd
())
// Stderr is an aliaas for sys.stderr.
Stderr
=
NewFileFromFD
(
os
.
Stderr
.
Fd
())
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment