From c2079443145a331129d06a9efdad3dfa52451ffd Mon Sep 17 00:00:00 2001 From: Kim Date: Wed, 29 Mar 2023 10:25:21 +0800 Subject: [PATCH] [CodeStyle][PLR1722] replace `exit()` with `sys.exit()` (#52151) * fix PLR1722 * fix issue * fix error --- .../paddle/distributed/auto_parallel/tuner/profiler.py | 3 ++- python/paddle/distributed/fleet/launch.py | 2 +- python/paddle/distributed/fleet/launch_utils.py | 4 ++-- python/paddle/distributed/utils/launch_utils.py | 4 ++-- .../fluid/tests/cpp_extension/test_cpp_extension_jit.py | 2 +- .../tests/cpp_extension/test_cpp_extension_setup.py | 2 +- .../fluid/tests/custom_runtime/test_custom_cpu_plugin.py | 2 +- .../custom_runtime/test_custom_cpu_profiler_plugin.py | 2 +- .../tests/custom_runtime/test_custom_cpu_to_static.py | 2 +- .../fluid/tests/custom_runtime/test_custom_op_setup.py | 2 +- .../tests/unittests/dygraph_to_static/test_mobile_net.py | 3 ++- python/paddle/fluid/tests/unittests/multi_process.py | 2 +- test/custom_kernel/test_custom_kernel_dot.py | 2 +- test/custom_kernel/test_custom_kernel_load.py | 2 +- test/custom_op/test_custom_relu_op_xpu_setup.py | 2 +- test/legacy_test/test_error_clip.py | 6 ++++-- tools/check_ctest_hung.py | 2 +- tools/check_op_benchmark_result.py | 5 +++-- tools/check_ut.py | 5 +++-- tools/continuous_integration/bisect.py | 4 ++-- tools/coverage/coverage_diff.py | 2 +- tools/coverage/coverage_diff_list.py | 2 +- tools/coverage/coverage_lines.py | 8 ++++---- tools/gen_ut_cmakelists.py | 3 ++- tools/get_pr_ut.py | 9 +++++---- tools/sampcd_processor.py | 8 ++++---- tools/test_runner.py | 2 +- 27 files changed, 50 insertions(+), 42 deletions(-) diff --git a/python/paddle/distributed/auto_parallel/tuner/profiler.py b/python/paddle/distributed/auto_parallel/tuner/profiler.py index d81db732f5c..c419ae7fcf7 100644 --- a/python/paddle/distributed/auto_parallel/tuner/profiler.py +++ b/python/paddle/distributed/auto_parallel/tuner/profiler.py @@ -16,6 +16,7 @@ import argparse import json import os import pickle +import sys import time import traceback @@ -291,7 +292,7 @@ def profiler(args): data_loader._inner_dataloader.reset() del data_loader._inner_dataloader - exit(1) + sys.exit(1) data_loader._inner_dataloader.reset() del data_loader._inner_dataloader diff --git a/python/paddle/distributed/fleet/launch.py b/python/paddle/distributed/fleet/launch.py index 1b4c5536e38..63c289cc2f4 100755 --- a/python/paddle/distributed/fleet/launch.py +++ b/python/paddle/distributed/fleet/launch.py @@ -476,7 +476,7 @@ def launch_collective(args): except: logger.warning("Terminating... exit") terminate_local_procs(procs) - exit(1) + sys.exit(1) if os.path.exists(tmp_dir): shutil.rmtree(tmp_dir) diff --git a/python/paddle/distributed/fleet/launch_utils.py b/python/paddle/distributed/fleet/launch_utils.py index 41dfd443c81..54fe8a77e43 100755 --- a/python/paddle/distributed/fleet/launch_utils.py +++ b/python/paddle/distributed/fleet/launch_utils.py @@ -370,7 +370,7 @@ def terminate_local_procs(procs): time.sleep(3) logger.fatal("can't kill all process and exit") - exit(1) + sys.exit(1) def get_host_name_ip(): @@ -678,7 +678,7 @@ def watch_local_trainers(procs, nranks): if error: terminate_local_procs(procs) - exit(1) + sys.exit(1) except KeyboardInterrupt: logger.warning("KeyboardInterrupt, exit") diff --git a/python/paddle/distributed/utils/launch_utils.py b/python/paddle/distributed/utils/launch_utils.py index c922eef9ccb..42c44700044 100644 --- a/python/paddle/distributed/utils/launch_utils.py +++ b/python/paddle/distributed/utils/launch_utils.py @@ -354,7 +354,7 @@ def terminate_local_procs(procs): time.sleep(3) logger.fatal("can't kill all process and exit") - exit(1) + sys.exit(1) def get_host_name_ip(): @@ -543,7 +543,7 @@ def watch_local_trainers(procs, nranks): if error: terminate_local_procs(procs) - exit(1) + sys.exit(1) except KeyboardInterrupt: logger.warning("KeyboardInterrupt, exit") diff --git a/python/paddle/fluid/tests/cpp_extension/test_cpp_extension_jit.py b/python/paddle/fluid/tests/cpp_extension/test_cpp_extension_jit.py index 757cee3087d..1dd5c969546 100644 --- a/python/paddle/fluid/tests/cpp_extension/test_cpp_extension_jit.py +++ b/python/paddle/fluid/tests/cpp_extension/test_cpp_extension_jit.py @@ -24,7 +24,7 @@ from paddle.utils.cpp_extension import load if os.name == 'nt' or sys.platform.startswith('darwin'): # only support Linux now - exit() + sys.exit() # Compile and load cpp extension Just-In-Time. sources = ["custom_extension.cc", "custom_sub.cc"] diff --git a/python/paddle/fluid/tests/cpp_extension/test_cpp_extension_setup.py b/python/paddle/fluid/tests/cpp_extension/test_cpp_extension_setup.py index c6e53360882..29280008dfc 100644 --- a/python/paddle/fluid/tests/cpp_extension/test_cpp_extension_setup.py +++ b/python/paddle/fluid/tests/cpp_extension/test_cpp_extension_setup.py @@ -318,5 +318,5 @@ class TestCppExtensionSetupInstall(unittest.TestCase): if __name__ == '__main__': if os.name == 'nt' or sys.platform.startswith('darwin'): # only support Linux now - exit() + sys.exit() unittest.main() diff --git a/python/paddle/fluid/tests/custom_runtime/test_custom_cpu_plugin.py b/python/paddle/fluid/tests/custom_runtime/test_custom_cpu_plugin.py index 4772ae00d3f..f9606fb240d 100755 --- a/python/paddle/fluid/tests/custom_runtime/test_custom_cpu_plugin.py +++ b/python/paddle/fluid/tests/custom_runtime/test_custom_cpu_plugin.py @@ -326,5 +326,5 @@ class TestCustomCPUPlugin(unittest.TestCase): if __name__ == '__main__': if os.name == 'nt' or sys.platform.startswith('darwin'): # only support Linux now - exit() + sys.exit() unittest.main() diff --git a/python/paddle/fluid/tests/custom_runtime/test_custom_cpu_profiler_plugin.py b/python/paddle/fluid/tests/custom_runtime/test_custom_cpu_profiler_plugin.py index 2926bea9f45..79630f4f505 100644 --- a/python/paddle/fluid/tests/custom_runtime/test_custom_cpu_profiler_plugin.py +++ b/python/paddle/fluid/tests/custom_runtime/test_custom_cpu_profiler_plugin.py @@ -71,5 +71,5 @@ class TestCustomCPUProfilerPlugin(unittest.TestCase): if __name__ == '__main__': if os.name == 'nt' or sys.platform.startswith('darwin'): # only support Linux now - exit() + sys.exit() unittest.main() diff --git a/python/paddle/fluid/tests/custom_runtime/test_custom_cpu_to_static.py b/python/paddle/fluid/tests/custom_runtime/test_custom_cpu_to_static.py index ed7588cfbd0..8cf475fc0ff 100644 --- a/python/paddle/fluid/tests/custom_runtime/test_custom_cpu_to_static.py +++ b/python/paddle/fluid/tests/custom_runtime/test_custom_cpu_to_static.py @@ -279,5 +279,5 @@ class TestCustomCPUPlugin(unittest.TestCase): if __name__ == '__main__': if os.name == 'nt' or sys.platform.startswith('darwin'): # only support Linux now - exit() + sys.exit() unittest.main() diff --git a/python/paddle/fluid/tests/custom_runtime/test_custom_op_setup.py b/python/paddle/fluid/tests/custom_runtime/test_custom_op_setup.py index 8b06cc91408..5f0e3d3ef1a 100644 --- a/python/paddle/fluid/tests/custom_runtime/test_custom_op_setup.py +++ b/python/paddle/fluid/tests/custom_runtime/test_custom_op_setup.py @@ -284,5 +284,5 @@ class TestNewCustomOpSetUpInstall(unittest.TestCase): if __name__ == "__main__": if os.name == 'nt' or sys.platform.startswith('darwin'): # only support Linux now - exit() + sys.exit() unittest.main() diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_mobile_net.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_mobile_net.py index c9bd401fbab..73e58251866 100644 --- a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_mobile_net.py +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_mobile_net.py @@ -13,6 +13,7 @@ # limitations under the License. import os +import sys import tempfile import time import unittest @@ -508,7 +509,7 @@ def train_mobilenet(args, to_static): print( "wrong model name, please try model = MobileNetV1 or MobileNetV2" ) - exit() + sys.exit() optimizer = create_optimizer(args=args, parameter_list=net.parameters()) diff --git a/python/paddle/fluid/tests/unittests/multi_process.py b/python/paddle/fluid/tests/unittests/multi_process.py index 74283be7b37..48ad6546024 100644 --- a/python/paddle/fluid/tests/unittests/multi_process.py +++ b/python/paddle/fluid/tests/unittests/multi_process.py @@ -51,7 +51,7 @@ def train_abort(prefix): if trainer_id == 0: try: # train abort - exit(1) + sys.exit(1) except SystemExit: name = "abort>>> selected_gpus:{} worker_endpoints:{} trainers_num:{} current_endpoint:{} trainer_id:{}".format( selected_gpus, diff --git a/test/custom_kernel/test_custom_kernel_dot.py b/test/custom_kernel/test_custom_kernel_dot.py index ce97cafcfdc..44aa6aec33c 100644 --- a/test/custom_kernel/test_custom_kernel_dot.py +++ b/test/custom_kernel/test_custom_kernel_dot.py @@ -91,5 +91,5 @@ class TestCustomKernelDotC(unittest.TestCase): if __name__ == '__main__': if os.name == 'nt' or sys.platform.startswith('darwin'): # only support Linux now - exit() + sys.exit() unittest.main() diff --git a/test/custom_kernel/test_custom_kernel_load.py b/test/custom_kernel/test_custom_kernel_load.py index 60398b7683e..929f42a3516 100644 --- a/test/custom_kernel/test_custom_kernel_load.py +++ b/test/custom_kernel/test_custom_kernel_load.py @@ -88,5 +88,5 @@ class TestCustomKernelLoad(unittest.TestCase): if __name__ == '__main__': if os.name == 'nt' or sys.platform.startswith('darwin'): # only support Linux now - exit() + sys.exit() unittest.main() diff --git a/test/custom_op/test_custom_relu_op_xpu_setup.py b/test/custom_op/test_custom_relu_op_xpu_setup.py index cbaf1adf50a..3eed65668eb 100644 --- a/test/custom_op/test_custom_relu_op_xpu_setup.py +++ b/test/custom_op/test_custom_relu_op_xpu_setup.py @@ -346,5 +346,5 @@ if __name__ == '__main__': # compile, install the custom op egg into site-packages under background # Currently custom XPU op does not support Windows if os.name == 'nt': - exit() + sys.exit() unittest.main() diff --git a/test/legacy_test/test_error_clip.py b/test/legacy_test/test_error_clip.py index ae4f4e47aff..1fba67df226 100644 --- a/test/legacy_test/test_error_clip.py +++ b/test/legacy_test/test_error_clip.py @@ -12,6 +12,8 @@ # See the License for the specific language governing permissions and # limitations under the License. +import sys + import paddle from paddle import fluid @@ -80,6 +82,6 @@ for data in train_reader(): (out1.clip(min=CLIP_MIN, max=CLIP_MAX) == out1_clip).all() and (out2 == out2_clip).all() ): - exit(1) + sys.exit(1) -exit(0) +sys.exit(0) diff --git a/tools/check_ctest_hung.py b/tools/check_ctest_hung.py index 3cd5acacb85..ae686346c9a 100644 --- a/tools/check_ctest_hung.py +++ b/tools/check_ctest_hung.py @@ -31,7 +31,7 @@ Diff: set(['test_parallel_executor_crf']) """ if len(sys.argv) < 2: print(usage) - exit(0) + sys.exit(0) logfile = sys.argv[1] started = set() diff --git a/tools/check_op_benchmark_result.py b/tools/check_op_benchmark_result.py index cda4c4b6394..07fab0ff6d3 100644 --- a/tools/check_op_benchmark_result.py +++ b/tools/check_op_benchmark_result.py @@ -16,6 +16,7 @@ import argparse import json import logging import os +import sys def check_path_exists(path): @@ -153,7 +154,7 @@ def update_api_info_file(fail_case_list, api_info_file): def summary_results(check_results, api_info_file): - """Summary results and return exit code.""" + """Summary results and return sys.exit code.""" for case_name in check_results["speed"]: logging.error("Check speed result with case \"%s\" failed." % case_name) @@ -217,4 +218,4 @@ if __name__ == "__main__": case_name, develop_result, pr_result, check_results ) - exit(summary_results(check_results, args.api_info_file)) + sys.exit(summary_results(check_results, args.api_info_file)) diff --git a/tools/check_ut.py b/tools/check_ut.py index 90cff366f4b..b91f762c33d 100644 --- a/tools/check_ut.py +++ b/tools/check_ut.py @@ -16,6 +16,7 @@ import os import os.path +import sys from github import Github @@ -36,11 +37,11 @@ class PRChecker: pr_id = os.getenv('GIT_PR_ID') if not pr_id: print('No PR ID') - exit(0) + sys.exit(0) print(pr_id) if not os.path.isfile(filename): print('No author to check') - exit(0) + sys.exit(0) self.repo = self.github.get_repo('PaddlePaddle/Paddle') pr = self.repo.get_pull(int(pr_id)) user = pr.user.login diff --git a/tools/continuous_integration/bisect.py b/tools/continuous_integration/bisect.py index b2a2aa1f9b6..bcfe3e90150 100644 --- a/tools/continuous_integration/bisect.py +++ b/tools/continuous_integration/bisect.py @@ -107,7 +107,7 @@ while True: if not commits: sys.stdout.write('no commits to bisect\n') - exit() + sys.exit() # checkout the picked branch. pick_idx = len(commits) / 2 pick = commits[pick_idx] @@ -129,7 +129,7 @@ while True: subprocess.check_output([cmd], shell=True) except subprocess.CalledProcessError as e: sys.stderr.write('failed to build commit: %s\n%s\n' % (pick, e)) - exit() + sys.exit() # test the selected branch. passed = True try: diff --git a/tools/coverage/coverage_diff.py b/tools/coverage/coverage_diff.py index 91fad291091..4ba3a6758e5 100644 --- a/tools/coverage/coverage_diff.py +++ b/tools/coverage/coverage_diff.py @@ -117,7 +117,7 @@ def get_info_file_lines(info_file, diff_file): if __name__ == '__main__': if len(sys.argv) < 3: - exit() + sys.exit() info_file = sys.argv[1] diff_file = sys.argv[2] diff --git a/tools/coverage/coverage_diff_list.py b/tools/coverage/coverage_diff_list.py index 127228d916c..7de3dd9be0d 100644 --- a/tools/coverage/coverage_diff_list.py +++ b/tools/coverage/coverage_diff_list.py @@ -58,7 +58,7 @@ def filter_by(list_file, max_rate): if __name__ == '__main__': if len(sys.argv) < 2: - exit() + sys.exit() list_file = sys.argv[1] max_rate = float(sys.argv[2]) diff --git a/tools/coverage/coverage_lines.py b/tools/coverage/coverage_lines.py index b3a7a73d00b..60df3d0c10a 100644 --- a/tools/coverage/coverage_lines.py +++ b/tools/coverage/coverage_lines.py @@ -47,21 +47,21 @@ def get_lines(info_file): if total == 0: print('no data found') - exit() + sys.exit() return hits / total if __name__ == '__main__': if len(sys.argv) < 3: - exit() + sys.exit() info_file = sys.argv[1] expected = float(sys.argv[2]) if not os.path.isfile(info_file): print('info file {} is not exists, ignored'.format(info_file)) - exit() + sys.exit() actual = get_lines(info_file) actual = round(actual, 3) @@ -73,7 +73,7 @@ if __name__ == '__main__': ) ) - exit(1) + sys.exit(1) print( 'expected >= {} %, actual {} %, passed'.format( diff --git a/tools/gen_ut_cmakelists.py b/tools/gen_ut_cmakelists.py index 648fde9bde7..99f9f064862 100644 --- a/tools/gen_ut_cmakelists.py +++ b/tools/gen_ut_cmakelists.py @@ -15,6 +15,7 @@ import argparse import os import re +import sys # port range (21200, 23000) is reserved for dist-ops @@ -556,7 +557,7 @@ class CMakeGenerator: print(e) print(f"[ERROR FILE]: {current_work_dir}/testslist.csv") print(f"[ERROR LINE {i+1}]: {line.strip()}") - exit(1) + sys.exit(1) for sub in sub_dirs: cmds += f"add_subdirectory({sub})\n" diff --git a/tools/get_pr_ut.py b/tools/get_pr_ut.py index 9fb14736351..3377bf9f7fc 100644 --- a/tools/get_pr_ut.py +++ b/tools/get_pr_ut.py @@ -19,6 +19,7 @@ import platform import re import ssl import subprocess +import sys import time import urllib.request @@ -52,7 +53,7 @@ class PRChecker: pr_id = os.getenv('GIT_PR_ID') if not pr_id: print('PREC No PR ID') - exit(0) + sys.exit(0) suffix = os.getenv('PREC_SUFFIX') if suffix: self.suffix = suffix @@ -311,7 +312,7 @@ class PRChecker: ) if not ret: print('PREC download file_ut.json failed') - exit(1) + sys.exit(1) with open('ut_file_map.json') as jsonfile: file_ut_map = json.load(jsonfile) @@ -371,7 +372,7 @@ class PRChecker: ut_list.append(ut.rstrip('\r\n')) else: print('PREC download prec_delta failed') - exit(1) + sys.exit(1) PRECISION_TEST_Cases_ratio = format( float(len(ut_list)) / float(self.get_all_count()), '.2f' ) @@ -484,7 +485,7 @@ class PRChecker: ut_list.append(ut.rstrip('\r\n')) else: print('PREC download prec_delta failed') - exit(1) + sys.exit(1) print("hitMapFiles: %s" % hitMapFiles) print("ipipe_log_param_PRECISION_TEST: true") print( diff --git a/tools/sampcd_processor.py b/tools/sampcd_processor.py index 4828685100d..747f2020f64 100644 --- a/tools/sampcd_processor.py +++ b/tools/sampcd_processor.py @@ -432,7 +432,7 @@ def execute_samplecode(tfname): else: logger.error("Error: fail to parse python version!") result = False - exit(1) + sys.exit(1) logger.info("----example code check----") logger.info("executing sample code: %s", tfname) @@ -724,7 +724,7 @@ if __name__ == '__main__': filenames = get_filenames(args.full_test) if len(filenames) == 0 and len(whl_error) == 0: logger.info("-----API_PR.spec is the same as API_DEV.spec-----") - exit(0) + sys.exit(0) logger.info("API_PR is diff from API_DEV: %s", filenames) threads = multiprocessing.cpu_count() @@ -760,7 +760,7 @@ if __name__ == '__main__': "In addition, mistakes found in sample codes: %s", temp[1] ) logger.info("----------------------------------------------------") - exit(1) + sys.exit(1) else: timeovered_test = {} for temp in result: @@ -810,7 +810,7 @@ if __name__ == '__main__': logger.info( "Mistakes found in sample codes. Please recheck the sample codes." ) - exit(1) + sys.exit(1) logger.info("Sample code check is successful!") diff --git a/tools/test_runner.py b/tools/test_runner.py index f06ccf79b9b..1a5da2da1df 100644 --- a/tools/test_runner.py +++ b/tools/test_runner.py @@ -63,7 +63,7 @@ def main(): paddle.disable_static() if some_test_failed: - exit(1) + sys.exit(1) if __name__ == '__main__': -- GitLab