Skip to content

MRG: delay set of gifti parser for circular import #406

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 18, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions nibabel/gifti/gifti.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
18 changes: 18 additions & 0 deletions nibabel/gifti/tests/test_1.py
Original file line number Diff line number Diff line change
@@ -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)