UnpicklingError: NEWOBJ class argument isn't a type object

skunkwerk skunkwerk at gmail.com
Mon Jul 8 01:27:13 EDT 2013


Hi,
  I'm using a custom pickler that replaces any un-pickleable objects (such as sockets or files) with a string representation of them, based on the code from Shane Hathaway here:
http://stackoverflow.com/questions/4080688/python-pickling-a-dict-with-some-unpicklable-items

It works most of the time, but when I try to unpickle a Django HttpResponse, I get the following error:
UnpicklingError: NEWOBJ class argument isn't a type object

I have no clue what the error actually means.  If it pickles okay, why should it not be able to unpickle?  Any ideas?

thanks for the help,
imran

Here is my code:

from cPickle import Pickler, Unpickler, UnpicklingError

class FilteredObject:
	def __init__(self, about):
		self.about = about
	def __repr__(self):
		return 'FilteredObject(%s)' % repr(self.about)

class MyPickler(object):
	def __init__(self, file, protocol=2):
		pickler = Pickler(file, protocol)
		pickler.persistent_id = self.persistent_id
		self.dump = pickler.dump
		self.clear_memo = pickler.clear_memo

	def persistent_id(self, obj):
               if not hasattr(obj, '__getstate__') and not isinstance(obj,
			(basestring, bool, int, long, float, complex, tuple, list, set, dict)):
			return ["filtered:%s" % str(obj)]
		else:
			return None

class MyUnpickler(object):
	def __init__(self, file):
		unpickler = Unpickler(file)
		unpickler.persistent_load = self.persistent_load
		self.load = unpickler.load
		self.noload = unpickler.noload

	def persistent_load(self, obj_id):
		if obj_id[0].startswith('filtered:'):
			return FilteredObject(obj_id[0][9:])
		else:
			raise UnpicklingError('Invalid persistent id')

###### serialize to file

f = open('test.txt','wb')
p = MyPickler(f)
p.dump(data)
f.close()

###### unserialize from file

f = open('test.txt','rb')
pickled_data = f.read()
f.seek(0)
u = MyUnpickler(f)
data = u.load()



More information about the Python-list mailing list