Skip to content

Commit 8a519ec

Browse files
Merge branch 'main' into gh-162421-mutex-spin-budget
2 parents 7ddd6e2 + 64c20ff commit 8a519ec

7 files changed

Lines changed: 95 additions & 91 deletions

File tree

‎Doc/library/mimetypes.rst‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,8 @@ An example usage of the module::
194194
'.tar.gz'
195195
>>> mimetypes.encodings_map['.gz']
196196
'gzip'
197-
>>> mimetypes.types_map['.tgz']
198-
'application/x-tar-gz'
197+
>>> mimetypes.types_map['.tar']
198+
'application/x-tar'
199199

200200

201201
.. _mimetypes-objects:

‎InternalDocs/fractions.md‎

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,3 +363,74 @@ require a potentially expensive `gcd` computation. We can use the
363363
In addition to the above proof, a formal, computer-verified proof
364364
of correctness (using Lean) of the algorithm can be found here:
365365
https://github.com/mdickinson/snippets/tree/1f47f8cc33185c646b679647e88cb24f29fd6a4e/proofs/limit_denominator
366+
367+
# Algorithms for rational arithmetic
368+
369+
In the following we assume that rational numbers $\frac{p}{q}$ are
370+
represented by fractions in lowest terms. That is, $p$ and $q$
371+
are coprime, $\gcd(p, q) = 1$. And, the denominator $q$ is positive.
372+
373+
Consider fractions $a/b$ and $c/d$.
374+
375+
## Addition/subtraction
376+
377+
Let $g = \gcd(b, d)$. Then\
378+
$$\frac{a}{b} \pm \frac{c}{d} = \frac{a d \pm b c}{b d}
379+
=\frac{a (d/g) \pm c (b/g)}{(b d)/g}
380+
=\frac{p}{q}$$
381+
382+
Now, if $g > 1$, we're working with smaller integers. Also,
383+
$p$, $b/g$, $d/g$ are pairwise coprime.
384+
385+
Indeed, $b/g$ and $d/g$ share no common factors (they were
386+
removed) and $b$ is coprime with $a$ (since input fractions are
387+
in lowest terms), hence $b/g$ and $a$ are coprime. By symmetry,
388+
$d/g$ and $c$ are coprime too. Then\
389+
$$\gcd(p, b/g)
390+
=\gcd(a (d/g) \pm c (b/g), b/g)
391+
=\gcd(a (d/g), b/g) = 1$$
392+
393+
and similarly\
394+
$$\gcd(p, d/g) = \gcd(c(b/g), d/g) = 1$$
395+
396+
The above allows us to optimize reduction of the result to lowest
397+
terms. Indeed,\
398+
$$h = \gcd(p, q)
399+
=\gcd(p, (b/g)(d/g) g)
400+
=\gcd(p, g)$$
401+
402+
and\
403+
$$\frac{a}{b}\pm\frac{c}{d}
404+
=\frac{p/h}{(b/g)(d/g)(g/h)}
405+
=\frac{(a (d/g) \pm c (b/g) )/h}
406+
{(b/g)(d/h)}$$
407+
408+
is a normalized fraction. This is useful because the unnormalized
409+
denominator $q$ could be much larger than $g$.
410+
411+
We should special-case $g = 1$ and $h = 1$, since 61% of randomly-chosen pairs of integers
412+
[are coprime](https://en.wikipedia.org/wiki/Coprime_integers#Probability_of_coprimality).
413+
Note that the second case always holds for fractions, obtained from floats: here
414+
$g$ is a power of 2 and the unnormalized numerator $p$ is an odd integer.
415+
416+
## Multiplication
417+
418+
Let $g = \gcd(a, d)$ and $h = \gcd(b, c)$, then\
419+
$$\frac{a}{b}\times\frac{c}{d}
420+
=\frac{(a/g)(c/h)}
421+
{(d/g)(b/h)}$$
422+
423+
Note that after divisions we're multiplying smaller integers.
424+
425+
Also, the resulting fraction is normalized, because each of
426+
two factors in the numerator is coprime to each of the two factors
427+
in the denominator.
428+
429+
Indeed, pick $a/g$. It's coprime with $b/h$, because input
430+
fractions are in lowest terms. It's also coprime with $d/g$,
431+
because common factors are removed by $g = \gcd(a, d)$.
432+
By symmetry, $c/h$ is also coprime with both factors in the denominator.
433+
434+
As for addition/subtraction, we should special-case $g = 1$
435+
and $h = 1$ for the same reason. That happens also for multiplying
436+
rationals, obtained from floats.

‎Lib/fractions.py‎

Lines changed: 3 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,8 @@ def limit_denominator(self, max_denominator=1000000):
401401
# approximation to x if, and only if, it is a convergent or
402402
# semiconvergent of the (unique shortest) continued fraction
403403
# associated to x. See e.g. Continued Fractions by A. Ya.
404-
# Khinchin (1964), Theorem 15.
404+
# Khinchin (1964), Theorem 15. See also
405+
# InternalDocs/fractions.md for details.
405406
#
406407
# To find a best rational approximation with denominator <= M,
407408
# we find the best upper and lower approximations with
@@ -723,72 +724,7 @@ def reverse(b, a):
723724
return forward, reverse
724725

725726
# Rational arithmetic algorithms: Knuth, TAOCP, Volume 2, 4.5.1.
726-
#
727-
# Assume input fractions a and b are normalized.
728-
#
729-
# 1) Consider addition/subtraction.
730-
#
731-
# Let g = gcd(da, db). Then
732-
#
733-
# na nb na*db ± nb*da
734-
# a ± b == -- ± -- == ------------- ==
735-
# da db da*db
736-
#
737-
# na*(db//g) ± nb*(da//g) t
738-
# == ----------------------- == -
739-
# (da*db)//g d
740-
#
741-
# Now, if g > 1, we're working with smaller integers.
742-
#
743-
# Note, that t, (da//g) and (db//g) are pairwise coprime.
744-
#
745-
# Indeed, (da//g) and (db//g) share no common factors (they were
746-
# removed) and da is coprime with na (since input fractions are
747-
# normalized), hence (da//g) and na are coprime. By symmetry,
748-
# (db//g) and nb are coprime too. Then,
749-
#
750-
# gcd(t, da//g) == gcd(na*(db//g), da//g) == 1
751-
# gcd(t, db//g) == gcd(nb*(da//g), db//g) == 1
752-
#
753-
# Above allows us optimize reduction of the result to lowest
754-
# terms. Indeed,
755-
#
756-
# g2 = gcd(t, d) == gcd(t, (da//g)*(db//g)*g) == gcd(t, g)
757-
#
758-
# t//g2 t//g2
759-
# a ± b == ----------------------- == ----------------
760-
# (da//g)*(db//g)*(g//g2) (da//g)*(db//g2)
761-
#
762-
# is a normalized fraction. This is useful because the unnormalized
763-
# denominator d could be much larger than g.
764-
#
765-
# We should special-case g == 1 (and g2 == 1), since 60.8% of
766-
# randomly-chosen integers are coprime:
767-
# https://en.wikipedia.org/wiki/Coprime_integers#Probability_of_coprimality
768-
# Note, that g2 == 1 always for fractions, obtained from floats: here
769-
# g is a power of 2 and the unnormalized numerator t is an odd integer.
770-
#
771-
# 2) Consider multiplication
772-
#
773-
# Let g1 = gcd(na, db) and g2 = gcd(nb, da), then
774-
#
775-
# na*nb na*nb (na//g1)*(nb//g2)
776-
# a*b == ----- == ----- == -----------------
777-
# da*db db*da (db//g1)*(da//g2)
778-
#
779-
# Note, that after divisions we're multiplying smaller integers.
780-
#
781-
# Also, the resulting fraction is normalized, because each of
782-
# two factors in the numerator is coprime to each of the two factors
783-
# in the denominator.
784-
#
785-
# Indeed, pick (na//g1). It's coprime with (da//g2), because input
786-
# fractions are normalized. It's also coprime with (db//g1), because
787-
# common factors are removed by g1 == gcd(na, db).
788-
#
789-
# As for addition/subtraction, we should special-case g1 == 1
790-
# and g2 == 1 for same reason. That happens also for multiplying
791-
# rationals, obtained from floats.
727+
# See also InternalDocs/fractions.md for details.
792728

793729
def _add(a, b):
794730
"""a + b"""

‎Lib/test/test_code.py‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1287,6 +1287,19 @@ def callback(code):
12871287
self.assertFalse(bool(coderef()))
12881288
self.assertTrue(self.called)
12891289

1290+
def test_code_const_reference_cycle_collected(self):
1291+
def f():
1292+
return 1234.5
1293+
1294+
a = []
1295+
code = f.__code__.replace(co_consts=f.__code__.co_consts + (a,))
1296+
coderef = weakref.ref(code)
1297+
a.append(code)
1298+
1299+
del code, a
1300+
gc_collect()
1301+
self.assertIsNone(coderef())
1302+
12901303
# Python implementation of location table parsing algorithm
12911304
def read(it):
12921305
return next(it)

‎Lib/test/test_marshal.py‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -395,8 +395,7 @@ def f():
395395
code = f.__code__
396396
a = []
397397
code = code.replace(co_consts=code.co_consts + (a,))
398-
# This test creates a reference loop which leads to reference leaks,
399-
# so we need to break the loop manually. See gh-148722.
398+
# Break the cycle without relying on cyclic GC after the test.
400399
self.addCleanup(a.clear)
401400
a.append(code)
402401
self.check_reference_loop(code, 'code', 3)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix a reference leak when a code object's constants contain a reference cycle
2+
back to the code object.

‎Objects/codeobject.c‎

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -735,12 +735,7 @@ _PyCode_New(struct _PyCodeConstructor *con)
735735
}
736736

