Skip to content
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
2 changes: 1 addition & 1 deletion Doc/library/xmlrpc.client.rst
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,7 @@ Convenience Functions

.. function:: dumps(params, methodname=None, methodresponse=None, encoding=None, allow_none=False)

Convert *params* into an XML-RPC request. or into a response if *methodresponse*
Convert *params* into an XML-RPC request, or into a response if *methodresponse*
is true. *params* can be either a tuple of arguments or an instance of the
:exc:`Fault` exception class. If *methodresponse* is true, only a single value
can be returned, meaning that *params* must be of length 1. *encoding*, if
Expand Down
26 changes: 26 additions & 0 deletions Lib/test/libregrtest/save_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@

from .utils import print_warning

# Import termios to save and restore terminal echo. This is only available on
# Unix, and it's fine if the module can't be found.
try:
import termios # noqa: F401
except ModuleNotFoundError:
pass


class SkipTestEnvironment(Exception):
pass
Expand Down Expand Up @@ -65,6 +72,7 @@ def __init__(self, test_name, verbose, quiet, *, pgo):
'shutil_archive_formats', 'shutil_unpack_formats',
'asyncio.events._event_loop_policy',
'urllib.requests._url_tempfiles', 'urllib.requests._opener',
'stty_echo',
)

def get_module(self, name):
Expand Down Expand Up @@ -292,6 +300,24 @@ def restore_warnings_showwarning(self, fxn):
warnings = self.get_module('warnings')
warnings.showwarning = fxn

def get_stty_echo(self):
termios = self.try_get_module('termios')
if not os.isatty(fd := sys.__stdin__.fileno()):
return None
attrs = termios.tcgetattr(fd)
lflags = attrs[3]
return bool(lflags & termios.ECHO)
def restore_stty_echo(self, echo):
termios = self.get_module('termios')
attrs = termios.tcgetattr(fd := sys.__stdin__.fileno())
if echo:
# Turn echo on.
attrs[3] |= termios.ECHO
else:
# Turn echo off.
attrs[3] &= ~termios.ECHO
termios.tcsetattr(fd, termios.TCSADRAIN, attrs)

def resource_info(self):
for name in self.resources:
method_suffix = name.replace('.', '_')
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/pickletester.py
Original file line number Diff line number Diff line change
Expand Up @@ -1882,7 +1882,7 @@ def test_bad_newobj_ex_args(self):
with self.assertRaises(TypeError) as cm:
self.dumps(obj, proto)
self.assertEqual(str(cm.exception),
'functools.partial() argument after ** must be a mapping, not list')
'Value after ** must be a mapping, not list')
self.assertEqual(cm.exception.__notes__, [
'when serializing test.pickletester.REX object'])
else:
Expand Down
16 changes: 16 additions & 0 deletions Lib/test/test_builtin.py
Original file line number Diff line number Diff line change
Expand Up @@ -1385,6 +1385,22 @@ def test_map_strict(self):
self.assertRaises(ValueError, tuple,
map(pack, (1, 2), (1, 2), 'abc', strict=True))

# gh-140517: Testing refleaks with mortal objects.
t1 = (None, object())
t2 = (object(), object())
t3 = (object(),)

self.assertRaises(ValueError, tuple,
map(pack, t1, 'a', strict=True))
self.assertRaises(ValueError, tuple,
map(pack, t1, t2, 'a', strict=True))
self.assertRaises(ValueError, tuple,
map(pack, t1, t2, t3, strict=True))
self.assertRaises(ValueError, tuple,
map(pack, 'a', t1, strict=True))
self.assertRaises(ValueError, tuple,
map(pack, 'a', t2, t3, strict=True))

def test_map_strict_iterators(self):
x = iter(range(5))
y = [0]
Expand Down
28 changes: 13 additions & 15 deletions Lib/test/test_extcall.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@
>>> g(*Nothing())
Traceback (most recent call last):
...
TypeError: test.test_extcall.g() argument after * must be an iterable, not Nothing
TypeError: Value after * must be an iterable, not Nothing

