Repository: ipython/ipython

Issue:
[qtconsole] %hist doesn't show anything in qtconsole
I have built rc2 on Windows XP and while the qtconsole works fine for the most part, %hist does not show any history in the qtconsole.



Discussion:
A bare %hist will show history from the start of the session, so be sure you've entered a few commands before trying it.

Does it produce any output after you type %hist? Any error messages? Do you get the next prompt? If you start `ipython qtconsole` (or whatever the command is in Windows) from a command prompt, do you get any error messages there? Does %hist still work in terminal IPython?

Yes. I type in a few command to see if they appear in the %hist. The 
terminal version shows %hist just fine.

If you start qtconsole with the following in a plain windows cmd console:

C:\Python26\python.exe c:\Python26\Scripts\ipython-qtconsole-script.pyw 
--pylab

Note that I'm using python.exe _not_ pythonw.exe, I get the following in 
the _cmd_ terminal when I try %hist in the qtconsole:

> C:\Python26\python.exe c:\Python26\Scripts\ipython-qtconsole-script.pyw --pylab
>   --IPythonWidget.font_size=13 --no-confirm-exit --autocall=2 
> --no-gui-completion

[IPKernelApp] To connect another client to this kernel, use:
[IPKernelApp] --existing --shell=1158 --iopub=1159 --stdin=1160 --hb=1161
Warning: received unknown payload of type 
u'IPython.zmq.zmqshell.ZMQInteractiveS
hell.auto_rewrite_input'
arange 10

Notice the "Warning: received unknown payload of type 
u'IPython.zmq.zmqshell.ZMQInteractiveS
hell.auto_rewrite_input"

Thus, %hist is reporting history in the cmd terminal, not in the qtconsole.

I hope that helps.

On 7/21/2011 7:35 AM, takluyver wrote:

> A bare %hist will show history from the start of the session, so be sure you've entered a few commands before trying it.
> 
> Does it produce any output after you type %hist? Any error messages? Do you get the next prompt? If you start `ipython qtconsole` (or whatever the command is in Windows) from a command prompt, do you get any error messages there? Does %hist still work in terminal IPython?

On 21 July 2011 16:32, reckoner <
reply@reply.github.com>wrote:

> Thus, %hist is reporting history in the cmd terminal, not in the qtconsole.

The warning is to do with something else, and it's harmless. But you're
right, it does look like the output is appearing at the terminal rather than
being forwarded.

@minrk: The %hist magic currently uses `print(...,
file=IPython.utils.io.stdout)`, and this gets redirected properly on Linux,
but seemingly not on Windows. Should it be printing to another object? Do we
just print to sys.stdout now?

utils.io.stout should be hooked up to sys.stdout (in shell.init_io).  it does a different thing on Windows for pyreadline, but that should only be done with the terminal frontend, rather that always.

-MinRK

On Jul 21, 2011, at 8:52, takluyverreply@reply.github.com wrote:

> On 21 July 2011 16:32, reckoner <
> reply@reply.github.com>wrote:
> 
> > Thus, %hist is reporting history in the cmd terminal, not in the qtconsole.
> 
> The warning is to do with something else, and it's harmless. But you're
> right, it does look like the output is appearing at the terminal rather than
> being forwarded.
> 
> @minrk: The %hist magic currently uses `print(...,
> file=IPython.utils.io.stdout)`, and this gets redirected properly on Linux,
> but seemingly not on Windows. Should it be printing to another object? Do we
> just print to sys.stdout now?
> 
> ## 
> 
> Reply to this email directly or view it on GitHub:
> https://github.com/ipython/ipython/issues/617#issuecomment-1624459

OK, looking at the code, it looks like it's happening an all cases on Windows where pyreadline is in use. I think the ZMQshell should have readline_use default to False (which will also speed up launching it a bit). Pull request coming up.

@reckoner: Can you try my i617 branch from here: https://github.com/takluyver/ipython/tree/i617

It may raise further problems, but I've just tested it here, and it doesn't obviously go wrong.

OK. I'll try. What're the relevent git commands to use?

On 7/21/2011 10:32 AM, takluyver wrote:

> @reckoner: Can you try my i617 branch from here: https://github.com/takluyver/ipython/tree/i617
> 
> It may raise further problems, but I've just tested it here, and it doesn't obviously go wrong.

<pre>
git remote add takluyver git://github.com/takluyver/ipython.git
git fetch takluyver
git checkout i617
</pre>


That should do it. I install in a virtualenv for testing, but you can run the source however you want, so long as it's definitely importing the correct modules.

Works! Except the history items are not numbered.

Also, how do I undo the git commands below to return to what I 
previously had.

Thanks!

On 7/21/2011 10:58 AM, takluyver wrote:

> <pre>
> git remote add takluyver git://github.com/takluyver/ipython.git
> git fetch takluyver
> git checkout i617
> </pre>
> 
> 
> That should do it. I install in a virtualenv for testing, but you can run the source however you want, so long as it's definitely importing the correct modules.

OK, great. -g doesn't automatically number the items, although it probably should. I'll tweak that.

Assuming you're working from a clone of the main codebase, `git checkout master` should take you back to the main branch.

great! If you get the numbering going, without which I don't see how you 
can use %rep or %macro, I'll test again for you on windows.

On 7/21/2011 11:25 AM, takluyver wrote:

> OK, great. -g doesn't automatically number the items, although it probably should. I'll tweak that.
> 
> Assuming you're working from a clone of the main codebase, `git checkout master` should take you back to the main branch.

The numbering would already happen if you use `%hist -ng pattern` (n for numbers). I've updated my i617 branch so that -g implies -n as well. See also Pull request #618.

On Thu, Jul 21, 2011 at 9:07 AM, reckoner
reply@reply.github.com
wrote:

