Skip to content

Commit

Permalink
REF: Only reload modules when in --http mode
Browse files Browse the repository at this point in the history
Maybe avoids problems with modules whose importing
has side effects.
  • Loading branch information
kernc committed May 3, 2019
1 parent facbb6f commit 0f5f96e
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
12 changes: 6 additions & 6 deletions pdoc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ def _render_template(template_name, **kwargs):
raise


def html(module_name, docfilter=None, **kwargs) -> str:
def html(module_name, docfilter=None, reload=False, **kwargs) -> str:
"""
Returns the documentation for the module `module_name` in HTML
format. The module must be a module or an importable string.
Expand All @@ -133,12 +133,12 @@ def html(module_name, docfilter=None, **kwargs) -> str:
that takes a single argument (a documentation object) and returns
`True` or `False`. If `False`, that object will not be documented.
"""
mod = Module(import_module(module_name), docfilter=docfilter)
mod = Module(import_module(module_name, reload=reload), docfilter=docfilter)
link_inheritance()
return mod.html(**kwargs)


def text(module_name, docfilter=None, **kwargs) -> str:
def text(module_name, docfilter=None, reload=False, **kwargs) -> str:
"""
Returns the documentation for the module `module_name` in plain
text format suitable for viewing on a terminal.
Expand All @@ -149,12 +149,12 @@ def text(module_name, docfilter=None, **kwargs) -> str:
that takes a single argument (a documentation object) and returns
`True` or `False`. If `False`, that object will not be documented.
"""
mod = Module(import_module(module_name), docfilter=docfilter)
mod = Module(import_module(module_name, reload=reload), docfilter=docfilter)
link_inheritance()
return mod.text(**kwargs)


def import_module(module) -> ModuleType:
def import_module(module, *, reload: bool = False) -> ModuleType:
"""
Return module object matching `module` specification (either a python
module path or a filesystem path to file/directory).
Expand Down Expand Up @@ -183,7 +183,7 @@ def _module_path(module):
assert inspect.ismodule(module)
# If this is pdoc itself, return without reloading. Otherwise later
# `isinstance(..., pdoc.Doc)` calls won't work correctly.
if not module.__name__.startswith(__name__):
if reload and not module.__name__.startswith(__name__):
module = importlib.reload(module)
return module

Expand Down
5 changes: 3 additions & 2 deletions pdoc/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ def do_GET(self):
importlib.invalidate_caches()
code = 200
if self.path == "/":
modules = [pdoc.import_module(module)
modules = [pdoc.import_module(module, reload=True)
for module in self.args.modules]
modules = sorted((module.__name__, inspect.getdoc(module))
for module in modules)
Expand Down Expand Up @@ -236,7 +236,8 @@ def html(self):
"""
# TODO: pass extra pdoc.html() params
return pdoc.html(self.import_path_from_req_url,
http_server=True, external_links=True, **self.template_config)
reload=True, http_server=True, external_links=True,
**self.template_config)

def resolve_ext(self, import_path):
def exists(p):
Expand Down

0 comments on commit 0f5f96e

Please sign in to comment.