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

1# coding: utf-8 

2"""Compatibility tricks for Python 3. Mainly to do with unicode. 

3 

4This file is deprecated and will be removed in a future version. 

5""" 

6import platform 

7import builtins as builtin_mod 

8 

9from .encoding import DEFAULT_ENCODING 

10 

11 

12def decode(s, encoding=None): 

13 encoding = encoding or DEFAULT_ENCODING 

14 return s.decode(encoding, "replace") 

15 

16 

17def encode(u, encoding=None): 

18 encoding = encoding or DEFAULT_ENCODING 

19 return u.encode(encoding, "replace") 

20 

21 

22def cast_unicode(s, encoding=None): 

23 if isinstance(s, bytes): 

24 return decode(s, encoding) 

25 return s 

26 

27 

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 

36 

37 try: 

38 return repr(e) 

39 except UnicodeError: 

40 pass 

41 

42 return "Unrecoverably corrupt evalue" 

43 

44 

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) 

49 

50 

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) 

56 

57 

58PYPY = platform.python_implementation() == "PyPy" 

59 

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 

65 

66 

67unicode_to_str = cast_bytes_py2 = no_code