737737
Py_ssize_t size = PyBytes_GET_SIZE(con->code) / sizeof(_Py_CODEUNIT);
738-
PyCodeObject *co;
739-
#ifdef Py_GIL_DISABLED
740-
co = PyObject_GC_NewVar(PyCodeObject, &PyCode_Type, size);
741-
#else
742-
co = PyObject_NewVar(PyCodeObject, &PyCode_Type, size);
743-
#endif
738+
PyCodeObject *co = PyObject_GC_NewVar(PyCodeObject, &PyCode_Type, size);
744739
if (co == NULL) {
745740
Py_XDECREF(replacement_locations);
746741
PyErr_NoMemory();
@@ -759,8 +754,8 @@ _PyCode_New(struct _PyCodeConstructor *con)
759754

760755
#ifdef Py_GIL_DISABLED
761756
co->_co_unique_id = _PyObject_AssignUniqueId((PyObject *)co);
762-
_PyObject_GC_TRACK(co);
763757
#endif
758+
_PyObject_GC_TRACK(co);
764759
Py_XDECREF(replacement_locations);
765760
return co;
766761
}
@@ -2411,9 +2406,7 @@ code_dealloc(PyObject *self)
24112406
return;
24122407
}
24132408

2414-
#ifdef Py_GIL_DISABLED
24152409
PyObject_GC_UnTrack(co);
2416-
#endif
24172410