>>> class Nothing:
... def __len__(self): return 5
Expand All @@ -146,7 +146,7 @@
>>> g(*Nothing())
Traceback (most recent call last):
...
TypeError: test.test_extcall.g() argument after * must be an iterable, not Nothing
TypeError: Value after * must be an iterable, not Nothing

>>> class Nothing():
... def __len__(self): return 5
Expand Down Expand Up @@ -266,7 +266,7 @@
>>> h(*h)
Traceback (most recent call last):
...
TypeError: test.test_extcall.h() argument after * must be an iterable, not function
TypeError: Value after * must be an iterable, not function

>>> h(1, *h)
Traceback (most recent call last):
Expand All @@ -281,55 +281,53 @@
>>> dir(*h)
Traceback (most recent call last):
...
TypeError: dir() argument after * must be an iterable, not function
TypeError: Value after * must be an iterable, not function

>>> nothing = None
>>> nothing(*h)
Traceback (most recent call last):
...
TypeError: None argument after * must be an iterable, \
not function
TypeError: Value after * must be an iterable, not function

>>> h(**h)
Traceback (most recent call last):
...
TypeError: test.test_extcall.h() argument after ** must be a mapping, not function
TypeError: Value after ** must be a mapping, not function

>>> h(**[])
Traceback (most recent call last):
...
TypeError: test.test_extcall.h() argument after ** must be a mapping, not list
TypeError: Value after ** must be a mapping, not list

>>> h(a=1, **h)
Traceback (most recent call last):
...
TypeError: test.test_extcall.h() argument after ** must be a mapping, not function
TypeError: Value after ** must be a mapping, not function

>>> h(a=1, **[])
Traceback (most recent call last):
...
TypeError: test.test_extcall.h() argument after ** must be a mapping, not list
TypeError: Value after ** must be a mapping, not list

>>> h(**{'a': 1}, **h)
Traceback (most recent call last):
...
TypeError: test.test_extcall.h() argument after ** must be a mapping, not function
TypeError: Value after ** must be a mapping, not function

>>> h(**{'a': 1}, **[])
Traceback (most recent call last):
...
TypeError: test.test_extcall.h() argument after ** must be a mapping, not list
TypeError: Value after ** must be a mapping, not list

>>> dir(**h)
Traceback (most recent call last):
...
TypeError: dir() argument after ** must be a mapping, not function
TypeError: Value after ** must be a mapping, not function

>>> nothing(**h)
Traceback (most recent call last):
...
TypeError: None argument after ** must be a mapping, \
not function
TypeError: Value after ** must be a mapping, not function

>>> dir(b=1, **{'b': 1})
Traceback (most recent call last):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Errors when calling functions with invalid values after ``*`` and ``**`` now do not
include the function name. Patch by Ilia Solin.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fixed a reference leak when iterating over the result of :func:`map`
with ``strict=True`` when the input iterables have different lengths.
Patch by Mikhail Efimov.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Preserve and restore the state of ``stty echo`` as part of the test environment.
44 changes: 25 additions & 19 deletions Python/bltinmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -1501,66 +1501,72 @@ map_next(PyObject *self)
}

