Skip to content

Commit f95caff

Browse files
author
Steve Canny
committed
img: add _AsciiIfdEntry._parse_value()
1 parent 38a278a commit f95caff

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

docx/image/tiff.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,14 @@ class _AsciiIfdEntry(_IfdEntry):
286286
"""
287287
IFD entry having the form of a NULL-terminated ASCII string
288288
"""
289+
@classmethod
290+
def _parse_value(cls, stream_rdr, offset, value_count, value_offset):
291+
"""
292+
Return the ASCII string parsed from *stream_rdr* at *value_offset*.
293+
The length of the string, including a terminating '\x00' (NUL)
294+
character, is in *value_count*.
295+
"""
296+
return stream_rdr.read_str(value_count-1, value_offset)
289297

290298

291299
class _ShortIfdEntry(_IfdEntry):

tests/image/test_tiff.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
from docx.image.constants import MIME_TYPE, TIFF_TAG
1515
from docx.image.helpers import BIG_ENDIAN, LITTLE_ENDIAN, StreamReader
1616
from docx.image.tiff import (
17-
_IfdEntries, _IfdEntry, _IfdEntryFactory, _IfdParser, _LongIfdEntry,
18-
_RationalIfdEntry, _ShortIfdEntry, Tiff, _TiffParser
17+
_AsciiIfdEntry, _IfdEntries, _IfdEntry, _IfdEntryFactory, _IfdParser,
18+
_LongIfdEntry, _RationalIfdEntry, _ShortIfdEntry, Tiff, _TiffParser
1919
)
2020

2121
from ..unitutil import (
@@ -423,6 +423,15 @@ def value_(self, request):
423423
return loose_mock(request)
424424

425425

426+
class Describe_AsciiIfdEntry(object):
427+
428+
def it_can_parse_an_ascii_string_IFD_entry(self):
429+
bytes_ = b'foobar\x00'
430+
stream_rdr = StreamReader(BytesIO(bytes_), BIG_ENDIAN)
431+
val = _AsciiIfdEntry._parse_value(stream_rdr, None, 7, 0)
432+
assert val == 'foobar'
433+
434+
426435
class Describe_ShortIfdEntry(object):
427436

428437
def it_can_parse_a_short_int_IFD_entry(self):

0 commit comments

Comments
 (0)