|
7 | 7 | from collections import OrderedDict
|
8 | 8 |
|
9 | 9 | import unittest
|
| 10 | +import test |
10 | 11 | from test.test_unittest.testmock import support
|
11 | 12 | from test.test_unittest.testmock.support import SomeClass, is_instance
|
12 | 13 |
|
| 14 | +from test.support.import_helper import DirsOnSysPath |
13 | 15 | from test.test_importlib.util import uncache
|
14 | 16 | from unittest.mock import (
|
15 | 17 | NonCallableMock, CallableMixin, sentinel,
|
@@ -1728,6 +1730,71 @@ def test(mock):
|
1728 | 1730 | 'exception traceback not propagated')
|
1729 | 1731 |
|
1730 | 1732 |
|
| 1733 | + def test_name_resolution_import_rebinding(self): |
| 1734 | + # Currently mock.patch uses pkgutil.resolve_name(), but repeat |
| 1735 | + # similar tests just for the case. |
| 1736 | + # The same data is also used for testing import in test_import and |
| 1737 | + # pkgutil.resolve_name() in test_pkgutil. |
| 1738 | + path = os.path.join(os.path.dirname(test.__file__), 'test_import', 'data') |
| 1739 | + def check(name): |
| 1740 | + p = patch(name) |
| 1741 | + p.start() |
| 1742 | + p.stop() |
| 1743 | + def check_error(name): |
| 1744 | + p = patch(name) |
| 1745 | + self.assertRaises(AttributeError, p.start) |
| 1746 | + with uncache('package3', 'package3.submodule'), DirsOnSysPath(path): |
| 1747 | + check('package3.submodule.A.attr') |
| 1748 | + check_error('package3.submodule.B.attr') |
| 1749 | + with uncache('package3', 'package3.submodule'), DirsOnSysPath(path): |
| 1750 | + check('package3.submodule:A.attr') |
| 1751 | + check_error('package3.submodule:B.attr') |
| 1752 | + with uncache('package3', 'package3.submodule'), DirsOnSysPath(path): |
| 1753 | + check('package3:submodule.B.attr') |
| 1754 | + check_error('package3:submodule.A.attr') |
| 1755 | + check('package3.submodule.A.attr') |
| 1756 | + check_error('package3.submodule.B.attr') |
| 1757 | + check('package3:submodule.B.attr') |
| 1758 | + check_error('package3:submodule.A.attr') |
| 1759 | + with uncache('package3', 'package3.submodule'), DirsOnSysPath(path): |
| 1760 | + check('package3:submodule.B.attr') |
| 1761 | + check_error('package3:submodule.A.attr') |
| 1762 | + check('package3.submodule:A.attr') |
| 1763 | + check_error('package3.submodule:B.attr') |
| 1764 | + check('package3:submodule.B.attr') |
| 1765 | + check_error('package3:submodule.A.attr') |
| 1766 | + |
| 1767 | + def test_name_resolution_import_rebinding2(self): |
| 1768 | + path = os.path.join(os.path.dirname(test.__file__), 'test_import', 'data') |
| 1769 | + def check(name): |
| 1770 | + p = patch(name) |
| 1771 | + p.start() |
| 1772 | + p.stop() |
| 1773 | + def check_error(name): |
| 1774 | + p = patch(name) |
| 1775 | + self.assertRaises(AttributeError, p.start) |
| 1776 | + with uncache('package4', 'package4.submodule'), DirsOnSysPath(path): |
| 1777 | + check('package4.submodule.A.attr') |
| 1778 | + check_error('package4.submodule.B.attr') |
| 1779 | + with uncache('package4', 'package4.submodule'), DirsOnSysPath(path): |
| 1780 | + check('package4.submodule:A.attr') |
| 1781 | + check_error('package4.submodule:B.attr') |
| 1782 | + with uncache('package4', 'package4.submodule'), DirsOnSysPath(path): |
| 1783 | + check('package4:submodule.B.attr') |
| 1784 | + check_error('package4:submodule.A.attr') |
| 1785 | + check('package4.submodule.A.attr') |
| 1786 | + check_error('package4.submodule.B.attr') |
| 1787 | + check('package4:submodule.A.attr') |
| 1788 | + check_error('package4:submodule.B.attr') |
| 1789 | + with uncache('package4', 'package4.submodule'), DirsOnSysPath(path): |
| 1790 | + check('package4:submodule.B.attr') |
| 1791 | + check_error('package4:submodule.A.attr') |
| 1792 | + check('package4.submodule:A.attr') |
| 1793 | + check_error('package4.submodule:B.attr') |
| 1794 | + check('package4:submodule.A.attr') |
| 1795 | + check_error('package4:submodule.B.attr') |
| 1796 | + |
| 1797 | + |
1731 | 1798 | def test_create_and_specs(self):
|
1732 | 1799 | for kwarg in ('spec', 'spec_set', 'autospec'):
|
1733 | 1800 | p = patch('%s.doesnotexist' % __name__, create=True,
|
|
0 commit comments