Skip to content

Fix TypeError serving a namespace package in http_server mode - #479

Open
cristianchiriac wants to merge 1 commit into
pdoc3:masterfrom
cristianchiriac:fix/namespace-package-http-server-crash
Open

cristianchiriac wants to merge 1 commit into
pdoc3:masterfrom
cristianchiriac:fix/namespace-package-http-server-crash

Conversation

@cristianchiriac

Copy link
Copy Markdown

Fixes #461.

Bug

A namespace package's __file__ attribute is None (not missing — Python sets it explicitly to None for PEP 420 namespace packages):

>>> import a  # a namespace package, no __init__.py
>>> a.__file__
None

Two places in pdoc's dev server pass that None straight to os.stat():

  • html.mako's auto-reload script: ${os.stat(module.obj.__file__).st_mtime}, evaluated while rendering the page itself.
  • _WebDoc.check_modified() in cli.py: os.stat(module.__file__).st_mtime, used by the auto-reload polling's HEAD requests.

Navigating to a namespace-package module's page crashes while rendering (matching the traceback in the issue), and even if that were avoided, the HEAD polling from the auto-reload script would crash the same way.

Fix

  • html.mako: only emit the auto-reload script when module.obj.__file__ is set, since there's nothing meaningful to stat for a namespace package.
  • cli.py: check_modified() now checks for module.__file__ is None up front and treats it as "modified" (HTTP 205) rather than crashing.

Test plan

  • Added test_namespace_html_http_server in pdoc/test/__init__.py, reusing the existing _namespace example-package fixtures, which renders a namespace package's HTML with http_server=True.
  • Verified the new test fails against the unmodified code with the exact TypeError: stat: path should be string, bytes, os.PathLike or integer, not NoneType from the issue, and passes with the fix.
  • python -m unittest pdoc.test: 84 tests, same 5 pre-existing failures with or without this change (Windows CRLF vs. \n line-ending mismatches in test_text/test_text_identifier/etc., unrelated to this fix — confirmed identical failure list on unmodified master).
  • flake8 on the changed Python files: clean.

Namespace packages (no __init__.py) have __file__ set to None. The
html.mako auto-reload script and _WebDoc.check_modified() both called
os.stat() on that None value directly, crashing with "TypeError: stat:
path should be string, bytes, os.PathLike or integer, not NoneType"
instead of rendering the page.

Skip the auto-reload script in the template when there's no real file
to stat, and treat a namespace package as always-modified in
check_modified() instead of raising.

Fixes pdoc3#461

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

TypeError on namespace package while working in server mode

1 participant