Skip to content

Commit a2d0583

Browse files
oprypincopybara-github
authored andcommitted
Clean up some references to older Python versions
* Remove comments about Python 2 * Consistently use the arg `subprocess.Popen(text=...)`, also use `subprocess.Popen(encoding=...)` in one applicable place * Drop some code supporting Python < 3.8 PiperOrigin-RevId: 766642814
1 parent 55c8f4d commit a2d0583

10 files changed

+46
-82
lines changed

absl/logging/__init__.py

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -721,23 +721,21 @@ def find_log_dir_and_names(program_name=None, log_dir=None):
721721
"""Computes the directory and filename prefix for log file.
722722
723723
Args:
724-
program_name: str|None, the filename part of the path to the program that
725-
is running without its extension. e.g: if your program is called
726-
``usr/bin/foobar.py`` this method should probably be called with
727-
``program_name='foobar`` However, this is just a convention, you can
728-
pass in any string you want, and it will be used as part of the
729-
log filename. If you don't pass in anything, the default behavior
730-
is as described in the example. In python standard logging mode,
731-
the program_name will be prepended with ``py_`` if it is the
732-
``program_name`` argument is omitted.
724+
program_name: str|None, the filename part of the path to the program that is
725+
running without its extension. e.g: if your program is called
726+
``usr/bin/foobar.py`` this method should probably be called with
727+
``program_name='foobar`` However, this is just a convention, you can pass
728+
in any string you want, and it will be used as part of the log filename.
729+
If you don't pass in anything, the default behavior is as described in the
730+
example. In python standard logging mode, the program_name will be
731+
prepended with ``py_`` if it is the ``program_name`` argument is omitted.
733732
log_dir: str|None, the desired log directory.
734733
735734
Returns:
736735
(log_dir, file_prefix, symlink_prefix)
737736
738737
Raises:
739-
FileNotFoundError: raised in Python 3 when it cannot find a log directory.
740-
OSError: raised in Python 2 when it cannot find a log directory.
738+
FileNotFoundError: raised when it cannot find a log directory.
741739
"""
742740
if not program_name:
743741
# Strip the extension (foobar.par becomes foobar, and
@@ -771,13 +769,12 @@ def find_log_dir(log_dir=None):
771769
772770
Args:
773771
log_dir: str|None, if specified, the logfile(s) will be created in that
774-
directory. Otherwise if the --log_dir command-line flag is provided,
775-
the logfile will be created in that directory. Otherwise the logfile
776-
will be created in a standard location.
772+
directory. Otherwise if the --log_dir command-line flag is provided, the
773+
logfile will be created in that directory. Otherwise the logfile will be
774+
created in a standard location.
777775
778776
Raises:
779-
FileNotFoundError: raised in Python 3 when it cannot find a log directory.
780-
OSError: raised in Python 2 when it cannot find a log directory.
777+
FileNotFoundError: raised when it cannot find a log directory.
781778
"""
782779
# Get a list of possible log dirs (will try to use them in order).
783780
# NOTE: Google's internal implementation has a special handling for Google

absl/logging/__init__.pyi

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -35,54 +35,42 @@ LOGGER_LEVELS: flags.FlagHolder[Dict[str, str]]
3535
STDERRTHRESHOLD: flags.FlagHolder[str]
3636
SHOWPREFIXFORINFO: flags.FlagHolder[bool]
3737

38-
3938
def get_verbosity() -> int:
4039
...
4140

42-
4341
def set_verbosity(v: Union[int, str]) -> None:
4442
...
4543

46-
4744
def set_stderrthreshold(s: Union[int, str]) -> None:
4845
...
4946

50-
5147
# TODO(b/277607978): Provide actual args+kwargs shadowing stdlib's logging functions.
5248
def fatal(msg: Any, *args: Any, **kwargs: Any) -> NoReturn:
5349
...
5450

55-
5651
def error(msg: Any, *args: Any, **kwargs: Any) -> None:
5752
...
5853

59-
6054
def warning(msg: Any, *args: Any, **kwargs: Any) -> None:
6155
...
6256

63-
6457
def warn(msg: Any, *args: Any, **kwargs: Any) -> None:
6558
...
6659

67-
6860
def info(msg: Any, *args: Any, **kwargs: Any) -> None:
6961
...
7062

71-
7263
def debug(msg: Any, *args: Any, **kwargs: Any) -> None:
7364
...
7465

75-
7666
def exception(msg: Any, *args: Any, **kwargs: Any) -> None:
7767
...
7868

79-
8069
def log_every_n(
8170
level: int, msg: Any, n: int, *args: Any, use_call_stack: bool = ...
8271
) -> None:
8372
...
8473

85-
8674
def log_every_n_seconds(
8775
level: int,
8876
msg: Any,
@@ -92,79 +80,61 @@ def log_every_n_seconds(
9280
) -> None:
9381
...
9482

95-
9683
def log_first_n(
9784
level: int, msg: Any, n: int, *args: Any, use_call_stack: bool = ...
9885
) -> None:
9986
...
10087

101-
10288
def log_if(level: int, msg: Any, condition: Any, *args: Any) -> None:
10389
...
10490

105-
10691
def log(level: int, msg: Any, *args: Any, **kwargs: Any) -> None:
10792
...
10893

109-
11094
def vlog(level: int, msg: Any, *args: Any, **kwargs: Any) -> None:
11195
...
11296

113-
11497
def vlog_is_on(level: int) -> bool:
11598
...
11699

117-
118100
def flush() -> None:
119101
...
120102

121-
122103
def level_debug() -> bool:
123104
...
124105

125-
126106
def level_info() -> bool:
127107
...
128108

129-
130109
def level_warning() -> bool:
131110
...
132111

133-
134112
level_warn = level_warning # Deprecated function.
135113

136-
137114
def level_error() -> bool:
138115
...
139116

140-
141117
def get_log_file_name(level: int = ...) -> str:
142118
...
143119

144-
145120
def find_log_dir_and_names(
146121
program_name: Optional[str] = ..., log_dir: Optional[str] = ...
147122
) -> Tuple[str, str, str]:
148123
...
149124

150-
151125
def find_log_dir(log_dir: Optional[str] = ...) -> str:
152126
...
153127

154-
155128
def get_absl_log_prefix(record: logging.LogRecord) -> str:
156129
...
157130

158-
159131
_SkipLogT = TypeVar('_SkipLogT', str, Callable[..., Any])
160132

161133
def skip_log_prefix(func: _SkipLogT) -> _SkipLogT:
162134
...
163135

164-
165136
_StreamT = TypeVar('_StreamT')
166137

167-
168138
class PythonHandler(logging.StreamHandler[_StreamT]): # type: ignore[type-var]
169139

170140
def __init__(
@@ -193,7 +163,6 @@ class PythonHandler(logging.StreamHandler[_StreamT]): # type: ignore[type-var]
193163
def close(self) -> None:
194164
...
195165

196-
197166
class ABSLHandler(logging.Handler):
198167

199168
def __init__(self, python_logging_formatter: PythonFormatter) -> None:
@@ -232,13 +201,11 @@ class ABSLHandler(logging.Handler):
232201
def start_logging_to_file(self, program_name=None, log_dir=None) -> None:
233202
...
234203

235-
236204
class PythonFormatter(logging.Formatter):
237205

238206
def format(self, record: logging.LogRecord) -> str:
239207
...
240208

241-
242209
class ABSLLogger(logging.Logger):
243210

244211
def findCaller(
@@ -279,20 +246,16 @@ class ABSLLogger(logging.Logger):
279246
) -> None:
280247
...
281248

282-
283249
# NOTE: Returns None before _initialize called but shouldn't occur after import.
284250
def get_absl_logger() -> ABSLLogger:
285251
...
286252

287-
288253
# NOTE: Returns None before _initialize called but shouldn't occur after import.
289254
def get_absl_handler() -> ABSLHandler:
290255
...
291256

292-
293257
def use_python_logging(quiet: bool = ...) -> None:
294258
...
295259

296-
297260
def use_absl_handler() -> None:
298261
...

absl/logging/tests/logging_functional_test.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -388,8 +388,12 @@ def _exec_test(self,
388388
print('env: %s' % env, file=sys.stderr)
389389
print('cmd: %s' % cmd, file=sys.stderr)
390390
process = subprocess.Popen(
391-
cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, env=env,
392-
universal_newlines=True)
391+
cmd,
392+
stdout=subprocess.PIPE,
393+
stderr=subprocess.STDOUT,
394+
env=env,
395+
text=True,
396+
)
393397
output, _ = process.communicate()
394398
status = process.returncode
395399

absl/testing/absltest.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -278,9 +278,8 @@ class _TempDir:
278278
279279
Creation of this class is internal. Using its public methods is OK.
280280
281-
This class implements the `os.PathLike` interface (specifically,
282-
`os.PathLike[str]`). This means, in Python 3, it can be directly passed
283-
to e.g. `os.path.join()`.
281+
This class implements the `os.PathLike[str]` interface. This means it can
282+
be directly passed to e.g. `os.path.join()`.
284283
"""
285284

