Skip to content

%magic% doesn't work in Emacs 25.1-2 ipython (run-python) buffer on Windows 7. #10211

Open

Description

@patfla

Programmer - normally I use ipython in emacs, in particular Ubuntu, but that machine is down for the count at the moment and so I'm, reluctantly, using windows and reconstructing the environment I like. Since there's a part not working thought I might have a try at the code.

IPython 5.1.0, Emacs 25.1-2 on Windows 7.

I run %magic% and get this trace. (same operation in ipython in a cmd window works fine)

(Pdb) c

BdbQuitTraceback (most recent call last)
<ipython-input-6-d4375e0cd73a> in <module>()
----> 1 get_ipython().magic(u'magic %')

c:\python27\lib\site-packages\IPython\core\interactiveshell.pyc in magic(self, arg_s)
   2156         magic_name, _, magic_arg_s = arg_s.partition(' ')
   2157         magic_name = magic_name.lstrip(prefilter.ESC_MAGIC)
-> 2158         return self.run_line_magic(magic_name, magic_arg_s)
   2159 
   2160     #-------------------------------------------------------------------------

c:\python27\lib\site-packages\IPython\core\interactiveshell.pyc in run_line_magic(self, magic_name, line)
   2077                 kwargs['local_ns'] = sys._getframe(stack_depth).f_locals
   2078             with self.builtin_trap:
-> 2079                 result = fn(*args,**kwargs)
   2080             return result
   2081 

<decorator-gen-32> in magic(self, parameter_s)

c:\python27\lib\site-packages\IPython\core\magic.pyc in <lambda>(f, *a, **k)
    186     # but it's overkill for just that one bit of state.
    187     def magic_deco(arg):
--> 188         call = lambda f, *a, **k: f(*a, **k)
    189 
    190         if callable(arg):

c:\python27\lib\site-packages\IPython\core\magics\basic.pyc in magic(self, parameter_s)
    257        str(self.lsmagic()),
    258        ]
--> 259         page.page('\n'.join(out))
    260 
    261 

c:\python27\lib\site-packages\IPython\core\page.py in page(data, start, screen_lines, pager_cmd)
    273 
    274     # fallback on default pager
--> 275     return pager_page(data, start, screen_lines, pager_cmd)
    276 
    277 

c:\python27\lib\site-packages\IPython\core\page.py in pager_page(strng, start, screen_lines, pager_cmd)
    186     numlines = max(num_newlines,int(len_str/80)+1)
    187     pdb.set_trace()
--> 188     screen_lines_def = get_terminal_size()[1]
    189 
    190     # auto-determine screen size

c:\python27\lib\site-packages\IPython\core\page.py in pager_page(strng, start, screen_lines, pager_cmd)
    186     numlines = max(num_newlines,int(len_str/80)+1)
    187     pdb.set_trace()
--> 188     screen_lines_def = get_terminal_size()[1]
    189 
    190     # auto-determine screen size

c:\python27\lib\bdb.pyc in trace_dispatch(self, frame, event, arg)
     47             return # None
     48         if event == 'line':
---> 49             return self.dispatch_line(frame)
     50         if event == 'call':
     51             return self.dispatch_call(frame, arg)

c:\python27\lib\bdb.pyc in dispatch_line(self, frame)
     65     def dispatch_line(self, frame):
     66         if self.stop_here(frame) or self.break_here(frame):
---> 67             self.user_line(frame)
     68             if self.quitting: raise BdbQuit
     69         return self.trace_dispatch

BdbQuit: 

It's crashing basically because get_terminal_size isn't working - here:

    def _get_terminal_size(fd):
        pdb.set_trace()
        columns = lines = 0

        try:
            handle = _handles[fd]
            csbi = create_string_buffer(22)
            res = windll.kernel32.GetConsoleScreenBufferInfo(handle, csbi)
            if res:
                res = struct.unpack("hhhhHhhhhhh", csbi.raw)
                left, top, right, bottom = res[5:9]
                columns = right - left + 1
                lines = bottom - top + 1
        except Exception:
            pass

windll.kernel32.GetConsoleScreenBufferInfo(handle, csbi) fails and returns 0. I can, later on, in pdb plug in a reasonable value for LINES and it works.

What's needed to have a crack at this?

Activity

  1. Carreau commented on Jan 28, 2017

    @Carreau
    Member

    If it's from _get_terminal_size, then it's a bug from or dependencies, so it will need to be reported here. Though the maintainer seem to have stopped releasing, so I would first try to install said package from master (see if it is fixed). And if necessary we can try to poke the maintainer, and if no response ask the PyPI folks in order to take over the package and publish an update.

    What's the raw excpetion returned by _get_terminal_size (if you don't use PDB) we can catch it and return a dummy value (like 80x40) as a workaround.

  2. patfla commented on Jan 30, 2017

    @patfla
    Author

    Thanks Carreau. Actually when I first ran it was with python 2.7, when I now rerun with 3.3 some of the problems go away. %magic% and %quickref work fine. The problem at the moment, and I'm stepping through the code to figure it out, is %recall returns nothing. The previous command is not put onto a next input line.

  3. takluyver commented on Jan 30, 2017

    @takluyver
    Member

    If you're using emacs 'inferior shell' mode, IPython switches to a simple fallback interface, because the full interface needs a real terminal. I expect that %recall is one of the things that doesn't work in the fallback.

  4. patfla commented on Jan 30, 2017

    @patfla
    Author

    Yes, it's inferior mode. What might be done to get %recall to work?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions