[h5py] 266/455: Implement Unicode filename handling; doc updates
Ghislain Vaillant
ghisvail-guest at moszumanska.debian.org
Thu Jul 2 18:19:40 UTC 2015
This is an automated email from the git hooks/post-receive script.
ghisvail-guest pushed a commit to annotated tag 1.3.0
in repository h5py.
commit f363743e1e2d63c8c189bdc56a265ae796144e44
Author: andrewcollette <andrew.collette at gmail.com>
Date: Tue Jun 2 21:45:59 2009 +0000
Implement Unicode filename handling; doc updates
---
docs/source/guide/file.rst | 12 +++++++-----
h5py/highlevel.py | 24 +++++++++++++++++++++---
h5py/tests/test_highlevel.py | 26 ++++++++++++++++++++++++++
3 files changed, 54 insertions(+), 8 deletions(-)
diff --git a/docs/source/guide/file.rst b/docs/source/guide/file.rst
index 5cfd4a1..e8dc02e 100644
--- a/docs/source/guide/file.rst
+++ b/docs/source/guide/file.rst
@@ -30,7 +30,7 @@ HDF5 ships with a variety of different low-level drivers, which map the logical
HDF5 address space to different storage mechanisms. You can specify which
driver you want to use when the file is opened::
- >>> f = h5py.File('myfile.hdf5', driver=*driver name*, *driver_kwds*)
+ >>> f = h5py.File('myfile.hdf5', driver=<driver name>, <driver_kwds>)
For example, the HDF5 "core" driver can be used to create a purely in-memory
HDF5 file, optionally written out to disk when it is closed. Currently
@@ -63,8 +63,8 @@ None
'family'
Store the file on disk as a series of fixed-length chunks. Useful
if the file system doesn't allow large files. Note: the filename
- you provide *must* contain the string "%d", which will be replaced
- by the file sequence number. Keywords:
+ you provide *must* contain a printf-style integer format code (e.g "%d"),
+ which will be replaced by the file sequence number. Keywords:
memb_size
Maximum file size (default is 2**31-1).
@@ -85,7 +85,8 @@ the full API of Group objects; in this case, the group in question is the
.. attribute:: filename
- HDF5 filename
+ HDF5 filename on disk. This is a plain string (str) for ASCII names,
+ Unicode otherwise.
.. attribute:: mode
@@ -97,7 +98,8 @@ the full API of Group objects; in this case, the group in question is the
.. method:: __init__(name, mode='a', driver=None, **driver_kwds)
- Open or create an HDF5 file.
+ Open or create an HDF5 file. See above for a summary of options.
+ Argument *name* may be an ASCII or Unicode string.
.. method:: close()
diff --git a/h5py/highlevel.py b/h5py/highlevel.py
index 17cfc37..e5de0c9 100644
--- a/h5py/highlevel.py
+++ b/h5py/highlevel.py
@@ -539,8 +539,9 @@ class File(Group):
'family'
Store the file on disk as a series of fixed-length chunks. Useful
if the file system doesn't allow large files. Note: the filename
- you provide *must* contain the string "%d", which will be replaced
- by the file sequence number. Keywords:
+ you provide *must* contain a printf-style integer format code
+ (e.g. %d"), which will be replaced by the file sequence number.
+ Keywords:
memb_size: Maximum file size (default is 2**31-1).
"""
@@ -548,7 +549,17 @@ class File(Group):
@property
def filename(self):
"""File name on disk"""
- return h5f.get_name(self.fid)
+ name = h5f.get_name(self.fid)
+ # Note the exception can happen in one of two ways:
+ # 1. The name doesn't comply with the file system encoding;
+ # return the raw byte string
+ # 2. The name can't be encoded down to ASCII; return it as
+ # a Unicode string object
+ try:
+ name = name.decode(sys.getfilesystemencoding())
+ return name.encode('ascii')
+ except UnicodeError:
+ return name
@property
def mode(self):
@@ -596,6 +607,13 @@ class File(Group):
else:
raise ValueError('Unknown driver type "%s"' % driver)
+ try:
+ # If the byte string doesn't match the default encoding, just
+ # pass it on as-is. Note Unicode objects can always be encoded.
+ name = name.encode(sys.getfilesystemencoding())
+ except UnicodeError:
+ pass
+
if mode == 'r':
self.fid = h5f.open(name, h5f.ACC_RDONLY, fapl=plist)
elif mode == 'r+':
diff --git a/h5py/tests/test_highlevel.py b/h5py/tests/test_highlevel.py
index b13ba03..001104e 100644
--- a/h5py/tests/test_highlevel.py
+++ b/h5py/tests/test_highlevel.py
@@ -55,6 +55,32 @@ class TestFile(HDF5TestCase):
def tearDown(self):
os.unlink(self.fname)
+ def test_unicode(self):
+ # Three cases:
+ # 1. Unicode
+ # Store w/filesystem encoding; should be readable as Unicode
+ # 2. Raw byte string in ASCII range
+ # Store w/filesystem encoding; should be read as ASCII
+ # 3. Raw byte string out of ASCII range
+ # Store as-is; since it doesn't conform to the filesystem
+ # encoding, just return the raw string.
+ fnames = (tempfile.mktemp(u'_\u1201.hdf5'),
+ tempfile.mktemp('_.hdf5'),
+ tempfile.mktemp('\xff\xff\xff.hdf5'))
+ print ""
+ for fname, typ in zip(fnames, (unicode, str, str)):
+ print 'checking "%r" (%s)' % (fname, typ)
+ try:
+ f = File(fname, 'w')
+ self.assert_(isinstance(f.filename, typ))
+ self.assertEqual(f.filename, fname)
+ finally:
+ try:
+ f.close()
+ finally:
+ if os.path.exists(fname):
+ os.unlink(fname)
+
def test_File_init_r(self):
with File(self.fname, 'r') as f:
self.assert_(isinstance(f["CompoundChunked"], Dataset))
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-science/packages/h5py.git
More information about the debian-science-commits
mailing list