Skip to content

Commit fdf13f5

Browse files
authored
fix: Reduce Python-side allocations churn (#275)
* fix: Reduce Python-side alloc churn * fix: Reduce Python-side alloc churn * fix: Clean up * Bump version from 0.32.11 to 0.32.12
1 parent d948655 commit fdf13f5

2 files changed

Lines changed: 16 additions & 4 deletions

File tree

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
44

55
[project]
66
name = "c2pa-python"
7-
version = "0.32.11"
7+
version = "0.32.12"
88
requires-python = ">=3.10"
99
description = "Python bindings for the C2PA Content Authenticity Initiative (CAI) library"
1010
readme = { file = "README.md", content-type = "text/markdown" }

src/c2pa/c2pa.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1812,7 +1812,17 @@ def read_callback(ctx, data, length):
18121812
if not data or length <= 0:
18131813
return -1
18141814

1815-
buffer = self._file_like_stream.read(length)
1815+
stream = self._file_like_stream
1816+
readinto = getattr(stream, "readinto", None)
1817+
if readinto is not None:
1818+
# Most streams have readinto
1819+
buf = (ctypes.c_char * length).from_address(
1820+
ctypes.addressof(data.contents))
1821+
n = readinto(buf)
1822+
return n if n else 0
1823+
1824+
# Fallback for streams without readinto.
1825+
buffer = stream.read(length)
18161826
if not buffer: # EOF
18171827
return 0
18181828

@@ -1846,8 +1856,10 @@ def seek_callback(ctx, offset, whence):
18461856
if not self._initialized or self._closed:
18471857
return -1
18481858
try:
1849-
file_stream.seek(offset, whence)
1850-
return file_stream.tell()
1859+
# Fall back to tell() only for stream objects that do not
1860+
# return the new absolute position and return None.
1861+
pos = file_stream.seek(offset, whence)
1862+
return pos if pos is not None else file_stream.tell()
18511863
except Exception:
18521864
return -1
18531865

0 commit comments

Comments
 (0)