Skip to content

Commit

Permalink
Fix missing _free method on the _Image class
Browse files Browse the repository at this point in the history
  • Loading branch information
denisenkom committed Dec 20, 2023
1 parent 4ce4712 commit 5a0c642
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/twain/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,15 +125,16 @@ def _twain1_alloc(size: int) -> ct.c_void_p:
_twain1_unlock = windows.GlobalUnlock

class _Image(_IImage):
def __init__(self, handle):
def __init__(self, handle, free: collections.abc.Callable[[typing.Any], None]):
self._handle = handle
self._free = free

def __del__(self):
self.close()

def close(self):
"""Releases memory of image"""
self._free(self._handle) # type: ignore # needs fixing
self._free(self._handle)
self._handle = None

def save(self, filepath: str):
Expand Down Expand Up @@ -904,7 +905,7 @@ def acquire_natively(
def xfer_ready_callback() -> int:
before(self.image_info)
handle, more = self.xfer_image_natively()
after(_Image(handle), more) # type: ignore # needs fixing
after(_Image(handle=handle, free=self._free), more) # type: ignore # needs fixing
return more

self.set_capability(
Expand Down
14 changes: 14 additions & 0 deletions tests/test_integ.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,17 @@ def test_scan(root_window):
)
twain.global_handle_free(handle)
index += 1


def test_acquire_natively(root_window):
"""
Testing acquire_natively method
"""
logger.info("creating source manager")
with twain.SourceManager(root_window) as sm:
logger.info("opening source")
with sm.open_source() as ss: # this posts a modeless dialog...
logger.info("calling acquire_natively")
ss.acquire_natively(
after=lambda img, no: img.close()
)

0 comments on commit 5a0c642

Please sign in to comment.