Skip to content
Permalink
Browse files

Merged in danpla/mutagen (pull request #8)

id3: Always validate tag attributes
  • Loading branch information
lazka committed Nov 11, 2015
2 parents 25e8981 + 07871c2 commit 69368c31e00dbae0c8a53cf8f81f0c0d8df1f992
Showing with 39 additions and 16 deletions.
  1. +0 −5 mutagen/id3/__init__.py
  2. +24 −10 mutagen/id3/_frames.py
  3. +14 −0 tests/test__id3frames.py
  4. +1 −1 tests/test_id3.py
@@ -635,11 +635,6 @@ def __update_common(self):
# Get rid of "(xx)Foobr" format.
self["TCON"].genres = self["TCON"].genres

# ID3v2.2 LNK frames are just way too different to upgrade.
for frame in self.getall("LINK"):
if len(frame.frameid) != 4:
del self[frame.HashKey]

mimes = {"PNG": "image/png", "JPG": "image/jpeg"}
for pic in self.getall("APIC"):
if pic.mime in mimes:
@@ -63,14 +63,16 @@ def __init__(self, *args, **kwargs):
other._to_other(self)
else:
for checker, val in izip(self._framespec, args):
setattr(self, checker.name, checker.validate(self, val))
setattr(self, checker.name, val)
for checker in self._framespec[len(args):]:
try:
validated = checker.validate(
self, kwargs.get(checker.name, None))
except ValueError as e:
raise ValueError("%s: %s" % (checker.name, e))
setattr(self, checker.name, validated)
setattr(self, checker.name, kwargs.get(checker.name))

def __setattr__(self, name, value):
for checker in self._framespec:
if checker.name == name:
self.__dict__[name] = checker.validate(self, value)
return
super(Frame, self).__setattr__(name, value)

def _to_other(self, other):
# this impl covers subclasses with the same framespec
@@ -222,11 +224,17 @@ def __init__(self, *args, **kwargs):
super(FrameOpt, self).__init__(*args, **kwargs)
for spec in self._optionalspec:
if spec.name in kwargs:
validated = spec.validate(self, kwargs[spec.name])
setattr(self, spec.name, validated)
setattr(self, spec.name, kwargs[spec.name])
else:
break

def __setattr__(self, name, value):
for checker in self._optionalspec:
if checker.name == name:
self.__dict__[name] = checker.validate(self, value)
return
super(FrameOpt, self).__setattr__(name, value)

def _to_other(self, other):
super(FrameOpt, self)._to_other(other)

@@ -1895,7 +1903,13 @@ def _to_other(self, other):
if not isinstance(other, LINK):
raise TypeError

other.frameid = self.frameid
if isinstance(other, LNK):
other.frameid = self.frameid
else:
try:
other.frameid = Frames_2_2[self.frameid].__bases__[0].__name__
except KeyError:
other.frameid = self.frameid.ljust(4)
other.url = self.url
if hasattr(self, "data"):
other.data = self.data
@@ -48,6 +48,20 @@ def test_PIC_upgrade(self):
new = APIC(frame)
self.assertEqual(new.mime, "foo")

def test_LNK_upgrade(self):
from mutagen.id3 import LNK, LINK
url = "http://foo.bar"

frame = LNK(frameid="PIC", url=url, data=b"\x00")
new = LINK(frame)
self.assertEqual(new.frameid, "APIC")
self.assertEqual(new.url, url)
self.assertEqual(new.data, b"\x00")

frame = LNK(frameid="o_O")
new = LINK(frame)
self.assertEqual(new.frameid, "o_O ")

def test_SIGN(self):
from mutagen.id3 import SIGN
frame = SIGN(group=1, sig=b"foo")
@@ -955,7 +955,7 @@ def test_lnk(self):
id3.version = (2, 2)
id3.add(LNK(frameid="PIC", url="http://foo.bar"))
id3.update_to_v24()
self.assertFalse(id3.getall("LINK"))
self.assertTrue(id3.getall("LINK"))

def test_tyer(self):
from mutagen.id3 import TYER

0 comments on commit 69368c3

Please sign in to comment.
You can’t perform that action at this time.