From e5a09878ce39bc26a4c6c3e239e83e8ea4ad82eb Mon Sep 17 00:00:00 2001 From: Matthew Brett Date: Fri, 12 Feb 2016 21:25:07 -0800 Subject: [PATCH 1/2] TST: add test for gifti file loading Test for issue https://github.com/nipy/nibabel/issues/392 It's a brittle test, because it depends on the order of file loading. --- nibabel/gifti/tests/test_1.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 nibabel/gifti/tests/test_1.py diff --git a/nibabel/gifti/tests/test_1.py b/nibabel/gifti/tests/test_1.py new file mode 100644 index 0000000000..a464ee49ef --- /dev/null +++ b/nibabel/gifti/tests/test_1.py @@ -0,0 +1,18 @@ +""" Testing loading of gifti file + +The file is ``test_1`` because we are testing a bug where, if we try to load a +file before instantiating some Gifti objects, loading fails with an +AttributeError (see: https://github.com/nipy/nibabel/issues/392). + +Thus, we have to run this test before the other gifti tests to catch the gifti +code unprepared. +""" + +from nibabel import load + +from .test_parse_gifti_fast import DATA_FILE3 + + +def test_load_gifti(): + # This expression should not raise an error + load(DATA_FILE3) From a891b7e2f8064d29b21a1181f1514ef3d78fc9c7 Mon Sep 17 00:00:00 2001 From: Matthew Brett Date: Fri, 12 Feb 2016 21:15:25 -0800 Subject: [PATCH 2/2] BF: fix for circular import error in gifti Suggested fix for gh-392 caused by circular import. The image class needs the parser class but the parser class needs the image class. --- nibabel/gifti/gifti.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/nibabel/gifti/gifti.py b/nibabel/gifti/gifti.py index ed48de8a6b..fce0179ac6 100644 --- a/nibabel/gifti/gifti.py +++ b/nibabel/gifti/gifti.py @@ -419,14 +419,15 @@ class GiftiImage(xml.XmlSerializable, FileBasedImage): valid_exts = ('.gii',) files_types = (('image', '.gii'),) + # The parser will in due course be a GiftiImageParser, but we can't set + # that now, because it would result in a circular import. We set it after + # the class has been defined, at the end of the class definition. + parser = None + def __init__(self, header=None, extra=None, file_map=None, meta=None, labeltable=None, darrays=None, version="1.0"): super(GiftiImage, self).__init__(header=header, extra=extra, file_map=file_map) - # placed here temporarily for git diff purposes - from .parse_gifti_fast import GiftiImageParser - GiftiImage.parser = GiftiImageParser - if darrays is None: darrays = [] if meta is None: @@ -606,3 +607,8 @@ def from_filename(klass, filename, buffer_size=35000000): file_map = klass.filespec_to_file_map(filename) img = klass.from_file_map(file_map, buffer_size=buffer_size) return img + + +# Now GiftiImage is defined, we can import the parser module and set the parser +from .parse_gifti_fast import GiftiImageParser +GiftiImage.parser = GiftiImageParser