Skip to content

Trailing space in output of print with comma #34

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
alanjds opened this issue Aug 21, 2018 · 8 comments
Open

Trailing space in output of print with comma #34

alanjds opened this issue Aug 21, 2018 · 8 comments
Labels
imported Imported from google/grumpy

Comments

@alanjds
Copy link

alanjds commented Aug 21, 2018

google#223 opened by @S-YOU on 27 Jan 2017

Minor issue, but reporting what I noticed. For given code.

for i in range(2):
  for j in range(2):
    print j,
  print
% python hello.py
0 1
0 1
% python hello.py | hexdump
0000000 30 20 31 0a 30 20 31 0a                        
0000008
% cat hello.py | make run | hexdump
0000000 30 20 31 20 0a 30 20 31 20 0a                  
000000a
@alanjds
Copy link
Author

alanjds commented Aug 21, 2018

Comment by trotterdylan
Friday Jan 27, 2017 at 17:44 GMT


Ah, good find. Thanks for reporting.

@alanjds
Copy link
Author

alanjds commented Aug 21, 2018

Comment by alanjds
Monday Feb 06, 2017 at 21:44 GMT


I am on it. Needed a push to learn Golang anyway...

@alanjds
Copy link
Author

alanjds commented Aug 21, 2018

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:

  • (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 ' ',
  • (3) when the last write operation on standard output was not a print statement.

@alanjds
Copy link
Author

alanjds commented Aug 21, 2018

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

@alanjds
Copy link
Author

alanjds commented Aug 21, 2018

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!

Update:

Just to note: There is 2 pypy print implementations:

  • The print function implementation (here)
  • The print statement implementation (here)

The "print STATEMENT" implementation is attached to a specific bytecode on Pypy.

@alanjds
Copy link
Author

alanjds commented Aug 21, 2018

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

@alanjds
Copy link
Author

alanjds commented Aug 21, 2018

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.

@alanjds
Copy link
Author

alanjds commented Aug 21, 2018

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#243.

The fix here will start to be implemented on the branch of google#243

@alanjds alanjds added the imported Imported from google/grumpy label Aug 21, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
imported Imported from google/grumpy
Projects
None yet
Development

No branches or pull requests

1 participant