Skip to content

Sync Fork from Upstream Repo #538

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 19, 2021
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
40 changes: 14 additions & 26 deletions Doc/library/pprint.rst
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ The :mod:`pprint` module defines one class:

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

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

.. versionchanged:: 3.11
No longer attempts to write to ``sys.stdout`` if it is ``None``.

>>> import pprint
>>> stuff = ['spam', 'eggs', 'lumberjack', 'knights', 'ni']
>>> stuff.insert(0, stuff[:])
Expand All @@ -107,24 +112,13 @@ The :mod:`pprint` module defines one class:
>>> pp.pprint(tup)
('spam', ('eggs', ('lumberjack', ('knights', ('ni', ('dead', (...)))))))


The :mod:`pprint` module also provides several shortcut functions:

.. function:: pformat(object, indent=1, width=80, depth=None, *, \
compact=False, sort_dicts=True, underscore_numbers=False)

Return the formatted representation of *object* as a string. *indent*,
*width*, *depth*, *compact*, *sort_dicts* and *underscore_numbers* will be passed to the
:class:`PrettyPrinter` constructor as formatting parameters.

.. versionchanged:: 3.4
Added the *compact* parameter.

.. versionchanged:: 3.8
Added the *sort_dicts* parameter.

.. versionchanged:: 3.10
Added the *underscore_numbers* parameter.
*width*, *depth*, *compact*, *sort_dicts* and *underscore_numbers* are
passed to the :class:`PrettyPrinter` constructor as formatting parameters
and their meanings are as described in its documentation above.


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

Prints the formatted representation of *object* on *stream*, followed by a
newline. If *stream* is ``None``, ``sys.stdout`` is used. This may be used
newline. If *stream* is ``None``, ``sys.stdout`` is used. This may be used
in the interactive interpreter instead of the :func:`print` function for
inspecting values (you can even reassign ``print = pprint.pprint`` for use
within a scope). *indent*, *width*, *depth*, *compact*, *sort_dicts* and *underscore_numbers* will
be passed to the :class:`PrettyPrinter` constructor as formatting parameters.

.. versionchanged:: 3.4
Added the *compact* parameter.

.. versionchanged:: 3.8
Added the *sort_dicts* parameter.
within a scope).

.. versionchanged:: 3.10
Added the *underscore_numbers* parameter.
The configuration parameters *stream*, *indent*, *width*, *depth*,
*compact*, *sort_dicts* and *underscore_numbers* are passed to the
:class:`PrettyPrinter` constructor and their meanings are as
described in its documentation above.

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


.. function:: isreadable(object)

.. index:: builtin: eval
Expand Down
5 changes: 4 additions & 1 deletion Doc/using/configure.rst
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ recommended for best performance.

.. versionadded:: 3.8

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

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

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

.. versionadded:: 3.6

.. versionadded:: 3.11
To use ThinLTO feature, use ``--with-lto=thin`` on Clang.

.. cmdoption:: --with-computed-gotos

Enable computed gotos in evaluation loop (enabled by default on supported
Expand Down
5 changes: 3 additions & 2 deletions Lib/pprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,9 @@ def __init__(self, indent=1, width=80, depth=None, stream=None, *,
self._underscore_numbers = underscore_numbers

def pprint(self, object):
self._format(object, self._stream, 0, 0, {}, 0)
self._stream.write("\n")
if self._stream is not None:
self._format(object, self._stream, 0, 0, {}, 0)
self._stream.write("\n")

def pformat(self, object):
sio = _StringIO()
Expand Down
8 changes: 8 additions & 0 deletions Lib/test/test_pprint.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-

import collections
import contextlib
import dataclasses
import io
import itertools
Expand Down Expand Up @@ -159,6 +160,13 @@ def test_basic(self):
self.assertTrue(pp.isreadable(safe),
"expected isreadable for %r" % (safe,))

def test_stdout_is_None(self):
with contextlib.redirect_stdout(None):
# smoke test - there is no output to check
value = 'this should not fail'
pprint.pprint(value)
pprint.PrettyPrinter().pprint(value)

def test_knotted(self):
# Verify .isrecursive() and .isreadable() w/ recursion
# Tie a knot.
Expand Down
25 changes: 25 additions & 0 deletions Lib/test/test_threading.py
Original file line number Diff line number Diff line change
Expand Up @@ -1604,6 +1604,31 @@ def test_interrupt_main_invalid_signal(self):
self.assertRaises(ValueError, _thread.interrupt_main, signal.NSIG)
self.assertRaises(ValueError, _thread.interrupt_main, 1000000)

@threading_helper.reap_threads
def test_can_interrupt_tight_loops(self):
cont = [True]
started = [False]
interrupted = [False]

def worker(started, cont, interrupted):
iterations = 100_000_000
started[0] = True
while cont[0]:
if iterations:
iterations -= 1
else:
return
pass
interrupted[0] = True

t = threading.Thread(target=worker,args=(started, cont, interrupted))
t.start()
while not started[0]:
pass
cont[0] = False
t.join()
self.assertTrue(interrupted[0])


class AtexitTests(unittest.TestCase):

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Add support for building with clang thin lto via --with-lto=thin/full. Patch
by Dong-hee Na and Brett Holman.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Update ``property_descr_set`` to use vectorcall if possible. Patch by Dong-hee
Na.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Make :mod:`pprint` (like the builtin ``print``) not attempt to write to ``stdout`` when it is ``None``.
25 changes: 18 additions & 7 deletions Objects/descrobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -1614,31 +1614,42 @@ property_descr_set(PyObject *self, PyObject *obj, PyObject *value)
propertyobject *gs = (propertyobject *)self;
PyObject *func, *res;

if (value == NULL)
if (value == NULL) {
func = gs->prop_del;
else
}
else {
func = gs->prop_set;
}

if (func == NULL) {
if (gs->prop_name != NULL) {
PyErr_Format(PyExc_AttributeError,
value == NULL ?
"can't delete attribute %R" :
"can't set attribute %R",
gs->prop_name);
} else {
}
else {
PyErr_SetString(PyExc_AttributeError,
value == NULL ?
"can't delete attribute" :
"can't set attribute");
}
return -1;
}
if (value == NULL)

if (value == NULL) {
res = PyObject_CallOneArg(func, obj);
else
res = PyObject_CallFunctionObjArgs(func, obj, value, NULL);
if (res == NULL)
}
else {
PyObject *args[] = { obj, value };
res = PyObject_Vectorcall(func, args, 2, NULL);
}

if (res == NULL) {
return -1;
}

Py_DECREF(res);
return 0;
}
Expand Down
7 changes: 6 additions & 1 deletion Python/ceval.c
Original file line number Diff line number Diff line change
Expand Up @@ -3638,14 +3638,17 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, PyFrameObject *f, int throwflag)
if (Py_IsFalse(cond)) {
Py_DECREF(cond);
JUMPTO(oparg);
CHECK_EVAL_BREAKER();
DISPATCH();
}
err = PyObject_IsTrue(cond);
Py_DECREF(cond);
if (err > 0)
;
else if (err == 0)
else if (err == 0) {
JUMPTO(oparg);
CHECK_EVAL_BREAKER();
}
else
goto error;
DISPATCH();
Expand All @@ -3662,12 +3665,14 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, PyFrameObject *f, int throwflag)
if (Py_IsTrue(cond)) {
Py_DECREF(cond);
JUMPTO(oparg);
CHECK_EVAL_BREAKER();
DISPATCH();
}
err = PyObject_IsTrue(cond);
Py_DECREF(cond);
if (err > 0) {
JUMPTO(oparg);
CHECK_EVAL_BREAKER();
}
else if (err == 0)
;
Expand Down
4 changes: 2 additions & 2 deletions aclocal.m4
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# generated automatically by aclocal 1.16.1 -*- Autoconf -*-
# generated automatically by aclocal 1.16.3 -*- Autoconf -*-

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

