python-3-changes.txt 4.5 KB

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