apt-get install output behaves differently when executed normally and from python

  apt, c++, pipe, stream

I noticed today that apt behaves differently depending on where (shell / python / …) it is run from.

Background
I am trying to find out the providing package of "watch" via the apt CLI. This works manually also very well:

apt-get install -s watch

NOTE: This is only a simulation!
  apt-get needs root privileges for real execution.
  Keep also in mind that locking is deactivated,
  so don't depend on the relevance to the real current situation!
Reading package lists... Done
Building dependency tree       
Reading state information... Done
Note, selecting 'procps' instead of 'watch'
procps is already the newest version (2:3.3.15-2).
procps set to manually installed.
0 upgraded, 0 newly installed, 0 to remove and 15 not upgraded.

The important line is Note, selecting 'procps' instead of 'watch': Apparently, watch is provided by procps. So far so good.

When I’m executing the same command from python’s subprocess library, this line disappears. With os.system, it is working again.

So I’ve tried to investigate the issue by simply piping the output from my commandline (stdout and stderr) into a log file (apt-get install -s watch &> /tmp/output)
Result:

NOTE: This is only a simulation!
      apt-get needs root privileges for real execution.
      Keep also in mind that locking is deactivated,
      so don't depend on the relevance to the real current situation!
Reading package lists...
Building dependency tree...
Reading state information...
procps is already the newest version (2:3.3.15-2).
procps set to manually installed.
0 upgraded, 0 newly installed, 0 to remove and 15 not upgraded.

The line is missing.

After that I searched around a bit in the source code of apt and found the corresponding line. The output stream here is out, some other ioprintf sections are using c1out instead. So this seems to be another stream… But why is my terminal printing the output of this stream and most other programs aren’t capable to process it?

Source: Windows Questions C++

LEAVE A COMMENT