Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/IPython/utils/py3compat.py: 37%
35 statements
« prev ^ index » next coverage.py v7.4.4, created at 2024-04-20 06:09 +0000
« prev ^ index » next coverage.py v7.4.4, created at 2024-04-20 06:09 +0000
1# coding: utf-8
2"""Compatibility tricks for Python 3. Mainly to do with unicode.
4This file is deprecated and will be removed in a future version.
5"""
6import platform
7import builtins as builtin_mod
9from .encoding import DEFAULT_ENCODING
12def decode(s, encoding=None):
13 encoding = encoding or DEFAULT_ENCODING
14 return s.decode(encoding, "replace")
17def encode(u, encoding=None):
18 encoding = encoding or DEFAULT_ENCODING
19 return u.encode(encoding, "replace")
22def cast_unicode(s, encoding=None):
23 if isinstance(s, bytes):
24 return decode(s, encoding)
25 return s
28def safe_unicode(e):
29 """unicode(e) with various fallbacks. Used for exceptions, which may not be
30 safe to call unicode() on.
31 """
32 try:
33 return str(e)
34 except UnicodeError:
35 pass
37 try:
38 return repr(e)
39 except UnicodeError:
40 pass
42 return "Unrecoverably corrupt evalue"
45# keep reference to builtin_mod because the kernel overrides that value
46# to forward requests to a frontend.
47def input(prompt=""):
48 return builtin_mod.input(prompt)
51def execfile(fname, glob, loc=None, compiler=None):
52 loc = loc if (loc is not None) else glob
53 with open(fname, "rb") as f:
54 compiler = compiler or compile
55 exec(compiler(f.read(), fname, "exec"), glob, loc)
58PYPY = platform.python_implementation() == "PyPy"
60# Cython still rely on that as a Dec 28 2019
61# See https://github.com/cython/cython/pull/3291 and
62# https://github.com/ipython/ipython/issues/12068
63def no_code(x, encoding=None):
64 return x
67unicode_to_str = cast_bytes_py2 = no_code