From 97381673f3784680dae6cf65a2cdd1bfc930175a Mon Sep 17 00:00:00 2001 From: moculus Date: Tue, 10 Mar 2009 03:46:49 +0000 Subject: [PATCH] 2.4? --HG-- branch : pmacs2 --- buffer/__init__.py | 2 +- mode/__init__.py | 2 +- util.py | 39 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 41 insertions(+), 2 deletions(-) diff --git a/buffer/__init__.py b/buffer/__init__.py index 12fa2da..ab5a918 100644 --- a/buffer/__init__.py +++ b/buffer/__init__.py @@ -1,4 +1,4 @@ -from collections import defaultdict +from util import defaultdict import codecs, datetime, grp, os, pwd, re, shutil, stat, string import fcntl, select, pty, threading import aes, dirutil, regex, highlight, lex, term diff --git a/mode/__init__.py b/mode/__init__.py index 44d90d3..964da8e 100644 --- a/mode/__init__.py +++ b/mode/__init__.py @@ -1,4 +1,4 @@ -from collections import defaultdict +from util import defaultdict import math, os, string import color, method from lex import Lexer diff --git a/util.py b/util.py index c6036fe..01aa21a 100644 --- a/util.py +++ b/util.py @@ -66,3 +66,42 @@ def get_margin_color(w, def_color='blue'): return w.application.config[lname] else: return w.application.config.get('margin_color', def_color) + +# emulate defaultdict for 2.4 +try: + from collections import defaultdict +except: + class defaultdict(dict): + def __init__(self, default_factory=None, *a, **kw): + if (default_factory is not None and + not hasattr(default_factory, '__call__')): + raise TypeError('first argument must be callable') + dict.__init__(self, *a, **kw) + self.default_factory = default_factory + def __getitem__(self, key): + try: + return dict.__getitem__(self, key) + except KeyError: + return self.__missing__(key) + def __missing__(self, key): + if self.default_factory is None: + raise KeyError(key) + self[key] = value = self.default_factory() + return value + def __reduce__(self): + if self.default_factory is None: + args = tuple() + else: + args = self.default_factory, + return type(self), args, None, None, self.items() + def copy(self): + return self.__copy__() + def __copy__(self): + return type(self)(self.default_factory, self) + def __deepcopy__(self, memo): + import copy + return type(self)(self.default_factory, + copy.deepcopy(self.items())) + def __repr__(self): + return 'defaultdict(%s, %s)' % (self.default_factory, + dict.__repr__(self))