diff --git a/src/horizon/listen.py b/src/horizon/listen.py index 0b36bf0e..a8fbc4ae 100644 --- a/src/horizon/listen.py +++ b/src/horizon/listen.py @@ -2,7 +2,7 @@ from os import kill, getpid from Queue import Full from multiprocessing import Process -from struct import Struct, unpack +from struct import Struct from msgpack import unpackb import logging @@ -10,19 +10,19 @@ logger = logging.getLogger("HorizonLog") -##SafeUnpickler taken from Carbon: https://github.com/graphite-project/carbon/blob/master/lib/carbon/util.py +# SafeUnpickler taken from Carbon: https://github.com/graphite-project/carbon/blob/master/lib/carbon/util.py import sys try: - from cStringIO import StringIO + from cStringIO import StringIO except ImportError: - from StringIO import StringIO + from StringIO import StringIO try: - import cPickle as pickle - USING_CPICKLE = True + import cPickle as pickle + USING_CPICKLE = True except: - import pickle - USING_CPICKLE = False + import pickle + USING_CPICKLE = False # This whole song & dance is due to pickle being insecure # yet performance critical for carbon. We leave the insecure @@ -30,47 +30,49 @@ # The SafeUnpickler classes were largely derived from # http://nadiana.com/python-pickle-insecure if USING_CPICKLE: - class SafeUnpickler(object): - PICKLE_SAFE = { - 'copy_reg' : set(['_reconstructor']), - '__builtin__' : set(['object']), - } + class SafeUnpickler(object): + PICKLE_SAFE = { + 'copy_reg': set(['_reconstructor']), + '__builtin__': set(['object']), + } @classmethod def find_class(cls, module, name): - if not module in cls.PICKLE_SAFE: - raise pickle.UnpicklingError('Attempting to unpickle unsafe module %s' % module) - __import__(module) - mod = sys.modules[module] - if not name in cls.PICKLE_SAFE[module]: - raise pickle.UnpicklingError('Attempting to unpickle unsafe class %s' % name) - return getattr(mod, name) + if module not in cls.PICKLE_SAFE: + raise pickle.UnpicklingError('Attempting to unpickle unsafe module %s' % module) + __import__(module) + mod = sys.modules[module] + if name not in cls.PICKLE_SAFE[module]: + raise pickle.UnpicklingError('Attempting to unpickle unsafe class %s' % name) + return getattr(mod, name) @classmethod def loads(cls, pickle_string): - pickle_obj = pickle.Unpickler(StringIO(pickle_string)) - pickle_obj.find_global = cls.find_class - return pickle_obj.load() + pickle_obj = pickle.Unpickler(StringIO(pickle_string)) + pickle_obj.find_global = cls.find_class + return pickle_obj.load() else: - class SafeUnpickler(pickle.Unpickler): - PICKLE_SAFE = { - 'copy_reg' : set(['_reconstructor']), - '__builtin__' : set(['object']), - } - def find_class(self, module, name): - if not module in self.PICKLE_SAFE: - raise pickle.UnpicklingError('Attempting to unpickle unsafe module %s' % module) - __import__(module) - mod = sys.modules[module] - if not name in self.PICKLE_SAFE[module]: - raise pickle.UnpicklingError('Attempting to unpickle unsafe class %s' % name) - return getattr(mod, name) + class SafeUnpickler(pickle.Unpickler): + PICKLE_SAFE = { + 'copy_reg': set(['_reconstructor']), + '__builtin__': set(['object']), + } + + def find_class(self, module, name): + if module not in self.PICKLE_SAFE: + raise pickle.UnpicklingError('Attempting to unpickle unsafe module %s' % module) + __import__(module) + mod = sys.modules[module] + if name not in self.PICKLE_SAFE[module]: + raise pickle.UnpicklingError('Attempting to unpickle unsafe class %s' % name) + return getattr(mod, name) @classmethod def loads(cls, pickle_string): - return cls(StringIO(pickle_string)).load() -##//SafeUnpickler + return cls(StringIO(pickle_string)).load() +# //SafeUnpickler + class Listen(Process): """ @@ -90,7 +92,7 @@ def __init__(self, port, queue, parent_pid, type="pickle"): self.current_pid = getpid() self.type = type - ##Use the safe unpickler that comes with carbon rather than standard python pickle/cpickle + # Use the safe unpickler that comes with carbon rather than standard python pickle/cpickle self.unpickler = SafeUnpickler def gen_unpickle(self, infile):