~pyreadline/pyreadline/trunk

« back to all changes in this revision

Viewing changes to pyreadline/unicode_helper.py

  • Committer: Jorgen Stenarson
  • Date: 2011-10-06 19:02:52 UTC
  • mfrom: (100.1.19 python3)
  • Revision ID: jorgen.stenarson@bostream.nu-20111006190252-efh961vx5qbcohom
Merging python3 fixes from Shimizukawa

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
if pyreadline_codepage is None:  
18
18
    pyreadline_codepage = u"ascii"
19
19
 
 
20
if sys.version_info < (2, 6):
 
21
    bytes = str
 
22
 
 
23
PY3 = (sys.version_info >= (3, 0))
 
24
 
20
25
def ensure_unicode(text):
21
26
    u"""helper to ensure that text passed to WriteConsoleW is unicode"""
22
 
    if isinstance(text, str):
 
27
    if isinstance(text, bytes):
23
28
        try:
24
29
            return text.decode(pyreadline_codepage, u"replace")
25
30
        except (LookupError, TypeError):
34
39
        except (LookupError, TypeError):
35
40
            return text.encode(u"ascii", u"replace")
36
41
    return text
 
42
 
 
43
def biter(text):
 
44
    if PY3 and isinstance(text, bytes):
 
45
        return (s.to_bytes(1, 'big') for s in text)
 
46
    else:
 
47
        return iter(text)