1561 module information (Module) |
1561 module information (Module) |
1562 """ |
1562 """ |
1563 global _modules |
1563 global _modules |
1564 |
1564 |
1565 _extensions = ( |
1565 _extensions = ( |
1566 [".py", ".pyw", ".ptl", ".rb"] if extensions is None else extensions[:] |
1566 [".py", ".pyw", ".pyi", ".ptl", ".rb"] if extensions is None else extensions[:] |
1567 ) |
1567 ) |
1568 with contextlib.suppress(ValueError): |
1568 with contextlib.suppress(ValueError): |
1569 _extensions.remove(".py") |
1569 _extensions.remove(".py") |
1570 |
1570 |
1571 modname = module |
1571 modname = module |
1574 path = [os.path.dirname(module)] |
1574 path = [os.path.dirname(module)] |
1575 if module.lower().endswith(".py"): |
1575 if module.lower().endswith(".py"): |
1576 module = module[:-3] |
1576 module = module[:-3] |
1577 if ( |
1577 if ( |
1578 os.path.exists(os.path.join(path[0], "__init__.py")) |
1578 os.path.exists(os.path.join(path[0], "__init__.py")) |
|
1579 or os.path.exists(os.path.join(path[0], "__init__.pyi")) |
1579 or os.path.exists(os.path.join(path[0], "__init__.rb")) |
1580 or os.path.exists(os.path.join(path[0], "__init__.rb")) |
1580 or inpackage |
1581 or inpackage |
1581 ): |
1582 ): |
1582 if basename: |
1583 if basename: |
1583 module = module.replace(basename, "") |
1584 module = module.replace(basename, "") |
1584 if os.path.isabs(module): |
1585 if os.path.isabs(module): |
1585 modname = os.path.splitdrive(module)[1][len(os.sep) :] |
1586 modname = os.path.splitdrive(module)[1][len(os.sep) :] |
1586 else: |
1587 else: |
1587 modname = module |
1588 modname = module |
1588 modname = modname.replace(os.sep, ".") |
1589 modname = modname.replace(os.sep, ".") |
1589 inpackage = 1 |
1590 inpackage = True |
1590 else: |
1591 else: |
1591 modname = os.path.basename(module) |
1592 modname = os.path.basename(module) |
1592 for ext in _extensions: |
1593 for ext in _extensions: |
1593 if modname.lower().endswith(ext): |
1594 if modname.lower().endswith(ext): |
1594 modname = modname[: -len(ext)] |
1595 modname = modname[: -len(ext)] |
1687 ) |
1688 ) |
1688 raise ImportError |
1689 raise ImportError |
1689 |
1690 |
1690 # standard Python module file |
1691 # standard Python module file |
1691 if name.lower().endswith(".py"): |
1692 if name.lower().endswith(".py"): |
1692 name = name[:-3] |
1693 name = os.path.splitext(name)[0] |
1693 |
1694 |
1694 spec = importlib.machinery.PathFinder.find_spec(name, path) |
1695 spec = importlib.machinery.PathFinder.find_spec(name, path) |
1695 if spec is None: |
1696 if spec is None: |
1696 raise ImportError |
1697 raise ImportError |
1697 if isinstance(spec.loader, importlib.machinery.SourceFileLoader): |
1698 if isinstance(spec.loader, importlib.machinery.SourceFileLoader): |
1718 the filename of the module file to be cleared. (string) |
1719 the filename of the module file to be cleared. (string) |
1719 """ |
1720 """ |
1720 modname = module |
1721 modname = module |
1721 |
1722 |
1722 if os.path.exists(module): |
1723 if os.path.exists(module): |
1723 path = [os.path.dirname(module)] |
1724 modulePath = [os.path.dirname(module)] |
1724 if module.lower().endswith(".py"): |
1725 if module.lower().endswith(".py"): |
1725 module = module[:-3] |
1726 module = os.path.splitext(module)[0] |
1726 if os.path.exists(os.path.join(path[0], "__init__.py")): |
1727 if os.path.exists(os.path.join(modulePath[0], "__init__.py")): |
1727 if basename: |
1728 if basename: |
1728 module = module.replace(basename, "") |
1729 module = module.replace(basename, "") |
1729 modname = module.replace(os.sep, ".") |
1730 modname = module.replace(os.sep, ".") |
1730 else: |
1731 else: |
1731 modname = os.path.basename(module) |
1732 modname = os.path.basename(module) |
1732 if modname.lower().endswith(".ptl") or modname.lower().endswith(".pyw"): |
1733 modname = os.path.splitext(modname)[0] |
1733 modname = modname[:-4] |
|
1734 elif modname.lower().endswith(".rb"): |
|
1735 modname = modname[:-3] |
|
1736 module = os.path.basename(module) |
1734 module = os.path.basename(module) |
1737 |
1735 |
1738 if modname in _modules: |
1736 if modname in _modules: |
1739 del _modules[modname] |
1737 del _modules[modname] |