-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython2x3.py
More file actions
53 lines (48 loc) · 1.91 KB
/
python2x3.py
File metadata and controls
53 lines (48 loc) · 1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/usr/bin/env python
'''Provides code and data to facilitate writing python code that runs on 2.x and 3.x, including pypy'''
# I'm afraid pylint won't like this one...
import sys
def python_major():
'''Return an integer corresponding to the major version # of the python interpreter we're running on'''
# This originally used the platform module, but platform fails on IronPython; sys.version seems to work
# on everything I've tried
result = sys.version_info[0]
return result
if python_major() == 2:
empty_bytes = ''
null_byte = '\0'
bytes_type = str
def intlist_to_binary(intlist):
'''Convert a list of integers to a binary string type'''
return ''.join(chr(byte) for byte in intlist)
def string_to_binary(string):
'''Convert a text string to a binary string type'''
return string
def binary_to_intlist(binary):
'''Convert a binary string to a list of integers'''
return [ ord(character) for character in binary ]
def binary_to_string(binary):
'''Convert a binary string to a text string'''
return binary
elif python_major() == 3:
empty_bytes = ''.encode('utf-8')
null_byte = bytes([ 0 ])
bytes_type = bytes
def intlist_to_binary(intlist):
'''Convert a list of integers to a binary string type'''
return bytes(intlist)
def string_to_binary(string):
'''Convert a text string (or binary string type) to a binary string type'''
if isinstance(string, str):
return string.encode('latin-1')
else:
return string
def binary_to_intlist(binary):
'''Convert a binary string to a list of integers'''
return binary
def binary_to_string(binary):
'''Convert a binary string to a text string'''
return binary.decode('latin-1')
else:
sys.stderr.write('%s: Python < 2 or > 3 not (yet) supported\n' % sys.argv[0])
sys.exit(1)