286285
def __init__(self, path: str) -> None:
@@ -361,9 +360,8 @@ class _TempFile:
361360
362361
Creation of this class is internal. Using its public methods is OK.
363362
364-
This class implements the `os.PathLike` interface (specifically,
365-
`os.PathLike[str]`). This means, in Python 3, it can be directly passed
366-
to e.g. `os.path.join()`.
363+
This class implements the `os.PathLike[str]` interface. This means it can
364+
be directly passed to e.g. `os.path.join()`.
367365
"""
368366

369367
def __init__(self, path: str) -> None:
@@ -443,9 +441,7 @@ def write_text(
443441
"""Write text to the file.
444442
445443
Args:
446-
text: Text to write. In Python 2, it can be bytes, which will be
447-
decoded using the `encoding` arg (this is as an aid for code that
448-
is 2 and 3 compatible).
444+
text: Text to write.
449445
mode: The mode to open the file for writing.
450446
encoding: The encoding to use when writing the text to the file.
451447
errors: The error handling strategy to use when converting text to bytes.

absl/testing/tests/absltest_fail_fast_test.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ def _run_fail_fast(self, fail_fast, use_app_run):
5858
env=env,
5959
stdout=subprocess.PIPE,
6060
stderr=subprocess.STDOUT,
61-
universal_newlines=True)
61+
text=True,
62+
)
6263
stdout = proc.communicate()[0]
6364

6465
logging.info('output: %s', stdout)

absl/testing/tests/absltest_filtering_test.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,15 @@ def _run_filtered(self, test_filter, use_env_variable, use_app_run):
6565
additional_args.extend(['-k=' + f for f in test_filter.split(' ')])
6666

6767
proc = subprocess.Popen(
68-
args=([_bazelize_command.get_executable_path(self._test_name)] +
69-
additional_args),
68+
args=(
69+
[_bazelize_command.get_executable_path(self._test_name)]
70+
+ additional_args
71+
),
7072
env=env,
7173
stdout=subprocess.PIPE,
7274
stderr=subprocess.STDOUT,
73-
universal_newlines=True)
75+
text=True,
76+
)
7477
stdout = proc.communicate()[0]
7578

7679
logging.info('output: %s', stdout)

absl/testing/tests/absltest_randomization_test.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ def _run_test(self, extra_argv, extra_env):
6060
env=env,
6161
stdout=subprocess.PIPE,
6262
stderr=subprocess.STDOUT,
63-
universal_newlines=True)
63+
text=True,
64+
)
6465

6566
stdout, _ = proc.communicate()
6667

absl/testing/tests/absltest_sharding_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ def _run_sharded(
8383
env=env,
8484
stdout=subprocess.PIPE,
8585
stderr=subprocess.STDOUT,
86-
universal_newlines=True,
86+
text=True,
8787
)
8888
stdout = proc.communicate()[0]
8989

absl/testing/tests/absltest_test.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,12 @@ def run_helper(
8080
command.append(f'--test_id={test_id}')
8181
command.extend(args)
8282
process = subprocess.Popen(
83-
command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=env,
84-
universal_newlines=True)
83+
command,
84+
stdout=subprocess.PIPE,
85+
stderr=subprocess.PIPE,
86+
env=env,
87+
text=True,
88+
)
8589
stdout, stderr = process.communicate()
8690
if expect_success:
8791
self.assertEqual(

absl/tests/app_test.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,6 @@ def patch_main_module_docstring(docstring):
4747
sys.modules['__main__'].__doc__ = old_doc
4848

4949

50-
def _normalize_newlines(s):
51-
return re.sub('(\r\n)|\r', '\n', s)
52-
53-
5450
class UnitTests(absltest.TestCase):
5551

5652
def test_install_exception_handler(self):
@@ -141,13 +137,12 @@ def run_helper(self, expect_success,
141137
process = subprocess.Popen(
142138
[_bazelize_command.get_executable_path(helper)] + list(arguments),
143139
stdout=subprocess.PIPE,
144-
stderr=subprocess.PIPE, env=env, universal_newlines=False)
140+
stderr=subprocess.PIPE,
141+
env=env,
142+
text=True,
143+
encoding='utf8',
144+
)
145145
stdout, stderr = process.communicate()
146-
# In Python 2, we can't control the encoding used by universal_newline
147-
# mode, which can cause UnicodeDecodeErrors when subprocess tries to
148-
# convert the bytes to unicode, so we have to decode it manually.
149-
stdout = _normalize_newlines(stdout.decode('utf8'))
150-
stderr = _normalize_newlines(stderr.decode('utf8'))
151146

152147
message = (
153148
'Command: {command}\n'

0 commit comments

Comments
 (0)