#1845·tqdm

CallbackIOWrapper undercounts writes of non-byte memoryviews

Author: xu91102Created Sep 26, 2026Updated Sep 26, 2026

CallbackIOWrapper(..., method='write') reports the input's element count rather than the byte count returned by the stream. For a non-byte-format memoryview, a successful 12-byte write is reported as 3:

python
from array import array
from io import BytesIO
from tqdm.utils import CallbackIOWrapper

stream = BytesIO()
counts = []
writer = CallbackIOWrapper(counts.append, stream, 'write')
print(writer.write(memoryview(array('I', [1, 2, 3])))) # 12 on this platform
print(len(stream.getvalue())) # 12
print(counts) # [3], expected [12]

Reproduced on master 9cf5a12b1f955468a17f0ba3c59092b23e4258ac, Python 3.12.13/macOS. The wrapper should use the integer count returned by standard stream writes; streams that return no count can retain the existing len(data) fallback. The new regression fails with [3] != [12] on the original code.

Investigated with OpenAI Codex assistance.