Skip to content

Commit 2980efd

Browse files
authored
Merge pull request #538 from python/main
Sync Fork from Upstream Repo
2 parents f9c0809 + b2cf251 commit 2980efd

File tree

13 files changed

+184
-69
lines changed

13 files changed

+184
-69
lines changed

Doc/library/pprint.rst

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ The :mod:`pprint` module defines one class:
4646

4747
*stream* (default ``sys.stdout``) is a :term:`file-like object` to
4848
which the output will be written by calling its :meth:`write` method.
49+
If both *stream* and ``sys.stdout`` are ``None``, then
50+
:meth:`~PrettyPrinter.pprint` silently returns.
4951

5052
Other values configure the manner in which nesting of complex data
5153
structures is displayed.
@@ -84,6 +86,9 @@ The :mod:`pprint` module defines one class:
8486
.. versionchanged:: 3.10
8587
Added the *underscore_numbers* parameter.
8688

89+
.. versionchanged:: 3.11
90+
No longer attempts to write to ``sys.stdout`` if it is ``None``.
91+
8792
>>> import pprint
8893
>>> stuff = ['spam', 'eggs', 'lumberjack', 'knights', 'ni']
8994
>>> stuff.insert(0, stuff[:])
@@ -107,24 +112,13 @@ The :mod:`pprint` module defines one class:
107112
>>> pp.pprint(tup)
108113
('spam', ('eggs', ('lumberjack', ('knights', ('ni', ('dead', (...)))))))
109114

110-
111-
The :mod:`pprint` module also provides several shortcut functions:
112-
113115
.. function:: pformat(object, indent=1, width=80, depth=None, *, \
114116
compact=False, sort_dicts=True, underscore_numbers=False)
115117

116118
Return the formatted representation of *object* as a string. *indent*,
117-
*width*, *depth*, *compact*, *sort_dicts* and *underscore_numbers* will be passed to the
118-
:class:`PrettyPrinter` constructor as formatting parameters.
119-
120-
.. versionchanged:: 3.4
121-
Added the *compact* parameter.
122-
123-
.. versionchanged:: 3.8
124-
Added the *sort_dicts* parameter.
125-
126-
.. versionchanged:: 3.10
127-
Added the *underscore_numbers* parameter.
119+
*width*, *depth*, *compact*, *sort_dicts* and *underscore_numbers* are
120+
passed to the :class:`PrettyPrinter` constructor as formatting parameters
121+
and their meanings are as described in its documentation above.
128122

129123

130124
.. function:: pp(object, *args, sort_dicts=False, **kwargs)
@@ -142,20 +136,15 @@ The :mod:`pprint` module also provides several shortcut functions:
142136
compact=False, sort_dicts=True, underscore_numbers=False)
143137

144138
Prints the formatted representation of *object* on *stream*, followed by a
145-
newline. If *stream* is ``None``, ``sys.stdout`` is used. This may be used
139+
newline. If *stream* is ``None``, ``sys.stdout`` is used. This may be used
146140
in the interactive interpreter instead of the :func:`print` function for
147141
inspecting values (you can even reassign ``print = pprint.pprint`` for use
148-
within a scope). *indent*, *width*, *depth*, *compact*, *sort_dicts* and *underscore_numbers* will
149-
be passed to the :class:`PrettyPrinter` constructor as formatting parameters.
150-
151-
.. versionchanged:: 3.4
152-
Added the *compact* parameter.
153-
154-
.. versionchanged:: 3.8
155-
Added the *sort_dicts* parameter.
142+
within a scope).
156143

157-
.. versionchanged:: 3.10
158-
Added the *underscore_numbers* parameter.
144+
The configuration parameters *stream*, *indent*, *width*, *depth*,
145+
*compact*, *sort_dicts* and *underscore_numbers* are passed to the
146+
:class:`PrettyPrinter` constructor and their meanings are as
147+
described in its documentation above.
159148

160149
>>> import pprint
161150
>>> stuff = ['spam', 'eggs', 'lumberjack', 'knights', 'ni']
@@ -168,7 +157,6 @@ The :mod:`pprint` module also provides several shortcut functions:
168157
'knights',
169158
'ni']
170159

171-
172160
.. function:: isreadable(object)
173161

