python / python/cpython

missing attribute in handler shutdown

Aberta
#153,812 2 comentários 0 reações 0 responsáveis Ver no GitHub

Ninguém assumiu esta issue ainda.

pending
Linguagem predominante
Python
Estrelas
77.2k
Forks
36k
Métricas de merge de PRs
Métricas de PR pendentes

Descrição

Bug report

Bug description:

My code uses the logging handler package (https://docs.python.org/3/library/logging.handlers.html). On closing, I was getting

Exception ignored in atexit callback <function shutdown at 0x0000017934F72770>:
Traceback (most recent call last):
File "...._init_.py", line 2244, in shutdown
if getattr(h, 'flushOnClose', True):
RuntimeError: wrapped C/C++ object of type QTextEditLogger has been deleted

'QTextEditLogger' being a child class of Handler:

class QTextEditLogger(Handler, QObject):
    appendPlainText = pyqtSignal(str)

    def __init__(self, parent):
        Handler.__init__(self) 
        QObject.__init__(self)
        ....

when the program closed.
I finally traced the problem back to the shutdown() function in the getLogger class.

       if h:
           try:
              h.acquire()
              # MemoryHandlers might not want to be flushed on close,
              # but circular imports prevent us scoping this to just
              # those handlers.  hence the default to True.
              if getattr(h, 'flushOnClose', True):
                  h.flush()
              h.close()

Not all handlers have the 'flushOnClose' attribute! So it throws an exception, as per the documentation, BTW, when getattr() is called and 'flushOnClose' isn't there.

The fix is simple, I added the hasattr() function to see if the attribute exists:

       **if hasattr(h, 'flushOnClose'):**                     
             if getattr(h, 'flushOnClose', True):
                h.flush()
        h.close()

CPython versions tested on:

3.14

Operating systems tested on:

Windows

Guia de contribuição

Abrir o guia de contribuição

Primeiros passos

  1. Leia a issue inteira e depois o guia de contribuição do projeto.
  2. Comente na issue dizendo que vai assumir — evita que duas pessoas façam o mesmo trabalho.
  3. Faça um fork do repositório e trabalhe em uma branch.
  4. Abra um pull request que referencie o número da issue.

Direção de pesquisa

Comece em init.py, próximo ao código de shutdown() mostrado no relatório, e inspecione como a limpeza do handler se comporta para o exemplo QTextEditLogger no CPython 3.14. Reproduza o traceback do encerramento no Windows e, em seguida, verifique se o caminho de limpeza corrigido não gera mais a exceção relatada, preservando o flush e o fechamento normais do handler.

Escrita pelo modelo de indexação a partir do texto da issue.

Avaliação

Stack de tecnologia
python
Domínio
tooling
Tipo de issue
Bug
Dificuldade
3/5
Tempo estimado
1-2 dias
Status de atividade
Pouca atividade
Clareza
Razoavelmente clara
Facilidade para iniciantes
52/100

Receba novas issues na sua caixa de entrada

Um resumo curto de issues do GitHub para quem está começando.