8000 Merge pull request #20238 from seberg/fixup-transfer-ownership · numpy/numpy@07447fd · GitHub
[go: up one dir, main page]

Skip to content

Commit 07447fd

Browse files
authored
Merge pull request #20238 from seberg/fixup-transfer-ownership
BUG: Fix environment checking logic for `NUMPY_WARN_IF_NO_MEM_POLICY`
2 parents ff33f28 + 8f53afd commit 07447fd

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed

numpy/core/src/multiarray/arrayobject.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,7 @@ array_dealloc(PyArrayObject *self)
503503
}
504504
if (fa->mem_handler == NULL) {
505505
char *env = getenv("NUMPY_WARN_IF_NO_MEM_POLICY");
506-
if ((env == NULL) || (strncmp(env, "1", 1) == 0)) {
506+
if ((env != NULL) && (strncmp(env, "1", 1) == 0)) {
507507
char const * msg = "Trying to dealloc data, but a memory policy "
508508
"is not set. If you take ownership of the data, you must "
509509
"set a base owning the data (e.g. a PyCapsule).";

numpy/core/tests/test_mem_policy.py

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -357,24 +357,39 @@ def test_new_policy(get_module):
357357
c = np.arange(10)
358358
assert np.core.multiarray.get_handler_name(c) == orig_policy_name
359359

360-
def test_switch_owner(get_module):
360+
@pytest.mark.xfail(sys.implementation.name == "pypy",
361+
reason=("bad interaction between getenv and "
362+
"os.environ inside pytest"))
363+
@pytest.mark.parametrize("policy", ["0", "1", None])
364+
def test_switch_owner(get_module, policy):
361365
a = get_module.get_array()
362366
assert np.core.multiarray.get_handler_name(a) is None
363367
get_module.set_own(a)
364368
oldval = os.environ.get('NUMPY_WARN_IF_NO_MEM_POLICY', None)
365-
os.environ['NUMPY_WARN_IF_NO_MEM_POLICY'] = "1"
369+
if policy is None:
370+
if 'NUMPY_WARN_IF_NO_MEM_POLICY' in os.environ:
371+
os.environ.pop('NUMPY_WARN_IF_NO_MEM_POLICY')
372+
else:
373+
os.environ['NUMPY_WARN_IF_NO_MEM_POLICY'] = policy
366374
try:
367375
# The policy should be NULL, so we have to assume we can call
368-
# "free"
369-
with assert_warns(RuntimeWarning) as w:
376+
# "free". A warning is given if the policy == "1"
377+
if policy == "1":
378+
with assert_warns(RuntimeWarning) as w:
379+
del a
380+
gc.collect()
381+
else:
370382
del a
371383
gc.collect()
384+
372385
finally:
373386
if oldval is None:
374-
os.environ.pop('NUMPY_WARN_IF_NO_MEM_POLICY')
387+
if 'NUMPY_WARN_IF_NO_MEM_POLICY' in os.environ:
388+
os.environ.pop('NUMPY_WARN_IF_NO_MEM_POLICY')
375389
else:
376390
os.environ['NUMPY_WARN_IF_NO_MEM_POLICY'] = oldval
377391

392+
def test_owner_is_base(get_module):
378393
a = get_module.get_array_with_base()
379394
with pytest.warns(UserWarning, match='warn_on_free'):
380395
del a

0 commit comments

Comments
 (0)
0