[issue28864] Add devnull file-like object

Martin Panter report at bugs.python.org
Sun Dec 4 21:30:08 EST 2016


Martin Panter added the comment:

If you only need the readable interface, use BytesIO or StringIO.

I once had an implementation like Serhiy’s, called dummywriter: <https://github.com/vadmium/python-lib/blob/99ec887/streams.py#L12>. To fully implement the writable file API it should also implement writable(), and write() should return the size of its argument.

Implementing this without opening a real file descriptor is slightly easier to manage (no need to worry about closing it, and no problem sharing instances or creating many copies). I don’t have a strong opinion about including it in the builtin library though, since it is pretty easy to implement the basics yourself.

I doubt anyone would need a readable and writable object, i.e. open(devnull, "r+"). On the other hand, it is occasionally useful to know how many bytes were written in total, so implementing tell() etc could be useful. (Linux’s /dev/null doesn’t work that way though; I find it always returns zero.)

----------
nosy: +martin.panter

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue28864>
_______________________________________


More information about the Python-bugs-list mailing list