本文整理汇总了Python中pyreadline.unicode_helper.ensure_str函数的典型用法代码示例。如果您正苦于以下问题:Python ensure_str函数的具体用法?Python ensure_str怎么用?Python ensure_str使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了ensure_str函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: log_sock
def log_sock(s, event_type=None):
if sock_silent:
pass
else:
if event_type is None:
logsocket.sendto(ensure_str(s), (host, port))
elif event_type in show_event:
logsocket.sendto(ensure_str(s), (host, port))
else:
pass
示例2: _get_completions
def _get_completions(self):
"""Return a list of possible completions for the string ending at the point.
Also set begidx and endidx in the process."""
completions = []
self.begidx = self.l_buffer.point
self.endidx = self.l_buffer.point
buf=self.l_buffer.line_buffer
if self.completer:
# get the string to complete
while self.begidx > 0:
self.begidx -= 1
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))
i = 0
while 1:
try:
r = self.completer(ensure_unicode(text), i)
except IndexError:
break
except TypeError:
break
i += 1
if r is None:
break
elif r and r not in completions:
completions.append(r)
else:
pass
log('text completions=<%s>' % list(map(ensure_unicode, completions)))
if (self.complete_filesystem == "on") and not completions:
# get the filename to complete
while self.begidx > 0:
self.begidx -= 1
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) + '*'.encode('ascii'))))
if self.mark_directories == 'on':
mc = []
for f in completions:
if os.path.isdir(f):
mc.append(f + os.sep)
else:
mc.append(f)
completions = mc
log('fnames=<%s>' % list(map(ensure_unicode, completions)))
return completions
示例3: _get_completions
def _get_completions(self):
'''Return a list of possible completions for the string ending at the point.
Also set begidx and endidx in the process.'''
completions = []
self.begidx = self.l_buffer.point
self.endidx = self.l_buffer.point
buf=self.l_buffer.line_buffer
if self.completer:
# get the string to complete
while self.begidx > 0:
self.begidx -= 1
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"' % text)
i = 0
while 1:
try:
r = ensure_unicode(self.completer(text, i))
except:
break
i += 1
if r and r not in completions:
completions.append(r)
else:
break
log('text completions=%s' % completions)
if not completions:
# get the filename to complete
while self.begidx > 0:
self.begidx -= 1
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"' % text)
completions = map(ensure_unicode, glob.glob(os.path.expanduser(text) + '*'))
if self.mark_directories == 'on':
mc = []
for f in completions:
if os.path.isdir(f):
mc.append(f + os.sep)
else:
mc.append(f)
completions = mc
log('fnames=%s' % completions)
return completions
示例4: __init__
def __init__(self):
self.history = []
self._history_length = 100
self._history_cursor = 0
self.history_filename = os.path.expanduser(ensure_str('~/.history')) #Cannot expand unicode strings correctly on python2.4
self.lastcommand = None
self.query = ""
self.last_search_for = ""
示例5: write_history_file
def write_history_file(self, filename=None):
"""Save a readline history file."""
if filename is None:
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("\n")
fp.close()
示例6: write_history_file
def write_history_file(self, filename = None):
'''Save a readline history file.'''
if filename is None:
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('\n'.encode('ascii'))
fp.close()
示例7: write_scrolling
def write_scrolling(self, text, attr=None):
'''write text at current cursor position while watching for scrolling.
If the window scrolls because you are at the bottom of the screen
buffer, all positions that you are storing will be shifted by the
scroll amount. For example, I remember the cursor position of the
prompt so that I can redraw the line but if the window scrolls,
the remembered position is off.
This variant of write tries to keep track of the cursor position
so that it will know when the screen buffer is scrolled. It
returns the number of lines that the buffer scrolled.
'''
x, y = self.pos()
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))
for chunk in chunks:
n = self.write_color(chunk, attr)
if len(chunk) == 1: # the funny characters will be alone
if chunk[0] == '\n': # newline
x = 0
y += 1
elif chunk[0] == '\r': # carriage return
x = 0
elif chunk[0] == '\t': # tab
x = 8 * (int(x / 8) + 1)
if x > w: # newline
x -= w
y += 1
elif chunk[0] == '\007': # bell
pass
elif chunk[0] == '\010':
x -= 1
if x < 0:
y -= 1 # backed up 1 line
else: # ordinary character
x += 1
if x == w: # wrap
x = 0
y += 1
if y == h: # scroll
scroll += 1
y = h - 1
else: # chunk of ordinary characters
x += n
l = int(x / w) # lines we advanced
x = x % w # new x value
y += l
if y >= h: # scroll
scroll += y - h + 1
y = h - 1
return scroll
示例8: scroll
def scroll(self, rect, dx, dy, attr=None, fill=" "):
u"""Scroll a rectangle."""
if attr is None:
attr = self.attr
x0, y0, x1, y1 = rect
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.Attributes = attr
return self.ScrollConsoleScreenBufferW(self.hout, byref(source), byref(source), dest, byref(style))
示例9: SetClipboardText
def SetClipboardText(text):
buffer = c_buffer(ensure_str(text))
bufferSize = sizeof(buffer)
hGlobalMem = GlobalAlloc(c_int(GHND), c_int(bufferSize))
GlobalLock.restype = c_void_p
lpGlobalMem = GlobalLock(c_int(hGlobalMem))
memcpy(lpGlobalMem, addressof(buffer), c_int(bufferSize))
GlobalUnlock(c_int(hGlobalMem))
if OpenClipboard(0):
EmptyClipboard()
SetClipboardData(c_int(CF_TEXT), c_int(hGlobalMem))
CloseClipboard()
示例10: hook_wrapper_23
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))
# make sure it returned the right sort of thing
if res and not isinstance(res, bytes):
raise TypeError('readline must return a string.')
except KeyboardInterrupt:
# GNU readline returns 0 on keyboard interrupt
return 0
except EOFError:
# It returns an empty string on EOF
res = ensure_str('')
except:
print('Readline internal error', file=sys.stderr)
traceback.print_exc()
res = ensure_str('\n')
# we have to make a copy because the caller expects to free the result
n = len(res)
p = Console.PyMem_Malloc(n + 1)
_strncpy(cast(p, c_char_p), res, n + 1)
return p
示例11: hook_wrapper
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))
# make sure it returned the right sort of thing
if res and not isinstance(res, str):
raise TypeError, 'readline must return a string.'
except KeyboardInterrupt:
# GNU readline returns 0 on keyboard interrupt
return 0
except EOFError:
# It returns an empty string on EOF
res = ''
except:
print >>sys.stderr, 'Readline internal error'
traceback.print_exc()
res = '\n'
# we have to make a copy because the caller expects to free the result
p = cdll.msvcrt._strdup(res)
return p
示例12: get_current_completed_word
def get_current_completed_word(self):
"""Return a current word_between completion separators"""
begidx = self.l_buffer.point
endidx = self.l_buffer.point
buf=self.l_buffer.line_buffer
l = len(buf)
if l == 0:
return ""
# if we immediately after the symbol go one character back
if begidx == l or buf[begidx] in self.completer_delims:
begidx -= 1
# find the beginning of the word
while begidx > 0:
if buf[begidx] in self.completer_delims:
begidx += 1
break
begidx -= 1
# find the end of the word
while endidx < l:
if buf[endidx] in self.completer_delims:
break
endidx += 1
text = ensure_str(u''.join(buf[begidx:endidx]))
return text
示例13: sethistoryfilename
def sethistoryfilename(filename):
self.mode._history.history_filename = os.path.expanduser(
ensure_str(filename))
示例14: write_color
def write_color(self, text, attr=None):
text = ensure_str(text)
junk = DWORD(0)
self.WriteFile(self.hout, text, len(text), byref(junk), None)
return len(text)
示例15: log
def log(s):
s = ensure_str(s)
pyreadline_logger.debug(s)