cat
cat

Reputation: 4020

Why does the Python runtime handle warnings this way?

Here's a traceback from a project I'm working on:

/usr/lib/python3/dist-packages/apport/report.py:13: PendingDeprecationWarning: the imp module is deprecated in favour of importlib; see the module's documentation for alternative uses
  import fnmatch, glob, traceback, errno, sys, atexit, locale, imp
Traceback (most recent call last):
  ...
  File "./mouse16.py", line 1050, in _lit_string
    rangeof = range(self.idx.v, self.idx.v + result.span()[1])
AttributeError: 'NoneType' object has no attribute 'span'

Now, there's a since-fixed bug in my code that caused the traceback itself; whatever.

I'm interested in the first line: the PendingDeprecationWarning for not-my-code. I use Ubuntu (as one can tell from apport's existence in the path), which is well-known for packaging and relying on Python for many things, notably things like package management and bug reporting (apport / ubuntu-bug).

imp is indeed deprecated: "Deprecated since version 3.4: The imp package is pending deprecation in favor of importlib.". My machine runs at least Python 3.4.3+ or better and it takes time and a lot of work to modernise and update software completely, so this warning is understandable.

But my program doesn't go anywhere near imp, importlib or apport, so my question is, why isn't a warning deriving from apport's source written to apport's logs or certainly collected by stderr on apport's parent process?

If I had to take a guess at this, it's because the devs decided to buffer -- but never flush nor write -- apport's stderr, and so the next time a python child process on the system opens stderr for writing (as an error in my program did), apport's buffered stderr is written too.

This isn't supported by what I (think I) know about Unix -- why would two separate Python instances interact in this way?


Upon request, here's the best I can do for an MCVE: a list of module-level imports.

import readline
import os
import sys
import warnings
import types
import typing

Is it because I import warnings? But... I still don't touch apport.


I think this question is more on-topic and will get better answers here on SO than AskUbuntu or Unix & Linux; flag it for migration if you feel strongly otherwise but I think the mods will agree with me.

Upvotes: 1

Views: 199

Answers (2)

Martin Konecny
Martin Konecny

Reputation: 59571

so my question is, why isn't a warning deriving from apport's source written to apport's logs or certainly collected by stderr on apport's parent process?

The apport python library isn't running in a separate process here. Sure the actual apport process is separate, but you are interacting/binding to it with library that is local to your code/process.

Since this Python library is using a deprecated module, that is running inside of your process, Python is correctly warning you.

As per Andrew's answer, the apport library is automatically invoked with an uncaught exception.

Upvotes: 2

nobody
nobody

Reputation: 20163

The Apport docs state:

If ... e. g. a packaged Python application raises an uncaught exception, the apport backend is automatically invoked

The copy of Python distributed by Ubuntu has been specifically modified to do this. The exception-handling has been modified/hooked, and the code that gets called when you cause an exception is triggering this warning.

Upvotes: 2

Related Questions