gpython icon indicating copy to clipboard operation
gpython copied to clipboard

gpython should treat standard input as a file, not as input to the REPL

Open raff opened this issue 7 years ago • 5 comments

This definition works:

class X(object):
  def __init__(self, x):
    self.x = x
  def __str__(self):
    return "my name is %s" % self.x

This doesn't:

class X(object):
  def __init__(self, x):
    self.x = x

  def __str__(self):
    return "my name is %s" % self.x

Both are accepted by C Python.

raff avatar Sep 07 '18 04:09 raff

If I save what you wrote to a file then it works fine in gpython,

Can you show us what you tried and the error it gave?

ncw avatar Sep 07 '18 11:09 ncw

Ah!

So, the problem is that it doesn't work from REPL. REPL should probably ignore empty lines.

What I did was, I was writing a test for the print stuff, and wanted to try in the REPL first. I also wrote in in a file, but didn't named it *.py, since it was just a test, but gpython currently doesn't read files if they don't have the .py extension (while regular python does), so I just did:

 go run main.go < xxx

raff avatar Sep 07 '18 16:09 raff

That doesn't work in the python3 REPL either. I tried pasting your example with the blank line and I get the same errors.

I think this is a known problem with the python REPL: eg https://stackoverflow.com/questions/37891771/python-interactive-interpreter-has-problems-with-blank-lines-when-i-paste-in-a-s

So I think gpython is following cpython here, unless I mis-understand what you mean.

ncw avatar Sep 10 '18 19:09 ncw

Given a file classes.py:

class X(object):
    def __init__(self, x):
        self.x = x

    def __str__(self):
        return "my name is " + str(self.x)

x = X("hello")
print(x)

x = X(42)
print(x)

This works:

$ python3.4 < classes.py 
my name is hello
my name is 42

This doesn't:

$ go run main.go < classes.py 
Gpython 3.4.0
Compile error: 
  File "<string>", line 1, offset 4
        def __str__(self):


SyntaxError: 'invalid syntax'
Compile error: 
  File "<string>", line 1, offset 8
            return "my name is " + str(self.x)


SyntaxError: 'invalid syntax'
<X object at 0xc0000efb00>
<X object at 0xc0000efb90>

I am actually testing against python3.7, that is what got installed on my Mac, but I wouldn't think this is something that changed between 3.4 and 3.7, since it works the same in 2.7

raff avatar Sep 10 '18 20:09 raff

Ah, I see - thanks for the explanation.

What appears to be happening is that gpython is starting the interactive interpreter when you do gpython < file.py whereas when you do python3 < file.py it doesn't start the REPL, it treats the input as a proper file.

From https://docs.python.org/3/using/cmdline.html

When called with standard input connected to a tty device, it prompts for commands and executes them until an EOF (an end-of-file character, you can produce that with Ctrl-D on UNIX or Ctrl-Z, Enter on Windows) is read.

When called with a file name argument or with a file as standard input, it reads and executes a script from that file.

Which I think agrees with the above.

ncw avatar Sep 12 '18 07:09 ncw