From cb2b7b1e57e965493e14f465de869a78f50967cb Mon Sep 17 00:00:00 2001 From: Alex Hedges Date: Tue, 21 Jun 2022 01:12:21 +0000 Subject: [PATCH] Fix code that triggers BytesWarning (#79868) Fixes #74812. I have fixed the multiple instances in the repository that trigger `BytesWarning`, and I have enabled the `-bb` option when tests are run to prevent regressions. Pull Request resolved: https://github.com/pytorch/pytorch/pull/79868 Approved by: https://github.com/janeyx99 --- test/run_test.py | 2 +- test/test_nn.py | 3 ++- test/test_torch.py | 4 ++-- torch/serialization.py | 2 +- 4 files changed, 6 insertions(+), 5 deletions(-) diff --git a/test/run_test.py b/test/run_test.py index 6abddc7ab7d..815c7de79e9 100644 --- a/test/run_test.py +++ b/test/run_test.py @@ -332,7 +332,7 @@ def get_executable_command(options, allow_pytest, disable_coverage=False): if options.coverage and not disable_coverage: executable = ["coverage", "run", "--parallel-mode", "--source=torch"] else: - executable = [sys.executable] + executable = [sys.executable, "-bb"] if options.pytest: if allow_pytest: executable += ["-m", "pytest"] diff --git a/test/test_nn.py b/test/test_nn.py index d59acb2e5b0..12b5f615406 100644 --- a/test/test_nn.py +++ b/test/test_nn.py @@ -15157,9 +15157,10 @@ torch.cuda.synchronize() [sys.executable, '-c', script], cwd=os.path.dirname(os.path.realpath(__file__)), capture_output=True, + text=True, ) - output = str(p.stdout) + '\n' + str(p.stderr) + output = p.stdout + '\n' + p.stderr error_msg = error_msgs[module_name] diff --git a/test/test_torch.py b/test/test_torch.py index dfc38f2ae1c..265bff919aa 100644 --- a/test/test_torch.py +++ b/test/test_torch.py @@ -748,7 +748,7 @@ class TestTorchDeviceType(TestCase): # Checks for cpp context in the warning message escaped_warning_message = str(warning.message).encode('unicode_escape') - self.assertTrue(re.search(s, str(escaped_warning_message), re.IGNORECASE) is not None) + self.assertTrue(re.search(s, repr(escaped_warning_message), re.IGNORECASE) is not None) # Checks the Python features of the warning # Note: the eager mode warning refers to the line in the function @@ -764,7 +764,7 @@ class TestTorchDeviceType(TestCase): # Checks for cpp context in the warning message escaped_warning_message = str(warning.message).encode('unicode_escape') - self.assertTrue(re.search(s, str(escaped_warning_message), re.IGNORECASE) is not None) + self.assertTrue(re.search(s, repr(escaped_warning_message), re.IGNORECASE) is not None) # Checks the Python features of the warning # Note: the jitted warning's lineno refers to the call to the jitted diff --git a/torch/serialization.py b/torch/serialization.py index 8262b9666d3..b4a3bad4d3f 100644 --- a/torch/serialization.py +++ b/torch/serialization.py @@ -56,7 +56,7 @@ def _is_zipfile(f) -> bool: start = f.tell() byte = f.read(1) - while byte != "": + while byte != b"": read_bytes.append(byte) if len(read_bytes) == 4: break