markdown.extensions.md_in_html fails with TypeError: expected string or bytes-like object · Issue #1040 · Python-Markdown/markdown (original) (raw)

I'm running into an issue with "simple" Markdown + HTML examples crashing with a TypeError. For example, the following throws a TypeError: expected string or bytes-like object exception:

markdown.markdown( 'Hello\n

', extensions=["markdown.extensions.md_in_html"], )

Versions: Markdown==3.3.1, CPython 3.8.6

I haven't had time yet to investigate this further, but I wanted to get an issue opened with some failing test cases in case others are running into this as well. I suspect this has something to do with the raw HTML parser rewrite in #803.

Steps to reproduce:

Save the following as mderror.py:

import traceback import markdown

def try_render(text): print(f"\n\n---- Rendering: {text!r}") try: html = markdown.markdown( text, extensions=["markdown.extensions.md_in_html"], ) print(f"---- PASS: {html!r}") except Exception as e: print("---- FAILED") traceback.print_exc()

try_render('Hello

') # Error try_render('Hello\n
') # Error try_render('Hello\n\n
') # Okay

Result:

$ python mderror.py

---- Rendering: 'Hello

' <Element 'div' at 0x110758310> ---- FAILED Traceback (most recent call last): File "mderror.py", line 8, in try_render html = markdown.markdown( File "PROJECT_ROOT/venv/lib/python3.8/site-packages/markdown/core.py", line 386, in markdown return md.convert(text) File "PROJECT_ROOT/venv/lib/python3.8/site-packages/markdown/core.py", line 290, in convert output = pp.run(output) File "PROJECT_ROOT/venv/lib/python3.8/site-packages/markdown/postprocessors.py", line 73, in run if self.isblocklevel(html): File "PROJECT_ROOT/venv/lib/python3.8/site-packages/markdown/postprocessors.py", line 94, in isblocklevel m = re.match(r"^</?([^ >]+)", html) File "/Users/dbader/.pyenv/versions/3.8.6/lib/python3.8/re.py", line 191, in match return _compile(pattern, flags).match(string) TypeError: expected string or bytes-like object

---- Rendering: 'Hello\n

' <Element 'div' at 0x110758900> ---- FAILED Traceback (most recent call last): File "mderror.py", line 8, in try_render html = markdown.markdown( File "PROJECT_ROOT/venv/lib/python3.8/site-packages/markdown/core.py", line 386, in markdown return md.convert(text) File "PROJECT_ROOT/venv/lib/python3.8/site-packages/markdown/core.py", line 290, in convert output = pp.run(output) File "PROJECT_ROOT/venv/lib/python3.8/site-packages/markdown/postprocessors.py", line 73, in run if self.isblocklevel(html): File "PROJECT_ROOT/venv/lib/python3.8/site-packages/markdown/postprocessors.py", line 94, in isblocklevel m = re.match(r"^</?([^ >]+)", html) File "/Users/dbader/.pyenv/versions/3.8.6/lib/python3.8/re.py", line 191, in match return _compile(pattern, flags).match(string) TypeError: expected string or bytes-like object

---- Rendering: 'Hello\n\n

'

---- PASS: '

Hello

\n
'