"""PyQt6 abi3 wheels drift the refcount of None; on Python 3.14 None loses immortality.

Run:  python pyqt6_none_refcount_repro.py           (quick: a few seconds)
      python pyqt6_none_refcount_repro.py --full    (full repro: about 5 minutes)

Requires CPython 3.14, 64-bit, GIL build.
"""
import ctypes
import os
import sys

os.environ.setdefault("QT_QPA_PLATFORM", "offscreen")

from PyQt6.QtCore import PYQT_VERSION_STR, QT_VERSION_STR, QObject, QPoint
from PyQt6.QtWidgets import QApplication


def low32():
    return ctypes.c_uint32.from_address(id(None)).value


def overflow():
    return ctypes.c_uint16.from_address(id(None) + 4).value


class A:
    def __init__(self):
        pass


app = QApplication(sys.argv)
print(f"Python {sys.version.split()[0]}, PyQt6 {PYQT_VERSION_STR}, Qt {QT_VERSION_STR}")
print(f"None refcount at start: low32={low32():#010x} ob_overflow={overflow()}")

N = 100_000
p, o = QPoint(), QObject()
for label, fn in (("QPoint.setX()", lambda: p.setX(5)),
                  ("QObject.setObjectName()", lambda: o.setObjectName("x")),
                  ("QApplication.processEvents()", app.processEvents),
                  ("QPoint.x()  [returns int]", p.x)):
    before = low32()
    for _ in range(N):
        fn()
    print(f"{label:30} None refcount delta per call: {(low32() - before) / N:+.2f}")

if "--full" in sys.argv:
    f = p.setX
    while overflow() == 0:          # about 1.07e9 calls in total
        for _ in range(5_000_000):
            f(5)
    print(f"None refcount now: low32={low32():#010x} ob_overflow={overflow()}")
    A()   # TypeError: __init__() should return None, not 'NoneType'
