Skip to content

Commit 7fa10a9

Browse files
committed
Add StrArray.assign_to()
Enable the StrArray() context manager to be used for assigning a list of strings to a pre-existing git_strarray structure. This is useful when some other structure contains a git_strarray (rather than a pointer to it).
1 parent 88580c2 commit 7fa10a9

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

pygit2/utils.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,16 @@ class StrArray:
9393
9494
with StrArray(list_of_strings) as arr:
9595
C.git_function_that_takes_strarray(arr.ptr)
96+
97+
To make a pre-existing git_strarray point to the provided list of strings,
98+
use the context manager's assign_to() method:
99+
100+
struct = ffi.new('git_strarray *', [ffi.NULL, 0])
101+
with StrArray(list_of_strings) as arr:
102+
arr.assign_to(struct)
103+
104+
The above construct is still subject to FFI scoping rules, i.e. the
105+
contents of 'struct' only remain valid within the StrArray context.
96106
"""
97107

98108
def __init__(self, l):
@@ -126,6 +136,14 @@ def __exit__(self, type, value, traceback):
126136
def ptr(self):
127137
return self.array
128138

139+
def assign_to(self, git_strarray):
140+
if self.array == ffi.NULL:
141+
git_strarray.strings = ffi.NULL
142+
git_strarray.count = 0
143+
else:
144+
git_strarray.strings = self._arr
145+
git_strarray.count = len(self._strings)
146+
129147

130148
class GenericIterator:
131149
"""Helper to easily implement an iterator.

0 commit comments

Comments
 (0)