Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Reverted from v. 10

...

Your .py files will now have a series of blocks that look like this, using C preprocessor syntax:

NOTE: The sense of the "#ifdef/#ifndef" is backwards from what we really want,

but this will be dealt with in the next sectiona later section below.

I've added annotated the lines further to make it clearer which lines are the python2 and which are the python3 code.

. . .

#ifndef USING_PYTHON2

from logginginterface import *                             # python2 code

#else /* USING_PYTHON2 */

from .logginginterface import *                            # python3 code

#endif /* USING_PYTHON2 */

. . .

#ifndef USING_PYTHON2

import urlliburllib                                              # python2 code

#else /* USING_PYTHON2 */

import urllib.request, urllib.parse, urllib.errorerror          # python3 code

#endif /* USING_PYTHON2 */

. . .

#ifndef USING_PYTHON2

  return urllib.quote(str(s), '')                          # python2 code

#else /* USING_PYTHON2 */

  return urllib.parse.quote(str(s), '')                    # python3 code

#endif /* USING_PYTHON2 */

. . .

#ifndef USING_PYTHON2

  os.umask(077)                                           # python2 code

#else /* USING_PYTHON2 */

  os.umask(0o77)                                          # python3 code

#endif /* USING_PYTHON2 */

. . .

#ifndef USING_PYTHON2

  if openstack.has_key('custom_configuration'):           # python2 code

#else /* USING_PYTHON2 */

  if 'custom_configuration' in openstack:openstack:                 # python3 code

#endif /* USING_PYTHON2 */

. . .

#ifndef USING_PYTHON2

  print >>fp, binascii.hexlify(b).decode('utf-8')        # python2 code

#else /* USING_PYTHON2 */

  print(binascii.hexlify(b).decode('utf-8'), file=fp)    # python3 code

#endif /* USING_PYTHON2 */

. . .

#ifndef USING_PYTHON2

  debug("ctx node has the following Properties: {}".format(i.properties.keys()))

#else /* USING_PYTHON2 */

  debug("ctx node has the following Properties: {}".format(list(i.properties.keys())))

#endif /* USING_PYTHON2 */

. . .

#ifndef USING_PYTHON2

  for k, nv in want.items():                           # python2 code

#else /* USING_PYTHON2 */

  for k, nv in list(want.items()):                     # python3 code

#endif /* USING_PYTHON2 */

. . .

...

You need to change blocks such as this:

#ifndef USING_PYTHON2

some python2 code

#else /* USING_PYTHON2 */

some python3 code

#endif /* USING_PYTHON2 */


#ifdef USING_PYTHON2

   some python3 code

#endif /* USING_PYTHON2 */

to this. Make CERTAIN that the indentation is adjusted as well.

if USING_PYTHON2:

  some python2 code

else:

  some python3 code


if not USING_PYTHON2:

  some python3 code

Yes, there is some cognitive dissonance for changing "#ifndef" to "if", but the rest of the generated code is set up properly to be able to quickly edit the code.

...

#ifndef USING_PYTHON2

from logginginterface import *     # python2 code

#else /* USING_PYTHON2 */

from .logginginterface import *    # python3 code

#endif /* USING_PYTHON2 */

...

#ifndef USING_PYTHON2

import urlliburllib     # python2 code

#else /* USING_PYTHON2 */

import urllib.request, urllib.parse, urllib.errorerror     # python3 code

#endif /* USING_PYTHON2 */

...

if USING_PYTHON2:

  from logginginterface import *     # python2 code

  import urllib

else:

  from .logginginterface import *     # python3 code

  import urllib.request, urllib.parse, urllib.error

...

#ifndef USING_PYTHON2

  return urllib.quote(str(s), '')           # python2 code

#else /* USING_PYTHON2 */

  return urllib.parse.quote(str(s), '')     # python3 code

#endif /* USING_PYTHON2 */

...

#ifndef USING_PYTHON2

  os.umask(077)     # python2 code

#else /* USING_PYTHON2 */

  os.umask(0o77)     # python3 code

#endif /* USING_PYTHON2 */

...

#ifndef USING_PYTHON2

  if openstack.has_key('custom_configuration'):     # python2 code

#else /* USING_PYTHON2 */

  if 'custom_configuration' in openstack:     # python3 code

#endif /* USING_PYTHON2 */

...

#ifndef USING_PYTHON2

  print >>fp, binascii.hexlify(b).decode('utf-8')     # python2 code

#else /* USING_PYTHON2 */

  print(binascii.hexlify(b).decode('utf-8'), file=fp)     # python3 code

#endif /* USING_PYTHON2 */

...

#ifndef USING_PYTHON2

  debug("ctx node has the following Properties: {}".format(i.properties.keys()))     # python2 code

#else /* USING_PYTHON2 */

  debug("ctx node has the following Properties: {}".format(list(i.properties.keys())))     # python3 code

#endif /* USING_PYTHON2 */

...