Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Python 3 everywhere #12809

Closed
wants to merge 4 commits into from
Closed

Use Python 3 everywhere #12809

wants to merge 4 commits into from

Conversation

julianoes
Copy link
Contributor

@julianoes julianoes commented Aug 26, 2019

Since Python 2 is retired in 4 months, we should move everything to 3.

Closes #12788.

@MaEtUgR do you mind checking appveyor? 😄

@cclauss
Copy link
Contributor

cclauss commented Sep 21, 2019

At least 6 Python 3 syntax errors to cleanup...
flake8 testing of https://github.com/PX4/Firmware on Python 3.7.1

$ flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics

./platforms/nuttx/Debug/Nuttx.py:394:6: F632 use ==/!= to compare str, bytes, and int literals
		if regs is 0:
     ^
./platforms/nuttx/Debug/Nuttx.py:419:14: F821 undefined name 'regmap'
		for reg in regmap:
             ^
./platforms/nuttx/Debug/Nuttx.py:420:24: F821 undefined name 'regmap'
			hex_addr= hex(int(a[regmap[reg]]))
                       ^
./platforms/nuttx/NuttX/tools/oldconfig.py:33:13: F821 undefined name 'raw_input'
    input = raw_input
            ^
./Tools/geotag_images_ulog.py:41:69: E999 SyntaxError: invalid syntax
    print "Usage: python geotag_images_ulog.py [logfile] [image dir]"
                                                                    ^
./Tools/fetch_file.py:85:19: E999 SyntaxError: invalid syntax
    print "Get %s:" % fn,
                  ^
./Tools/upload_log.py:43:15: F821 undefined name 'raw_input'
        ret = raw_input(ask_string)
              ^
./Tools/run-clang-tidy.py:57:57: E999 SyntaxError: invalid syntax
      print 'Error: could not find compilation database.'
                                                        ^
./Tools/px4moduledoc/srcparser.py:350:23: F821 undefined name 'reduce'
        getopt_args = reduce(lambda a, b: a + b[1], getopt_args, '').replace(':', '')
                      ^
./Tools/uorb_graph/create.py:521:36: E999 TabError: inconsistent use of tabs and spaces in indentation
	graph.render(file_name, view=False)
                                   ^
./integrationtests/python_src/px4_it/mavros/mavros_offboard_attctl_test.py:126:18: F821 undefined name 'xrange'
        for i in xrange(timeout * loop_freq):
                 ^
./integrationtests/python_src/px4_it/mavros/mavros_offboard_posctl_test.py:133:18: F821 undefined name 'xrange'
        for i in xrange(timeout * loop_freq):
                 ^
./integrationtests/python_src/px4_it/mavros/mavros_offboard_posctl_test.py:172:18: F821 undefined name 'xrange'
        for i in xrange(len(positions)):
                 ^
./integrationtests/python_src/px4_it/mavros/mavros_test_common.py:173:18: F821 undefined name 'xrange'
        for i in xrange(timeout * loop_freq):
                 ^
./integrationtests/python_src/px4_it/mavros/mavros_test_common.py:203:18: F821 undefined name 'xrange'
        for i in xrange(timeout * loop_freq):
                 ^
./integrationtests/python_src/px4_it/mavros/mavros_test_common.py:234:18: F821 undefined name 'xrange'
        for i in xrange(timeout * loop_freq):
                 ^
./integrationtests/python_src/px4_it/mavros/mavros_test_common.py:257:18: F821 undefined name 'xrange'
        for i in xrange(timeout * loop_freq):
                 ^
./integrationtests/python_src/px4_it/mavros/mavros_test_common.py:285:18: F821 undefined name 'xrange'
        for i in xrange(timeout * loop_freq):
                 ^
./integrationtests/python_src/px4_it/mavros/mavros_test_common.py:308:18: F821 undefined name 'xrange'
        for i in xrange(timeout * loop_freq):
                 ^
./integrationtests/python_src/px4_it/mavros/mavros_test_common.py:341:18: F821 undefined name 'xrange'
        for i in xrange(timeout * loop_freq):
                 ^
./integrationtests/python_src/px4_it/mavros/mavros_test_common.py:377:18: F821 undefined name 'xrange'
        for i in xrange(timeout * loop_freq):
                 ^
