From 50b957772a0f6d9ca4ce2c9ab877ff0f498ba5cf Mon Sep 17 00:00:00 2001 From: jstenar <> Date: Mon, 13 Nov 2006 18:06:44 +0000 Subject: [PATCH] pyreadline-refactor: More improvements of ironpython handling, more testcases --- doc/ChangeLog | 21 ++++++++++++ pyreadline/console/ansi.py | 9 +++--- pyreadline/console/ironpython_console.py | 15 ++------- pyreadline/keysyms/__init__.py | 14 +++++--- pyreadline/keysyms/ironpython_keysyms.py | 4 +-- pyreadline/lineeditor/history.py | 10 +++--- pyreadline/modes/basemode.py | 5 +-- pyreadline/modes/emacs.py | 7 ++-- pyreadline/rlmain.py | 14 ++------ pyreadline/test/emacs_test.py | 41 +++++++++++++++++++++--- 10 files changed, 91 insertions(+), 49 deletions(-) diff --git a/doc/ChangeLog b/doc/ChangeLog index 08927c1..1baf3cb 100644 --- a/doc/ChangeLog +++ b/doc/ChangeLog @@ -1,3 +1,24 @@ +2006-11-06 Jörgen Stenarson + * Fixed bug in tab completion when point is not at end of line + * Changed clr.AddReference to clr.AddReferenceToFileAndPath (ironpython_console) + * Fix points position after tab completion + * Fix for difference in handling of multiline input between cpython and ironpython (emacs.py) + * added test_complete case to emacs_test.py + +2006-11-03 Jörgen Stenarson + * Improvements in ironpython handling. + * Detection of ironpython explicit on sys.version instead of relying on presense of ctypes + * Lots of fixes to take care of peculiaritis of System.Console in .NET it does not have + the full windows console functionality. Fill a rectangle is missing, writing without + affecting cursor is missing, altgr keys show up as alt+ctrl so had to blank alt+ctrl in + keyboard event checking, endoffile signal to from readline is not using EOFError in ironpython + rather returning None + * Set selectioncolor by looking at foregroundcolor at startup + * open in ironpython does not ignore unknown letters in mode silently, hade "rt" in history open + * Have to print prompt in ironpython since ironpythons readline looks at sys.ps1 and print it instead + of passing it into readline explicitly. + * Fixed another bug in historysearch + 2006-10-25 Jörgen Stenarson * port set_text_color and set_prompt_color config file options from trunk * Fix bug in history search and add tests for the case. diff --git a/pyreadline/console/ansi.py b/pyreadline/console/ansi.py index e20bf10..ffc0119 100644 --- a/pyreadline/console/ansi.py +++ b/pyreadline/console/ansi.py @@ -1,6 +1,5 @@ # -*- coding: ISO-8859-1 -*- -import re,sys,os,pprint -pprint=pprint.pprint +import re,sys,os terminal_escape = re.compile('(\001?\033\\[[0-9;]*m\002?)') escape_parts = re.compile('\001?\033\\[([0-9;]*)m\002?') @@ -153,7 +152,9 @@ def write_color_old( text, attr=None): #trtable={0:"black",1:"red",2:"green",3:"yellow",4:"blue",5:"magenta",6:"cyan",7:"white"} if __name__=="__main__": - import startup + import pprint + pprint=pprint.pprint + s="\033[0;31mred\033[0;32mgreen\033[0;33myellow\033[0;34mblue\033[0;35mmagenta\033[0;36mcyan\033[0;37mwhite\033[0m" pprint (write_color(s)) pprint (write_color_old(s)) @@ -177,4 +178,4 @@ if __name__=="__main__": c.write_color("\033[0;32mIn [\033[1;32m1\033[0;32m]:") print pprint (write_color("\033[0;32mIn [\033[1;32m1\033[0;32m]:")) - \ No newline at end of file + diff --git a/pyreadline/console/ironpython_console.py b/pyreadline/console/ironpython_console.py index 3fa5ee2..9a535ca 100644 --- a/pyreadline/console/ironpython_console.py +++ b/pyreadline/console/ironpython_console.py @@ -22,16 +22,13 @@ # # # - - # primitive debug printing that won't interfere with the screen -import clr -clr.AddReference("ipy.exe") +import clr,sys +clr.AddReferenceToFileAndPath(sys.executable) import IronPythonConsole import sys -import traceback import re import os @@ -401,14 +398,6 @@ def install_readline(hook): IronPythonConsole.PythonCommandLine.MyConsole = IronPythonWrapper() -def getconsole(buffer=1): - """Get a console handle. - - If buffer is non-zero, a new console buffer is allocated and - installed. Otherwise, this returns a handle to the current - console buffer""" - c = Console(buffer) - return c if __name__ == '__main__': import time, sys diff --git a/pyreadline/keysyms/__init__.py b/pyreadline/keysyms/__init__.py index 113610b..b7b6ed9 100644 --- a/pyreadline/keysyms/__init__.py +++ b/pyreadline/keysyms/__init__.py @@ -1,16 +1,20 @@ -import glob +import sys success=False -try: - from keysyms import * - success=True -except ImportError,x: +in_ironpython=sys.version.startswith("IronPython") +if in_ironpython: try: from ironpython_keysyms import * success=True except ImportError,x: pass +else: + try: + from keysyms import * + success=True + except ImportError,x: + pass if not success: raise ImportError("Could not import keysym for local pythonversion",x) \ No newline at end of file diff --git a/pyreadline/keysyms/ironpython_keysyms.py b/pyreadline/keysyms/ironpython_keysyms.py index b856ad7..dc7168a 100644 --- a/pyreadline/keysyms/ironpython_keysyms.py +++ b/pyreadline/keysyms/ironpython_keysyms.py @@ -8,7 +8,7 @@ #***************************************************************************** import System from common import validkey,KeyPress,make_KeyPress_from_keydescr -from pyreadline.logger import log_sock +#from pyreadline.logger import log_sock c32=System.ConsoleKey Shift=System.ConsoleModifiers.Shift Control=System.ConsoleModifiers.Control @@ -196,7 +196,7 @@ def make_KeyPress(char,state,keycode): control=bool(int(state)&int(Control)) meta=bool(int(state)&int(Alt)) keyname=code2sym_map.get(keycode,"").lower() - log_sock("make key %s %s %s %s"%(shift,control,meta,keycode),"keysyms") +# log_sock("make key %s %s %s %s"%(shift,control,meta,keycode),"keysyms") if control and meta: #equivalent to altgr so clear flags control=False meta=False diff --git a/pyreadline/lineeditor/history.py b/pyreadline/lineeditor/history.py index 4b91e06..8007b76 100644 --- a/pyreadline/lineeditor/history.py +++ b/pyreadline/lineeditor/history.py @@ -142,7 +142,7 @@ class LineHistory(object): pyreadline.rl._clear_after() event = c.getkeypress() - log_sock(str(event)) + log_sock(str(event),"history") if event.keyinfo.keyname == 'backspace': if len(query) > 0: @@ -155,7 +155,7 @@ class LineHistory(object): break else: pyreadline.rl._bell() - log_sock(query) + log_sock(query,"history") res="" if query: if direction==-1: @@ -163,7 +163,7 @@ class LineHistory(object): else: res=self.forward_search_history(query) - log_sock(res) + log_sock(res,"history") return lineobj.ReadLineTextBuffer(res,point=0) def non_incremental_reverse_search_history(self,current): # (M-p) @@ -184,7 +184,7 @@ class LineHistory(object): self.lastcommand != self.history_search_backward): self.query = ''.join(partial[0:partial.point].get_line_text()) hcstart=max(self.history_cursor,0) - log_sock("hcstart %s"%hcstart) + log_sock("hcstart %s"%hcstart,"history") hc = self.history_cursor + direction while (direction < 0 and hc >= 0) or (direction > 0 and hc < len(self.history)): h = self.history[hc] @@ -209,7 +209,7 @@ class LineHistory(object): return lineobj.ReadLineTextBuffer(partial,point=partial.point) return lineobj.ReadLineTextBuffer(self.query,point=min(len(self.query),partial.point)) except IndexError: - log_sock("hcstart:%s %s"%(hcstart,len(self.history))) + log_sock("hcstart:%s %s"%(hcstart,len(self.history)),"history") raise def history_search_forward(self,partial): # () diff --git a/pyreadline/modes/basemode.py b/pyreadline/modes/basemode.py index 3689de7..32edb0a 100644 --- a/pyreadline/modes/basemode.py +++ b/pyreadline/modes/basemode.py @@ -8,7 +8,7 @@ #***************************************************************************** import os,re,math,glob,sys import pyreadline.logger as logger -from pyreadline.logger import log +from pyreadline.logger import log,log_sock from pyreadline.keysyms.common import make_KeyPress_from_keydescr import pyreadline.lineeditor.lineobj as lineobj import pyreadline.lineeditor.history as history @@ -186,8 +186,9 @@ class BaseMode(object): if completions: cprefix = commonprefix(completions) rep = [ c for c in cprefix ] + point=self.l_buffer.point self.l_buffer[self.begidx:self.endidx] = rep - self.l_buffer.point += len(rep) - (self.endidx - self.begidx) + self.l_buffer.point = point + len(rep) - (self.endidx - self.begidx) if len(completions) > 1: if self.show_all_if_ambiguous == 'on': self._display_completions(completions) diff --git a/pyreadline/modes/emacs.py b/pyreadline/modes/emacs.py index 9231302..69b3338 100644 --- a/pyreadline/modes/emacs.py +++ b/pyreadline/modes/emacs.py @@ -62,7 +62,7 @@ class EmacsMode(basemode.BaseMode): dispatch_func = self.key_dispatch.get(keyinfo,default) log("readline from keyboard:%s,%s"%(keyinfo,dispatch_func)) - log_sock("%s|%s"%(format(keyinfo),dispatch_func.__name__)) + log_sock("%s|%s"%(format(keyinfo),dispatch_func.__name__),"bound_function") r = None if dispatch_func: r = dispatch_func(event) @@ -119,7 +119,10 @@ class EmacsMode(basemode.BaseMode): self.add_history(self.l_buffer.copy()) log('returning(%s)' % self.l_buffer.get_line_text()) - return self.l_buffer.get_line_text() + '\n' + if in_ironpython: + return self.l_buffer.get_line_text() + else: + return self.l_buffer.get_line_text() + '\n' ######### History commands def previous_history(self, e): # (C-p) diff --git a/pyreadline/rlmain.py b/pyreadline/rlmain.py index 52716e6..639ecfd 100644 --- a/pyreadline/rlmain.py +++ b/pyreadline/rlmain.py @@ -7,16 +7,8 @@ # the file COPYING, distributed as part of this software. #***************************************************************************** ''' an attempt to implement readline for Python in Python using ctypes''' - -import string -import math -import sys +import sys,os,re from glob import glob -import os,pdb -import re -import traceback -import operator -import exceptions import clipboard,logger,console from logger import log,log_sock @@ -35,6 +27,7 @@ if in_ironpython:#ironpython does not provide a prompt string to readline default_prompt=">>> " else: default_prompt="" + import pdb def quote_char(c): @@ -143,7 +136,6 @@ class Readline(object): self.mode._bind_key(key, func) except: log('error') - traceback.print_exc() raise def get_line_buffer(self): @@ -268,7 +260,7 @@ class Readline(object): if self.bell_style == 'none': pass elif self.bell_style == 'visible': - raise exceptions.NotImplementedError("Bellstyle visible is not implemented yet.") + raise NotImplementedError("Bellstyle visible is not implemented yet.") elif self.bell_style == 'audible': self.console.bell() else: diff --git a/pyreadline/test/emacs_test.py b/pyreadline/test/emacs_test.py index 18a4d0a..8b3194b 100644 --- a/pyreadline/test/emacs_test.py +++ b/pyreadline/test/emacs_test.py @@ -15,6 +15,11 @@ from pyreadline import keysyms from pyreadline.lineeditor import lineobj from common import * +from pyreadline.logger import log_sock +import pyreadline.logger as logger +logger.sock_silent=True +logger.show_event=["debug"] + #---------------------------------------------------------------------- @@ -28,6 +33,8 @@ class EmacsModeTest (EmacsMode): self.completer = self.mock_completer self.completer_delims = ' ' self.tabstop = 4 + self.mark_directories=False + self.show_all_if_ambiguous=False def get_mock_console (self): return self.mock_console @@ -53,9 +60,8 @@ class EmacsModeTest (EmacsMode): keyinfo, event = keytext_to_keyinfo_and_event (key) dispatch_func = self.key_dispatch.get(keyinfo.tuple(),self.self_insert) self.tested_commands[dispatch_func.__name__]=dispatch_func -# print key,dispatch_func.__name__ + log_sock("keydisp: %s %s"%( key,dispatch_func.__name__),"debug") dispatch_func (event) - log_sock("emacs readline from keyboard:%s->%s"%(keyinfo.tuple(),dispatch_func)) self.previous_func=dispatch_func def accept_line (self, e): @@ -317,8 +323,33 @@ class TestsHistory (unittest.TestCase): r.input ('Up') self.assert_line(r,'k',1) + def test_complete (self): + import rlcompleter + logger.sock_silent=False + log_sock("-"*50,"debug") + r=EmacsModeTest() + r.completer=rlcompleter.Completer().complete + r._bind_key("tab",r.complete) + r.input('"exi(ksdjksjd)"') + r.input('Control-a') + r.input('Right') + r.input('Right') + r.input('Right') + r.input('Tab') + self.assert_line(r,"exit(ksdjksjd)",4) + r.input('Escape') + r.input('"exi"') + r.input('Control-a') + r.input('Right') + r.input('Right') + r.input('Right') + r.input('Tab') + self.assert_line(r,"exit",4) + + + def assert_line(self,r,line,cursor): self.assertEqual (r.line, line) self.assertEqual (r.line_cursor, cursor) @@ -332,9 +363,9 @@ if __name__ == '__main__': Tester() tested=EmacsModeTest.tested_commands.keys() tested.sort() - print " Tested functions ".center(60,"-") - print "\n".join(tested) - print +# print " Tested functions ".center(60,"-") +# print "\n".join(tested) +# print all_funcs=dict([(x.__name__,x) for x in EmacsModeTest().key_dispatch.values()]) all_funcs=all_funcs.keys()