From e34bd6b1ee50372918b1201c2e40f7df65064e72 Mon Sep 17 00:00:00 2001 From: Petr Viktorin Date: Fri, 24 Nov 2017 23:07:51 +0100 Subject: [PATCH] Doc: Allow building documentation even without a compiled extension This involves a fake _ldap module and carefully bootstrapping ldap.__init__ (which imports * from _ldap). --- Doc/conf.py | 9 ++++++- Doc/fake_ldap_module_for_documentation.py | 30 +++++++++++++++++++++++ Lib/ldap/constants.py | 3 +++ 3 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 Doc/fake_ldap_module_for_documentation.py diff --git a/Doc/conf.py b/Doc/conf.py index d40e822..d81bf29 100644 --- a/Doc/conf.py +++ b/Doc/conf.py @@ -12,9 +12,16 @@ # serve to show the default value. import sys +import os # If your extensions are in another directory, add it here. -#sys.path.append('some/directory') +_doc_dir = os.path.dirname(__file__) +sys.path.append(_doc_dir) +sys.path.append(os.path.join(_doc_dir, '../Lib/')) +sys.path.insert(0, os.path.join(_doc_dir, '../Lib/ldap')) + +# Import fake `_ldap` module +import fake_ldap_module_for_documentation # General configuration # --------------------- diff --git a/Doc/fake_ldap_module_for_documentation.py b/Doc/fake_ldap_module_for_documentation.py new file mode 100644 index 0000000..3080781 --- /dev/null +++ b/Doc/fake_ldap_module_for_documentation.py @@ -0,0 +1,30 @@ +""" +A module that mocks `_ldap` for the purposes of generating documentation + +This module provides placeholders for the contents of `_ldap`, making it +possible to generate documentation even _ldap is not compiled. +It should also make the documentation independent of which features are +available in the system OpenLDAP library. + +The overly long module name will show up in AttributeError messages, +hinting that this is not the actual _ldap. + +See https://www.python-ldap.org/ for details. +""" + +import sys + +# Cause `import _ldap` to import this module instead of the actual `_ldap`. +sys.modules['_ldap'] = sys.modules[__name__] + +from constants import CONSTANTS +from pkginfo import __version__ + +for constant in CONSTANTS: + globals()[constant.name] = constant + +def get_option(num): + pass + +class LDAPError: + pass diff --git a/Lib/ldap/constants.py b/Lib/ldap/constants.py index 6469a30..aafa265 100644 --- a/Lib/ldap/constants.py +++ b/Lib/ldap/constants.py @@ -10,6 +10,9 @@ """ +# This module cannot import anything from ldap. +# When building documentation, it is used to initialize ldap.__init__. + from __future__ import print_function class Constant(object):