Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Move test to data folder and improve error formatting
  • Loading branch information
DinoV committed Jan 16, 2020
commit f2cfb2f7fa5392e68c88a7451d1cb6356c8f36e9
3 changes: 1 addition & 2 deletions Lib/importlib/_bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -982,8 +982,7 @@ def _find_and_load_unlocked(name, import_):
try:
setattr(parent_module, child, module)
except AttributeError:
msg = (f"Can't set child package '{child}' on "
f"parent '{parent}'")
msg = f"Cannot set an attribute on {parent!r} for child module {child!r}"
_warnings.warn(msg, ImportWarning)
return module

Expand Down
36 changes: 10 additions & 26 deletions Lib/test/test_import/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1343,38 +1343,22 @@ def test_circular_from_import(self):
)

def test_unwritable_module(self):
package = inspect.cleandoc('''
import sys

class MyMod(object):
__slots__ = ['__builtins__', '__cached__', '__doc__',
'__file__', '__loader__', '__name__',
'__package__', '__path__', '__spec__']
def __init__(self):
for attr in self.__slots__:
setattr(self, attr, globals()[attr])


sys.modules['spam'] = MyMod()
''')

init = os.path.join('spam', '__init__')
with _ready_to_import(init, package) as (name, path):
with open(os.path.join(os.path.dirname(path), 'x.py'), 'w+'):
pass

sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'data'))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the path insertion instead of importing test.test_import.data.unwritable?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was actually just following the pattern I saw in test_concurrency but this is certainly much cleaner!

try:
try:
import spam
import unwritable
with self.assertWarns(ImportWarning):
from spam import x
from unwritable import x

self.assertNotEqual(type(spam), ModuleType)
self.assertNotEqual(type(unwritable), ModuleType)
self.assertEqual(type(x), ModuleType)
with self.assertRaises(AttributeError):
spam.x = 42
unwritable.x = 42
finally:
del sys.modules['spam.x']

sys.modules['unwritable.x']

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this is delete the module then you can use a helper in test.support and register it with addCleanup().

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, will do!

sys.modules['unwritable']
finally:
del sys.path[0]

if __name__ == '__main__':
# Test needs to be a package, so we can do relative imports.
Expand Down
12 changes: 12 additions & 0 deletions Lib/test/test_import/data/unwritable/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import sys

class MyMod(object):
__slots__ = ['__builtins__', '__cached__', '__doc__',
'__file__', '__loader__', '__name__',
'__package__', '__path__', '__spec__']
def __init__(self):
for attr in self.__slots__:
setattr(self, attr, globals()[attr])


sys.modules[__name__] = MyMod()
Empty file.
Loading