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

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?