Skip to content

Commit

Permalink
Merge pull request #311 from lgarrison/master
Browse files Browse the repository at this point in the history
Clear the f_locals dict to avoid extra ref counts in the calling scop…
  • Loading branch information
robbmcleod committed Aug 10, 2018
2 parents 54fc031 + 08e5c6f commit efd333d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 10 deletions.
32 changes: 22 additions & 10 deletions numexpr/necompiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -724,18 +724,30 @@ def getExprNames(text, context):
def getArguments(names, local_dict=None, global_dict=None):
"""Get the arguments based on the names."""
call_frame = sys._getframe(2)
if local_dict is None:

local_dict_as_arg = local_dict is not None

if not local_dict_as_arg:
local_dict = call_frame.f_locals
if global_dict is None:
global_dict = call_frame.f_globals

arguments = []
for name in names:
try:
a = local_dict[name]
except KeyError:
a = global_dict[name]
arguments.append(numpy.asarray(a))
try:
if global_dict is None:
global_dict = call_frame.f_globals

arguments = []
for name in names:
try:
a = local_dict[name]
except KeyError:
a = global_dict[name]
arguments.append(numpy.asarray(a))
finally:
# If we generated local_dict via an explicit reference to f_locals,
# clear the dict to prevent creating extra ref counts in the caller's scope
# See https://github.com/pydata/numexpr/issues/310
if not local_dict_as_arg:
local_dict.clear()

return arguments


Expand Down
7 changes: 7 additions & 0 deletions numexpr/tests/test_numexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,13 @@ def test_where_scalar_bool(self):
res = evaluate('where(a, b, c)')
assert_array_equal(res, c)

# Regression test for issue #310
def test_refcount(self):
a = array([1])
assert sys.getrefcount(a) == 2
evaluate('1')
assert sys.getrefcount(a) == 2


class test_numexpr2(test_numexpr):
"""Testing with 2 threads"""
Expand Down

0 comments on commit efd333d

Please sign in to comment.