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

Aperta
#763 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 3 giorni

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
53/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
python
Ambito
backend

Direzione di ricerca

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.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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

Lingua principale
Python
Stelle
2.8k
Fork
477
Merge medio
4g 6h
PR unite (30g)
12

Preparare l'ambiente

Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di trentm/python-markdown2

Tutte le issue di trentm/python-markdown2

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.