|
32 | 32 | from pyrepl.historical_reader import HistoricalReader |
33 | 33 | from pyrepl.completing_reader import CompletingReader |
34 | 34 | from pyrepl.unix_console import UnixConsole, _error |
| 35 | +try: |
| 36 | + unicode |
| 37 | + PY3 = False |
| 38 | +except NameError: |
| 39 | + PY3 = True |
| 40 | + unicode = str |
| 41 | + unichr = chr |
| 42 | + basestring = bytes, str |
35 | 43 |
|
36 | 44 |
|
37 | 45 | ENCODING = sys.getfilesystemencoding() or 'latin1' # XXX review |
@@ -199,7 +207,14 @@ def raw_input(self, prompt=''): |
199 | 207 | except _error: |
200 | 208 | return _old_raw_input(prompt) |
201 | 209 | reader.ps1 = prompt |
202 | | - return reader.readline(startup_hook=self.startup_hook) |
| 210 | + |
| 211 | + ret = reader.readline(startup_hook=self.startup_hook) |
| 212 | + if not PY3: |
| 213 | + return ret |
| 214 | + |
| 215 | + # Unicode/str is required for Python 3 (3.5.2). |
| 216 | + # Ref: https://bitbucket.org/pypy/pyrepl/issues/20/#comment-30647029 |
| 217 | + return unicode(ret, ENCODING) |
203 | 218 |
|
204 | 219 | def multiline_input(self, more_lines, ps1, ps2, returns_unicode=False): |
205 | 220 | """Read an input on possibly multiple lines, asking for more |
@@ -229,12 +244,15 @@ def set_completer_delims(self, string): |
229 | 244 | self.config.completer_delims = dict.fromkeys(string) |
230 | 245 |
|
231 | 246 | def get_completer_delims(self): |
232 | | - chars = self.config.completer_delims.keys() |
| 247 | + chars = list(self.config.completer_delims.keys()) |
233 | 248 | chars.sort() |
234 | 249 | return ''.join(chars) |
235 | 250 |
|
236 | 251 | def _histline(self, line): |
237 | 252 | line = line.rstrip('\n') |
| 253 | + if PY3: |
| 254 | + return line |
| 255 | + |
238 | 256 | try: |
239 | 257 | return unicode(line, ENCODING) |
240 | 258 | except UnicodeDecodeError: # bah, silently fall back... |
@@ -423,9 +441,14 @@ def _old_raw_input(prompt=''): |
423 | 441 |
|
424 | 442 | else: |
425 | 443 | # this is not really what readline.c does. Better than nothing I guess |
426 | | - import __builtin__ |
427 | | - _old_raw_input = __builtin__.raw_input |
428 | | - __builtin__.raw_input = _wrapper.raw_input |
| 444 | + try: |
| 445 | + import __builtin__ |
| 446 | + _old_raw_input = __builtin__.raw_input |
| 447 | + __builtin__.raw_input = _wrapper.raw_input |
| 448 | + except ImportError: |
| 449 | + import builtins |
| 450 | + _old_raw_input = builtins.input |
| 451 | + builtins.input = _wrapper.raw_input |
429 | 452 |
|
430 | 453 | _old_raw_input = None |
431 | 454 | _setup() |
0 commit comments