Skip to content

Commit cbb0b0a

Browse files
committed
Plumb optional pretty argument into the print() function.
1 parent d74a963 commit cbb0b0a

File tree

6 files changed

+64
-14
lines changed

6 files changed

+64
-14
lines changed

Include/internal/pycore_global_objects_fini_generated.h

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Include/internal/pycore_global_strings.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -688,6 +688,7 @@ struct _Py_global_strings {
688688
STRUCT_FOR_ID(posix)
689689
STRUCT_FOR_ID(prec)
690690
STRUCT_FOR_ID(preserve_exc)
691+
STRUCT_FOR_ID(pretty)
691692
STRUCT_FOR_ID(print_file_and_line)
692693
STRUCT_FOR_ID(priority)
693694
STRUCT_FOR_ID(progress)

Include/internal/pycore_runtime_init_generated.h

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Include/internal/pycore_unicodeobject_generated.h

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Python/bltinmodule.c

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2213,6 +2213,8 @@ print as builtin_print
22132213
a file-like object (stream); defaults to the current sys.stdout.
22142214
flush: bool = False
22152215
whether to forcibly flush the stream.
2216+
pretty: object = None
2217+
a pretty-printing object, None, or True.
22162218
22172219
Prints the values to a stream, or to sys.stdout by default.
22182220
@@ -2221,10 +2223,11 @@ Prints the values to a stream, or to sys.stdout by default.
22212223
static PyObject *
22222224
builtin_print_impl(PyObject *module, PyObject * const *objects,
22232225
Py_ssize_t objects_length, PyObject *sep, PyObject *end,
2224-
PyObject *file, int flush)
2225-
/*[clinic end generated code: output=38d8def56c837bcc input=ff35cb3d59ee8115]*/
2226+
PyObject *file, int flush, PyObject *pretty)
2227+
/*[clinic end generated code: output=2c26c52acf1807b9 input=e5c1e64da822042c]*/
22262228
{
22272229
int i, err;
2230+
PyObject *printer = NULL;
22282231

22292232
if (file == Py_None) {
22302233
file = PySys_GetAttr(&_Py_ID(stdout));
@@ -2262,6 +2265,31 @@ builtin_print_impl(PyObject *module, PyObject * const *objects,
22622265
Py_DECREF(file);
22632266
return NULL;
22642267
}
2268+
if (pretty == Py_True) {
2269+
/* Use default `pprint.PrettyPrinter` */
2270+
PyObject *printer_factory = PyImport_ImportModuleAttrString("pprint", "PrettyPrinter");
2271+
PyObject *printer = NULL;
2272+
2273+
if (!printer_factory) {
2274+
Py_DECREF(file);
2275+
return NULL;
2276+
}
2277+
printer = PyObject_CallNoArgs(printer_factory);
2278+
Py_DECREF(printer_factory);
2279+
2280+
if (!printer) {
2281+
Py_DECREF(file);
2282+
return NULL;
2283+
}
2284+
}
2285+
else if (pretty == Py_None) {
2286+
/* Don't use a pretty printer */
2287+
}
2288+
else {
2289+
/* Use the given object as the pretty printer */
2290+
printer = pretty;
2291+
Py_INCREF(printer);
2292+
}
22652293

22662294
for (i = 0; i < objects_length; i++) {
22672295
if (i > 0) {
@@ -2273,12 +2301,14 @@ builtin_print_impl(PyObject *module, PyObject * const *objects,
22732301
}
22742302
if (err) {
22752303
Py_DECREF(file);
2304+
Py_XDECREF(printer);
22762305
return NULL;
22772306
}
22782307
}
22792308
err = PyFile_WriteObject(objects[i], file, Py_PRINT_RAW);
22802309
if (err) {
22812310
Py_DECREF(file);
2311+
Py_XDECREF(printer);
22822312
return NULL;
22832313
}
22842314
}
@@ -2291,16 +2321,19 @@ builtin_print_impl(PyObject *module, PyObject * const *objects,
22912321
}
22922322
if (err) {
22932323
Py_DECREF(file);
2324+
Py_XDECREF(printer);
22942325
return NULL;
22952326
}
22962327

22972328
if (flush) {
22982329
if (_PyFile_Flush(file) < 0) {
22992330
Py_DECREF(file);
2331+
Py_XDECREF(printer);
23002332
return NULL;
23012333
}
23022334
}
23032335
Py_DECREF(file);
2336+
Py_XDECREF(printer);
23042337

23052338
Py_RETURN_NONE;
23062339
}

Python/clinic/bltinmodule.c.h

Lines changed: 22 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)