@@ -2270,6 +2270,7 @@ builtin_print_impl(PyObject *module, PyObject * const *objects,
22702270 PyObject * printer_factory = PyImport_ImportModuleAttrString ("pprint" , "PrettyPrinter" );
22712271 PyObject * printer = NULL ;
22722272
2273+ PyObject_Print (printer_factory , stderr , 0 );
22732274 if (!printer_factory ) {
22742275 Py_DECREF (file );
22752276 return NULL ;
@@ -2281,6 +2282,7 @@ builtin_print_impl(PyObject *module, PyObject * const *objects,
22812282 Py_DECREF (file );
22822283 return NULL ;
22832284 }
2285+ PyObject_Print (printer , stderr , 0 );
22842286 }
22852287 else if (pretty == Py_None ) {
22862288 /* Don't use a pretty printer */
@@ -2305,7 +2307,33 @@ builtin_print_impl(PyObject *module, PyObject * const *objects,
23052307 return NULL ;
23062308 }
23072309 }
2308- err = PyFile_WriteObject (objects [i ], file , Py_PRINT_RAW );
2310+ /* XXX: I have a couple of thoughts about how this could be handled. We could add a
2311+ PyFile_WriteObjectEx() function which would look largely like PyFile_WriteObject() but
2312+ would take a pretty printer object (or None, in which case it would just fall back to
2313+ PyFile_WriteObject()). Then we could put the logic for the (TBD) "pretty printing
2314+ protocol" in there.
2315+
2316+ For now though, let's keep things localized so all the logic is in the print() function's
2317+ implementation. Maybe a better way will come to mind as we pan this idea out.
2318+
2319+ Or, this currently calls `printer.pformat(object)` so a pretty printing protocol could
2320+ be implemented there. Or maybe we want a more generic method name.
2321+ */
2322+ PyObject_Print (printer , stderr , 0 );
2323+ if (printer ) {
2324+ PyObject * prettified = PyObject_CallMethod (printer , "pformat" , "O" , objects [i ]);
2325+
2326+ if (!prettified ) {
2327+ Py_DECREF (file );
2328+ Py_DECREF (printer );
2329+ return NULL ;
2330+ }
2331+ err = PyFile_WriteObject (prettified , file , Py_PRINT_RAW );
2332+ Py_XDECREF (prettified );
2333+ }
2334+ else {
2335+ err = PyFile_WriteObject (objects [i ], file , Py_PRINT_RAW );
2336+ }
23092337 if (err ) {
23102338 Py_DECREF (file );
23112339 Py_XDECREF (printer );
0 commit comments