Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix swig ownership when assigning to struct pointer field #3139

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 1 addition & 24 deletions faiss/python/class_wrappers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1066,28 +1066,6 @@ def add_to_referenced_objects(self, ref):
else:
self.referenced_objects.append(ref)

class RememberSwigOwnership:
"""
SWIG's seattr transfers ownership of SWIG wrapped objects to the class
(btw this seems to contradict https://www.swig.org/Doc1.3/Python.html#Python_nn22
31.4.2)
This interferes with how we manage ownership: with the referenced_objects
table. Therefore, we reset the thisown field in this context manager.
"""

def __init__(self, obj):
self.obj = obj

def __enter__(self):
if hasattr(self.obj, "thisown"):
self.old_thisown = self.obj.thisown
else:
self.old_thisown = None

def __exit__(self, *ignored):
if self.old_thisown is not None:
self.obj.thisown = self.old_thisown


def handle_SearchParameters(the_class):
""" this wrapper is to enable initializations of the form
Expand All @@ -1102,8 +1080,7 @@ def replacement_init(self, **args):
self.original_init()
for k, v in args.items():
assert hasattr(self, k)
with RememberSwigOwnership(v):
setattr(self, k, v)
setattr(self, k, v)
if type(v) not in (int, float, bool, str):
add_to_referenced_objects(self, v)

Expand Down
14 changes: 14 additions & 0 deletions faiss/python/swigfaiss.swig
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,20 @@ typedef uint64_t size_t;
Py_END_ALLOW_THREADS
}

/********************************************************
* Don't transfer ownership when assigning to a struct field
********************************************************/

// https://github.com/swig/swig/issues/2709
// override default behavior
%typemap(in, noblock=1) SWIGTYPE * (void *argp = 0, int res = 0) {
res = SWIG_ConvertPtr($input, &argp,$descriptor, /* $disown | */ %convertptr_flags);
if (!SWIG_IsOK(res)) {
%argument_fail(res, "$type", $symname, $argnum);
}
$1 = %reinterpret_cast(argp, $ltype);
}

#endif


Expand Down
7 changes: 7 additions & 0 deletions tests/test_search_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,13 @@ def test_ownership(self):
self.assertTrue(sel1.this.own())
self.assertTrue(sel2.this.own())

def test_ownership_2(self):
subset = np.arange(0, 5000000)
sel = faiss.IDSelectorBatch(subset)
assert sel.this.own() # True: correct
_ = faiss.SearchParameters(sel=sel)
assert sel.this.own() # False: why???


class TestSelectorCallback(unittest.TestCase):

Expand Down