Skip to content
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

added guard for null 'options' to fix crash (#1163) #1325

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions import_export/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ def __new__(cls, name, bases, attrs):
# Collect the Meta options
options = getattr(base, 'Meta', None)
for option in [option for option in dir(options)
if not option.startswith('_')]:
if not option.startswith('_') and hasattr(options, option)]:
setattr(meta, option, getattr(options, option))

# Add direct fields
Expand All @@ -198,7 +198,7 @@ def __new__(cls, name, bases, attrs):
# Add direct options
options = getattr(new_class, 'Meta', None)
for option in [option for option in dir(options)
if not option.startswith('_')]:
if not option.startswith('_') and hasattr(options, option)]:
setattr(meta, option, getattr(options, option))
new_class._meta = meta

Expand Down
9 changes: 9 additions & 0 deletions tests/core/tests/test_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,15 @@ def test_meta(self):
self.assertIsInstance(self.my_resource._meta,
resources.ResourceOptions)

@mock.patch("builtins.dir")
def test_new_handles_null_options(self, mock_dir):
# #1163 - simulates a call to dir() returning additional attributes
mock_dir.return_value = ['attrs']
class A(MyResource):
pass

A()

def test_get_export_order(self):
self.assertEqual(self.my_resource.get_export_headers(),
['email', 'name', 'extra'])
Expand Down