# This file is free software; the Free Software Foundation
# gives unlimited permission to copy and/or distribute it,
Expand Down
72 changes: 54 additions & 18 deletions configure
Original file line number Diff line number Diff line change
Expand Up @@ -1545,7 +1545,8 @@ Optional Packages:
--with-trace-refs enable tracing references for debugging purpose
(default is no)
--with-assertions build with C assertions enabled (default is no)
--with-lto enable Link-Time-Optimization in any build (default
--with-lto=[full|thin|no|yes]
enable Link-Time-Optimization in any build (default
is no)
--with-hash-algorithm=[fnv|siphash24]
select hash algorithm for use in Python/pyhash.c
Expand Down Expand Up @@ -3039,27 +3040,27 @@ VERSION=3.11

SOVERSION=1.0

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

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


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

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


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

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


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

Expand Down Expand Up @@ -6585,16 +6586,36 @@ $as_echo_n "checking for --with-lto... " >&6; }
# Check whether --with-lto was given.
if test "${with_lto+set}" = set; then :
withval=$with_lto;
if test "$withval" != no
then
Py_LTO='true'
{ $as_echo "$as_me:${as_lineno-$LINENO}: result: yes" >&5
$as_echo "yes" >&6; };
else
Py_LTO='false'
{ $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5
$as_echo "no" >&6; };
fi
case "$withval" in
full)
Py_LTO='true'
Py_LTO_POLICY='full'
{ $as_echo "$as_me:${as_lineno-$LINENO}: result: yes" >&5
$as_echo "yes" >&6; }
;;
thin)
Py_LTO='true'
Py_LTO_POLICY='thin'
{ $as_echo "$as_me:${as_lineno-$LINENO}: result: yes" >&5
$as_echo "yes" >&6; }
;;
yes)
Py_LTO='true'
Py_LTO_POLICY='default'
{ $as_echo "$as_me:${as_lineno-$LINENO}: result: yes" >&5
$as_echo "yes" >&6; }
;;
no)
Py_LTO='false'
{ $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5
$as_echo "no" >&6; }
;;
*)
Py_LTO='false'
as_fn_error $? "unknown lto option: '$withval'" "$LINENO" 5
;;
esac

else
{ $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5
$as_echo "no" >&6; }
Expand Down Expand Up @@ -6732,15 +6753,30 @@ $as_echo "$as_me: llvm-ar found via xcrun: ${LLVM_AR}" >&6;}
case $ac_sys_system in
Darwin*)
# Any changes made here should be reflected in the GCC+Darwin case below
LTOFLAGS="-flto -Wl,-export_dynamic"
LTOCFLAGS="-flto"
if test $Py_LTO_POLICY = default
then
LTOFLAGS="-flto -Wl,-export_dynamic"
LTOCFLAGS="-flto"
else
LTOFLAGS="-flto=${Py_LTO_POLICY} -Wl,-export_dynamic"
LTOCFLAGS="-flto=${Py_LTO_POLICY}"
fi
;;
*)
LTOFLAGS="-flto"
if test $Py_LTO_POLICY = default
then
LTOFLAGS="-flto"
else
LTOFLAGS="-flto=${Py_LTO_POLICY}"
fi
;;
esac
;;
*gcc*)
if test $Py_LTO_POLICY = thin
then
as_fn_error $? "thin lto is not supported under gcc compiler." "$LINENO" 5
fi
case $ac_sys_system in
Darwin*)
LTOFLAGS="-flto -Wl,-export_dynamic"
Expand Down
Loading