From 911d91ab0fefcfd8f1e6cfbe1a87abc1da55d120 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Lalinsk=C3=BD?= Date: Sun, 15 Apr 2007 22:20:15 +0200 Subject: [PATCH] Don't write string terminators to ID3 frames. --- NEWS.txt | 1 + picard/formats/id3.py | 24 +++++++++++++++++++++--- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/NEWS.txt b/NEWS.txt index c1f96f81f..5e4a2b6b2 100644 --- a/NEWS.txt +++ b/NEWS.txt @@ -1,6 +1,7 @@ Version 0.9.0beta1 - 2007-XX-YY * Bug Fixes: * Save XSOP frame to ID3v2.3 tags. (#2484) + * Don't write string terminators to ID3 frames. Version 0.9.0alpha8 - 2007-04-15 * New Features: diff --git a/picard/formats/id3.py b/picard/formats/id3.py index 2074280ea..6eb4f5648 100644 --- a/picard/formats/id3.py +++ b/picard/formats/id3.py @@ -31,14 +31,32 @@ from picard.util import encode_filename, sanitize_date # unsupported characters and this better than encoding, decoding and # again encoding. def patched_EncodedTextSpec_write(self, frame, value): - if hasattr(self, 'encodings'): - enc, term = self.encodings[frame.encoding] - else: + try: enc, term = self._encodings[frame.encoding] + except AttributeError: + enc, term = self.encodings[frame.encoding] return value.encode(enc, 'ignore') + term id3.EncodedTextSpec.write = patched_EncodedTextSpec_write +# One more "monkey patch". The ID3 spec says that multiple text +# values should be _separated_ by the string terminator, which +# means that e.g. 'a\x00' are two values, 'a' and ''. +def patched_MultiSpec_write(self, frame, value): + data = self._write_orig(frame, value) + spec = self.specs[-1] + if isinstance(spec, id3.EncodedTextSpec): + try: + term = spec._encodings[frame.encoding][1] + except AttributeError: + term = spec.encodings[frame.encoding][1] + if data.endswith(term): + data = data[:-len(term)] + return data +id3.MultiSpec._write_orig = id3.MultiSpec.write +id3.MultiSpec.write = patched_MultiSpec_write + + class ID3File(File): """Generic ID3-based file.""" _File = None