Skip to content

Commit

Permalink
fix: use print() function in both Python 2 and Python 3 (#18395)
Browse files Browse the repository at this point in the history
Legacy print statements are syntax errors in Python 3 but print() function works as expected in both Python 2 and Python 3.

Old style exceptions are syntax errors in Python 3 but new style exceptions work as expected in both Python 2 and Python 3.
  • Loading branch information
cclauss authored and codebytere committed Jun 15, 2019
1 parent 7d0a938 commit 1d6e5e6
Show file tree
Hide file tree
Showing 18 changed files with 62 additions and 894 deletions.
3 changes: 2 additions & 1 deletion build/npm-run.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env python
from __future__ import print_function
import os
import subprocess
import sys
Expand All @@ -13,6 +14,6 @@
] + sys.argv[1:]
try:
subprocess.check_output(args, stderr=subprocess.STDOUT)
except subprocess.CalledProcessError, e:
except subprocess.CalledProcessError as e:
print("NPM script '" + sys.argv[2] + "' failed with code '" + str(e.returncode) + "':\n" + e.output)
sys.exit(e.returncode)
3 changes: 2 additions & 1 deletion build/zip.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env python
from __future__ import print_function
import os
import subprocess
import sys
Expand Down Expand Up @@ -41,7 +42,7 @@ def execute(argv):
output = subprocess.check_output(argv, stderr=subprocess.STDOUT)
return output
except subprocess.CalledProcessError as e:
print e.output
print(e.output)
raise e

def main(argv):
Expand Down
5 changes: 3 additions & 2 deletions script/check-relative-doc-links.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/env python

from __future__ import print_function
import os
import sys
import re
Expand Down Expand Up @@ -99,9 +100,9 @@ def checkSections(sections, lines):

def print_errors(filepath, brokenLink):
if brokenLink:
print "File Location: " + filepath
print("File Location: " + filepath)
for link in brokenLink:
print "\tBroken links: " + link
print("\tBroken links: " + link)


if __name__ == '__main__':
Expand Down
3 changes: 2 additions & 1 deletion script/check-trailing-whitespace.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/env python

from __future__ import print_function
import argparse
import os
import sys
Expand Down Expand Up @@ -48,7 +49,7 @@ def hasTrailingWhiteSpace(filepath, fix):
for line in lines:
fixed_lines.append(line.rstrip() + '\n')
if not fix and line != line.rstrip():
print "Trailing whitespace in: " + filepath
print("Trailing whitespace in: " + filepath)
return True
if fix:
with open(filepath, 'w') as f:
Expand Down
3 changes: 2 additions & 1 deletion script/get-version.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
#!/usr/bin/env python

from __future__ import print_function
import sys

from lib.util import get_electron_version

def main():
print get_electron_version()
print(get_electron_version())

if __name__ == '__main__':
sys.exit(main())
6 changes: 4 additions & 2 deletions script/lib/config.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/env python

from __future__ import print_function
import errno
import os
import platform
Expand Down Expand Up @@ -47,7 +48,8 @@ def get_env_var(name):
# TODO Remove ATOM_SHELL_* fallback values
value = os.environ.get('ATOM_SHELL_' + name, '')
if value:
print 'Warning: Use $ELECTRON_' + name + ' instead of $ATOM_SHELL_' + name
print('Warning: Use $ELECTRON_' + name +
' instead of $ATOM_SHELL_' + name)
return value


Expand All @@ -63,7 +65,7 @@ def s3_config():


def enable_verbose_mode():
print 'Running in verbose mode'
print('Running in verbose mode')
global verbose_mode
verbose_mode = True

Expand Down
5 changes: 5 additions & 0 deletions script/lib/native_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@
sys.path.append(PYYAML_LIB_DIR)
import yaml #pylint: disable=wrong-import-position,wrong-import-order

try:
basestring # Python 2
except NameError: # Python 3
basestring = str # pylint: disable=redefined-builtin


class Verbosity:
CHATTY = 'chatty' # stdout and stderr
Expand Down
19 changes: 10 additions & 9 deletions script/lib/util.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/env python

from __future__ import print_function
import atexit
import contextlib
import datetime
Expand Down Expand Up @@ -64,7 +65,7 @@ def download(text, url, path):
if hasattr(ssl, '_create_unverified_context'):
ssl._create_default_https_context = ssl._create_unverified_context

print "Downloading %s to %s" % (url, path)
print("Downloading %s to %s" % (url, path))
web_file = urllib2.urlopen(url)
file_size = int(web_file.info().getheaders("Content-Length")[0])
downloaded_size = 0
Expand All @@ -83,12 +84,12 @@ def download(text, url, path):
if not ci:
percent = downloaded_size * 100. / file_size
status = "\r%s %10d [%3.1f%%]" % (text, downloaded_size, percent)
print status,
print(status, end=' ')

if ci:
print "%s done." % (text)
print("%s done." % (text))
else:
print
print()
return path


Expand Down Expand Up @@ -148,27 +149,27 @@ def execute(argv, env=None, cwd=None):
if env is None:
env = os.environ
if is_verbose_mode():
print ' '.join(argv)
print(' '.join(argv))
try:
output = subprocess.check_output(argv, stderr=subprocess.STDOUT,
env=env, cwd=cwd)
if is_verbose_mode():
print output
print(output)
return output
except subprocess.CalledProcessError as e:
print e.output
print(e.output)
raise e


def execute_stdout(argv, env=None, cwd=None):
if env is None:
env = os.environ
if is_verbose_mode():
print ' '.join(argv)
print(' '.join(argv))
try:
subprocess.check_call(argv, env=env, cwd=cwd)
except subprocess.CalledProcessError as e:
print e.output
print(e.output)
raise e
else:
execute(argv, env, cwd)
Expand Down

0 comments on commit 1d6e5e6

Please sign in to comment.