1

[EDIT] It is not important why I do want to have a dummy of writeable file-like object, but I would like to avoid unnecessary possibility of a memory leak io.StringIO.

Is there any standard (preferably stl, but any widely accepted by the community will do) implementation of such object?

According to Does the same answer imply that the questions should be closed as duplicate? the question is not a duplicate of Suppress stdout / stderr print from Python functions as it is about a standard dummy object, not about the suppression of stderr. Such object may be useful also for mocking etc.

Indeed, one of the answers there (linking to NullDevice) is compatible with my present solution.

abukaj
  • 2,582
  • 1
  • 22
  • 45
  • I recommend you follow the context manager approach outlined in the duplicate comment. – BoarGules Apr 09 '18 at 18:40
  • this is a dup ... you literally say like `2> /dev/null` but in your script ... if you want to capture the output to disk you can open any file other than devnull, if you want to capture the output to memory usually people use StringIO ... although there is certainly nothing stoping you from writing your own mock class (or even just using pymock)... if you can explain why this is not a dupe better, and convince me, I will undupe it, if you can explain better then the community can also undupe it (alternatively there are also other implementations in that answer) – Joran Beasley Apr 09 '18 at 18:47
  • @JoranBeasley to avoid misunderstanding I decided to hide the rationale behind my question. It is solely about a dummy object now (also I am reading the answers you linked to, as one of them may be the one I am looking for). – abukaj Apr 09 '18 at 18:53
  • Why do you think writing to `StringIO` would cause a "memory leak"? – ekhumoro Apr 09 '18 at 19:04
  • @ekhumoro `StringIO` is a buffer that will consume memory to store unnecessary information. If there is a lot of writing to `stderr` - there would be a serious memory leak. – abukaj Apr 09 '18 at 19:07
  • @JoranBeasley one of the answers in the question you have linked is compatible with my current solution, so I will keep with it. – abukaj Apr 09 '18 at 19:14
  • @BoarGules in one particular scenario I am unable to use context manager (no indentation allowed) but in general I agree. – abukaj Apr 09 '18 at 19:16
  • 1
    @abukaj. By definition, memory leaks are *always* unintentional. The `StringIO` solution is deliberately intended to *capture* stdout/stderr, whereas it sounds like you actually want to *suppress* stdout/stderr. The title of your question is therefore quite misleading. Anyway, it is trivial to write a file-like class that implements dummy `write` and `flush` methods, so an stl solution isn't really needed. – ekhumoro Apr 09 '18 at 19:21
  • @ekhumoro agreed. My intention is not to store unnecessary data (thus I consider keeping it in RAM a memory leak). – abukaj Apr 09 '18 at 19:35
  • @abukaj Python without indentation is like wine without grapes. But if you *must* have a one-liner (I presume, as input from a command line in a shell that doesn't understand embedded carriage returns) you still can: the `with` clause and the statement that is in its scope can be on the same line, just as with a one-line `if`. – BoarGules Apr 10 '18 at 08:14
  • @BoarGules the case I have in mind is about a code snippet in a literate programming paradigm, not about production code. The snippet should not contain any irrelevant details (as `stderr` output suppression of a message on module import). However it is even more complicated (the weave tool seems to replace `sys.stderr` with its own implementation for every snippet) so I have solved the issue in a completely different way. – abukaj Apr 10 '18 at 09:38

0 Answers0