> I have built rc2 on Windows XP and while the qtconsole works fine for the most part, %hist does not show any history in the qtconsole.

Mmh, can anyone confirm this, and whether the problem is
windows-specific? On linux %hist works fine in the Qt console...

@fperez: If you're replying by email, do click through and read the intervening discussion. We've tracked it down to a problem related to pyreadline, and I've got a fix that apparently works (PR #618).

Oops, sorry :) I forgot gh only sends the first email...

Closed by ff5e7cd7aececbe596d8a60cc083dff44c0b0666.

Hi folks, I'm afraid I have to reopen this, because I'm effectively going to revert b641eb66099e85685d315fb420aab560889fbc48.  Unfortunately it caused %run to break in the Qt console with this error:

<pre>
In [1]: run ~/scratch/error.py
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
/home/fperez/ipython/ipython/tools/<ipython-input-1-eebf1ad259b0> in <module>()
----> 1 get_ipython().magic(u"run ~/scratch/error.py")

/home/fperez/usr/lib/python2.6/site-packages/IPython/core/interactiveshell.py in magic(self, arg_s, next_input)
   1892                 self._magic_locals = sys._getframe(1).f_locals
   1893             with self.builtin_trap:
-> 1894                 result = fn(magic_args)
   1895             # Ensure we're not keeping object references around:
   1896             self._magic_locals = {}

/home/fperez/usr/lib/python2.6/site-packages/IPython/core/magic.pyc in magic_run(self, parameter_s, runner, file_finder)
   1634         try:
   1635             stats = None
-> 1636             with self.readline_no_record:
   1637                 if opts.has_key('p'):
   1638                     stats = self.magic_prun('',0,opts,arg_lst,prog_ns)

AttributeError: 'ZMQInteractiveShell' object has no attribute 'readline_no_record'
</pre>


but even if I try to make an 'empty' context manager to play the role of readliine_no_record, then syntax highlighting is broken.  I simply don't have time right now to go into deep debugging right prior to a release.

This is why, even the most innocent-looking changes, should never be made between an RC and a final release.  It was by pure accident that I caught the problem, and since we don't have any way to automatically test the magics from the Qt console, we'd only have seen this after an embarrassing public release.

Ouch, well spotted. Your commit message seems to have automatically closed it again, so I'll re-reopen it.


Resolving patch:
diff --git a/IPython/core/interactiveshell.py b/IPython/core/interactiveshell.py
--- a/IPython/core/interactiveshell.py
+++ b/IPython/core/interactiveshell.py
@@ -108,6 +108,11 @@ def softspace(file, newvalue):
 
 def no_op(*a, **kw): pass
 
+class NoOpContext(object):
+    def __enter__(self): pass
+    def __exit__(self, type, value, traceback): pass
+no_op_context = NoOpContext()
+
 class SpaceInInput(Exception): pass
 
 class Bunch: pass
@@ -242,6 +247,15 @@ class InteractiveShell(SingletonConfigurable, Magic):
                              default_value=get_default_colors(), config=True,
         help="Set the color scheme (NoColor, Linux, or LightBG)."
     )
+    colors_force = CBool(False, help=
+        """
+        Force use of ANSI color codes, regardless of OS and readline
+        availability.
+        """
+        # FIXME: This is essentially a hack to allow ZMQShell to show colors
+        # without readline on Win32. When the ZMQ formatting system is
+        # refactored, this should be removed.
+    )
     debug = CBool(False, config=True)
     deep_reload = CBool(False, config=True, help=
         """
@@ -1636,10 +1650,12 @@ def init_readline(self):
             self.has_readline = False
             self.readline = None
             # Set a number of methods that depend on readline to be no-op
+            self.readline_no_record = no_op_context
             self.set_readline_completer = no_op
             self.set_custom_completer = no_op
             self.set_completer_frame = no_op
-            warn('Readline services not available or not loaded.')
+            if self.readline_use:
+                warn('Readline services not available or not loaded.')
         else:
             self.has_readline = True
             self.readline = readline
diff --git a/IPython/core/magic.py b/IPython/core/magic.py
--- a/IPython/core/magic.py
+++ b/IPython/core/magic.py
@@ -2496,7 +2496,8 @@ def color_switch_err(name):
 
         import IPython.utils.rlineimpl as readline
 
-        if not readline.have_readline and sys.platform == "win32":
+        if not shell.colors_force and \
+                not readline.have_readline and sys.platform == "win32":
             msg = """\
 Proper color support under MS Windows requires the pyreadline library.
 You can find it at:
@@ -2510,7 +2511,7 @@ def color_switch_err(name):
             warn(msg)
         
         # readline option is 0
-        if not shell.has_readline:
+        if not shell.colors_force and not shell.has_readline:
             new_scheme = 'NoColor'
             
         # Set prompt colors
diff --git a/IPython/zmq/zmqshell.py b/IPython/zmq/zmqshell.py
--- a/IPython/zmq/zmqshell.py
+++ b/IPython/zmq/zmqshell.py
@@ -84,13 +84,8 @@ class ZMQInteractiveShell(InteractiveShell):
     # Override the traitlet in the parent class, because there's no point using
     # readline for the kernel. Can be removed when the readline code is moved
     # to the terminal frontend.
-
-    # FIXME.  This is disabled for now, even though it may cause problems under
-    # Windows, because it breaks %run in the Qt console.  See gh-617 for more
-    # details.  Re-enable once we've fully tested that %run works in the Qt
-    # console with syntax highlighting in tracebacks.
-    # readline_use = CBool(False)
-    # /FIXME
+    colors_force = CBool(True)
+    readline_use = CBool(False)
     
     exiter = Instance(ZMQExitAutocall)
     def _exiter_default(self):


Test patch: