Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/main'
Browse files Browse the repository at this point in the history
  • Loading branch information
SonicField committed May 9, 2024
2 parents 1242ecd + 82acc5f commit dc6b26b
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 78 deletions.
7 changes: 7 additions & 0 deletions Doc/whatsnew/3.14.rst
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,13 @@ Deprecated
Removed
=======

argparse
--------

* The *type*, *choices*, and *metavar* parameters
of :class:`!argparse.BooleanOptionalAction` are removed.
They were deprecated since 3.12.

email
-----

Expand Down
28 changes: 0 additions & 28 deletions Lib/argparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -831,19 +831,13 @@ def __call__(self, parser, namespace, values, option_string=None):
raise NotImplementedError(_('.__call__() not defined'))


# FIXME: remove together with `BooleanOptionalAction` deprecated arguments.
_deprecated_default = object()

class BooleanOptionalAction(Action):
def __init__(self,
option_strings,
dest,
default=None,
type=_deprecated_default,
choices=_deprecated_default,
required=False,
help=None,
metavar=_deprecated_default,
deprecated=False):

_option_strings = []
Expand All @@ -854,35 +848,13 @@ def __init__(self,
option_string = '--no-' + option_string[2:]
_option_strings.append(option_string)

# We need `_deprecated` special value to ban explicit arguments that
# match default value. Like:
# parser.add_argument('-f', action=BooleanOptionalAction, type=int)
for field_name in ('type', 'choices', 'metavar'):
if locals()[field_name] is not _deprecated_default:
import warnings
warnings._deprecated(
field_name,
"{name!r} is deprecated as of Python 3.12 and will be "
"removed in Python {remove}.",
remove=(3, 14))

if type is _deprecated_default:
type = None
if choices is _deprecated_default:
choices = None
if metavar is _deprecated_default:
metavar = None

super().__init__(
option_strings=_option_strings,
dest=dest,
nargs=0,
default=default,
type=type,
choices=choices,
required=required,
help=help,
metavar=metavar,
deprecated=deprecated)


Expand Down
43 changes: 0 additions & 43 deletions Lib/test/test_argparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -765,49 +765,6 @@ def test_const(self):

self.assertIn("got an unexpected keyword argument 'const'", str(cm.exception))

def test_deprecated_init_kw(self):
# See gh-92248
parser = argparse.ArgumentParser()

with self.assertWarns(DeprecationWarning):
parser.add_argument(
'-a',
action=argparse.BooleanOptionalAction,
type=None,
)
with self.assertWarns(DeprecationWarning):
parser.add_argument(
'-b',
action=argparse.BooleanOptionalAction,
type=bool,
)

with self.assertWarns(DeprecationWarning):
parser.add_argument(
'-c',
action=argparse.BooleanOptionalAction,
metavar=None,
)
with self.assertWarns(DeprecationWarning):
parser.add_argument(
'-d',
action=argparse.BooleanOptionalAction,
metavar='d',
)

with self.assertWarns(DeprecationWarning):
parser.add_argument(
'-e',
action=argparse.BooleanOptionalAction,
choices=None,
)
with self.assertWarns(DeprecationWarning):
parser.add_argument(
'-f',
action=argparse.BooleanOptionalAction,
choices=(),
)

class TestBooleanOptionalActionRequired(ParserTestCase):
"""Tests BooleanOptionalAction required"""

Expand Down
5 changes: 2 additions & 3 deletions Lib/test/test_os.py
Original file line number Diff line number Diff line change
Expand Up @@ -3226,9 +3226,8 @@ def test_stat_inaccessible_file(self):
self.skipTest("Unable to create inaccessible file")

def cleanup():
# Give delete permission. We are the file owner, so we can do this
# even though we removed all permissions earlier.
subprocess.check_output([ICACLS, filename, "/grant", "Everyone:(D)"],
# Give delete permission to the owner (us)
subprocess.check_output([ICACLS, filename, "/grant", "*WD:(D)"],
stderr=subprocess.STDOUT)
os.unlink(filename)

Expand Down
25 changes: 25 additions & 0 deletions Lib/test/test_trace.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from test.support.script_helper import assert_python_ok, assert_python_failure
import textwrap
import unittest
from types import FunctionType

import trace
from trace import Trace
Expand Down Expand Up @@ -559,5 +560,29 @@ def test_run_as_module(self):
assert_python_failure('-m', 'trace', '-l', '--module', 'not_a_module_zzz')


class TestTrace(unittest.TestCase):
def setUp(self):
self.addCleanup(sys.settrace, sys.gettrace())
self.tracer = Trace(count=0, trace=1)
self.filemod = my_file_and_modname()

def test_no_source_file(self):
filename = "<unknown>"
co = traced_func_linear.__code__
co = co.replace(co_filename=filename)
f = FunctionType(co, globals())

with captured_stdout() as out:
self.tracer.runfunc(f, 2, 3)

out = out.getvalue().splitlines()
firstlineno = get_firstlineno(f)
self.assertIn(f" --- modulename: {self.filemod[1]}, funcname: {f.__code__.co_name}", out[0])
self.assertIn(f"{filename}({firstlineno + 1})", out[1])
self.assertIn(f"{filename}({firstlineno + 2})", out[2])
self.assertIn(f"{filename}({firstlineno + 3})", out[3])
self.assertIn(f"{filename}({firstlineno + 4})", out[4])


if __name__ == '__main__':
unittest.main()
16 changes: 12 additions & 4 deletions Lib/trace.py
Original file line number Diff line number Diff line change
Expand Up @@ -565,8 +565,12 @@ def localtrace_trace_and_count(self, frame, why, arg):
if self.start_time:
print('%.2f' % (_time() - self.start_time), end=' ')
bname = os.path.basename(filename)
print("%s(%d): %s" % (bname, lineno,
linecache.getline(filename, lineno)), end='')
line = linecache.getline(filename, lineno)
print("%s(%d)" % (bname, lineno), end='')
if line:
print(": ", line, end='')
else:
print()
return self.localtrace

def localtrace_trace(self, frame, why, arg):
Expand All @@ -578,8 +582,12 @@ def localtrace_trace(self, frame, why, arg):
if self.start_time:
print('%.2f' % (_time() - self.start_time), end=' ')
bname = os.path.basename(filename)
print("%s(%d): %s" % (bname, lineno,
linecache.getline(filename, lineno)), end='')
line = linecache.getline(filename, lineno)
print("%s(%d)" % (bname, lineno), end='')
if line:
print(": ", line, end='')
else:
print()
return self.localtrace

def localtrace_count(self, frame, why, arg):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix lack of newline characters in :mod:`trace` module output when line tracing is enabled but source code line for current frame is not available.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Remove *type*, *choices*, and *metavar* parameters of
:class:`!argparse.BooleanOptionalAction`.
They were deprecated since Python 3.12.

0 comments on commit dc6b26b

Please sign in to comment.