From 1c2b7071facdbdecf1be6b80af5e14377f83caf2 Mon Sep 17 00:00:00 2001 From: Mikhail Golubev Date: Wed, 30 Aug 2017 19:32:25 +0300 Subject: [PATCH] PY-25885 Remove registered sphinxcontrib modules in docstring_formatter.py Otherwise their presense doesn't let us import sphinxcontrib.napoleon that we need to render docstrings in Numpy/Google Style formats. It's not enough to remove site-packages/dist-packages from sys.path to isolate the script from such "malicious" packages, since they are registered and installed in sys.modules on interpreter startup before any path modifications happen. Launching interpreter with -S option prevents it from running these hooks and also excludes site-packages from sys.path but makes inherited stdlib modules not available in virtualenv. What's more, sphinxcontrib.napoleon itself depends on setuptools as it tries to register itself in a similar fashion in its __init__.py. We could patch it but that would make future updates of the library more tedious and error-prone. --- python/helpers/docstring_formatter.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/python/helpers/docstring_formatter.py b/python/helpers/docstring_formatter.py index 3a641e74bb30..35ab5ab584eb 100644 --- a/python/helpers/docstring_formatter.py +++ b/python/helpers/docstring_formatter.py @@ -369,6 +369,11 @@ def format_epytext(docstring): def main(): + # Remove existing Sphinx extensions registered via + # sphinxcontrib setuptools namespace package, as they + # conflict with sphinxcontrib.napoleon that we bundle. + sys.modules.pop('sphinxcontrib', None) + args = sys.argv[1:] docstring_format = args[0] if args else 'rest' @@ -393,4 +398,8 @@ def main(): if __name__ == '__main__': - main() + try: + main() + except ImportError: + print_safe('sys.path = %s\n\n' % sys.path, error=True) + raise