Tracking ignored exception in Python?

My application has its own audio library, which itself uses the BASS library.

I create and destroy BASS stream objects throughout the program.

When my program exits, by accident (I still do not understand the template), I get the following notification on my console:

Exception TypeError: "'NoneType' object is not callable" in <bound method stream.__del__ of <audio.audio_player.stream object at 0xaeda2f0>> ignored 

My audio library (audio / audio_player.py [Stream class]) contains a class that creates a BASS stream object and then allows the code to manipulate it. When a class is destroyed (in the del subroutine), it calls BASS_StreamFree to clear any resources that BASS could allocate.

(audio_player.py)

 from pybass import * from ctypes import pointer, c_float, c_long, c_ulong, c_buffer import os.path, time, threading # initialize the BASS engine BASS_Init(-1, 44100, 0, 0, None) class stream(object): """Represents a single audio stream""" def __init__(self, file): # check for file existence if (os.path.isfile(file) == False): raise ValueError("File %s not found." % file) # initialize a bass channel self.cAddress = BASS_StreamCreateFile(False, file, 0, 0, 0) def __del__(self): BASS_StreamFree(self.cAddress) def play(self): BASS_ChannelPlay(self.cAddress, True) while (self.playing == False): pass ..more code.. 

My first inclination based on this post is that somewhere in my code an instance of my stream class becomes an orphan (no longer assigned to a variable), and Python is still trying to call its del when the application closes, but by then when he tries to delete an object.

This application does use wxWidgets and therefore uses some threads. The fact that I have not been given the actual name of the variable makes me believe in what I said in the previous paragraph.

I'm not sure which code will be relevant for debugging. The message seems harmless, but I don't like the idea of ​​a “ignored” exception in the final production code.

Are there any tips for debugging?

+6
source share
1 answer

The message that the exception was ignored is due to the fact that all exceptions thrown by the __del__ method __del__ ignored in order to keep the data model reasonable. Here is the relevant part of the docs :

Warning: Due to the volatile circumstances in which the __del__() methods are called, exceptions that occur during their execution are ignored, and a warning is displayed in sys.stderr instead. In addition, when __del__() is called in response to a module to be removed (for example, when a program is executed), other global variables referenced by the __del__() method may have already been deleted or during demolition (for example, closing imported equipment) . For this reason, the __del__() methods must fulfill the absolute minimum necessary to maintain external invariants. Starting with version 1.5, Python ensures that global names whose names begin with a single underscore are removed from their module before other global characters are deleted; if there are no other references to such global tables, this can help ensure that the imported modules are still available when the __del__() method is __del__() .

As for debugging, you can start by placing the try / except block around the code in your __del__ method and print out additional information about the state of the program at the time it occurs. Or you could do less in the __del__ method or completely get rid of it!

+7
source

Source: https://habr.com/ru/post/945313/


All Articles