##// END OF EJS Templates
inputhook: disable CTRL+C when a hook is active....
inputhook: disable CTRL+C when a hook is active. On systems with 'readline', it's very likely to intercept a signal during a select() call. The default SIGINT handler will schedule a KeyboardInterrupt exception to be raised as soon as possible. If ctypes is used to install a Python callback for PyOS_InputHook, this will happen as soon as the bytecode execution starts, so even if the first instruction of the callback is a `try: ... except KeyboardInterrupt` clause, it's actually too late. As ctypes doesn't allow a Python callback to raise an exception, this ends up with IPython detecting an internal error... not pretty. We must therefore ignore the SIGINT signals until we are sure the exception handler is active, in the Python callback.

File last commit:

r4780:336cd830
r4944:0fc80df3
Show More
py3compat.py
99 lines | 2.6 KiB | text/x-python | PythonLexer
# coding: utf-8
"""Compatibility tricks for Python 3. Mainly to do with unicode."""
import sys
import types
orig_open = open
def no_code(x, encoding=None):
return x
def decode(s, encoding=None):
encoding = encoding or sys.stdin.encoding or sys.getdefaultencoding()
return s.decode(encoding, "replace")
def encode(u, encoding=None):
encoding = encoding or sys.stdin.encoding or sys.getdefaultencoding()
return u.encode(encoding, "replace")
def cast_unicode(s, encoding=None):
if isinstance(s, bytes):
return decode(s, encoding)
return s
def cast_bytes(s, encoding=None):
if not isinstance(s, bytes):
return encode(s, encoding)
return s
if sys.version_info[0] >= 3:
PY3 = True
input = input
builtin_mod_name = "builtins"
str_to_unicode = no_code
unicode_to_str = no_code
str_to_bytes = encode
bytes_to_str = decode
cast_bytes_py2 = no_code
def isidentifier(s, dotted=False):
if dotted:
return all(isidentifier(a) for a in s.split("."))
return s.isidentifier()
open = orig_open
MethodType = types.MethodType
def execfile(fname, glob, loc=None):
loc = loc if (loc is not None) else glob
exec compile(open(fname).read(), fname, 'exec') in glob, loc
else:
PY3 = False
input = raw_input
builtin_mod_name = "__builtin__"
str_to_unicode = decode
unicode_to_str = encode
str_to_bytes = no_code
bytes_to_str = no_code
cast_bytes_py2 = cast_bytes
import re
_name_re = re.compile(r"[a-zA-Z_][a-zA-Z0-9_]*$")
def isidentifier(s, dotted=False):
if dotted:
return all(isidentifier(a) for a in s.split("."))
return bool(_name_re.match(s))
class open(object):
"""Wrapper providing key part of Python 3 open() interface."""
def __init__(self, fname, mode="r", encoding="utf-8"):
self.f = orig_open(fname, mode)
self.enc = encoding
def write(self, s):
return self.f.write(s.encode(self.enc))
def read(self, size=-1):
return self.f.read(size).decode(self.enc)
def close(self):
return self.f.close()
def __enter__(self):
return self
def __exit__(self, etype, value, traceback):
self.f.close()
def MethodType(func, instance):
return types.MethodType(func, instance, type(instance))
# don't override system execfile on 2.x:
execfile = execfile