174162
.. index:: builtin: eval

Doc/using/configure.rst

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ recommended for best performance.
171171

172172
.. versionadded:: 3.8
173173

174-
.. cmdoption:: --with-lto
174+
.. cmdoption:: --with-lto=[full|thin|no|yes]
175175

176176
Enable Link Time Optimization (LTO) in any build (disabled by default).
177177

@@ -180,6 +180,9 @@ recommended for best performance.
180180

181181
.. versionadded:: 3.6
182182

183+
.. versionadded:: 3.11
184+
To use ThinLTO feature, use ``--with-lto=thin`` on Clang.
185+
183186
.. cmdoption:: --with-computed-gotos
184187

185188
Enable computed gotos in evaluation loop (enabled by default on supported

Lib/pprint.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,9 @@ def __init__(self, indent=1, width=80, depth=None, stream=None, *,
148148
self._underscore_numbers = underscore_numbers
149149

150150
def pprint(self, object):
151-
self._format(object, self._stream, 0, 0, {}, 0)
152-
self._stream.write("\n")
151+
if self._stream is not None:
152+
self._format(object, self._stream, 0, 0, {}, 0)
153+
self._stream.write("\n")
153154

154155
def pformat(self, object):
155156
sio = _StringIO()

Lib/test/test_pprint.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# -*- coding: utf-8 -*-
22

33
import collections
4+
import contextlib
45
import dataclasses
56
import io
67
import itertools
@@ -159,6 +160,13 @@ def test_basic(self):
159160
self.assertTrue(pp.isreadable(safe),
160161
"expected isreadable for %r" % (safe,))
161162

163+
def test_stdout_is_None(self):
164+
with contextlib.redirect_stdout(None):
165+
# smoke test - there is no output to check
166+
value = 'this should not fail'
167+
pprint.pprint(value)
168+
pprint.PrettyPrinter().pprint(value)
169+
162170
def test_knotted(self):
163171
# Verify .isrecursive() and .isreadable() w/ recursion
164172
# Tie a knot.

Lib/test/test_threading.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1604,6 +1604,31 @@ def test_interrupt_main_invalid_signal(self):
16041604
self.assertRaises(ValueError, _thread.interrupt_main, signal.NSIG)
16051605
self.assertRaises(ValueError, _thread.interrupt_main, 1000000)
16061606

1607+
@threading_helper.reap_threads
1608+
def test_can_interrupt_tight_loops(self):
1609+
cont = [True]
1610+
started = [False]
1611+
interrupted = [False]
1612+
1613+
def worker(started, cont, interrupted):
1614+
iterations = 100_000_000
1615+
started[0] = True
1616+
while cont[0]:
1617+
if iterations:
1618+
iterations -= 1
1619+
else:
1620+
return
1621+
pass
1622+
interrupted[0] = True
1623+
1624+
t = threading.Thread(target=worker,args=(started, cont, interrupted))
1625+
t.start()
1626+
while not started[0]:
1627+
pass
1628+
cont[0] = False
1629+
t.join()
1630+
self.assertTrue(interrupted[0])
1631+
16071632

16081633
class AtexitTests(unittest.TestCase):
16091634

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Add support for building with clang thin lto via --with-lto=thin/full. Patch
2+
by Dong-hee Na and Brett Holman.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Update ``property_descr_set`` to use vectorcall if possible. Patch by Dong-hee
2+
Na.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Make :mod:`pprint` (like the builtin ``print``) not attempt to write to ``stdout`` when it is ``None``.

Objects/descrobject.c

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1614,31 +1614,42 @@ property_descr_set(PyObject *self, PyObject *obj, PyObject *value)
16141614
propertyobject *gs = (propertyobject *)self;
16151615
PyObject *func, *res;
16161616

1617-
if (value == NULL)
1617+
if (value == NULL) {
16181618
func = gs->prop_del;
1619-
else
1619+
}
1620+
else {
16201621
func = gs->prop_set;
1622+
}
1623+
16211624
if (func == NULL) {
16221625
if (gs->prop_name != NULL) {
16231626
PyErr_Format(PyExc_AttributeError,
16241627
value == NULL ?
16251628
"can't delete attribute %R" :
16261629
"can't set attribute %R",
16271630
gs->prop_name);
1628-
} else {
1631+
}
1632+
else {
16291633
PyErr_SetString(PyExc_AttributeError,
16301634
value == NULL ?
16311635
"can't delete attribute" :
16321636
"can't set attribute");
16331637
}
16341638
return -1;
16351639
}
1636-
if (value == NULL)
1640+
1641+
if (value == NULL) {
16371642
res = PyObject_CallOneArg(func, obj);
1638-
else
1639-
res = PyObject_CallFunctionObjArgs(func, obj, value, NULL);
1640-
if (res == NULL)
1643+
}
1644+
else {
1645+
PyObject *args[] = { obj, value };
1646+
res = PyObject_Vectorcall(func, args, 2, NULL);
1647+
}
1648+
1649+
if (res == NULL) {
16411650
return -1;
1651+
}
1652+
16421653
Py_DECREF(res);
16431654
return 0;
16441655
}

Python/ceval.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3638,14 +3638,17 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, PyFrameObject *f, int throwflag)
36383638
if (Py_IsFalse(cond)) {
36393639
Py_DECREF(cond);
36403640
JUMPTO(oparg);
3641+
CHECK_EVAL_BREAKER();
36413642
DISPATCH();
36423643
}
36433644
err = PyObject_IsTrue(cond);
36443645
Py_DECREF(cond);
36453646
if (err > 0)
36463647
;
3647-
else if (err == 0)
3648+
else if (err == 0) {
36483649
JUMPTO(oparg);
3650+
CHECK_EVAL_BREAKER();
3651+
}
36493652
else
36503653
goto error;
36513654
DISPATCH();
@@ -3662,12 +3665,14 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, PyFrameObject *f, int throwflag)
36623665
if (Py_IsTrue(cond)) {
36633666
Py_DECREF(cond);
36643667
JUMPTO(oparg);
3668+
CHECK_EVAL_BREAKER();
36653669
DISPATCH();
36663670
}
36673671
err = PyObject_IsTrue(cond);
36683672
Py_DECREF(cond);
36693673
if (err > 0) {
36703674
JUMPTO(oparg);
3675+
CHECK_EVAL_BREAKER();
36713676
}
36723677
else if (err == 0)
36733678
;