./integrationtests/python_src/px4_it/mavros/mission_test.py:190:18: F821 undefined name 'xrange'
        for i in xrange(timeout * loop_freq):
                 ^
./integrationtests/python_src/px4_it/dronekit/MissionCheck.py:61:18: E999 SyntaxError: invalid syntax
print "Connecting"
                 ^
./src/lib/parameters/px4params/markdownout.py:50:20: F632 use ==/!= to compare str, bytes, and int literals
                if long_desc is not '':
                   ^
./src/modules/mavlink/mavlink_tests/mavlink_ftp_test_data.py:2:26: E999 SyntaxError: invalid syntax
print 'Arguments: file - ' + sys.argv[1] + ', length - ' + sys.argv[2]
                         ^
./msg/tools/uorb_rtps_classifier.py:198:27: F821 undefined name 'errno'
            if e.errno == errno.ENOENT:
                          ^
./msg/tools/uorb_rtps_classifier.py:199:31: F821 undefined name 'errno'
                raise IOError(errno.ENOENT, os.strerror(
                              ^
./msg/tools/uorb_rtps_classifier.py:200:21: F821 undefined name 'errno'
                    errno.ENOENT), yaml_file)
                    ^
./msg/tools/uorb_to_ros_rtps_ids.py:79:23: F821 undefined name 'errno'
        if e.errno == errno.ENOENT:
                      ^
./msg/tools/uorb_to_ros_rtps_ids.py:80:27: F821 undefined name 'errno'
            raise IOError(errno.ENOENT, os.strerror(errno.ENOENT), file)
                          ^
./msg/tools/uorb_to_ros_rtps_ids.py:80:53: F821 undefined name 'errno'
            raise IOError(errno.ENOENT, os.strerror(errno.ENOENT), file)
                                                    ^
./msg/tools/uorb_to_ros_rtps_ids.py:97:20: F821 undefined name 'basestring'
                v, basestring) else v for k, v in dictionary.iteritems()}
                   ^
./msg/tools/uorb_to_ros_rtps_ids.py:129:23: F821 undefined name 'errno'
        if e.errno == errno.ENOENT:
                      ^
./msg/tools/uorb_to_ros_rtps_ids.py:130:27: F821 undefined name 'errno'
            raise IOError(errno.ENOENT, os.strerror(errno.ENOENT), file)
                          ^
./msg/tools/uorb_to_ros_rtps_ids.py:130:53: F821 undefined name 'errno'
            raise IOError(errno.ENOENT, os.strerror(errno.ENOENT), file)
                                                    ^
