Skip to content

Make build.py compatible with Python 2 and 3#2123

Merged
rachel-fenichel merged 1 commit intoRaspberryPiFoundation:developfrom
gomercin:python3_upgrade
Nov 17, 2018
Merged

Make build.py compatible with Python 2 and 3#2123
rachel-fenichel merged 1 commit intoRaspberryPiFoundation:developfrom
gomercin:python3_upgrade

Conversation

@gomercin
Copy link
Contributor

The basics

  • I branched from develop
  • My pull request is against develop
  • My code follows the style guide

The details

Following changes are made with minimal touches in the rest;

  • version dependent module imports
  • codecs.open instead of open (for encoding errors)
  • string_is_ascii modified for better compatibility
  • In create_messages.py, synonym_defs are sorted as well to increase
    testability, as Python 3 does not guarantee key order.
    Note that this will cause previously generated files to have different
    orders from the files generated without this patch.
  • small changes; parenthesis around "print"s, has_key -> in,
    "except x, y" -> "except x as y", sorted -> sort

Resolves

#2112

Proposed Changes

Reason for Changes

Enabling the upgrade to Python3.x

Test Coverage

Tested on:
Tested on Windows by comparing generated files using:

  • original code with Python 2.7
  • patched code with Python 2.7
  • patched code with Python 3.6

There is a change of order due to sorting synonym_defs but there seems to be no easy
solution to keep the order from the original files.

Additional Information

Other files need similar small changes but are not touched as they are not
referenced from build.py

Following changes are made with minimal touches;
- version dependent module imports
- codecs.open instead of open (for encoding errors)
- string_is_ascii modified for better compatibility
- In create_messages.py, synonym_defs are sorted as well to increase
  testability, as Python 3 does not guarantee key order.
  Note that this will cause previously generated files to have different
  orders from the files generated without this patch.
- small changes; paranthesis around "print"s, has_key -> in,
  "except x, y" -> "except x as y", sorted -> sort

Other files need similar small changes but are not touched as they are not
referenced from build.py

Fixes RaspberryPiFoundation#2112
@rachel-fenichel
Copy link
Collaborator

Thank you! And feel free to make followup PRs to change any other python files that we have that only run in Python 2.

@rachel-fenichel rachel-fenichel merged commit 246580b into RaspberryPiFoundation:develop Nov 17, 2018
@gomercin
Copy link
Contributor Author

Thank you too, I will take a look at them as well

@gomercin gomercin deleted the python3_upgrade branch November 17, 2018 16:41
tansly added a commit to tansly/scratch-blocks that referenced this pull request Jul 12, 2019
delasare pushed a commit to scratchfoundation/scratch-blocks that referenced this pull request Oct 25, 2023
Port of RaspberryPiFoundation/blockly#2123 (by @gomercin)

Fix issues related to Python 3 compatibility

Port of RaspberryPiFoundation/blockly#2125 (by @gomercin)

Fix compatibility problems with Python 3's reduce(), filter() et al.

Fix Python 3 compatibility problems related to str encodings

Remove obsolete UnicodeEncodeError handling
delasare pushed a commit to scratchfoundation/scratch-blocks that referenced this pull request Oct 25, 2023
Port of RaspberryPiFoundation/blockly#2123 (by @gomercin)

Fix issues related to Python 3 compatibility

Port of RaspberryPiFoundation/blockly#2125 (by @gomercin)

Fix compatibility problems with Python 3's reduce(), filter() et al.

Fix Python 3 compatibility problems related to str encodings

Remove obsolete UnicodeEncodeError handling
DBDev-IT pushed a commit to DashBlocks/scratch-blocks that referenced this pull request Sep 2, 2025
Port of RaspberryPiFoundation/blockly#2123 (by @gomercin)

Fix issues related to Python 3 compatibility

Port of RaspberryPiFoundation/blockly#2125 (by @gomercin)

Fix compatibility problems with Python 3's reduce(), filter() et al.

Fix Python 3 compatibility problems related to str encodings

Remove obsolete UnicodeEncodeError handling
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants