mirror of
https://github.com/wassname/pyreadline.git
synced 2026-06-27 16:10:38 +08:00
change function name: ensure_str -> ensure_bytes.
change function name: ensure_unicode -> ensure_str.
This commit is contained in:
@@ -34,7 +34,7 @@
|
||||
|
||||
from ctypes import *
|
||||
from pyreadline.keysyms.winconstants import CF_TEXT, GHND
|
||||
from pyreadline.unicode_helper import ensure_unicode,ensure_str
|
||||
from pyreadline.unicode_helper import ensure_str,ensure_bytes
|
||||
|
||||
OpenClipboard = windll.user32.OpenClipboard
|
||||
OpenClipboard.argtypes = [c_int]
|
||||
@@ -88,10 +88,10 @@ def GetClipboardText():
|
||||
text = GlobalLock(hClipMem)
|
||||
GlobalUnlock(hClipMem)
|
||||
CloseClipboard()
|
||||
return ensure_unicode(text)
|
||||
return ensure_str(text)
|
||||
|
||||
def SetClipboardText(text):
|
||||
buffer = c_buffer(ensure_str(text))
|
||||
buffer = c_buffer(ensure_bytes(text))
|
||||
bufferSize = sizeof(buffer)
|
||||
hGlobalMem = GlobalAlloc(c_int(GHND), c_int(bufferSize))
|
||||
GlobalLock.restype = c_void_p
|
||||
|
||||
@@ -20,7 +20,7 @@ import re
|
||||
import pyreadline.unicode_helper as unicode_helper
|
||||
|
||||
from pyreadline.logger import log
|
||||
from pyreadline.unicode_helper import ensure_unicode, ensure_str
|
||||
from pyreadline.unicode_helper import ensure_str, ensure_bytes
|
||||
from pyreadline.keysyms import make_KeyPress, KeyPress
|
||||
from pyreadline.console.ansi import AnsiState,AnsiWriter
|
||||
|
||||
@@ -306,7 +306,7 @@ class Console(object):
|
||||
w, h = self.size()
|
||||
scroll = 0 # the result
|
||||
# split the string into ordinary characters and funny characters
|
||||
chunks = self.motion_char_re.split(ensure_str(text))
|
||||
chunks = self.motion_char_re.split(ensure_bytes(text))
|
||||
for chunk in chunks:
|
||||
n = self.write_color(chunk, attr)
|
||||
if len(chunk) == 1: # the funny characters will be alone
|
||||
@@ -345,7 +345,7 @@ class Console(object):
|
||||
return scroll
|
||||
|
||||
def write_color(self, text, attr=None):
|
||||
text = ensure_unicode(text)
|
||||
text = ensure_str(text)
|
||||
n, res= self.ansiwriter.write_color(text, attr)
|
||||
junk = c_int(0)
|
||||
for attr,chunk in res:
|
||||
@@ -359,14 +359,14 @@ class Console(object):
|
||||
|
||||
def write_plain(self, text, attr=None):
|
||||
'''write text at current cursor position.'''
|
||||
text = ensure_unicode(text)
|
||||
text = ensure_str(text)
|
||||
log('write("%s", %s)' %(text, attr))
|
||||
if attr is None:
|
||||
attr = self.attr
|
||||
n = c_int(0)
|
||||
self.SetConsoleTextAttribute(self.hout, attr)
|
||||
for short_chunk in split_block(chunk):
|
||||
self.WriteConsoleW(self.hout, ensure_unicode(short_chunk),
|
||||
self.WriteConsoleW(self.hout, ensure_str(short_chunk),
|
||||
len(short_chunk), byref(junk), None)
|
||||
return len(text)
|
||||
|
||||
@@ -374,7 +374,7 @@ class Console(object):
|
||||
#Emacs sets the EMACS environment variable
|
||||
if "EMACS" in os.environ:
|
||||
def write_color(self, text, attr=None):
|
||||
text = ensure_str(text)
|
||||
text = ensure_bytes(text)
|
||||
junk = c_int(0)
|
||||
self.WriteFile(self.hout, text, len(text), byref(junk), None)
|
||||
return len(text)
|
||||
@@ -382,7 +382,7 @@ class Console(object):
|
||||
|
||||
# make this class look like a file object
|
||||
def write(self, text):
|
||||
text = ensure_unicode(text)
|
||||
text = ensure_str(text)
|
||||
log('write("%s")' % text)
|
||||
return self.write_color(text)
|
||||
|
||||
@@ -455,7 +455,7 @@ class Console(object):
|
||||
source = SMALL_RECT(x0, y0, x1 - 1, y1 - 1)
|
||||
dest = self.fixcoord(x0 + dx, y0 + dy)
|
||||
style = CHAR_INFO()
|
||||
style.Char.AsciiChar = ensure_str(fill[0])
|
||||
style.Char.AsciiChar = ensure_bytes(fill[0])
|
||||
style.Attributes = attr
|
||||
|
||||
return self.ScrollConsoleScreenBufferW(self.hout, byref(source),
|
||||
@@ -688,7 +688,7 @@ def hook_wrapper_23(stdin, stdout, prompt):
|
||||
'''Wrap a Python readline so it behaves like GNU readline.'''
|
||||
try:
|
||||
# call the Python hook
|
||||
res = ensure_str(readline_hook(prompt))
|
||||
res = ensure_bytes(readline_hook(prompt))
|
||||
# make sure it returned the right sort of thing
|
||||
if res and not isinstance(res, bytes):
|
||||
raise TypeError('readline must return a string.')
|
||||
@@ -712,7 +712,7 @@ def hook_wrapper(prompt):
|
||||
'''Wrap a Python readline so it behaves like GNU readline.'''
|
||||
try:
|
||||
# call the Python hook
|
||||
res = ensure_str(readline_hook(prompt))
|
||||
res = ensure_bytes(readline_hook(prompt))
|
||||
# make sure it returned the right sort of thing
|
||||
if res and not isinstance(res, bytes):
|
||||
raise TypeError('readline must return a string.')
|
||||
|
||||
@@ -13,7 +13,7 @@ try:
|
||||
except NameError:
|
||||
from sets import Set as set
|
||||
|
||||
from pyreadline.unicode_helper import ensure_unicode
|
||||
from pyreadline.unicode_helper import ensure_str
|
||||
|
||||
validkey =set(['cancel', 'backspace', 'tab', 'clear',
|
||||
'return', 'shift_l', 'control_l', 'alt_l',
|
||||
@@ -62,7 +62,7 @@ class KeyPress(object):
|
||||
keyname = create("keyname")
|
||||
|
||||
def __repr__(self):
|
||||
return "(%s,%s,%s,%s)"%tuple(map(ensure_unicode, self.tuple()))
|
||||
return "(%s,%s,%s,%s)"%tuple(map(ensure_str, self.tuple()))
|
||||
|
||||
def tuple(self):
|
||||
if self.keyname:
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
#*****************************************************************************
|
||||
import re, operator,string, sys,os
|
||||
|
||||
from pyreadline.unicode_helper import ensure_unicode, ensure_str
|
||||
from pyreadline.unicode_helper import ensure_str, ensure_bytes
|
||||
if "pyreadline" in sys.modules:
|
||||
pyreadline = sys.modules["pyreadline"]
|
||||
else:
|
||||
@@ -79,7 +79,7 @@ class LineHistory(object):
|
||||
filename = self.history_filename
|
||||
try:
|
||||
for line in open(filename, 'r'):
|
||||
self.add_history(lineobj.ReadLineTextBuffer(ensure_unicode(line.rstrip())))
|
||||
self.add_history(lineobj.ReadLineTextBuffer(ensure_str(line.rstrip())))
|
||||
except IOError:
|
||||
self.history = []
|
||||
self.history_cursor = 0
|
||||
@@ -90,7 +90,7 @@ class LineHistory(object):
|
||||
filename = self.history_filename
|
||||
fp = open(filename, 'wb')
|
||||
for line in self.history[-self.history_length:]:
|
||||
fp.write(ensure_str(line.get_line_text()))
|
||||
fp.write(ensure_bytes(line.get_line_text()))
|
||||
fp.write('\n')
|
||||
fp.close()
|
||||
|
||||
|
||||
@@ -10,7 +10,7 @@ import re, operator, sys
|
||||
from . import wordmatcher
|
||||
import pyreadline.clipboard as clipboard
|
||||
from pyreadline.logger import log
|
||||
from pyreadline.unicode_helper import ensure_unicode
|
||||
from pyreadline.unicode_helper import ensure_str
|
||||
|
||||
kill_ring_to_clipboard = False #set to true to copy every addition to kill ring to clipboard
|
||||
|
||||
@@ -248,11 +248,11 @@ class TextLine(object):
|
||||
def quoted_text(self):
|
||||
quoted = [ quote_char(c) for c in self.line_buffer ]
|
||||
self.line_char_width = [ len(c) for c in quoted ]
|
||||
return ''.join(map(ensure_unicode, quoted))
|
||||
return ''.join(map(ensure_str, quoted))
|
||||
|
||||
def get_line_text(self):
|
||||
buf = self.line_buffer
|
||||
buf = list(map(ensure_unicode, buf))
|
||||
buf = list(map(ensure_str, buf))
|
||||
return ''.join(buf)
|
||||
|
||||
def set_line(self, text, cursor = None):
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
#*****************************************************************************
|
||||
|
||||
import socket, logging, logging.handlers
|
||||
from pyreadline.unicode_helper import ensure_str
|
||||
from pyreadline.unicode_helper import ensure_bytes
|
||||
|
||||
host = "localhost"
|
||||
port = logging.handlers.DEFAULT_TCP_LOGGING_PORT
|
||||
@@ -28,7 +28,7 @@ class SocketStream(object):
|
||||
self.logsocket = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
|
||||
|
||||
def write(self, s):
|
||||
self.logsocket.sendto(ensure_str(s), (host, port))
|
||||
self.logsocket.sendto(ensure_bytes(s), (host, port))
|
||||
|
||||
def flush(self):
|
||||
pass
|
||||
@@ -69,5 +69,5 @@ def stop_logging():
|
||||
stop_socket_log()
|
||||
|
||||
def log(s):
|
||||
s = ensure_str(s)
|
||||
s = ensure_bytes(s)
|
||||
pyreadline_logger.debug(s)
|
||||
|
||||
@@ -14,7 +14,7 @@ import pyreadline.lineeditor.lineobj as lineobj
|
||||
import pyreadline.lineeditor.history as history
|
||||
import pyreadline.clipboard as clipboard
|
||||
from pyreadline.error import ReadlineError,GetSetError
|
||||
from pyreadline.unicode_helper import ensure_str, ensure_unicode
|
||||
from pyreadline.unicode_helper import ensure_bytes, ensure_str
|
||||
import collections
|
||||
in_ironpython="IronPython" in sys.version
|
||||
|
||||
@@ -191,12 +191,12 @@ class BaseMode(object):
|
||||
if buf[self.begidx] in self.completer_delims:
|
||||
self.begidx += 1
|
||||
break
|
||||
text = ensure_str(''.join(buf[self.begidx:self.endidx]))
|
||||
log('complete text="%s"' % ensure_unicode(text))
|
||||
text = ensure_bytes(''.join(buf[self.begidx:self.endidx]))
|
||||
log('complete text="%s"' % ensure_str(text))
|
||||
i = 0
|
||||
while 1:
|
||||
try:
|
||||
r = self.completer(ensure_unicode(text), i)
|
||||
r = self.completer(ensure_str(text), i)
|
||||
except IndexError:
|
||||
break
|
||||
i += 1
|
||||
@@ -206,7 +206,7 @@ class BaseMode(object):
|
||||
completions.append(r)
|
||||
else:
|
||||
pass
|
||||
log('text completions=<%s>' % list(map(ensure_unicode, completions)))
|
||||
log('text completions=<%s>' % list(map(ensure_str, completions)))
|
||||
if (self.complete_filesystem == "on") and not completions:
|
||||
# get the filename to complete
|
||||
while self.begidx > 0:
|
||||
@@ -214,9 +214,9 @@ class BaseMode(object):
|
||||
if buf[self.begidx] in ' \t\n':
|
||||
self.begidx += 1
|
||||
break
|
||||
text = ensure_str(''.join(buf[self.begidx:self.endidx]))
|
||||
log('file complete text="%s"' % ensure_unicode(text))
|
||||
completions = list(map(ensure_unicode, glob.glob(os.path.expanduser(text) + b'*')))
|
||||
text = ensure_bytes(''.join(buf[self.begidx:self.endidx]))
|
||||
log('file complete text="%s"' % ensure_str(text))
|
||||
completions = list(map(ensure_str, glob.glob(os.path.expanduser(text) + b'*')))
|
||||
if self.mark_directories == 'on':
|
||||
mc = []
|
||||
for f in completions:
|
||||
@@ -225,7 +225,7 @@ class BaseMode(object):
|
||||
else:
|
||||
mc.append(f)
|
||||
completions = mc
|
||||
log('fnames=<%s>' % list(map(ensure_unicode, completions)))
|
||||
log('fnames=<%s>' % list(map(ensure_str, completions)))
|
||||
return completions
|
||||
|
||||
|
||||
|
||||
@@ -13,7 +13,7 @@ from pyreadline.lineeditor.lineobj import Point
|
||||
import pyreadline.lineeditor.lineobj as lineobj
|
||||
import pyreadline.lineeditor.history as history
|
||||
from . import basemode
|
||||
from pyreadline.unicode_helper import ensure_unicode
|
||||
from pyreadline.unicode_helper import ensure_str
|
||||
|
||||
|
||||
def format(keyinfo):
|
||||
|
||||
@@ -19,7 +19,7 @@ import pyreadline.console as console
|
||||
import pyreadline.logger as logger
|
||||
|
||||
from pyreadline.keysyms.common import make_KeyPress_from_keydescr
|
||||
from pyreadline.unicode_helper import ensure_unicode
|
||||
from pyreadline.unicode_helper import ensure_str
|
||||
from .logger import log
|
||||
from .modes import editingmodes
|
||||
from .error import ReadlineError, GetSetError
|
||||
@@ -161,7 +161,7 @@ class BaseReadline(object):
|
||||
'''Load a readline history file. The default filename is ~/.history.'''
|
||||
if filename is None:
|
||||
filename = self.mode._history.history_filename
|
||||
log("read_history_file from %s"%ensure_unicode(filename))
|
||||
log("read_history_file from %s"%ensure_str(filename))
|
||||
self.mode._history.read_history_file(filename)
|
||||
|
||||
def write_history_file(self, filename=None):
|
||||
|
||||
@@ -17,7 +17,7 @@ except AttributeError:
|
||||
if pyreadline_codepage is None:
|
||||
pyreadline_codepage = "ascii"
|
||||
|
||||
def ensure_unicode(text):
|
||||
def ensure_str(text):
|
||||
"""helper to ensure that text passed to WriteConsoleW is unicode"""
|
||||
if isinstance(text, bytes):
|
||||
try:
|
||||
@@ -26,7 +26,7 @@ def ensure_unicode(text):
|
||||
return text.decode("ascii", "replace")
|
||||
return text
|
||||
|
||||
def ensure_str(text):
|
||||
def ensure_bytes(text):
|
||||
"""Convert unicode to str using pyreadline_codepage"""
|
||||
if isinstance(text, str):
|
||||
try:
|
||||
|
||||
Reference in New Issue
Block a user