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: 2 additions & 0 deletions Doc/library/stdtypes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1214,6 +1214,8 @@ accepts integers that meet the value restriction ``0 <= x <= 255``).
| ``s[i] = x`` | item *i* of *s* is replaced by | |
| | *x* | |
+------------------------------+--------------------------------+---------------------+
| ``del s[i]`` | removes item *i* of *s* | |
+------------------------------+--------------------------------+---------------------+
| ``s[i:j] = t`` | slice of *s* from *i* to *j* | |
| | is replaced by the contents of | |
| | the iterable *t* | |
Expand Down
3 changes: 3 additions & 0 deletions Include/internal/pycore_object.h
Original file line number Diff line number Diff line change
Expand Up @@ -897,6 +897,9 @@ extern PyObject *_PyType_LookupRefAndVersion(PyTypeObject *, PyObject *,
extern unsigned int
_PyType_LookupStackRefAndVersion(PyTypeObject *type, PyObject *name, _PyStackRef *out);

extern int _PyObject_GetMethodStackRef(PyThreadState *ts, PyObject *obj,
PyObject *name, _PyStackRef *method);

// Cache the provided init method in the specialization cache of type if the
// provided type version matches the current version of the type.
//
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix crash in the :term:`free threading` build's QSBR code that could occur
when changing an object's ``__dict__`` attribute.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Allow the JIT to remove unnecessary ``_ITER_CHECK_TUPLE`` ops.
35 changes: 22 additions & 13 deletions Objects/call.c
Original file line number Diff line number Diff line change
Expand Up @@ -834,12 +834,15 @@ PyObject_VectorcallMethod(PyObject *name, PyObject *const *args,
assert(PyVectorcall_NARGS(nargsf) >= 1);

PyThreadState *tstate = _PyThreadState_GET();
PyObject *callable = NULL;
_PyCStackRef method;
_PyThreadState_PushCStackRef(tstate, &method);
/* Use args[0] as "self" argument */
int unbound = _PyObject_GetMethod(args[0], name, &callable);
if (callable == NULL) {
int unbound = _PyObject_GetMethodStackRef(tstate, args[0], name, &method.ref);
if (PyStackRef_IsNull(method.ref)) {
_PyThreadState_PopCStackRef(tstate, &method);
return NULL;
}
PyObject *callable = PyStackRef_AsPyObjectBorrow(method.ref);

if (unbound) {
/* We must remove PY_VECTORCALL_ARGUMENTS_OFFSET since
Expand All @@ -855,7 +858,7 @@ PyObject_VectorcallMethod(PyObject *name, PyObject *const *args,
EVAL_CALL_STAT_INC_IF_FUNCTION(EVAL_CALL_METHOD, callable);
PyObject *result = _PyObject_VectorcallTstate(tstate, callable,
args, nargsf, kwnames);
Py_DECREF(callable);
_PyThreadState_PopCStackRef(tstate, &method);
return result;
}

Expand All @@ -868,19 +871,22 @@ PyObject_CallMethodObjArgs(PyObject *obj, PyObject *name, ...)
return null_error(tstate);
}

PyObject *callable = NULL;
int is_method = _PyObject_GetMethod(obj, name, &callable);
if (callable == NULL) {
_PyCStackRef method;
_PyThreadState_PushCStackRef(tstate, &method);
int is_method = _PyObject_GetMethodStackRef(tstate, obj, name, &method.ref);
if (PyStackRef_IsNull(method.ref)) {
_PyThreadState_PopCStackRef(tstate, &method);
return NULL;
}
PyObject *callable = PyStackRef_AsPyObjectBorrow(method.ref);
obj = is_method ? obj : NULL;

va_list vargs;
va_start(vargs, name);
PyObject *result = object_vacall(tstate, obj, callable, vargs);
va_end(vargs);

Py_DECREF(callable);
_PyThreadState_PopCStackRef(tstate, &method);
return result;
}

Expand All @@ -897,20 +903,23 @@ _PyObject_CallMethodIdObjArgs(PyObject *obj, _Py_Identifier *name, ...)
if (!oname) {
return NULL;
}

PyObject *callable = NULL;
int is_method = _PyObject_GetMethod(obj, oname, &callable);
if (callable == NULL) {
_PyCStackRef method;
_PyThreadState_PushCStackRef(tstate, &method);
int is_method = _PyObject_GetMethodStackRef(tstate, obj, oname, &method.ref);
if (PyStackRef_IsNull(method.ref)) {
_PyThreadState_PopCStackRef(tstate, &method);
return NULL;
}
PyObject *callable = PyStackRef_AsPyObjectBorrow(method.ref);

obj = is_method ? obj : NULL;

va_list vargs;
va_start(vargs, name);
PyObject *result = object_vacall(tstate, obj, callable, vargs);
va_end(vargs);

Py_DECREF(callable);
_PyThreadState_PopCStackRef(tstate, &method);
return result;
}

Expand Down
110 changes: 110 additions & 0 deletions Objects/object.c
Original file line number Diff line number Diff line change
Expand Up @@ -1664,6 +1664,116 @@ _PyObject_GetMethod(PyObject *obj, PyObject *name, PyObject **method)
return 0;
}

int
_PyObject_GetMethodStackRef(PyThreadState *ts, PyObject *obj,
PyObject *name, _PyStackRef *method)
{
int meth_found = 0;

assert(PyStackRef_IsNull(*method));

PyTypeObject *tp = Py_TYPE(obj);
if (!_PyType_IsReady(tp)) {
if (PyType_Ready(tp) < 0) {
return 0;
}
}

if (tp->tp_getattro != PyObject_GenericGetAttr || !PyUnicode_CheckExact(name)) {
PyObject *res = PyObject_GetAttr(obj, name);
if (res != NULL) {
*method = PyStackRef_FromPyObjectSteal(res);
}
return 0;
}

_PyType_LookupStackRefAndVersion(tp, name, method);
PyObject *descr = PyStackRef_AsPyObjectBorrow(*method);
descrgetfunc f = NULL;
if (descr != NULL) {
if (_PyType_HasFeature(Py_TYPE(descr), Py_TPFLAGS_METHOD_DESCRIPTOR)) {
meth_found = 1;
}
else {
f = Py_TYPE(descr)->tp_descr_get;
if (f != NULL && PyDescr_IsData(descr)) {
PyObject *value = f(descr, obj, (PyObject *)Py_TYPE(obj));
PyStackRef_CLEAR(*method);
if (value != NULL) {
*method = PyStackRef_FromPyObjectSteal(value);
}
return 0;
}
}
}
PyObject *dict, *attr;
if ((tp->tp_flags & Py_TPFLAGS_INLINE_VALUES) &&
_PyObject_TryGetInstanceAttribute(obj, name, &attr)) {
if (attr != NULL) {
PyStackRef_CLEAR(*method);
*method = PyStackRef_FromPyObjectSteal(attr);
return 0;
}
dict = NULL;
}
else if ((tp->tp_flags & Py_TPFLAGS_MANAGED_DICT)) {
dict = (PyObject *)_PyObject_GetManagedDict(obj);
}
else {
PyObject **dictptr = _PyObject_ComputedDictPointer(obj);
if (dictptr != NULL) {
dict = FT_ATOMIC_LOAD_PTR_ACQUIRE(*dictptr);
}
else {
dict = NULL;
}
}
if (dict != NULL) {
// TODO: use _Py_dict_lookup_threadsafe_stackref
Py_INCREF(dict);
PyObject *value;
if (PyDict_GetItemRef(dict, name, &value) != 0) {
// found or error
Py_DECREF(dict);
PyStackRef_CLEAR(*method);
if (value != NULL) {
*method = PyStackRef_FromPyObjectSteal(value);
}
return 0;
}
// not found
Py_DECREF(dict);
}

if (meth_found) {
assert(!PyStackRef_IsNull(*method));
return 1;
}

if (f != NULL) {
PyObject *value = f(descr, obj, (PyObject *)Py_TYPE(obj));
PyStackRef_CLEAR(*method);
if (value) {
*method = PyStackRef_FromPyObjectSteal(value);
}
return 0;
}

if (descr != NULL) {
assert(!PyStackRef_IsNull(*method));
return 0;
}

PyErr_Format(PyExc_AttributeError,
"'%.100s' object has no attribute '%U'",
tp->tp_name, name);

_PyObject_SetAttributeErrorContext(obj, name);
assert(PyStackRef_IsNull(*method));
return 0;
}


/* Generic GetAttr functions - put these in your tp_[gs]etattro slot. */

PyObject *
Expand Down
22 changes: 11 additions & 11 deletions Objects/obmalloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -1238,15 +1238,15 @@ work_queue_first(struct llist_node *head)
}

static void
process_queue(struct llist_node *head, struct _qsbr_thread_state *qsbr,
process_queue(struct llist_node *head, _PyThreadStateImpl *tstate,
bool keep_empty, delayed_dealloc_cb cb, void *state)
{
while (!llist_empty(head)) {
struct _mem_work_chunk *buf = work_queue_first(head);

if (buf->rd_idx < buf->wr_idx) {
struct _mem_work_item *item = &buf->array[buf->rd_idx];
if (!_Py_qsbr_poll(qsbr, item->qsbr_goal)) {
if (!_Py_qsbr_poll(tstate->qsbr, item->qsbr_goal)) {
return;
}

Expand All @@ -1270,19 +1270,19 @@ process_queue(struct llist_node *head, struct _qsbr_thread_state *qsbr,

static void
process_interp_queue(struct _Py_mem_interp_free_queue *queue,
struct _qsbr_thread_state *qsbr, delayed_dealloc_cb cb,
_PyThreadStateImpl *tstate, delayed_dealloc_cb cb,
void *state)
{
assert(PyMutex_IsLocked(&queue->mutex));
process_queue(&queue->head, qsbr, false, cb, state);
process_queue(&queue->head, tstate, false, cb, state);

int more_work = !llist_empty(&queue->head);
_Py_atomic_store_int_relaxed(&queue->has_work, more_work);
}

static void
maybe_process_interp_queue(struct _Py_mem_interp_free_queue *queue,
struct _qsbr_thread_state *qsbr, delayed_dealloc_cb cb,
_PyThreadStateImpl *tstate, delayed_dealloc_cb cb,
void *state)
{
if (!_Py_atomic_load_int_relaxed(&queue->has_work)) {
Expand All @@ -1291,7 +1291,7 @@ maybe_process_interp_queue(struct _Py_mem_interp_free_queue *queue,

// Try to acquire the lock, but don't block if it's already held.
if (_PyMutex_LockTimed(&queue->mutex, 0, 0) == PY_LOCK_ACQUIRED) {
process_interp_queue(queue, qsbr, cb, state);
process_interp_queue(queue, tstate, cb, state);
PyMutex_Unlock(&queue->mutex);
}
}
Expand All @@ -1303,10 +1303,10 @@ _PyMem_ProcessDelayed(PyThreadState *tstate)
_PyThreadStateImpl *tstate_impl = (_PyThreadStateImpl *)tstate;

// Process thread-local work
process_queue(&tstate_impl->mem_free_queue, tstate_impl->qsbr, true, NULL, NULL);
process_queue(&tstate_impl->mem_free_queue, tstate_impl, true, NULL, NULL);

// Process shared interpreter work
maybe_process_interp_queue(&interp->mem_free_queue, tstate_impl->qsbr, NULL, NULL);
maybe_process_interp_queue(&interp->mem_free_queue, tstate_impl, NULL, NULL);
}

void
Expand All @@ -1316,10 +1316,10 @@ _PyMem_ProcessDelayedNoDealloc(PyThreadState *tstate, delayed_dealloc_cb cb, voi
_PyThreadStateImpl *tstate_impl = (_PyThreadStateImpl *)tstate;

// Process thread-local work
process_queue(&tstate_impl->mem_free_queue, tstate_impl->qsbr, true, cb, state);
process_queue(&tstate_impl->mem_free_queue, tstate_impl, true, cb, state);

// Process shared interpreter work
maybe_process_interp_queue(&interp->mem_free_queue, tstate_impl->qsbr, cb, state);
maybe_process_interp_queue(&interp->mem_free_queue, tstate_impl, cb, state);
}

void
Expand Down Expand Up @@ -1348,7 +1348,7 @@ _PyMem_AbandonDelayed(PyThreadState *tstate)

// Process the merged queue now (see gh-130794).
_PyThreadStateImpl *this_tstate = (_PyThreadStateImpl *)_PyThreadState_GET();
process_interp_queue(&interp->mem_free_queue, this_tstate->qsbr, NULL, NULL);
process_interp_queue(&interp->mem_free_queue, this_tstate, NULL, NULL);

PyMutex_Unlock(&interp->mem_free_queue.mutex);

Expand Down
8 changes: 8 additions & 0 deletions Python/crossinterp_data_lookup.h
Original file line number Diff line number Diff line change
Expand Up @@ -701,6 +701,14 @@ _PyFunction_FromXIData(_PyXIData_t *xidata)
Py_DECREF(code);
return NULL;
}
PyThreadState *tstate = _PyThreadState_GET();
if (PyDict_SetItem(globals, &_Py_ID(__builtins__),
tstate->interp->builtins) < 0)
{
Py_DECREF(code);
Py_DECREF(globals);
return NULL;
}
PyObject *func = PyFunction_New(code, globals);
Py_DECREF(code);
Py_DECREF(globals);
Expand Down
5 changes: 3 additions & 2 deletions Python/gc_free_threading.c
Original file line number Diff line number Diff line change
Expand Up @@ -2062,7 +2062,7 @@ gc_should_collect_mem_usage(GCState *gcstate)
// 70,000 new container objects.
return true;
}
Py_ssize_t last_mem = gcstate->last_mem;
Py_ssize_t last_mem = _Py_atomic_load_ssize_relaxed(&gcstate->last_mem);
Py_ssize_t mem_threshold = Py_MAX(last_mem / 10, 128);
if ((mem - last_mem) > mem_threshold) {
// The process memory usage has increased too much, do a collection.
Expand Down Expand Up @@ -2245,7 +2245,8 @@ gc_collect_internal(PyInterpreterState *interp, struct collection_state *state,

// Store the current memory usage, can be smaller now if breaking cycles
// freed some memory.
state->gcstate->last_mem = get_process_mem_usage();
Py_ssize_t last_mem = get_process_mem_usage();
_Py_atomic_store_ssize_relaxed(&state->gcstate->last_mem, last_mem);

// Append objects with legacy finalizers to the "gc.garbage" list.
handle_legacy_finalizers(state);
Expand Down
7 changes: 7 additions & 0 deletions Python/optimizer_bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -914,6 +914,13 @@ dummy_func(void) {
}
}

op(_ITER_CHECK_TUPLE, (iter, null_or_index -- iter, null_or_index)) {
if (sym_matches_type(iter, &PyTuple_Type)) {
REPLACE_OP(this_instr, _NOP, 0, 0);
}
sym_set_type(iter, &PyTuple_Type);
}

op(_ITER_NEXT_RANGE, (iter, null_or_index -- iter, null_or_index, next)) {
next = sym_new_type(ctx, &PyLong_Type);
}
Expand Down
6 changes: 6 additions & 0 deletions Python/optimizer_cases.c.h

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

Loading
Loading