Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@
import unittest
import inspect
from threading import Barrier
from unittest import TestCase

from test.support import threading_helper, Py_GIL_DISABLED
from test.support import threading_helper

threading_helper.requires_working_threading(module=True)

Expand All @@ -25,11 +24,48 @@ def set_func_annotation(f, b):
return f.__annotations__


@unittest.skipUnless(Py_GIL_DISABLED, "Enable only in FT build")
class TestFTFuncAnnotations(TestCase):
class TestFunction(unittest.TestCase):
NUM_THREADS = 4

def test_concurrent_read(self):
def test_name_attribute_race(self):
# gh-153297
def shared_func():
return 1

def setter():
for i in range(2000):
shared_func.__name__ = "q_%d_%d" % (id(i), i & 15)

def reader():
for _ in range(2000):
_ = shared_func.__name__
repr(shared_func)

threading_helper.run_concurrently([
*[setter for _ in range(6)],
*[reader for _ in range(4)],
])

def test_qualname_attribute_race(self):
# gh-153297
def shared_func():
return 1

def setter():
for i in range(2000):
shared_func.__qualname__ = "q_%d_%d" % (id(i), i & 15)

def reader():
for _ in range(2000):
_ = shared_func.__qualname__
repr(shared_func)

threading_helper.run_concurrently([
*[setter for _ in range(6)],
*[reader for _ in range(4)],
])

def test_concurrent_read_annotations(self):
def f(x: int) -> int:
return x + 1

Expand All @@ -50,7 +86,7 @@ def f(x: int) -> int:
self.assertIsNotNone(annotate)
self.assertEqual(annotate, {'x': int, 'return': int})

def test_concurrent_write(self):
def test_concurrent_write_annotations(self):
def bar(x: int, y: float) -> float:
return y ** x

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix data race on assigning ``__name__`` and ``__qualname__`` attrs to
function objects on FT builds.
102 changes: 101 additions & 1 deletion Objects/clinic/funcobject.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

62 changes: 45 additions & 17 deletions Objects/funcobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -690,48 +690,72 @@ func_set_code(PyObject *self, PyObject *value, void *Py_UNUSED(ignored))
return 0;
}

/*[clinic input]
@critical_section
@getter
function.__name__
[clinic start generated code]*/

static PyObject *
func_get_name(PyObject *self, void *Py_UNUSED(ignored))
function___name___get_impl(PyFunctionObject *self)
/*[clinic end generated code: output=436852c5b4f6d259 input=3f1d1304d3fd103b]*/
{
PyFunctionObject *op = _PyFunction_CAST(self);
return Py_NewRef(op->func_name);
return Py_NewRef(self->func_name);
}

/*[clinic input]
@critical_section
@setter
function.__name__
[clinic start generated code]*/

static int
func_set_name(PyObject *self, PyObject *value, void *Py_UNUSED(ignored))
function___name___set_impl(PyFunctionObject *self, PyObject *value)
/*[clinic end generated code: output=2c571635b003b9cc input=705634aafaa00198]*/
{
PyFunctionObject *op = _PyFunction_CAST(self);
/* Not legal to del f.func_name or to set it to anything
* other than a string object. */
if (value == NULL || !PyUnicode_Check(value)) {
PyErr_SetString(PyExc_TypeError,
"__name__ must be set to a string object");
return -1;
}
Py_XSETREF(op->func_name, Py_NewRef(value));
Py_XSETREF(self->func_name, Py_NewRef(value));
return 0;
}

/*[clinic input]
@critical_section
@getter
function.__qualname__
[clinic start generated code]*/

static PyObject *
func_get_qualname(PyObject *self, void *Py_UNUSED(ignored))
function___qualname___get_impl(PyFunctionObject *self)
/*[clinic end generated code: output=8fbd60e64464da5f input=761d5ab45fc13493]*/
{
PyFunctionObject *op = _PyFunction_CAST(self);
return Py_NewRef(op->func_qualname);
return Py_NewRef(self->func_qualname);
}

/*[clinic input]
@critical_section
@setter
function.__qualname__
[clinic start generated code]*/

static int
func_set_qualname(PyObject *self, PyObject *value, void *Py_UNUSED(ignored))
function___qualname___set_impl(PyFunctionObject *self, PyObject *value)
/*[clinic end generated code: output=4cc5e270fd55f139 input=c803ac4dfdf04c87]*/
{
PyFunctionObject *op = _PyFunction_CAST(self);
/* Not legal to del f.__qualname__ or to set it to anything
* other than a string object. */
if (value == NULL || !PyUnicode_Check(value)) {
PyErr_SetString(PyExc_TypeError,
"__qualname__ must be set to a string object");
return -1;
}
handle_func_event(PyFunction_EVENT_MODIFY_QUALNAME, (PyFunctionObject *) op, value);
Py_XSETREF(op->func_qualname, Py_NewRef(value));
handle_func_event(PyFunction_EVENT_MODIFY_QUALNAME, self, value);
Py_XSETREF(self->func_qualname, Py_NewRef(value));
return 0;
}

Expand Down Expand Up @@ -976,8 +1000,8 @@ static PyGetSetDef func_getsetlist[] = {
FUNCTION___ANNOTATIONS___GETSETDEF
FUNCTION___ANNOTATE___GETSETDEF
{"__dict__", PyObject_GenericGetDict, PyObject_GenericSetDict},
{"__name__", func_get_name, func_set_name},
{"__qualname__", func_get_qualname, func_set_qualname},
FUNCTION___NAME___GETSETDEF
FUNCTION___QUALNAME___GETSETDEF
FUNCTION___TYPE_PARAMS___GETSETDEF
{NULL} /* Sentinel */
};
Expand Down Expand Up @@ -1147,8 +1171,12 @@ static PyObject*
func_repr(PyObject *self)
{
PyFunctionObject *op = _PyFunction_CAST(self);
return PyUnicode_FromFormat("<function %U at %p>",
op->func_qualname, op);
PyObject *result;
Py_BEGIN_CRITICAL_SECTION(self);
result = PyUnicode_FromFormat("<function %U at %p>",
op->func_qualname, op);
Py_END_CRITICAL_SECTION();
return result;
}

static int
Expand Down
Loading