Skip to content

Commit

Permalink
Corrected an issue in the ptypes.pstr library module when trying to d…
Browse files Browse the repository at this point in the history
…ecode a string using a codec that does not have any keyword parameters.
  • Loading branch information
arizvisa committed Jun 21, 2024
1 parent e1742e9 commit 5831c87
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion lib/ptypes/pstr.py
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ def __format__(self, spec):
prefix, spec = spec[:-1], spec[-1:]
if '#' in prefix and spec in 's':
data = self.serialize()
decoded, length = self.encoding.decode(data, errors='ignore')
decoded, length = self.encoding.decode(data, 'ignore')
return "{:{:s}s}".format(string_escape(decoded.rstrip('\0')), ''.join(char for char in prefix if char != '#'))
elif spec in 's':
return "{:{:s}s}".format(self.str(), prefix)
Expand Down Expand Up @@ -984,6 +984,20 @@ def test_str_multi_set_length_bytes():
if self.serialize() == b'\xe3\x82\xb3\xe3\x83\xbc\xe3\x83\x89\0\0\0' and self.blocksize() == 12 and self.size() == 12:
raise Success

@TestCase
def test_str_format_alternative_1():
data = b'\xe3\x82\xb3\xe3\x83\xbc\xe3\x83\x89'
self = pstr.string(length=12, encoding='latin1').set(data)
if "{:#s}".format(self) == r'\XE3\X82\XB3\XE3\X83\XBC\XE3\X83\X89'.lower():
raise Success

@TestCase
def test_str_format_alternative_2():
data = b'\xe3\x82\xb3\xe3\x83\xbc\xe3\x83\x89'
self = pstr.string(encoding='utf-8').set(data)
if "{:#s}".format(self) == r'\U30B3\U30FC\U30C9'.lower():
raise Success

if __name__ == '__main__':
import logging
ptypes.config.defaults.log.setLevel(logging.DEBUG)
Expand Down

0 comments on commit 5831c87

Please sign in to comment.