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

Fix python linting errors #112499

Merged
merged 1 commit into from
Jun 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions compiler/rustc_codegen_gcc/tools/generate_intrinsics.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import re
import sys
import subprocess
from os import walk


def run_command(command, cwd=None):
Expand Down Expand Up @@ -180,7 +179,7 @@ def update_intrinsics(llvm_path, llvmint, llvmint2):
intrinsics[arch].sort(key=lambda x: (x[0], x[2]))
out.write(' // {}\n'.format(arch))
for entry in intrinsics[arch]:
if entry[2] == True: # if it is a duplicate
if entry[2] is True: # if it is a duplicate
out.write(' // [DUPLICATE]: "{}" => "{}",\n'.format(entry[0], entry[1]))
elif "_round_mask" in entry[1]:
out.write(' // [INVALID CONVERSION]: "{}" => "{}",\n'.format(entry[0], entry[1]))
Expand Down
2 changes: 1 addition & 1 deletion library/core/src/unicode/printable.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ def print_singletons(uppers, lowers, uppersname, lowersname):
print("#[rustfmt::skip]")
print("const {}: &[u8] = &[".format(lowersname))
for i in range(0, len(lowers), 8):
print(" {}".format(" ".join("{:#04x},".format(l) for l in lowers[i:i+8])))
print(" {}".format(" ".join("{:#04x},".format(x) for x in lowers[i:i+8])))
print("];")

def print_normal(normal, normalname):
Expand Down
4 changes: 2 additions & 2 deletions src/bootstrap/bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -620,7 +620,7 @@ def get_answer():
# The latter one does not exist on NixOS when using tmpfs as root.
try:
with open("/etc/os-release", "r") as f:
if not any(l.strip() in ("ID=nixos", "ID='nixos'", 'ID="nixos"') for l in f):
if not any(ln.strip() in ("ID=nixos", "ID='nixos'", 'ID="nixos"') for ln in f):
return False
except FileNotFoundError:
return False
Expand Down Expand Up @@ -872,7 +872,7 @@ def build_bootstrap(self, color, warnings, verbose_count):
}
for var_name, toml_key in var_data.items():
toml_val = self.get_toml(toml_key, build_section)
if toml_val != None:
if toml_val is not None:
env["{}_{}".format(var_name, host_triple_sanitized)] = toml_val

# preserve existing RUSTFLAGS
Expand Down
10 changes: 5 additions & 5 deletions src/bootstrap/configure.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
rust_dir = os.path.dirname(rust_dir)
rust_dir = os.path.dirname(rust_dir)
sys.path.append(os.path.join(rust_dir, "src", "bootstrap"))
import bootstrap
import bootstrap # noqa: E402


class Option(object):
Expand Down Expand Up @@ -319,7 +319,7 @@ def apply_args(known_args, option_checking, config):
for key in known_args:
# The `set` option is special and can be passed a bunch of times
if key == 'set':
for option, value in known_args[key]:
for _option, value in known_args[key]:
keyval = value.split('=', 1)
if len(keyval) == 1 or keyval[1] == "true":
value = True
Expand Down Expand Up @@ -401,7 +401,7 @@ def parse_example_config(known_args, config):
top_level_keys = []

for line in open(rust_dir + '/config.example.toml').read().split("\n"):
if cur_section == None:
if cur_section is None:
if line.count('=') == 1:
top_level_key = line.split('=')[0]
top_level_key = top_level_key.strip(' #')
Expand Down Expand Up @@ -523,8 +523,8 @@ def write_uncommented(target, f):
block.append(line)
if len(line) == 0:
if not is_comment:
for l in block:
f.write(l + "\n")
for ln in block:
f.write(ln + "\n")
block = []
is_comment = True
continue
Expand Down
Empty file modified src/ci/cpu-usage-over-time.py
100644 → 100755
Empty file.
Empty file modified src/ci/docker/host-x86_64/test-various/uefi_qemu_test/run.py
100644 → 100755
Empty file.
17 changes: 8 additions & 9 deletions src/ci/docker/scripts/android-sdk-manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@
# Simpler reimplementation of Android's sdkmanager
# Extra features of this implementation are pinning and mirroring

