Skip to content

Commit 99807e1

Browse files
committed
Merge pull request nipy#406 from matthew-brett/alternative-gifti-fix
MRG: delay set of gifti parser for circular import The GiftiImage class sets GiftiImageParser as a class attribute, but the GiftiImageParser needs to import and use the GiftiImage class. So, delay the setting of the parser class attribute until GiftiImage is fully defined, and therefore usable from `parse_fast_gifti.py`, which defines GiftiImageParser.
2 parents e514c9c + 94b4cec commit 99807e1

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

nibabel/gifti/gifti.py

+10-4
Original file line numberDiff line numberDiff line change
@@ -440,14 +440,15 @@ class GiftiImage(xml.XmlSerializable, FileBasedImage):
440440
valid_exts = ('.gii',)
441441
files_types = (('image', '.gii'),)
442442

443+
# The parser will in due course be a GiftiImageParser, but we can't set
444+
# that now, because it would result in a circular import. We set it after
445+
# the class has been defined, at the end of the class definition.
446+
parser = None
447+
443448
def __init__(self, header=None, extra=None, file_map=None, meta=None,
444449
labeltable=None, darrays=None, version="1.0"):
445450
super(GiftiImage, self).__init__(header=header, extra=extra,
446451
file_map=file_map)
447-
# placed here temporarily for git diff purposes
448-
from .parse_gifti_fast import GiftiImageParser
449-
GiftiImage.parser = GiftiImageParser
450-
451452
if darrays is None:
452453
darrays = []
453454
if meta is None:
@@ -627,3 +628,8 @@ def from_filename(klass, filename, buffer_size=35000000):
627628
file_map = klass.filespec_to_file_map(filename)
628629
img = klass.from_file_map(file_map, buffer_size=buffer_size)
629630
return img
631+
632+
633+
# Now GiftiImage is defined, we can import the parser module and set the parser
634+
from .parse_gifti_fast import GiftiImageParser
635+
GiftiImage.parser = GiftiImageParser

nibabel/gifti/tests/test_1.py

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
""" Testing loading of gifti file
2+
3+
The file is ``test_1`` because we are testing a bug where, if we try to load a
4+
file before instantiating some Gifti objects, loading fails with an
5+
AttributeError (see: https://github.com/nipy/nibabel/issues/392).
6+
7+
Thus, we have to run this test before the other gifti tests to catch the gifti
8+
code unprepared.
9+
"""
10+
11+
from nibabel import load
12+
13+
from .test_parse_gifti_fast import DATA_FILE3
14+
15+
16+
def test_load_gifti():
17+
# This expression should not raise an error
18+
load(DATA_FILE3)

0 commit comments

Comments
 (0)