With z3c.extfile, if the path name of the HashDir utility is a unicode
string, as configured by the <hashDir> directive in zcml, a unicode
digest will be returned. However, loading the file with this digest
raises a ValueError, since the getPath function only recognizes
StringType as digest:

>>> from z3c.extfile import hashdir
>>> import tempfile, os
>>> tmp = tempfile.mkdtemp()
>>> hdPath = os.path.join(tmp, u'testhashdir')
>>> hdPath
>>> hd = hashdir.HashDir(hdPath)

>>> from z3c.extfile.property import ExtBytesProperty
>>> from cStringIO import StringIO
>>> class Foo(object):
...     data = ExtBytesProperty('data')
>>> foo = Foo()
>>> from z3c.extfile.interfaces import IHashDir
>>> from zope import component
>>> component.provideUtility(hd, provides=IHashDir)
>>> si = StringIO('hello world')
>>> foo.data=si
>>> foo.data
<ReadFile named u'2aae6c35c94fcfb415dbe95f408b9ce91ee846ed'>
>>> foo.data.digest
>>> [u'2aae6c35c94fcfb415dbe95f408b9ce91ee846ed']
>>> hd.getPath(foo.data.digest)
Traceback (most recent call last):
  File "<console>", line 1, in <module>
line 64, in getPath
    raise ValueError, repr(digest)
ValueError: u'2aae6c35c94fcfb415dbe95f408b9ce91ee846ed'

Below is the patch that fixes this. I am not sure if this is the best,
since it allows the file digest to be unicode. Can someone have a

Index: hashdir.py
--- hashdir.py  (revision 85015)
+++ hashdir.py  (working copy)
@@ -3,7 +3,7 @@
 import stat
 import tempfile
 import shutil
-from types import StringType
+from types import StringTypes
 import interfaces
 from zope import interface
 from persistent import Persistent
@@ -60,7 +60,7 @@
         return os.listdir(self.var)

     def getPath(self, digest):
-        if  type(digest) != StringType or len(digest) != 40:
+        if  isinstance(digest, StringTypes) or len(digest) != 40:
             raise ValueError, repr(digest)
         path = os.path.join(self.var, digest)
         if not os.path.isfile(path):

By the way, do I also use https://launchpad.net/zope3/+bugs for
tracking bugs in z3c projects?

Hong Yuan

Zope3-users mailing list

Reply via email to