This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author graphite
Recipients graphite
Date 2015-07-09.13:00:11
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1436446812.02.0.787958220284.issue24595@psf.upfronthosting.co.za>
In-reply-to
Content
I have a use-case when I need to forward InteractiveConsole through Unix/TCP socket. Expected implementation:

class InteractiveSocket(InteractiveConsole):
  def __init__(self, socket):
    self._socket = socket
    ...

  def raw_input(...):
    # read from socket

  def write(...):
    # write to socket

However, only syntax errors and tracebacks are written into a socket, while actual output still appears on servers stdout. Digging through it I realized, that the output happens inside exec call in InteractiveInterpreter.runcode and here is why:

>>> c=compile('42', '', 'single')
>>> dis.dis(c)
<<< 1           0 LOAD_CONST               0 (42)
                3 PRINT_EXPR          
                4 LOAD_CONST               1 (None)
                7 RETURN_VALUE

where PRINT_EXPR uses _Py_IDENTIFIER(displayhook);

I ended up with the following dirty hack in my derived class:

def runcode(self, code):
  class OutWriter:
    pass
  writer = OutWriter()
  writer.write = self.write
  old = sys.stdout
  sys.stdout = writer
  try:
    exec(code, self.locals)
  except SystemExit:
    raise
  except:
    self.showtraceback()
  sys.stdout = old

I think it would be nice to make InteractiveInterpreter extendable out of the box, though I don't have exact solution here.
History
Date User Action Args
2015-07-09 13:00:12graphitesetrecipients: + graphite
2015-07-09 13:00:12graphitesetmessageid: <1436446812.02.0.787958220284.issue24595@psf.upfronthosting.co.za>
2015-07-09 13:00:11graphitelinkissue24595 messages
2015-07-09 13:00:11graphitecreate