Reused `Markdown` instance keeps link URLs and code spans from earlier documents

未关闭
#763 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 3 天内回复

还没有人认领这个 Issue。

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
53/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
python
领域
backend

调研方向

Read reset() and the initialization path in the Markdown class, especially how _escape_table and _code_table are used by _setup_extras(). The issue provides a reproducer showing the URL escaping difference and reports that resetting both tables should preserve single document output. Verify reused instances match fresh instances across successive convert() calls and check the existing tests for regression coverage.

由索引模型根据 Issue 内容生成。

描述

Describe the bug

reset() doesn't clear _escape_table or _code_table, so a reused Markdown() (as in the README's markdowner example) keeps every URL and code span it has seen. markdown2.markdown() is unaffected.

It slows down. One instance, 40 documents of 200 links and 200 code spans each:

Document Reused New instance
1st 9.4 ms 8.5 ms
10th 74.6 ms 8.6 ms
40th 349.9 ms 8.6 ms

It also changes output: a backslash before a URL seen in an earlier document disappears.

To Reproduce

import markdown2

md = markdown2.Markdown()
md.convert("[a link](https://example.com/page)")

print(md.convert(r"Type \https://example.com/page to see it."))
# <p>Type https://example.com/page to see it.</p>

print(markdown2.Markdown().convert(r"Type \https://example.com/page to see it."))
# <p>Type \https://example.com/page to see it.</p>

Expected behavior

Each convert() matches a new instance in output and time.

Debug info

Version: master (cecee4f). Released versions too: code spans since 2.4.3, URLs since 2.4.7.

Extras: none.

Additional context

Fix: set both tables up in a helper called from __init__ and from reset() (before _setup_extras(), which reads _escape_table for middle-word-em). Output for a single document is unchanged across all test cases.

Threading: instances aren't thread-safe now (reset() already replaces urls, titles, and html_blocks). Shared across threads, inline links and code spans survive on master only because their tables never reset. With the fix, 4 threads sharing one instance got 9 to 10 of 120 documents wrong (master: 0). With reference links, master already gets about half wrong.

Patch: https://github.com/ptweezy/python-markdown2/commit/16426734f4d656165f08b9e7ee4c09856655da3a

主要语言
Python
星标
2.8k
派生
477
平均合并
4 天 6 小时
30 天内合并 PR
12

环境准备

这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

trentm/python-markdown2 的其他 Issue

查看 trentm/python-markdown2 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。