aclocal.m4

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
# generated automatically by aclocal 1.16.1 -*- Autoconf -*-
1+
# generated automatically by aclocal 1.16.3 -*- Autoconf -*-
22

3-
# Copyright (C) 1996-2018 Free Software Foundation, Inc.
3+
# Copyright (C) 1996-2020 Free Software Foundation, Inc.
44

55
# This file is free software; the Free Software Foundation
66
# gives unlimited permission to copy and/or distribute it,

configure

Lines changed: 54 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1545,7 +1545,8 @@ Optional Packages:
15451545
--with-trace-refs enable tracing references for debugging purpose
15461546
(default is no)
15471547
--with-assertions build with C assertions enabled (default is no)
1548-
--with-lto enable Link-Time-Optimization in any build (default
1548+
--with-lto=[full|thin|no|yes]
1549+
enable Link-Time-Optimization in any build (default
15491550
is no)
15501551
--with-hash-algorithm=[fnv|siphash24]
15511552
select hash algorithm for use in Python/pyhash.c
@@ -3039,27 +3040,27 @@ VERSION=3.11
30393040

30403041
SOVERSION=1.0
30413042

3042-
# The later defininition of _XOPEN_SOURCE disables certain features
3043+
# The later definition of _XOPEN_SOURCE disables certain features
30433044
# on Linux, so we need _GNU_SOURCE to re-enable them (makedev, tm_zone).
30443045

30453046
$as_echo "#define _GNU_SOURCE 1" >>confdefs.h
30463047

30473048

3048-
# The later defininition of _XOPEN_SOURCE and _POSIX_C_SOURCE disables
3049+
# The later definition of _XOPEN_SOURCE and _POSIX_C_SOURCE disables
30493050
# certain features on NetBSD, so we need _NETBSD_SOURCE to re-enable
30503051
# them.
30513052

30523053
$as_echo "#define _NETBSD_SOURCE 1" >>confdefs.h
30533054

30543055

3055-
# The later defininition of _XOPEN_SOURCE and _POSIX_C_SOURCE disables
3056+
# The later definition of _XOPEN_SOURCE and _POSIX_C_SOURCE disables
30563057
# certain features on FreeBSD, so we need __BSD_VISIBLE to re-enable
30573058
# them.
30583059

30593060
$as_echo "#define __BSD_VISIBLE 1" >>confdefs.h
30603061

30613062

3062-
# The later defininition of _XOPEN_SOURCE and _POSIX_C_SOURCE disables
3063+
# The later definition of _XOPEN_SOURCE and _POSIX_C_SOURCE disables
30633064
# certain features on Mac OS X, so we need _DARWIN_C_SOURCE to re-enable
30643065
# them.
30653066

@@ -6585,16 +6586,36 @@ $as_echo_n "checking for --with-lto... " >&6; }
65856586
# Check whether --with-lto was given.
65866587
if test "${with_lto+set}" = set; then :
65876588
withval=$with_lto;
6588-
if test "$withval" != no
6589-
then
6590-
Py_LTO='true'
6591-
{ $as_echo "$as_me:${as_lineno-$LINENO}: result: yes" >&5
6592-
$as_echo "yes" >&6; };
6593-
else
6594-
Py_LTO='false'
6595-
{ $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5
6596-
$as_echo "no" >&6; };
6597-
fi
6589+
case "$withval" in
6590+
full)
6591+
Py_LTO='true'
6592+
Py_LTO_POLICY='full'
6593+
{ $as_echo "$as_me:${as_lineno-$LINENO}: result: yes" >&5
6594+
$as_echo "yes" >&6; }
6595+
;;
6596+
thin)
6597+
Py_LTO='true'
6598+
Py_LTO_POLICY='thin'
6599+
{ $as_echo "$as_me:${as_lineno-$LINENO}: result: yes" >&5
6600+
$as_echo "yes" >&6; }
6601+
;;
6602+
yes)
6603+
Py_LTO='true'
6604+
Py_LTO_POLICY='default'
6605+
{ $as_echo "$as_me:${as_lineno-$LINENO}: result: yes" >&5
6606+
$as_echo "yes" >&6; }
6607+
;;
6608+
no)
6609+
Py_LTO='false'
6610+
{ $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5
6611+
$as_echo "no" >&6; }
6612+
;;
6613+
*)
6614+
Py_LTO='false'
6615+
as_fn_error $? "unknown lto option: '$withval'" "$LINENO" 5
6616+
;;
6617+
esac
6618+
65986619
else
65996620
{ $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5
66006621
$as_echo "no" >&6; }
@@ -6732,15 +6753,30 @@ $as_echo "$as_me: llvm-ar found via xcrun: ${LLVM_AR}" >&6;}
67326753
case $ac_sys_system in
67336754
Darwin*)
67346755
# Any changes made here should be reflected in the GCC+Darwin case below
6735-
LTOFLAGS="-flto -Wl,-export_dynamic"
6736-
LTOCFLAGS="-flto"
6756+
if test $Py_LTO_POLICY = default
6757+
then
6758+
LTOFLAGS="-flto -Wl,-export_dynamic"
6759+
LTOCFLAGS="-flto"
6760+
else
6761+
LTOFLAGS="-flto=${Py_LTO_POLICY} -Wl,-export_dynamic"
6762+
LTOCFLAGS="-flto=${Py_LTO_POLICY}"
6763+
fi
67376764
;;
67386765
*)
6739-
LTOFLAGS="-flto"
6766+
if test $Py_LTO_POLICY = default
6767+
then
6768+
LTOFLAGS="-flto"
6769+
else
6770+
LTOFLAGS="-flto=${Py_LTO_POLICY}"
6771+
fi
67406772
;;
67416773
esac
67426774
;;
67436775
*gcc*)
6776+
if test $Py_LTO_POLICY = thin
6777+
then
6778+
as_fn_error $? "thin lto is not supported under gcc compiler." "$LINENO" 5
6779+
fi
67446780
case $ac_sys_system in
67456781
Darwin*)
67466782
LTOFLAGS="-flto -Wl,-export_dynamic"

0 commit comments

Comments
 (0)