FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-124538: Fix crash when using `gc.get_referents` on an untracked capsule object by ZeroIntensity · Pull Request #124559 · python/cpython · GitHub

/ cpython Public
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .c  (1) .py  (1) .rst  (1) All 3 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
18 changes: 18 additions & 0 deletions Lib/test/test_gc.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -1048,6 +1048,24 @@ class Z:
callback.assert_not_called()
gc.enable()

@cpython_only
def test_get_referents_on_capsule(self):
# gh-124538: Calling gc.get_referents() on an untracked capsule must not crash.
import _datetime
import _socket
untracked_capsule = _datetime.datetime_CAPI
tracked_capsule = _socket.CAPI

# For whoever sees this in the future: if this is failing
# after making datetime's capsule tracked, that's fine -- this isn't something
# users are relying on. Just find a different capsule that is untracked.
self.assertFalse(gc.is_tracked(untracked_capsule))
self.assertTrue(gc.is_tracked(tracked_capsule))

self.assertEqual(len(gc.get_referents(untracked_capsule)), 0)
gc.get_referents(tracked_capsule)



class IncrementalGCTests(unittest.TestCase):

Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed crash when using :func:`gc.get_referents` on a capsule object.
10 changes: 7 additions & 3 deletions Objects/capsule.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -317,10 +317,14 @@ static int
capsule_traverse(PyCapsule *capsule, visitproc visit, void *arg)
{
// Capsule object is only tracked by the GC
// if _PyCapsule_SetTraverse() is called
assert(capsule->traverse_func != NULL);
// if _PyCapsule_SetTraverse() is called, but
// this can still be manually triggered by gc.get_referents()

if (capsule->traverse_func != NULL) {
return capsule->traverse_func((PyObject*)capsule, visit, arg);
}

return capsule->traverse_func((PyObject*)capsule, visit, arg);
return 0;
}


Expand Down

Back | FazBrowse Home | New Git URL