1

I am working on a code in Python 2.7. I use a very specific library written partially in C with a Python API. This library uses printf to output to stdout. I would like to suppress that. I already found this question: How do I prevent a C shared library to print on stdout in python? All of the provided answers use sys.stdout.fileno(). When I run my code I get:

original_stdout_fd = sys.stdout.fileno()
AttributeError: 'FlushingStringIO' object has no attribute 'fileno'

I suspect the problem is that I am using Python 2.7. Are my assumptions correct and is there a way to achieve this with Python 2.7?

Mike Spencer
  • 85
  • 2
  • 10
  • 1
    `original_stdout_fd = 1` is likely to work too. – Eric Oct 08 '19 at 18:45
  • 2
    `sys.stdout` defaults to an instance of `file` in Python 2, not `FlushingStringIO` (whatever that is), so this isn't really a 2-vs-3 question. – chepner Oct 08 '19 at 18:50
  • 1
    `sys.stdout.fileno()` works in Python 2 as well; the problem is that the OP's code base has already replaced `sys.stdout` with an object of some other type that *doesn't* have a `fileno` method. – chepner Oct 08 '19 at 18:51
  • I cannot reproduce your problem on Python 2.7.10. – jjramsey Oct 08 '19 at 18:52
  • I see the problem now @chepner. Cant you think a way around it? – Mike Spencer Oct 08 '19 at 19:07
  • Not without knowing what `FlushingStringIO` is or why it is being used, short of just replacing `sys.stdout` with its original value (available from `sys.__stdout__`). – chepner Oct 08 '19 at 19:11
  • @chepner I do understand. Now that I know the problem is not Python version I found another possibility with wurlitzer.sys_pipes() from https://github.com/minrk/wurlitzer but it actually broke the functionality of https://github.com/flexible-atomic-code/fac which I suspect replaced sys.stdout in the first place. – Mike Spencer Oct 08 '19 at 19:20

1 Answers1

0

The problem was that sys.stdout was replaced by FlushingStringIO by a library: https://github.com/flexible-atomic-code/fac. It also somehow depends on this detail and breaks otherwise. As there is no reasonable alternative to the library there is no way to suppress stdout output in this case.

Mike Spencer
  • 85
  • 2
  • 10