24182411
_PyFunction_ClearCodeByVersion(co->co_version);
24192412
if (co->co_extra != NULL) {
@@ -2470,18 +2463,16 @@ code_dealloc(PyObject *self)
24702463
PyMem_Free(co->co_tlbc);
24712464
}
24722465
#endif
2473-
PyObject_Free(co);
2466+
PyObject_GC_Del(co);
24742467
}
24752468

2476-
#ifdef Py_GIL_DISABLED
24772469
static int
24782470
code_traverse(PyObject *self, visitproc visit, void *arg)
24792471
{
24802472
PyCodeObject *co = _PyCodeObject_CAST(self);
24812473
Py_VISIT(co->co_consts);
24822474
return 0;
24832475
}
2484-
#endif
24852476

24862477
static PyObject *
24872478
code_repr(PyObject *self)
@@ -2901,17 +2892,9 @@ PyTypeObject PyCode_Type = {
29012892
PyObject_GenericGetAttr, /* tp_getattro */
29022893
0, /* tp_setattro */
29032894
0, /* tp_as_buffer */
2904-
#ifdef Py_GIL_DISABLED
29052895
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
2906-
#else
2907-
Py_TPFLAGS_DEFAULT, /* tp_flags */
2908-
#endif
29092896
code_new__doc__, /* tp_doc */
2910-
#ifdef Py_GIL_DISABLED
29112897
code_traverse, /* tp_traverse */
2912-
#else
2913-
0, /* tp_traverse */
2914-
#endif
29152898
0, /* tp_clear */
29162899
code_richcompare, /* tp_richcompare */
29172900
offsetof(PyCodeObject, co_weakreflist), /* tp_weaklistoffset */

0 commit comments

Comments
 (0)