Skip to content

Commit

Permalink
Add BasePoolArray.raw_access
Browse files Browse the repository at this point in the history
  • Loading branch information
touilleMan committed Apr 11, 2018
1 parent 7540875 commit e9347d6
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
14 changes: 14 additions & 0 deletions pythonscript/embedded/godot/pool_arrays.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from collections.abc import MutableSequence
from contextlib import contextmanager
from pythonscriptcffi import lib, ffi

from godot.hazmat.base import BaseBuiltinWithGDObjOwnership
Expand Down Expand Up @@ -106,6 +107,15 @@ def __delitem__(self, idx):
def __len__(self):
return self._gd_array_size(self._gd_ptr)

@contextmanager
def raw_access(self):
write_access = self._gd_array_write(self._gd_ptr)
try:
yield self._gd_array_write_access_ptr(write_access)

finally:
self._gd_array_write_access_destroy(write_access)

# Methods

def append(self, value):
Expand Down Expand Up @@ -157,6 +167,10 @@ def _copy_gdobj(gdobj):
"invert",
"push_back",
"resize",
# Raw access APIs
"write",
"write_access_ptr",
"write_access_destroy",
):
nmspc["_gd_array_%s" % suffix] = getattr(lib, "godot_%s_%s" % (gdname, suffix))
if py_to_gd:
Expand Down
17 changes: 17 additions & 0 deletions tests/bindings/test_pool_arrays.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,3 +340,20 @@ def test_as_both(self):
assert len(a[0]) == 2000
a[0].resize(3000)
assert len(a[0]) == 3000


class TestPoolArrayRawAccess:

def test_raw_access(self):
arr = PoolIntArray()
arr.resize(30)

with arr.raw_access() as ptr:
for i in range(30):
ptr[i] = i
assert arr == [i for i in range(30)]

# Also test read access
with arr.raw_access() as ptr:
for i in range(30):
assert ptr[i] == i

0 comments on commit e9347d6

Please sign in to comment.