OLD | NEW |
(Empty) | |
| 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 |
| 27 In Python 2.x, strings (of type ''str'') contain binary data, including encoded |
| 28 Unicode text (e.g. UTF-8). The separate type ''unicode'' holds Unicode text. |
| 29 Unicode literals are specified via the u'...' prefix. Indexing or slicing |
| 30 either type always produces a string of the same type as the original. |
| 31 Data read from a file is always of '''str'' type. |
| 32 |
| 33 In Python 3.x, strings (type ''str'') may only contain Unicode text. The u'...' |
| 34 prefix 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 |
| 37 literal. Indexing or slicing a string produces another string. Slicing a byte |
| 38 string produces another byte string, but the indexing operation produces an |
| 39 integer. Data read from a file is of '''str'' type if the file was opened in |
| 40 text mode, or of ''bytes'' type otherwise. |
| 41 |
| 42 Since PyCrypto aims at supporting both Python 2.x and 3.x, the following helper |
| 43 functions are used to keep the rest of the library as independent as possible |
| 44 from the actual Python version. |
| 45 |
| 46 In general, the code should always deal with binary strings, and use integers |
| 47 instead of 1-byte character strings. |
| 48 |
| 49 b(s) |
| 50 Take a text string literal (with no prefix or with u'...' prefix) and |
| 51 make a byte string. |
| 52 bchr(c) |
| 53 Take an integer and make a 1-character byte string. |
| 54 bord(c) |
| 55 Take the result of indexing on a byte string and make an integer. |
| 56 tobytes(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 |
| 63 import sys |
| 64 |
| 65 if sys.version_info[0] == 2: |
| 66 def b(s): |
| 67 return s |
| 68 def bchr(s): |
| 69 return chr(s) |
| 70 def bstr(s): |
| 71 return str(s) |
| 72 def bord(s): |
| 73 return ord(s) |
| 74 if sys.version_info[1] == 1: |
| 75 def tobytes(s): |
| 76 try: |
| 77 return s.encode('latin-1') |
| 78 except: |
| 79 return ''.join(s) |
| 80 else: |
| 81 def tobytes(s): |
| 82 if isinstance(s, unicode): |
| 83 return s.encode("latin-1") |
| 84 else: |
| 85 return ''.join(s) |
| 86 else: |
| 87 def b(s): |
| 88 return s.encode("latin-1") # utf-8 would cause some side-effects we don't
want |
| 89 def bchr(s): |
| 90 return bytes([s]) |
| 91 def bstr(s): |
| 92 if isinstance(s,str): |
| 93 return bytes(s,"latin-1") |
| 94 else: |
| 95 return bytes(s) |
| 96 def bord(s): |
| 97 return s |
| 98 def tobytes(s): |
| 99 if isinstance(s,bytes): |
| 100 return s |
| 101 else: |
| 102 if isinstance(s,str): |
| 103 return s.encode("latin-1") |
| 104 else: |
| 105 return bytes(s) |
| 106 |
| 107 # vim:set ts=4 sw=4 sts=4 expandtab: |
OLD | NEW |