import argparse
import hashlib
import os
import subprocess
import tempfile
import urllib.request
import xml.etree.ElementTree as ET

# These URLs are the Google repositories containing the list of available
# packages and their versions. The list has been generated by listing the URLs
# fetched while executing `tools/bin/sdkmanager --list`
Expand All @@ -27,15 +35,6 @@
MIRROR_BUCKET_REGION = "us-west-1"
MIRROR_BASE_DIR = "rustc/android/"

import argparse
import hashlib
import os
import subprocess
import sys
import tempfile
import urllib.request
import xml.etree.ElementTree as ET

class Package:
def __init__(self, path, url, sha1, deps=None):
if deps is None:
Expand Down
4 changes: 2 additions & 2 deletions src/ci/docker/scripts/fuchsia-test-runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -568,8 +568,8 @@ def log(msg):
env_vars += f'\n "{var_name}={var_value}",'

# Default to no backtrace for test suite
if os.getenv("RUST_BACKTRACE") == None:
env_vars += f'\n "RUST_BACKTRACE=0",'
if os.getenv("RUST_BACKTRACE") is None:
env_vars += '\n "RUST_BACKTRACE=0",'

cml.write(
self.CML_TEMPLATE.format(env_vars=env_vars, exe_name=exe_name)
Expand Down
4 changes: 2 additions & 2 deletions src/ci/stage-build.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ def retry_action(action, name: str, max_fails: int = 5):
try:
action()
return
except:
except BaseException: # also catch ctrl+c/sysexit
LOGGER.error(f"Action `{name}` has failed\n{traceback.format_exc()}")

raise Exception(f"Action `{name}` has failed after {max_fails} attempts")
Expand Down Expand Up @@ -818,7 +818,7 @@ def extract_dist_dir(name: str) -> Path:
# Extract rustc, libstd, cargo and src archives to create the optimized sysroot
rustc_dir = extract_dist_dir(f"rustc-nightly-{PGO_HOST}") / "rustc"
libstd_dir = extract_dist_dir(f"rust-std-nightly-{PGO_HOST}") / f"rust-std-{PGO_HOST}"
cargo_dir = extract_dist_dir(f"cargo-nightly-{PGO_HOST}") / f"cargo"
cargo_dir = extract_dist_dir(f"cargo-nightly-{PGO_HOST}") / "cargo"
extracted_src_dir = extract_dist_dir("rust-src-nightly") / "rust-src"

# We need to manually copy libstd to the extracted rustc sysroot
Expand Down
4 changes: 1 addition & 3 deletions src/etc/dec2flt_table.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@
available here: <https://github.com/fastfloat/fast_float/blob/main/script/table_generation.py>.
"""
from __future__ import print_function
from math import ceil, floor, log, log2
from fractions import Fraction
from math import ceil, floor, log
from collections import deque

HEADER = """
Expand Down Expand Up @@ -97,7 +96,6 @@ def print_proper_powers(min_exp, max_exp, bias):
print('#[rustfmt::skip]')
typ = '[(u64, u64); N_POWERS_OF_FIVE]'
print('pub static POWER_OF_FIVE_128: {} = ['.format(typ))
lo_mask = (1 << 64) - 1
for c, exp in powers:
hi = '0x{:x}'.format(c // (1 << 64))
lo = '0x{:x}'.format(c % (1 << 64))
Expand Down
1 change: 1 addition & 0 deletions src/etc/gdb_load_rust_pretty_printers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
self_dir = path.dirname(path.realpath(__file__))
sys.path.append(self_dir)

# ruff: noqa: E402
import gdb
import gdb_lookup

Expand Down
4 changes: 3 additions & 1 deletion src/etc/generate-deriving-span-tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,9 @@ def write_file(name, string):
traits[trait] = (ALL, supers, errs)

for (trait, (types, super_traits, error_count)) in traits.items():
mk = lambda ty: create_test_case(ty, trait, super_traits, error_count)
def mk(ty, t=trait, st=super_traits, ec=error_count):
return create_test_case(ty, t, st, ec)

if types & ENUM:
write_file(trait + '-enum', mk(ENUM_TUPLE))
write_file(trait + '-enum-struct-variant', mk(ENUM_STRUCT))
Expand Down
8 changes: 5 additions & 3 deletions src/etc/htmldocck.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@

# Python 2 -> 3 compatibility
try:
unichr
unichr # noqa: B018 FIXME: py2
except NameError:
unichr = chr

Expand Down Expand Up @@ -348,7 +348,9 @@ def get_tree(self, path):
try:
tree = ET.fromstringlist(f.readlines(), CustomHTMLParser())
except Exception as e:
raise RuntimeError('Cannot parse an HTML file {!r}: {}'.format(path, e))
raise RuntimeError( # noqa: B904 FIXME: py2
'Cannot parse an HTML file {!r}: {}'.format(path, e)
)
self.trees[path] = tree
return self.trees[path]

Expand Down Expand Up @@ -422,7 +424,7 @@ def check_snapshot(snapshot_name, actual_tree, normalize_to_text):
if bless:
expected_str = None
else:
raise FailedCheck('No saved snapshot value')
raise FailedCheck('No saved snapshot value') # noqa: B904 FIXME: py2

if not normalize_to_text:
actual_str = ET.tostring(actual_tree).decode('utf-8')
Expand Down
2 changes: 1 addition & 1 deletion src/etc/lldb_batchmode.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ def listen():
breakpoint = lldb.SBBreakpoint.GetBreakpointFromEvent(event)
print_debug("breakpoint added, id = " + str(breakpoint.id))
new_breakpoints.append(breakpoint.id)
except:
except BaseException: # explicitly catch ctrl+c/sysexit
print_debug("breakpoint listener shutting down")

# Start the listener and let it run as a daemon
Expand Down
2 changes: 1 addition & 1 deletion src/etc/lldb_providers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import sys

from lldb import SBValue, SBData, SBError, eBasicTypeLong, eBasicTypeUnsignedLong, \
from lldb import SBData, SBError, eBasicTypeLong, eBasicTypeUnsignedLong, \
eBasicTypeUnsignedChar

# from lldb.formatters import Logger
Expand Down
Empty file modified src/etc/test-float-parse/runtests.py
100644 → 100755
Empty file.
20 changes: 14 additions & 6 deletions src/tools/miri/test-cargo-miri/run-test.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
and the working directory to contain the cargo-miri-test project.
'''

import sys, subprocess, os, re, difflib
import difflib
import os
import re
import sys
import subprocess

CGREEN = '\33[32m'
CBOLD = '\33[1m'
Expand Down Expand Up @@ -46,7 +50,9 @@ def check_output(actual, path, name):
print(f"--- END diff {name} ---")
return False

def test(name, cmd, stdout_ref, stderr_ref, stdin=b'', env={}):
def test(name, cmd, stdout_ref, stderr_ref, stdin=b'', env=None):
if env is None:
env = {}
print("Testing {}...".format(name))
## Call `cargo miri`, capture all output
p_env = os.environ.copy()
Expand All @@ -64,13 +70,15 @@ def test(name, cmd, stdout_ref, stderr_ref, stdin=b'', env={}):

stdout_matches = check_output(stdout, stdout_ref, "stdout")
stderr_matches = check_output(stderr, stderr_ref, "stderr")

if p.returncode == 0 and stdout_matches and stderr_matches:
# All good!
return
fail("exit code was {}".format(p.returncode))

def test_no_rebuild(name, cmd, env={}):
def test_no_rebuild(name, cmd, env=None):
if env is None:
env = {}
print("Testing {}...".format(name))
p_env = os.environ.copy()
p_env.update(env)
Expand All @@ -84,13 +92,13 @@ def test_no_rebuild(name, cmd, env={}):
stdout = stdout.decode("UTF-8")
stderr = stderr.decode("UTF-8")
if p.returncode != 0:
fail("rebuild failed");
fail("rebuild failed")
# Also check for 'Running' as a sanity check.
if stderr.count(" Compiling ") > 0 or stderr.count(" Running ") == 0:
print("--- BEGIN stderr ---")
print(stderr, end="")
print("--- END stderr ---")
fail("Something was being rebuilt when it should not be (or we got no output)");
fail("Something was being rebuilt when it should not be (or we got no output)")

def test_cargo_miri_run():
test("`cargo miri run` (no isolation)",
Expand Down
16 changes: 8 additions & 8 deletions src/tools/publish_toolstate.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import urllib.request as urllib2
from urllib.error import HTTPError
try:
import typing
import typing # noqa: F401 FIXME: py2
except ImportError:
pass

Expand Down Expand Up @@ -152,8 +152,8 @@ def update_latest(
latest = json.load(f, object_pairs_hook=collections.OrderedDict)

current_status = {
os: read_current_status(current_commit, 'history/' + os + '.tsv')
for os in ['windows', 'linux']
os_: read_current_status(current_commit, 'history/' + os_ + '.tsv')
for os_ in ['windows', 'linux']
}

slug = 'rust-lang/rust'
Expand All @@ -170,23 +170,23 @@ def update_latest(
changed = False
create_issue_for_status = None # set to the status that caused the issue

for os, s in current_status.items():
old = status[os]
for os_, s in current_status.items():
old = status[os_]
new = s.get(tool, old)
status[os] = new
status[os_] = new
maintainers = ' '.join('@'+name for name in MAINTAINERS.get(tool, ()))
# comparing the strings, but they are ordered appropriately:
# "test-pass" > "test-fail" > "build-fail"
if new > old:
# things got fixed or at least the status quo improved
changed = True
message += '🎉 {} on {}: {} → {} (cc {}).\n' \
.format(tool, os, old, new, maintainers)
.format(tool, os_, old, new, maintainers)
elif new < old:
# tests or builds are failing and were not failing before
changed = True
title = '💔 {} on {}: {} → {}' \
.format(tool, os, old, new)
.format(tool, os_, old, new)
message += '{} (cc {}).\n' \
.format(title, maintainers)
# See if we need to create an issue.
Expand Down
Empty file modified tests/run-make/coverage-reports/sort_subviews.py
100644 → 100755
Empty file.
2 changes: 1 addition & 1 deletion tests/run-make/libtest-junit/validate_junit.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
for line in sys.stdin:
try:
ET.fromstring(line)
except ET.ParseError as pe:
except ET.ParseError:
print("Invalid xml: %r" % line)
raise
2 changes: 1 addition & 1 deletion tests/run-make/rustdoc-map-file/validate_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@


def find_redirect_map_file(folder, errors):
for root, dirs, files in os.walk(folder):
for root, _dirs, files in os.walk(folder):
for name in files:
if not name.endswith("redirect-map.json"):
continue
Expand Down
2 changes: 1 addition & 1 deletion tests/run-make/sysroot-crates-are-unstable/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def check_lib(lib):
'--target', os.environ['TARGET'],
'--extern', '{}={}'.format(lib['name'], lib['path'])],
to_input=('extern crate {};'.format(lib['name'])).encode('utf-8'))
if not 'use of unstable library feature' in '{}{}'.format(stdout, stderr):
if 'use of unstable library feature' not in '{}{}'.format(stdout, stderr):
print('crate {} "{}" is not unstable'.format(lib['name'], lib['path']))
print('{}{}'.format(stdout, stderr))
print('')
Expand Down