@@ -231,7 +231,11 @@ def get_completer_delims(self):
231
231
return '' .join (chars )
232
232
233
233
def _histline (self , line ):
234
- return unicode (line .rstrip ('\n ' ), ENCODING )
234
+ line = line .rstrip ('\n ' )
235
+ try :
236
+ return unicode (line , ENCODING )
237
+ except UnicodeDecodeError : # bah, silently fall back...
238
+ return unicode (line , 'utf-8' )
235
239
236
240
def get_history_length (self ):
237
241
return self .saved_history_length
@@ -268,7 +272,10 @@ def write_history_file(self, filename='~/.history'):
268
272
f = open (os .path .expanduser (filename ), 'w' )
269
273
for entry in history :
270
274
if isinstance (entry , unicode ):
271
- entry = entry .encode (ENCODING )
275
+ try :
276
+ entry = entry .encode (ENCODING )
277
+ except UnicodeEncodeError : # bah, silently fall back...
278
+ entry = entry .encode ('utf-8' )
272
279
entry = entry .replace ('\n ' , '\r \n ' ) # multiline history support
273
280
f .write (entry + '\n ' )
274
281
f .close ()
@@ -395,9 +402,21 @@ def _setup():
395
402
_wrapper .f_in = f_in
396
403
_wrapper .f_out = f_out
397
404
398
- if hasattr (sys , '__raw_input__' ): # PyPy
399
- _old_raw_input = sys .__raw_input__
405
+ if '__pypy__' in sys .builtin_module_names : # PyPy
406
+
407
+ def _old_raw_input (prompt = '' ):
408
+ # sys.__raw_input__() is only called when stdin and stdout are
409
+ # as expected and are ttys. If it is the case, then get_reader()
410
+ # should not really fail in _wrapper.raw_input(). If it still
411
+ # does, then we will just cancel the redirection and call again
412
+ # the built-in raw_input().
413
+ try :
414
+ del sys .__raw_input__
415
+ except AttributeError :
416
+ pass
417
+ return raw_input (prompt )
400
418
sys .__raw_input__ = _wrapper .raw_input
419
+
401
420
else :
402
421
# this is not really what readline.c does. Better than nothing I guess
403
422
import __builtin__
0 commit comments