Debian Codegraph @

Doc/python-3-changes.txt

raw
1 Py code:
2
3 setup.py invokes 2to3 automatically. This handles int/long and print issues,
4 among others.
5 setup.py will touch nt.py on win32 after build and build again. This is
6 necessary so 2to3 can do its magic on that file.
7
8 There are still a lot of places in the code that need manual attention even
9 with 2to3. They mostly have to do with string (2.x) vs. byte/unicode (3.x)
10 representation
11
12 Use "if sys.version_info[0] == 2:" where needed. Ideally, most of the
13 conditional code can be in py3compat.
14
15 Replace str(x) with bstr(x) if bytes were intended. Becomes str(x) in 2.x and
16 bytes(x) in 3.x through py3compat module.
17 Replace chr(x) with bchr(x) if bytes were intended. Becomes chr(x) in 2.x and
18 bytes([x]) in 3.x through py3compat module.
19 Replace ord(x) with bord(x) if bytes were intended. Becomes ord(x) in 2.x and
20 x in 3.x through py3compat module.
21
22 Comparing a string index to a string literal needs to be changed in 3.x, as
23 b'string'[0] returns an integer, not b's'.
24 The comparison can be fixed by indexing the right side, too:
25 "if s[0]==b('\x30')[0]:" or "if self.typeTag!=self.typeTags['SEQUENCE'][0]:"
26
27 String literals need to be bytes if bytes were intended.
28 Replace "x" with b("x") if bytes were intended. Becomes "x" in 2.x, and
29 s.encode("x","latin-1") in 3.x through py3compat module.
30 For example, '"".join' is replaced by 'b("").join', and 's = ""' becomes
31 's = b("")'.
32 Search for \x to find literals that may have been intended as byte strings
33 !! However, where a human-readable ASCII text string output was intended,
34 such as in AllOrNothing.undigest(), leave as a string literal !!
35
36 Only load py21compat.py
37 "if sys.version_info[0] == 2 and sys.version_info[1] == 1:" .
38 The assignment to True, False generates syntax errors in 3.x, and >= 2.2 don't
39 need the compatibility code.
40
41 Where print is used with >> to redirect, use a separate function instead.
42 See setup.py for an example
43
44 The string module has been changed in 3.x. It lost join and split, maketrans
45 now expects bytes, and so on.
46 Replace string.join(a,b) with b.join(a).
47 Replace string.split(a) with a.split().
48 Replace body of white-space-stripping functions with 'return "".join(s.split())'
49
50 Integer division via the "/" operator can return a float in 3.x. This causes
51 issues in Util.number.getStrongPrime. As 2.1 does not support the "//"
52 operator, divmod(a,b)[0] is used instead, to conform with an existing practice
53 throughout the rest of the pycrypto code base.
54
55 Do not use assert_/failUnless or failIf. These are deprecated and are scheduled
56 to be removed in Python 3.3 and 3.4.
57 Use instead assertEqual(expr,True) for assert_ and assertEqual(expr,False) for
58 failIf
59
60 Added unit tests for Crypto.Random.random. Fixed random.shuffle().
61 random.sample() changed to no longer fail on Python 2.1.
62
63 Added unit test for Crypto.Protocol.AllOrNothing.
64 AllOrNothing changed to no longer fail occasionally.
65
66 C code:
67
68 Extended "pycrypto_compat.h". It handles #define's for Python 3.x forward
69 compatibility
70
71 #include "pycrypto_compat.h"
72 // All other local includes after this, so they can benefit from the
73 // definitions in pycrypto_compat.h
74
75 The compat header #defines IS_PY3K if compiling on 3.x
76 The compat header #defines PyBytes_*, PyUnicode_*, PyBytesObject to resolve to
77 their PyString* counterparts if compiling on 2.x.
78 PyLong_* can be dangerous depending on code construct (think an if that runs
79 PyInt_* with else PyLong_*),
80 therefore it is #defined in each individual module if needed and safe to do so.
81
82 PyString_* has been replaced with PyBytes_* or PyUnicode_* depending on intent
83 PyStringObject has been replaced with PyBytesObject or PyUnicodeObject
84 depending on intent.
85 PyInt_* has been replaced with PyLong_*, where safe to do (in all cases so far)
86
87 The C code uses "#ifdef IS_PY3K" liberally.
88 Code duplication has been avoided.
89
90 Module initialization and module structure differ significantly in 3.x.
91 Conditionals take care of it.
92
93 myModuleType.ob_type assignment conditionally becomes PyTypeReady for 3.x.
94
95 getattr cannot be used to check against custom attributes in 3.x. For 3.x,
96 conditional code uses getattro and PyUnicode_CompareWithASCIIString instead
97 of strcmp
98
99 hexdigest() needed to be changed to return a Unicode object with 3.x
100
101
102 TODO for extra credit:
103 - Check for type of string in functions and throw an error when it's not
104 correct. While at it, ensure that functions observe the guidelines below
105 re type.
106 This is friendlier than just relying on Python's errors.
107 - Make sure DerSequence slicing is tested, since I took the explicit slice
108 functions away in 3.x
109