Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/Crypto/Util/py3compat.py: 41%

39 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-06-07 07:03 +0000

1# -*- coding: utf-8 -*- 

2# 

3# Util/py3compat.py : Compatibility code for handling Py3k / Python 2.x 

4# 

5# Written in 2010 by Thorsten Behrens 

6# 

7# =================================================================== 

8# The contents of this file are dedicated to the public domain. To 

9# the extent that dedication to the public domain is not available, 

10# everyone is granted a worldwide, perpetual, royalty-free, 

11# non-exclusive license to exercise all rights associated with the 

12# contents of this file for any purpose whatsoever. 

13# No rights are reserved. 

14# 

15# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 

16# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 

17# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 

18# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 

19# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 

20# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 

21# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 

22# SOFTWARE. 

23# =================================================================== 

24 

25"""Compatibility code for handling string/bytes changes from Python 2.x to Py3k 

26 

27In Python 2.x, strings (of type ''str'') contain binary data, including encoded 

28Unicode text (e.g. UTF-8). The separate type ''unicode'' holds Unicode text. 

29Unicode literals are specified via the u'...' prefix. Indexing or slicing 

30either type always produces a string of the same type as the original. 

31Data read from a file is always of '''str'' type. 

32 

33In Python 3.x, strings (type ''str'') may only contain Unicode text. The u'...' 

34prefix and the ''unicode'' type are now redundant. A new type (called 

35''bytes'') has to be used for binary data (including any particular 

36''encoding'' of a string). The b'...' prefix allows one to specify a binary 

37literal. Indexing or slicing a string produces another string. Slicing a byte 

38string produces another byte string, but the indexing operation produces an 

39integer. Data read from a file is of '''str'' type if the file was opened in 

40text mode, or of ''bytes'' type otherwise. 

41 

42Since PyCrypto aims at supporting both Python 2.x and 3.x, the following helper 

43functions are used to keep the rest of the library as independent as possible 

44from the actual Python version. 

45 

46In general, the code should always deal with binary strings, and use integers 

47instead of 1-byte character strings. 

48 

49b(s) 

50 Take a text string literal (with no prefix or with u'...' prefix) and 

51 make a byte string. 

52bchr(c) 

53 Take an integer and make a 1-character byte string. 

54bord(c) 

55 Take the result of indexing on a byte string and make an integer. 

56tobytes(s) 

57 Take a text string, a byte string, or a sequence of character taken from 

58 a byte string and make a byte string. 

59""" 

60 

61__revision__ = "$Id$" 

62 

63import sys 

64 

65if sys.version_info[0] == 2: 

66 from types import UnicodeType as _UnicodeType # In Python 2.1, 'unicode' is a function, not a type. 

67 

68 def b(s): 

69 return s 

70 def bchr(s): 

71 return chr(s) 

72 def bstr(s): 

73 return str(s) 

74 def bord(s): 

75 return ord(s) 

76 def tobytes(s): 

77 if isinstance(s, _UnicodeType): 

78 return s.encode("latin-1") 

79 else: 

80 return ''.join(s) 

81 def tostr(bs): 

82 return str(bs, 'latin-1') 

83 # In Pyton 2.x, StringIO is a stand-alone module 

84 from io import StringIO as BytesIO 

85else: 

86 def b(s): 

87 return s.encode("latin-1") # utf-8 would cause some side-effects we don't want 

88 def bchr(s): 

89 return bytes([s]) 

90 def bstr(s): 

91 if isinstance(s,str): 

92 return bytes(s,"latin-1") 

93 else: 

94 return bytes(s) 

95 def bord(s): 

96 return s 

97 def tobytes(s): 

98 if isinstance(s,bytes): 

99 return s 

100 else: 

101 if isinstance(s,str): 

102 return s.encode("latin-1") 

103 else: 

104 return bytes(s) 

105 def tostr(bs): 

106 return bs.decode("latin-1") 

107 # In Pyton 3.x, StringIO is a sub-module of io 

108 from io import BytesIO 

109 

110# vim:set ts=4 sw=4 sts=4 expandtab: