[PyQt] Multiple inheritance
Florian Bruhin
me at the-compiler.org
Fri Aug 29 08:51:44 BST 2014
* Florian Bruhin <me at the-compiler.org> [2014-08-07 11:04:59 +0200]:
> Hi,
>
> I'm trying to create a subclass of Python's
> code.InteractiveInterpreter[1] which emits a pyqtSignal instead of
> printing to sys.stderr to output lines. This can be done by overriding
> its write method. However I also have to inherit QObject, so I tried
> using multiple inheritance.
>
> In this approach, even though I did override write(), output still
> gets to sys.stderr:
>
> ########
> class ConsoleInteractiveInterpreter(InteractiveInterpreter, QObject):
>
> write_output = pyqtSignal(str)
>
> def __init__(self, parent=None):
> QObject.__init__(self, parent)
> InteractiveInterpreter.__init__(self)
>
> def write(self, data):
> self.write_output.emit(data)
> ########
>
> Example:
>
> ########
> >>> inter = ConsoleInteractiveInterpreter()
> >>> _ = inter.runsource("1+1")
> 2
> ########
>
> Why is that? Did I understand something wrong?
>
> Thanks,
>
> Florian
>
> [1] https://docs.python.org/3/library/code.html#interactive-interpreter-objects
Turns out the multiple inheritance works just fine and I misunderstood
how Python's code library works.
InteractiveInterpreter.write is only used for exceptions and the like,
and there the signal triggers just fine.
However the repr's of the runned source are not printed by 'code', but
by Python itself (I think by exec()), which goes to stderr anyways.
I solved it by using a contextmanager which replaces
sys.stdout/sys.stderr with a fake stream while executing the code:
http://git.the-compiler.org/qutebrowser/tree/qutebrowser/utils/utils.py?id=8bd64382bd369b671a7f3f78a45d8c5b3a67f1ee#n448
Florian
--
http://www.the-compiler.org | me at the-compiler.org (Mail/XMPP)
GPG 0xFD55A072 | http://the-compiler.org/pubkey.asc
I love long mails! | http://email.is-not-s.ms/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 819 bytes
Desc: not available
URL: <http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20140829/3b0e8c9e/attachment-0001.sig>
More information about the PyQt
mailing list