From 043bed89bd5e922446b0705e5c42ece51940b075 Mon Sep 17 00:00:00 2001
From: Sandro <devel@penguinpee.nl>
Date: Mon, 4 Aug 2025 13:29:58 +0200
Subject: [PATCH] Account for change in ref count in Python 3.14+ (#3098)

Python has changed the way references are counted (borrowed references)
in version 3.14.
---
 tools/python/test/test_numpy_returns.py | 17 +++++++++++++++--
 1 file changed, 15 insertions(+), 2 deletions(-)

diff --git a/tools/python/test/test_numpy_returns.py b/tools/python/test/test_numpy_returns.py
index fd001aacef..48785bb5d3 100644
--- a/tools/python/test/test_numpy_returns.py
+++ b/tools/python/test/test_numpy_returns.py
@@ -56,7 +56,14 @@ def test_regression_issue_1220_get_face_chip():
     # we expect two references:
     # 1.) the local variable
     # 2.) the temporary passed to getrefcount
-    assert sys.getrefcount(face_chip) == 2
+    #
+    # Python 3.14 has changed the way references are counted (borrowed references)
+    # https://docs.python.org/3.14/whatsnew/3.14.html#limited-c-api-changes
+    # https://github.com/davisking/dlib/issues/3096
+    if sys.version_info < (3, 14):
+        assert sys.getrefcount(face_chip) == 2
+    else:
+        assert sys.getrefcount(face_chip) == 1
 
 
 @pytest.mark.skipif(not utils.is_numpy_installed(), reason="requires numpy")
@@ -67,6 +74,12 @@ def test_regression_issue_1220_get_face_chips():
     """
     face_chips = get_test_face_chips()
     count = sys.getrefcount(face_chips)
-    assert count == 2
+    # Python 3.14 has changed the way references are counted (borrowed references)
+    # https://docs.python.org/3.14/whatsnew/3.14.html#limited-c-api-changes
+    # https://github.com/davisking/dlib/issues/3096
+    if sys.version_info < (3, 14):
+        assert count == 2
+    else:
+        assert count == 1
     count = sys.getrefcount(face_chips[0])
     assert count == 2