Py_ssize_t nargs = 0;
for (i=0; i < niters; i++) {
for (i = 0; i < niters; i++) {
PyObject *it = PyTuple_GET_ITEM(lz->iters, i);
PyObject *val = Py_TYPE(it)->tp_iternext(it);
if (val == NULL) {
if (lz->strict) {
goto check;
}
goto exit;
goto exit_no_result;
}
stack[i] = val;
nargs++;
}

result = _PyObject_VectorcallTstate(tstate, lz->func, stack, nargs, NULL);
goto exit;

exit:
for (i=0; i < nargs; i++) {
Py_DECREF(stack[i]);
}
if (stack != small_stack) {
PyMem_Free(stack);
}
return result;
check:
if (PyErr_Occurred()) {
if (!PyErr_ExceptionMatches(PyExc_StopIteration)) {
// next() on argument i raised an exception (not StopIteration)
return NULL;
goto exit_no_result;
}
PyErr_Clear();
}
if (i) {
// ValueError: map() argument 2 is shorter than argument 1
// ValueError: map() argument 3 is shorter than arguments 1-2
const char* plural = i == 1 ? " " : "s 1-";
return PyErr_Format(PyExc_ValueError,
"map() argument %d is shorter than argument%s%d",
i + 1, plural, i);
PyErr_Format(PyExc_ValueError,
"map() argument %d is shorter than argument%s%d",
i + 1, plural, i);
goto exit_no_result;
}
for (i = 1; i < niters; i++) {
PyObject *it = PyTuple_GET_ITEM(lz->iters, i);
PyObject *val = (*Py_TYPE(it)->tp_iternext)(it);
if (val) {
Py_DECREF(val);
const char* plural = i == 1 ? " " : "s 1-";
return PyErr_Format(PyExc_ValueError,
"map() argument %d is longer than argument%s%d",
i + 1, plural, i);
PyErr_Format(PyExc_ValueError,
"map() argument %d is longer than argument%s%d",
i + 1, plural, i);
goto exit_no_result;
}
if (PyErr_Occurred()) {
if (!PyErr_ExceptionMatches(PyExc_StopIteration)) {
// next() on argument i raised an exception (not StopIteration)
return NULL;
goto exit_no_result;
}
PyErr_Clear();
}
// Argument i is exhausted. So far so good...
}
// All arguments are exhausted. Success!
goto exit;

exit_no_result:
assert(result == NULL);

exit:
for (i = 0; i < nargs; i++) {
Py_DECREF(stack[i]);
}
if (stack != small_stack) {
PyMem_Free(stack);
}
return result;
}

static PyObject *
Expand Down
27 changes: 7 additions & 20 deletions Python/ceval.c
Original file line number Diff line number Diff line change
Expand Up @@ -3272,17 +3272,9 @@ int
_Py_Check_ArgsIterable(PyThreadState *tstate, PyObject *func, PyObject *args)
{
if (Py_TYPE(args)->tp_iter == NULL && !PySequence_Check(args)) {
/* _Py_Check_ArgsIterable() may be called with a live exception:
* clear it to prevent calling _PyObject_FunctionStr() with an
* exception set. */
_PyErr_Clear(tstate);
PyObject *funcstr = _PyObject_FunctionStr(func);
if (funcstr != NULL) {
_PyErr_Format(tstate, PyExc_TypeError,
"%U argument after * must be an iterable, not %.200s",
funcstr, Py_TYPE(args)->tp_name);
Py_DECREF(funcstr);
}
_PyErr_Format(tstate, PyExc_TypeError,
"Value after * must be an iterable, not %.200s",
Py_TYPE(args)->tp_name);
return -1;
}
return 0;
Expand All @@ -3298,15 +3290,10 @@ _PyEval_FormatKwargsError(PyThreadState *tstate, PyObject *func, PyObject *kwarg
* is not a mapping.
*/
if (_PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) {
_PyErr_Clear(tstate);
PyObject *funcstr = _PyObject_FunctionStr(func);
if (funcstr != NULL) {
_PyErr_Format(
tstate, PyExc_TypeError,
"%U argument after ** must be a mapping, not %.200s",
funcstr, Py_TYPE(kwargs)->tp_name);
Py_DECREF(funcstr);
}
_PyErr_Format(
tstate, PyExc_TypeError,
"Value after ** must be a mapping, not %.200s",
Py_TYPE(kwargs)->tp_name);
}
else if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
PyObject *exc = _PyErr_GetRaisedException(tstate);
Expand Down
Loading