diff --git a/src/sage/doctest/sources.py b/src/sage/doctest/sources.py index a33817dd062..31fc5f84e80 100644 --- a/src/sage/doctest/sources.py +++ b/src/sage/doctest/sources.py @@ -27,7 +27,7 @@ from sage.cpython.string import bytes_to_str from sage.repl.load import load from sage.misc.lazy_attribute import lazy_attribute -from sage.misc.namespace_package import is_package_or_sage_namespace_package_dir +from sage.misc.package_dir import is_package_or_sage_namespace_package_dir from .parsing import SageDocTestParser from .util import NestedName from sage.structure.dynamic_class import dynamic_class @@ -652,7 +652,7 @@ def in_lib(self): Such files aren't loaded before running tests. - This uses :func:`~sage.misc.namespace_package.is_package_or_sage_namespace_package_dir` + This uses :func:`~sage.misc.package_dir.is_package_or_sage_namespace_package_dir` but can be overridden via :class:`~sage.doctest.control.DocTestDefaults`. EXAMPLES:: diff --git a/src/sage/misc/namespace_package.py b/src/sage/misc/namespace_package.py index 73bab836bc9..75d9f1d8dcc 100644 --- a/src/sage/misc/namespace_package.py +++ b/src/sage/misc/namespace_package.py @@ -2,7 +2,6 @@ Utility functions for namespace packages in Sage """ from importlib import import_module -import os, glob def install_doc(package, doc): """ @@ -19,57 +18,3 @@ def install_doc(package, doc): pkg = import_module(package) pkg.__doc__ = doc # enable sage.package? pkg.getdoc = lambda: doc # enable help(sage.package) - - -def is_package_or_sage_namespace_package_dir(path): - """ - Return whether ``path`` is a directory that contains a Python package. - - Ordinary Python packages are recognized by the presence of `__init__.py`. - - Implicit namespace packages (PEP 420) are only recognized if they - follow the conventions of the Sage library, i.e., the directory contains - a file ``all.py`` or a file matching the pattern ``all__*.py`` - such as ``all__sagemath_categories.py``. - - EXAMPLES: - - :mod:`sage.cpython` is an ordinary package:: - - sage: from sage.misc.namespace_package import is_package_or_sage_namespace_package_dir - sage: directory = os.path.dirname(sage.cpython.__file__); directory - '.../sage/cpython' - sage: is_package_or_sage_namespace_package_dir(directory) - True - - :mod:`sage.libs.mpfr` only has an ``__init__.pxd`` file, but we consider - it a package directory for consistency with Cython:: - - sage: directory = os.path.join(os.path.dirname(sage.libs.all.__file__), 'mpfr'); directory - '.../sage/libs/mpfr' - sage: is_package_or_sage_namespace_package_dir(directory) - True - - :mod:`sage` is designated to become an implicit namespace package:: - - sage: directory = os.path.dirname(sage.env.__file__); directory - '.../sage' - sage: is_package_or_sage_namespace_package_dir(directory) - True - - Not a package:: - - sage: directory = os.path.join(os.path.dirname(sage.symbolic.__file__), 'ginac'); directory - '.../sage/symbolic/ginac' - sage: is_package_or_sage_namespace_package_dir(directory) - False - """ - if os.path.exists(os.path.join(path, '__init__.py')): # ordinary package - return True - if os.path.exists(os.path.join(path, '__init__.pxd')): # for consistency with Cython - return True - if os.path.exists(os.path.join(path, 'all.py')): # complete namespace package - return True - for _ in glob.iglob(os.path.join(path, 'all__*.py')): - return True # partial namespace package - return False diff --git a/src/sage/misc/package_dir.py b/src/sage/misc/package_dir.py new file mode 100644 index 00000000000..11f83a06f73 --- /dev/null +++ b/src/sage/misc/package_dir.py @@ -0,0 +1,57 @@ +""" +Recognizing package directories +""" +import os, glob + +def is_package_or_sage_namespace_package_dir(path): + """ + Return whether ``path`` is a directory that contains a Python package. + + Ordinary Python packages are recognized by the presence of `__init__.py`. + + Implicit namespace packages (PEP 420) are only recognized if they + follow the conventions of the Sage library, i.e., the directory contains + a file ``all.py`` or a file matching the pattern ``all__*.py`` + such as ``all__sagemath_categories.py``. + + EXAMPLES: + + :mod:`sage.cpython` is an ordinary package:: + + sage: from sage.misc.package_dir import is_package_or_sage_namespace_package_dir + sage: directory = os.path.dirname(sage.cpython.__file__); directory + '.../sage/cpython' + sage: is_package_or_sage_namespace_package_dir(directory) + True + + :mod:`sage.libs.mpfr` only has an ``__init__.pxd`` file, but we consider + it a package directory for consistency with Cython:: + + sage: directory = os.path.join(os.path.dirname(sage.libs.all.__file__), 'mpfr'); directory + '.../sage/libs/mpfr' + sage: is_package_or_sage_namespace_package_dir(directory) + True + + :mod:`sage` is designated to become an implicit namespace package:: + + sage: directory = os.path.dirname(sage.env.__file__); directory + '.../sage' + sage: is_package_or_sage_namespace_package_dir(directory) + True + + Not a package:: + + sage: directory = os.path.join(os.path.dirname(sage.symbolic.__file__), 'ginac'); directory + '.../sage/symbolic/ginac' + sage: is_package_or_sage_namespace_package_dir(directory) + False + """ + if os.path.exists(os.path.join(path, '__init__.py')): # ordinary package + return True + if os.path.exists(os.path.join(path, '__init__.pxd')): # for consistency with Cython + return True + if os.path.exists(os.path.join(path, 'all.py')): # complete namespace package + return True + for _ in glob.iglob(os.path.join(path, 'all__*.py')): + return True # partial namespace package + return False diff --git a/src/sage_setup/find.py b/src/sage_setup/find.py index bda78ac4aab..58e01a25698 100644 --- a/src/sage_setup/find.py +++ b/src/sage_setup/find.py @@ -18,7 +18,7 @@ from collections import defaultdict -from sage.misc.namespace_package import is_package_or_sage_namespace_package_dir as is_package_or_namespace_package_dir +from sage.misc.package_dir import is_package_or_sage_namespace_package_dir as is_package_or_namespace_package_dir def read_distribution(src_file):