Python icon indicating copy to clipboard operation
Python copied to clipboard

Can't print to stderr

Open henridwyer opened this issue 11 years ago • 2 comments

When printing to stderr, nothing shows up in the console.

    import sys
    print >> sys.stderr, "test"
    import sys
    sys.stderr.write("test2")

Both of these ways work if you replace stderr with stdout.

This raises an error for both stderr and stdout:

    from __future__ import print_function
    print('spam', file=sys.stderr)
Traceback (most recent call last):
  File "~/Library/Application Support/LightTable/plugins/Python/py-src/ltmain.py", line 267, in ipyEval
    compile(ensureUtf(code), ensureUtf(data[2]["name"]), 'exec')
  File "test.py", line 9
    print("test", file=sys.stdout)
                      ^
SyntaxError: invalid syntax

henridwyer avatar Jan 23 '15 14:01 henridwyer

It's unclear if the stderr part is https://github.com/LightTable/Python/blob/134751c32a86699c06ccafe46ae73a17a0e2d012/py-src/ltipy.py#L222 or https://github.com/LightTable/Python/blob/134751c32a86699c06ccafe46ae73a17a0e2d012/py-src/ltipy.py#L199 (those are my guesses).

The print_function bug is different... scoping of exec?

Edit: I can reproduce this locally:

In [2]: exec(compile("from __future__ import print_function", "hello.py", 'exec'))

In [3]: exec(compile("print('spam', file=sys.stderr)", "hello.py", 'exec'))
  File "hello.py", line 1
    print('spam', file=sys.stderr)
                      ^
SyntaxError: invalid syntax

hayd avatar Jul 01 '15 07:07 hayd

Had a look at the print_function bug. It appears to be related to the fact LightTable compiles each form in the file in sequence in py-src/ltmain.py:

  for form in toExec:
      code = form[1]
      loc = form[0]
      #conditional stuff for eval vs. exec
          code= compile(code, data[2]["name"], 'exec')

and the fact that the print_function is a monkey patch to syntax that exists only at runtime.

Attaching the from __future__ import print_function line to the code in py-src/ltmain.py allows something like print('spam', file=sys.stderr) to be evaluated successfully:

if "print" in code:
  code = "from __future__ import print_function\n" + code

code= compile(code, data[2]["name"], 'exec')

I'm not familiar with Python/LightTable internals, so can't propose an actual solution.

Edit: just realized that I've got an older version of the Python plugin installed, will need to check out the issue with the latest, but it looks like he basic problem remains the same.

dosmoc avatar Sep 07 '15 11:09 dosmoc