python / python/cpython

missing attribute in handler shutdown

Đang mở
#153,812 2 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

pending
Ngôn ngữ chính
Python
Star
77.2k
Fork
35.9k
Chỉ số merge pull request
Chỉ số pull request đang chờ

Mô tả

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

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Bắt đầu trong init.py, tại phần mã shutdown() được hiển thị trong báo cáo, và kiểm tra cách hoạt động của việc dọn dẹp handler đối với ví dụ QTextEditLogger trên CPython 3.14. Tái hiện traceback khi tắt Windows, sau đó xác minh rằng đường dẫn dọn dẹp đã được sửa không còn phát sinh ngoại lệ được báo cáo, đồng thời vẫn duy trì việc flush và đóng handler như bình thường.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
python
Lĩnh vực
tooling
Loại issue
Lỗi
Độ khó
3/5
Thời gian dự kiến
1-2 ngày
Mức độ hoạt động
Ít trao đổi
Độ rõ ràng
Khá rõ ràng
Mức phù hợp với người mới
52/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.