Skip to content
This repository has been archived by the owner on Dec 18, 2019. It is now read-only.

fix pep8 issues #102

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 41 additions & 39 deletions src/horizon/listen.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,75 +2,77 @@
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
import settings

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
# mode (which is faster) as an option (USE_INSECURE_UNPICKLER).
# 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):
"""
Expand All @@ -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):
Expand Down