GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — MAINT: add more comments · numpy/numpy@366db47

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • numpy/_core
    • src/multiarray
      • descriptor.c
    • tests
      • test_dtype.py
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎numpy/_core/src/multiarray/descriptor.c‎

Copy file name to clipboardExpand all lines: numpy/_core/src/multiarray/descriptor.c
+17Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2097,13 +2097,30 @@ arraydescr_dealloc(PyArray_Descr *self)
20972097
* reference (descriptors support neither weakrefs nor GC), so
20982098
* stealing the link is unobservable and each node still runs
20992099
* its own, now shallow, dealloc.
2100+
*
2101+
* If descriptors ever become heap types and/or GC types, we can
2102+
* use CPython's stack protection via the trashcan macros
2103+
* instead.
21002104
*/
21012105
PyArray_Descr *base = lself->subarray->base;
21022106
PyArray_free(lself->subarray);
2107+
/*
2108+
* The Py_REFCNT(..) == 1 check is intentional. This happens in
2109+
* a deallocator for a type that doesn't support weakrefs and
2110+
* isn't a GC type, so it's impossible for another thread to
2111+
* concurrently change the reference count if we observe the
2112+
* reference count is 1 here. We can't use
2113+
* PyUnstable_Object_IsUniquelyReferenced because that excludes
2114+
* objects on remote threads.
2115+
*/
21032116
while (base != NULL && Py_REFCNT(base) == 1 && PyDataType_HASSUBARRAY(base)) {
21042117
_PyArray_LegacyDescr *lbase = (_PyArray_LegacyDescr *)base;
2118+
// steal reference owned by lbase and stash it in base
2119+
// (Py_CLEAR without a DECREF)
21052120
base = lbase->subarray->base;
21062121
lbase->subarray->base = NULL;
2122+
// lbase no longer owns a reference to base, so its deallocator
2123+
// doesn't fire
21072124
Py_DECREF(lbase);
21082125
}
21092126
Py_XDECREF(base);
Collapse file

‎numpy/_core/tests/test_dtype.py‎

Copy file name to clipboardExpand all lines: numpy/_core/tests/test_dtype.py
+19-19Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -902,25 +902,25 @@ def test_tuple_recursion(self):
902902
@pytest.mark.skipif(not HAS_SUBPROCESSES,
903903
reason="platform cannot start subprocesses")
904904
def test_deep_subarray_dtype_dealloc(self):
905-
script = textwrap.dedent("""
906-
import threading
907-
908-
import numpy as np
909-
910-
def build_and_drop():
911-
d = np.dtype(np.int32)
912-
for _ in range(200000):
913-
d = np.dtype((d, (1,)))
914-
held = d.base
915-
del d
916-
del held
917-
918-
threading.stack_size(1024 * 1024)
919-
t = threading.Thread(target=build_and_drop)
920-
t.start()
921-
t.join()
922-
""")
923-
run_subprocess([sys.executable, "-c", script], timeout=180)
905+
import threading
906+
907+
import numpy as np
908+
909+
def build_and_drop():
910+
d = np.dtype(np.int32)
911+
for _ in range(200000):
912+
d = np.dtype((d, (1,)))
913+
# hold a second reference so teardown exercises stopping
914+
# and resuming
915+
held = d.base
916+
del d
917+
del held
918+
919+
# small stack size to fail reliably if deallocation is recusive
920+
threading.stack_size(1024 * 1024)
921+
t = threading.Thread(target=build_and_drop)
922+
t.start()
923+
t.join()
924924

925925
@requires_deep_recursion
926926
def test_dict_recursion(self):

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.