./msg/tools/generate_microRTPS_bridge.py:246:25: F821 undefined name 'raw_input'
        _continue = str(raw_input("\nFiles in " + agent_out_dir +
                        ^
./msg/tools/generate_microRTPS_bridge.py:256:25: F821 undefined name 'raw_input'
        _continue = str(raw_input(
                        ^
6     E999 SyntaxError: invalid syntax
2     F632 use ==/!= to compare str, bytes, and int literals
29    F821 undefined name 'reduce'
37

E901,E999,F821,F822,F823 are the "showstopper" flake8 issues that can halt the runtime with a SyntaxError, NameError, etc. These 5 are different from most other flake8 issues which are merely "style violations" -- useful for readability but they do not effect runtime safety.

  • F821: undefined name name
  • F822: undefined name name in __all__
  • F823: local variable name referenced before assignment
  • E901: SyntaxError or IndentationError
  • E999: SyntaxError -- failed to compile a file into an Abstract Syntax Tree

@lamping7
Copy link
Member

lamping7 commented Oct 18, 2019

The current linting for the stuff I've touched with testing was done with yapf. We've never created a standard here for linting in python as we have with astyle. Some of these things are part of the ROS integration tests which will stay at 2.7. This explains the xrange issues too. The print statement should be functions (from __future__ import print_function) and other similar issues.

So, I'm not sure how to review this. Are we to fix all this or just review the current stage as a step in the direction given by the title of the PR?

@cclauss
Copy link
Contributor

cclauss commented Oct 18, 2019

  1. The conflicts should be fixed.
  2. It is not best practice to solve all Python 3 compatibility issues is a single pull request (unless the codebase is trivial) so if these changes do not break Python 2 compatibility and improve Python 3 compatibility then they should be reviewed favorably and landed.

75 days until Python 2 end of life.

lamping7
lamping7 previously approved these changes Oct 18, 2019
Copy link
Member

@lamping7 lamping7 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested this by simply running make from a clean pull. With Python 3.5, this worked after installing some dependencies. For reference I needed: pip3 install --user jinja2 catkin_pkg numpy toml pyyaml

@lamping7
Copy link
Member

@cclauss Can you open an issue with your comments here. I don't think they fall in line with what the purpose of this PR was -- python 3 for the build process -- and I don't want to drag it down.

@cclauss
Copy link
Contributor

cclauss commented Oct 18, 2019

Agreed. Conflicts must be resolved and tests must pass.

@TSC21
Copy link
Member

TSC21 commented Nov 20, 2019

I am not sure what's happening but I am totally confident that jinja2 is installed on the containers: http://ci.px4.io:8080/blue/organizations/jenkins/PX4%2Fcontainers/detail/master/150/pipeline#step-32-log-724

@cclauss
Copy link
Contributor

cclauss commented Nov 20, 2019

In several of these files we need to make the same text change numerous times. Would it be possible instead to define a variable that can be reused multiple times so that future modifications are simpler to make and to review?

@TSC21
Copy link
Member

TSC21 commented Nov 20, 2019

In several of these files we need to make the same text change numerous times. Would it be possible instead to define a variable that can be reused multiple times so that future modifications are simpler to make and to review?

Are you referring to the container tags? The thing about it is that it's not mandatory that we update all the containers tag at the same time. There could even be situations where that doesn't happen.

@cclauss
Copy link
Contributor

cclauss commented Nov 20, 2019

I am totally confident that jinja2 is installed on the containers
pip2 install Jinja2 makes enables that module for Python 2 but not for Python 3.
pip3 install Jinja2 makes enables that module for Python 3 but not for Python 2.

@TSC21
Copy link
Member

TSC21 commented Nov 20, 2019

I am totally confident that jinja2 is installed on the containers
pip2 install Jinja2 makes enables that module for Python 2 but not for Python 3.
pip3 install Jinja2 makes enables that module for Python 3 but not for Python 2.

@cclauss what's your point here? The containers were updated: https://github.com/PX4/containers/blob/master/docker/Dockerfile_base-bionic#L60-L63

@TSC21
Copy link
Member

TSC21 commented Nov 20, 2019

I am totally confident that jinja2 is installed on the containers
pip2 install Jinja2 makes enables that module for Python 2 but not for Python 3.
pip3 install Jinja2 makes enables that module for Python 3 but not for Python 2.

@cclauss what's your point here? The containers were updated: https://github.com/PX4/containers/blob/master/docker/Dockerfile_base-bionic#L60-L63

Looks like we missed Python3 in the first place: PX4/PX4-containers#211

@MaEtUgR
Copy link
Member

MaEtUgR commented Nov 21, 2019

@julianoes Good call. Well I never checked anything Python 3 for the Windows toolchain because at the time it was completely unsupported by the build. Now I could change the toolchain installation procedure to only include 3.

@TSC21
Copy link
Member

TSC21 commented Nov 22, 2019

@julianoes we have another issue here: genmsg and gencpp seem to use Python 2 instead of Python 3. We need to update our fork on those as well.

@TSC21
Copy link
Member

TSC21 commented Nov 22, 2019

Actually, we have already Python 3 compatibility added here: PX4/genmsg@1ad8e13. This just needs to get it and we should update both genmsg and gencpp modules.

@MaEtUgR
Copy link
Member

MaEtUgR commented Nov 22, 2019

@julianoes I found the necessary Cygwin toolchain changes locally and it builds this pr fine. I can create a pr on the toolchain repository and let CI generate a new toolchain installer soon. I'm assuming that I need to keep backward compatibility and leave python 2 working like before as well.

@MaEtUgR MaEtUgR force-pushed the pr-build-with-python3 branch from 00bce6c to 0442295 Compare November 23, 2019 23:55
@MaEtUgR
Copy link
Member

MaEtUgR commented Nov 24, 2019

I

@julianoes I would say the Windows Toolchain/CI is ready (first) 😉

@julianoes
Copy link
Contributor Author

This has been replaced by #13572 and #13949, closing.

@julianoes julianoes closed this Jan 15, 2020
@julianoes julianoes deleted the pr-build-with-python3 branch January 15, 2020 09:42
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.

5 participants