From f027925deef57d0d7a3768642cb616b4c541a94d Mon Sep 17 00:00:00 2001 From: bryan newbold Date: Mon, 6 Apr 2015 10:12:33 -0700 Subject: python: python3 porting --- software/python.page | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'software') diff --git a/software/python.page b/software/python.page index b0a01e0..bcbf499 100644 --- a/software/python.page +++ b/software/python.page @@ -126,4 +126,24 @@ or in-line, always at a particular point: Use ipdb (debian: python-ipdb) instead of pdb to get a nicer IPython prompt. +Python 3 Porting +------------------- + +To help port and support both Python 2.7 and 3.0+, start with an import:: + + from __future__ import absolute_import, division, print_function + +str/bytes/unicode is indeed the major porting challenge. Using bytearrays +helps. Use ``b'asdf'`` style byte array definitions for most low-level +constants. + +``struct.unpack()`` wants doesn't allow ``bytearray()``; use ``bytes()`` +instead. + +Make sure ``rase Exception ("Message here")`` style is used everywhere, instead +of ``raise Exception, "Message here"``. + +There was some change in comparison between None and integers, which makes +``if args.verbose > 0`` break. Use ``if args.verbose and args.verbose > 1`` +instead. -- cgit v1.2.3