Puts function for PyObjects

I’ve noticed I have a lot of PyObject_Print in my code, and every single one of them uses the following stdout and Py_PRINT_RAW, followed by a puts("") to add the newline. Perhaps a macro could be added to make this simpler?

#define PyObject_Puts(obj) do { PyObject_Print(obj, stdout, Py_PRINT_RAW); puts(""); } while (0)

I get that this macro is easy to implement yourself, but I don’t see why CPython couldn’t officially support this.

It looks like something very specific to your application. I do not think that PyObject_Print() is much used in the modern code. It is superseded by PySys_FormatStdout/PySys_FormatStderr/PyFile_WriteObject which work with Python streams instead of C files.

3 Likes

I see. Maybe PyObject_Print should get deprecated then? It’s not a part of the stable ABI according to the docs.

What good will deprecating PyObject_Print bring? It does not pose a security threat and is not fundamentally broken. Its maintain has almost zero cost. It will probably not be included in the next global C API revision. On the other hand, it can remain as a CPython-specific function for those cases where the use of Python streams is not possible (early startup or later shutdown phases).

2 Likes

Well, you did say it was superseded, I just assumed it wasn’t beneficial then. If it still has uses, a puts variation of it could be helpful.