From e514912378f6c2f65f633fde1f7c55c62af53807 Mon Sep 17 00:00:00 2001 From: Brian Crowell Date: Mon, 15 Oct 2012 16:19:55 -0500 Subject: [PATCH] py3k: Rewrite dictionary support for Tiff ImageFileDictionary This commit brings in the collections.MutableMapping mixin to provide full dictionary support for ImageFileDictionary. --- PIL/TiffImagePlugin.py | 37 +++++++++++++++++-------------------- 1 file changed, 17 insertions(+), 20 deletions(-) diff --git a/PIL/TiffImagePlugin.py b/PIL/TiffImagePlugin.py index 335fe2616..cdc17d242 100644 --- a/PIL/TiffImagePlugin.py +++ b/PIL/TiffImagePlugin.py @@ -45,6 +45,8 @@ import Image, ImageFile import ImagePalette import array, sys +import collections +import itertools II = "II" # little-endian (intel-style) MM = "MM" # big-endian (motorola-style) @@ -204,7 +206,7 @@ def _accept(prefix): ## # Wrapper for TIFF IFDs. -class ImageFileDirectory: +class ImageFileDirectory(collections.MutableMapping): # represents a TIFF tag directory. to speed things up, # we don't decode tags unless they're asked for. @@ -227,16 +229,7 @@ class ImageFileDirectory: self.tagtype = {} # added 2008-06-05 by Florian Hoech self.next = None - # dictionary API (sort of) - - def keys(self): - return self.tagdata.keys() + self.tags.keys() - - def items(self): - items = self.tags.items() - for tag in self.tagdata.keys(): - items.append((tag, self[tag])) - return items + # dictionary API def __len__(self): return len(self.tagdata) + len(self.tags) @@ -251,12 +244,6 @@ class ImageFileDirectory: del self.tagdata[tag] return data - def get(self, tag, default=None): - try: - return self[tag] - except KeyError: - return default - def getscalar(self, tag, default=None): try: value = self[tag] @@ -272,14 +259,24 @@ class ImageFileDirectory: raise return default - def has_key(self, tag): - return self.tags.has_key(tag) or self.tagdata.has_key(tag) + def __contains__(self, tag): + return tag in self.tags or tag in self.tagdata + + if sys.version_info < (3,0): + def has_key(self, tag): + return tag in self def __setitem__(self, tag, value): - if type(value) is not type(()): + if not isinstance(value, tuple): value = (value,) self.tags[tag] = value + def __delitem__(self, tag): + self.tags.pop(tag, self.tagdata.pop(tag, None)) + + def __iter__(self): + return itertools.chain(self.tags.__iter__(), self.tagdata.__iter__()) + # load primitives load_dispatch = {}