def test_does_not_double_encode(self): x = u"λ" x_e = forcestr(x) assert forcestr(x_e) == x_e x_u = forceunicode(x_e) assert forceunicode(x_u) == x_u
def test_idempotent(self): x = u"a" assert forceunicode(forcestr(x)) == x x = u"λ" assert forceunicode(forcestr(x)) == x assert forceunicode(forcestr(SOURCE1)) == SOURCE1 x = "a" assert forcestr(forceunicode(x)) == x
def splitline(line, re_word=re.compile( forcestr(r'[^\s"]\S*|["]["]|["].*?[^\\]["]'))): import ast result = [] for word in re_word.findall(line): if word.startswith(b'"'): word = ast.literal_eval(forceunicode(word)) result.append(word) return result
def input(self, prompt): """Ask the user to input something. Returns the string that the user entered, or None if the user pressed Esc. """ def draw(text): margin_x = margin_y = 0 padding_x = padding_y = 8 fgcolor = self.INPUT_FGCOLOR bgcolor = self.INPUT_BGCOLOR width = self.width - 2*margin_x lines = renderline(text, self.font, fgcolor, width - 2*padding_x) height = totalheight(lines) + 2 * padding_y block = pygame.Surface((width, height), SWSURFACE | SRCALPHA) block.fill(bgcolor) sx = padding_x sy = padding_y for img in lines: w, h = img.get_size() block.blit(img, (sx, sy)) sy += h block.set_alpha(int(255 * self.INPUT_ALPHA)) # This can be slow. It would be better to take a screenshot # and use it as the background. self.viewer.render() if self.statusbarinfo: self.drawstatusbar() self.screen.blit(block, (margin_x, margin_y)) pygame.display.flip() draw(prompt) text = "" self.must_redraw = True while True: wait_for_events() old_text = text events = EventQueue[:] del EventQueue[:] for e in events: if e.type == QUIT: EventQueue.insert(0, e) # re-insert a QUIT return None elif e.type == KEYDOWN: if e.key == K_ESCAPE: return None elif e.key == K_RETURN: return forceunicode(text) # return encoded unicode elif e.key == K_BACKSPACE: text = text[:-1] elif e.unicode and ord(e.unicode) >= ord(' '): text += e.unicode if text != old_text: draw(prompt + text)
def test_file(self): udir = py.path.local.make_numbered_dir(prefix='usession-dot-', keep=3) full_filename = str(udir.join(FILENAME)) f = codecs.open(full_filename, 'wb', RAW_ENCODING) f.write(SOURCE1) f.close() with open(full_filename) as f1: assert forceunicode(f1.read()) == SOURCE1 f3 = codecs.open(full_filename, 'r', RAW_ENCODING) c = f3.read() f3.close() result = (c == SOURCE1) assert result
def test_forceunicode_should_not_fail(self): garbage = "\xef\xff\xbb\xbf\xce\xbb\xff\xff" # garbage with a lambda result = forceunicode(garbage) # should not raise
def setstatusbar(self, text, fgcolor=None, bgcolor=None): info = (forceunicode(text), fgcolor or self.STATUSBAR_FGCOLOR, bgcolor or self.STATUSBAR_BGCOLOR) if info != self.statusbarinfo: self.statusbarinfo = info self.must_redraw = True