Commit cf05aa72 authored by Julien Muchembled's avatar Julien Muchembled

Better handling of Pdata object to avoid MemoryError

parent 7dab2ee4
...@@ -48,10 +48,11 @@ def hashPdataObject(pdata_object): ...@@ -48,10 +48,11 @@ def hashPdataObject(pdata_object):
to minimize memory footprint. to minimize memory footprint.
""" """
md5_hash = md5() md5_hash = md5()
next = pdata_object while pdata_object is not None:
while next is not None: chunk = pdata_object.aq_base
md5_hash.update(next.data) md5_hash.update(chunk.data)
next = next.next pdata_object = chunk.next
chunk._p_deactivate()
return md5_hash.hexdigest() return md5_hash.hexdigest()
class CachedConvertableMixin: class CachedConvertableMixin:
...@@ -130,8 +131,9 @@ class CachedConvertableMixin: ...@@ -130,8 +131,9 @@ class CachedConvertableMixin:
size = 0 size = 0
elif isinstance(data, Pdata): elif isinstance(data, Pdata):
cached_value = aq_base(data) cached_value = aq_base(data)
conversion_md5 = hashPdataObject(cached_value) size = str(cached_value) # not a size but avoids a 'del' statement
size = len(cached_value) conversion_md5 = md5(size).hexdigest()
size = len(size)
elif isinstance(data, OFSImage): elif isinstance(data, OFSImage):
cached_value = data cached_value = data
conversion_md5 = md5(str(data.data)).hexdigest() conversion_md5 = md5(str(data.data)).hexdigest()
...@@ -244,10 +246,10 @@ class CachedConvertableMixin: ...@@ -244,10 +246,10 @@ class CachedConvertableMixin:
def updateContentMd5(self): def updateContentMd5(self):
"""Update md5 checksum from the original file """Update md5 checksum from the original file
""" """
data = self.getData() data = self._baseGetData()
if data not in (None, ''): if data:
if isinstance(data, Pdata): if isinstance(data, Pdata):
self._setContentMd5(hashPdataObject(aq_base(data))) self._setContentMd5(hashPdataObject(data))
else: else:
self._setContentMd5(md5(data).hexdigest()) # Reindex is useless self._setContentMd5(md5(data).hexdigest()) # Reindex is useless
else: else:
......
...@@ -28,12 +28,9 @@ ...@@ -28,12 +28,9 @@
############################################################################## ##############################################################################
from OFS.Image import Pdata from OFS.Image import Pdata
"""
This patch add a getLastPdata method to return the last Pdata element
of a Pdata
"""
def getLastPdata(self): def getLastPdata(self):
"""Return the last Pdata chunk"""
next = self.next next = self.next
while next is not None: while next is not None:
...@@ -42,3 +39,12 @@ def getLastPdata(self): ...@@ -42,3 +39,12 @@ def getLastPdata(self):
return self return self
Pdata.getLastPdata = getLastPdata Pdata.getLastPdata = getLastPdata
def __nonzero__(self):
while not self.data:
self = self.next
if self is None:
return False
return True
Pdata.__nonzero__ = __nonzero__
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment