python / python/cpython

missing attribute in handler shutdown

オープン
#153,812 コメント 2 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

pending
主要言語
Python
スター
77.2k
フォーク
35.9k
PR マージ指標
PR 指標を取得中

説明

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

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

レポートに示されているshutdown()コード付近の__init__.pyから始め、CPython 3.14でのQTextEditLoggerの例におけるハンドラーのクリーンアップ動作を調べます。Windowsのシャットダウン時のトレースバックを再現し、修正後のクリーンアップ経路で報告された例外が発生しなくなることを確認します。同時に、通常のハンドラーのフラッシュとクローズは維持します。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
python
領域
tooling
issue の種類
バグ
難易度
3/5
見積もり時間
1〜2日
活発さ
静か
明瞭さ
おおむね明確
初心者へのやさしさ
52/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。