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
98 changes: 98 additions & 0 deletions Lib/test/test_free_threading/test_functions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import random
import unittest
from unittest import TestCase

from test.support import threading_helper

threading_helper.requires_working_threading(module=True)

NUM_THREADS = 8
ITERS = 200


def random_string():
return ''.join(random.choice('0123456789ABCDEF') for _ in range(10))


def template_a(): pass
def template_b(): pass


class TestFTFunctionAttributes(TestCase):

def stress_attribute(self, attr, make_value):
def target(x=1):
return x

def writer():
for _ in range(ITERS):
setattr(target, attr, make_value())
getattr(target, attr)

threading_helper.run_concurrently(writer, NUM_THREADS)

def test_name(self):
self.stress_attribute("__name__", random_string)

def test_qualname(self):
self.stress_attribute("__qualname__", random_string)

def test_code(self):
codes = (template_a.__code__, template_b.__code__)
self.stress_attribute("__code__", lambda: random.choice(codes))

def test_defaults(self):
self.stress_attribute("__defaults__", lambda: (random_string(),))

def test_kwdefaults(self):
self.stress_attribute("__kwdefaults__", lambda: {"x": random_string()})

def test_annotations(self):
self.stress_attribute("__annotations__",
lambda: {"x": random_string()})

def test_annotate(self):
self.stress_attribute("__annotate__",
lambda: (lambda format: {"x": str}))

def test_type_params(self):
self.stress_attribute("__type_params__", lambda: (random_string(),))

def test_annotations_and_annotate(self):
# The __annotations__ and __annotate__ setters clear each other.
def target(): pass

def set_annotations():
for _ in range(ITERS):
target.__annotations__ = {"x": random_string()}
target.__annotations__

def set_annotate():
for _ in range(ITERS):
target.__annotate__ = lambda format: {"x": str}
target.__annotate__

threading_helper.run_concurrently(
[set_annotations, set_annotate] * (NUM_THREADS // 2))

def test_call_while_replacing_defaults(self):
# The eval loop reads __defaults__ and __kwdefaults__ without holding
# a lock while pushing a frame.
def target(x="init", *, y="init"):
return x, y

def writer():
for _ in range(ITERS):
target.__defaults__ = (random_string(),)
target.__kwdefaults__ = {"y": random_string()}

def caller():
for _ in range(ITERS):
target()

threading_helper.run_concurrently(
[writer, caller] * (NUM_THREADS // 2))


if __name__ == "__main__":
unittest.main()
31 changes: 31 additions & 0 deletions Lib/test/test_xml_etree.py
Original file line number Diff line number Diff line change
Expand Up @@ -3477,6 +3477,37 @@ def test_find_xpath(self):
self.assertRaisesRegex(SyntaxError, 'XPath', e.find, './tag[last()-0]')
self.assertRaisesRegex(SyntaxError, 'XPath', e.find, './tag[last()+1]')

def test_find_xpath_index_no_quadratic_complexity(self):
class CountingElement(ET.Element):
findall_calls = 0
def findall(self, *args, **kwargs):
type(self).findall_calls += 1
return super().findall(*args, **kwargs)

def work(n, pattern):
root = CountingElement("root")
for _ in range(n):
ET.SubElement(root, "a")
CountingElement.findall_calls = 0
root.findall(pattern)
return CountingElement.findall_calls

for pattern in [".//a[1]", ".//a[last()]"]:
w1 = work(1024, pattern)
w2 = work(2048, pattern)
w3 = work(4096, pattern)

self.assertGreater(w1, 0)
r1 = w2 / w1
r2 = w3 / w2
# Doubling N must not ~double the parent.findall calls.
# Linear-in-N call counts indicate the cache is missing.
self.assertLess(
max(r1, r2), 1.5,
msg=f"Possible quadratic behavior on {pattern!r}: "
f"calls={w1, w2, w3} ratios={r1, r2}",
)

def test_findall(self):
e = ET.XML(SAMPLE_XML)
e[2] = ET.XML(SAMPLE_SECTION)
Expand Down
17 changes: 12 additions & 5 deletions Lib/xml/etree/ElementPath.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,15 +324,22 @@ def select_negated(context, result):
index = -1
def select(context, result):
parent_map = get_parent_map(context)
cache = {}
for elem in result:
try:
parent = parent_map[elem]
except KeyError:
continue
key = (parent, elem.tag)
if key not in cache:
# FIXME: what if the selector is "*" ?
elems = list(parent.findall(elem.tag))
if elems[index] is elem:
yield elem
except (IndexError, KeyError):
pass
elems = parent.findall(elem.tag)
try:
cache[key] = elems[index]
except IndexError:
cache[key] = None
if cache[key] is elem:
yield elem
return select
raise SyntaxError("invalid predicate")

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix data races when setting attributes of function objects
on the :term:`free threaded <free threading>` build.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix :func:`os.sendfile` on illumos:
it no longer reports a successful transfer
when the underlying system call failed without writing any data.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
The :class:`xml.etree.ElementTree.Element` methods
:meth:`~xml.etree.ElementTree.Element.findall`,
:meth:`~xml.etree.ElementTree.Element.iterfind` and
:meth:`~xml.etree.ElementTree.Element.find` avoid quadratic behavior when
using XPath index predicates (``[1]``, ``[last()]``, ``[last()-N]``) on XML
documents with many same-tag siblings.
38 changes: 23 additions & 15 deletions Modules/posixmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -12614,27 +12614,35 @@ os_sendfile_impl(PyObject *module, int out_fd, int in_fd, PyObject *offobj,
return PyLong_FromLong(0);
}

// On illumos specifically sendfile() may perform a partial write but
// return -1/an error (in one confirmed case the destination socket
// had a 5 second timeout set and errno was EAGAIN) and it's on the client
// code to check if the offset parameter was modified by sendfile().
//
// We need this variable to track said change.
off_t original_offset = offset;
#endif
// sendfile() may perform a partial write and still return -1, so the
// number of transferred bytes must be taken from the out parameter.
// sendfile() reports it by adding it to the offset, but does not
// initialize it when the transfer fails before writing any data, so use
// sendfilev(), which reports it explicitly.
sendfilevec_t vec;
size_t xferred;

vec.sfv_fd = in_fd;
vec.sfv_flag = 0;
vec.sfv_off = offset;
vec.sfv_len = count;

do {
Py_BEGIN_ALLOW_THREADS
ret = sendfile(out_fd, in_fd, &offset, count);
#if defined(__sun) && defined(__SVR4)
// This handles illumos-specific sendfile() partial write behavior,
// see a comment above for more details.
if (ret < 0 && offset != original_offset) {
ret = offset - original_offset;
xferred = 0;
ret = sendfilev(out_fd, &vec, 1, &xferred);
if (ret < 0 && xferred != 0) {
ret = (Py_ssize_t)xferred;
}
#endif
Py_END_ALLOW_THREADS
} while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
#else
do {
Py_BEGIN_ALLOW_THREADS
ret = sendfile(out_fd, in_fd, &offset, count);
Py_END_ALLOW_THREADS
} while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
#endif
if (ret < 0)
return (!async_err) ? posix_error() : NULL;
return PyLong_FromSsize_t(ret);
Expand Down
Loading
Loading