Upgrade to Pro — share decks privately, control downloads, hide ads and more …

By Your Bootstraps: Porting your App to Python3 by Tres Seaver

PyCon 2014
April 11, 2014
490

By Your Bootstraps: Porting your App to Python3 by Tres Seaver

PyCon 2014

April 11, 2014
Tweet

More Decks by PyCon 2014

Transcript

  1. Objectives “Straddling” Python 2 / 3 in single codebase Choosing

    target Python versions Porting as an iterative process Ordering components by dependencies
  2. Objectives “Straddling” Python 2 / 3 in single codebase Choosing

    target Python versions Porting as an iterative process Ordering components by dependencies Adding test coverage to reduce risk
  3. Objectives “Straddling” Python 2 / 3 in single codebase Choosing

    target Python versions Porting as an iterative process Ordering components by dependencies Adding test coverage to reduce risk Covering C extensions
  4. Objectives “Straddling” Python 2 / 3 in single codebase Choosing

    target Python versions Porting as an iterative process Ordering components by dependencies Adding test coverage to reduce risk Covering C extensions Porting Hygeine
  5. Background Experience report / best practices summary Based on porting

    ~180 kLOC Python, ~25 kLOC c codebase Zope3 component architecture ZODB WebOb Pyramid Other dependencies
  6. Porting Strategies Port once, abandon Python2 Not the subject of

    this talk Customers / users still need Python2 More feasible for applications than libraries 2to3 may be useful starting point
  7. Porting Strategies Port once, abandon Python2 Not the subject of

    this talk Customers / users still need Python2 More feasible for applications than libraries 2to3 may be useful starting point
  8. Porting Strategies “Fix up” at installation using 2to3 Python2 users

    unaffected Python3 source “drifts” from canonical version Bug reports don't match 2to3 painfully slow on large codebases
  9. Porting Strategies “Fix up” at installation using 2to3 Python2 users

    unaffected Python3 source “drifts” from canonical version Bug reports don't match 2to3 painfully slow on large codebases
  10. Porting Strategies “Straddling” in a single codebase Use “compatible subset”

    of Python syntax Conditional imports mask stdlib changes six module can help (but you might not need it)
  11. Porting Strategies “Straddling” in a single codebase WIN! Use “compatible

    subset” of Python syntax Conditional imports mask stdlib changes six module can help (but you might not need it)
  12. Targeting Python Versions Syntax changes make Python2 < 2.6 hard

    No b'' literals No except Exception as e:
  13. Targeting Python Versions Syntax changes make Python2 < 2.6 hard

    No b'' literals No except Exception as e: Much more cruft / pain
  14. Targeting Python Versions Syntax changes make Python2 < 2.6 hard

    No b'' literals No except Exception as e: Much more cruft / pain 2.4 / 2.5 are long past EOL But some folks need system Python in “enterprisey” systems
  15. Targeting Python Versions Incompatibilities make Python3 < 3.2 hard PEP

    3333 fixes WSGI in Py3k callable() restored in 3.2 3.3 restores u'' literals 3.2 is “system Python3” on some LTS systems
  16. Managing Porting Risks Ports are great opportunities for bug injection

    Fear of breaking working software is the barrier Even more than effort
  17. Managing Porting Risks Some mitigations also improve your software Improved

    testing Modernized idioms in Python2 Clarity in text vs. bytes
  18. Bottom-Up Porting Port packages with no dependencies first Then port

    packages with already-ported dependencies Note Python versions supported by dependencies
  19. Bottom-Up Porting Port packages with no dependencies first Then port

    packages with already-ported dependencies Note Python versions supported by dependencies Lather, rinse, repeat...
  20. Bottom-Up Porting Port packages with no dependencies first Then port

    packages with already-ported dependencies Note Python versions supported by dependencies Lather, rinse, repeat... Finally, port the application
  21. “Common subset” idioms Lennart Regebro's book, http://python3porting.com/noconv.html python2.7 -3 can

    point out problem areas “Modernize” idioms in Python2 code Distinguish bytes vs. text Use b'' / u'' for all literals
  22. “Common subset” idioms Adopt new syntax E.g., except … as

    … print()(function-like) Use new stdlib facilities E.g., io.BytesIO vs StringIO.StringIO
  23. Testing Avoids Hair-Loss Untested code is where the bugs go

    to hide 100% coverage is ideal before porting Unit testing preferable for libraries Functional testing best for applications
  24. Testing Avoids Hair-Loss Untested code is where the bugs go

    to hide 100% coverage is ideal before porting Unit testing preferable for libraries Functional testing best for applications Measure coverage: https://pypi.python.org/pypi/coverage
  25. Testing Avoids Hair-Loss Work to improve assertions as well as

    coverage Assert contracts, not implementation details
  26. Testing Avoids Hair-Loss Doctests make straddling really hard Replace doctests

    with unit / functional tests If at all feasible, convert doctests to Sphinx examples
  27. Testing Avoids Hair-Loss Automate running tests tox helps ensure that

    tests pass under all supported versions Testing with pypy! Example
  28. Considerations for C Extensions Python reference implementation Easier to test

    Supports PyPy Design for same API as C 100% coverage for Python
  29. Considerations for C Extensions Python reference implementation Easier to test

    Supports PyPy Design for same API as C 100% coverage for Python Ensure C version passes same tests
  30. Hygeine Issues Signal supported versions using Trove classifiers Consider bumping

    major version Allow users to stick with “safe” versions as you iterates
  31. Hygeine Issues Signal supported versions using Trove classifiers Consider bumping

    major version Allow users to stick with “safe” versions as you iterate Apply continuous integration Travis-CI Jenkins Shining Panda for Windows