Skip to content

Commit 148237b

Browse files
committed
BUG: HTML-escape function parameter default values
Such as `object()` rendering as `<object ...`, breaking HTML. Tentatively fixes #108.
1 parent d12db69 commit 148237b

File tree

4 files changed

+23
-3
lines changed

4 files changed

+23
-3
lines changed

pdoc/__init__.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1100,10 +1100,21 @@ def _params(func_obj, annotate=False, link=None, module=None):
11001100
return ["..."]
11011101

11021102
def safe_default_value(p: inspect.Parameter):
1103+
if p.default is inspect.Parameter.empty:
1104+
return p
1105+
1106+
replacement = None
11031107
if p.default is os.environ:
1108+
replacement = 'os.environ'
1109+
elif inspect.isclass(p.default):
1110+
replacement = p.default.__module__ + '.' + p.default.__qualname__
1111+
elif ' at 0x' in repr(p.default):
1112+
replacement = re.sub(r' at 0x\w+', '', repr(p.default))
1113+
1114+
if replacement:
11041115
class mock:
11051116
def __repr__(self):
1106-
return 'os.environ'
1117+
return replacement
11071118
return p.replace(default=mock())
11081119
return p
11091120

pdoc/templates/html.mako

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@
9494
if returns:
9595
returns = ' ->\N{NBSP}' + returns
9696
%>
97-
<span>${f.funcdef()} ${ident(f.name)}</span>(<span>${params})${returns}</span>
97+
<span>${f.funcdef()} ${ident(f.name)}</span>(<span>${params | h})${returns | h}</span>
9898
</code></dt>
9999
<dd>${show_desc(f)}</dd>
100100
</%def>

pdoc/test/__init__.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ def _check_files(self, include_patterns=(), exclude_patterns=(), file_pattern='*
130130

131131
def test_html(self):
132132
include_patterns = [
133+
'a=&lt;object',
133134
'CONST docstring',
134135
'var docstring',
135136
'foreign_var',
@@ -162,6 +163,7 @@ def test_html(self):
162163
' class="ident">static',
163164
]
164165
exclude_patterns = [
166+
'<object ',
165167
' class="ident">_private',
166168
' class="ident">_Private',
167169
]
@@ -264,6 +266,7 @@ def test_link_prefix(self):
264266

265267
def test_text(self):
266268
include_patterns = [
269+
'object_as_arg_default(*args, a=<object ',
267270
'CONST docstring',
268271
'var docstring',
269272
'foreign_var',
@@ -615,6 +618,9 @@ def test_Function_params(self):
615618
lambda a=os.environ: None)
616619
self.assertEqual(func.params(), ['a=os.environ'])
617620

621+
func = pdoc.Function('f', mod, lambda a=object(): None)
622+
self.assertEqual(func.params(), ['a=<object object>'])
623+
618624
# typed
619625
def f(a: int, *b, c: typing.List[pdoc.Doc] = []): pass
620626
func = pdoc.Function('f', mod, f)

pdoc/test/example_pkg/__init__.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@
1818

1919
def foo(env=os.environ):
2020
"""Doesn't leak environ"""
21-
pass
21+
22+
23+
def object_as_arg_default(*args, a=object(), **kwargs):
24+
"""Html-encodes angle brackets in params"""
2225

2326

2427
class A:

0 commit comments

Comments
 (0)