Open alanjds opened 6 years ago
Comment by alanjds Monday Feb 06, 2017 at 21:44 GMT
I am on it. Needed a push to learn Golang anyway...
Comment by alanjds Tuesday Feb 07, 2017 at 03:59 GMT
From https://docs.python.org/2/reference/simple_stmts.html#the-print-statement:
A space is written before each object is (converted and) written, unless the output system believes it is positioned at the beginning of a line. This is the case (1) when no characters have yet been written to standard output, (2) when the last character written to standard output is a whitespace character except ' ', or (3) when the last write operation on standard output was not a print statement. (In some cases it may be functional to write an empty string to standard output for this reason.)
This seems to be not that easy...
Do not print a space before:
Comment by alanjds Tuesday Feb 07, 2017 at 04:00 GMT
(and I had not yet found the CPython source of print statement)
Found here: https://github.com/python/cpython/blob/2.7/Python/bltinmodule.c#L1580
Comment by alanjds Tuesday Feb 07, 2017 at 14:31 GMT
Pypy does the right thing, I guess:
$ python hello.py | hexdump
0000000 2030 0a31 2030 0a31
0000008
$ pypy hello.py | hexdump
0000000 2030 0a31 2030 0a31
0000008
$ cat hello.py | make run | hexdump
0000000 2030 2031 300a 3120 0a20
000000a
$ uname -a
Linux b2wdill 4.4.0-59-generic google/grumpy#80-Ubuntu SMP Fri Jan 6 17:47:47 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux
And the pypy code is so easier to understand!
Just to note: There is 2 pypy print
implementations:
The "print STATEMENT" implementation is attached to a specific bytecode on Pypy.
Comment by alanjds Tuesday Feb 07, 2017 at 20:15 GMT
Looks like the way to go is via the softspace attribute of file
objects.
I had updated grumpy File but it seems to be not available yet. Is there anything yet I need to do to access this in Python code?
diff --git a/runtime/file.go b/runtime/file.go
index a5b1c46..b775213 100644
--- a/runtime/file.go
+++ b/runtime/file.go
@@ -37,2 +37,3 @@ type File struct {
open bool
+ softspace int
reader *bufio.Reader
@@ -131,2 +132,3 @@ func fileInit(f *Frame, o *Object, args Args, _ KWArgs) (*Object, *BaseException
file.file = osFile
+ file.softspace = 0
file.reader = bufio.NewReader(osFile)
$ python -c 'import sys; sys.stdout.softspace'
$ echo 'import sys; sys.stdout.softspace' | make run
AttributeError: 'file' object has no attribute 'softspace'
exit status 1
make: *** [run] Error 1
Comment by trotterdylan Wednesday Feb 08, 2017 at 20:08 GMT
Discussed on gitter, but the softspace attr is not currently supported. Looks like Alan is going to add it.
Comment by alanjds Thursday Feb 09, 2017 at 20:15 GMT
After discussion on gitter, implemented the attr_mode:"rw"
allowing native public attributes to be changed via Python. Is on PR google/grumpy#243.
The fix here will start to be implemented on the branch of google/grumpy#243
google/grumpy#223 opened by @S-YOU on 27 Jan 2017
Minor issue, but reporting what I noticed. For given code.