Skip to content

feat(python): selectolax users had no way to try Parsec from Python - #80

Merged
jamesconwaydev merged 5 commits into
mainfrom
bindings
Nov 18, 2026
Merged

jamesconwaydev merged 5 commits into
mainfrom
bindings

Conversation

@jamesconwaydev

@jamesconwaydev jamesconwaydev commented Nov 18, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

Most Python users meet Lexbor through selectolax, and there was no way to try Parsec from Python at all (#23).

Change

  • bindings/python: the parsec-html package, built with maturin and PyO3 0.30, imported as parsec_html. I didn't use parsec as the import name because PyPI already has a parsec (parser combinators), and two packages that install the same module overwrite each other.
  • The API is selectolax's. I ran the same calls through both against selectolax 1.0.2 and matched the results: HTMLParser(html, is_fragment, fragment_tag, fragment_namespace, scripting) with root, head, body, html, text(deep, separator, strip, skip_empty), css, css_first(default, strict), css_matches, any_css_matches, tags, create_node, merge_text_nodes, strip_tags, unwrap_tags. Nodes have tag, id, attributes, attrs (a live mapping), html, inner_html (settable), text_content, comment_content, parent/next/prev/first_child/last_child, iter, traverse, decompose/remove, unwrap, replace_with, insert_before/insert_after/insert_child (with move=), merge_text_nodes and the is_*_node checks. Type stubs and py.typed ship in the wheel. The README and the wiki's Python page list each difference.
  • Threads: one parser owns its document behind a RwLock, so a page can be read from many threads and edited from any of them. The module is declared gil_used = false, and parsing and css() let go of the GIL while they run. Nothing waits on the lock while attached to the interpreter, and nothing creates a Python object while holding it, so neither a stop-the-world pause nor a finalizer can deadlock it.
  • Parsec additions: budget=, max_depth= and url= (which makes :target and :local-link match). Selectors like :lang(), :dir(), :valid and :scope work; selectolax 1.0.2 rejects them as unparseable.
  • .github/workflows/python.yml lints the crate, runs cargo deny, and builds and pytest-tests the wheel on Linux, macOS and Windows with 3.9, 3.14 and 3.14t, plus Linux with 3.15 and the abi3t wheel on 3.15 and 3.15t. 3.9 runs on Ubuntu 24.04, since 26.04 has none. PyO3 0.30 doesn't support free-threaded 3.13, so 3.13t isn't built. Pip installs use hashed ci-requirements.txt.
  • .github/workflows/release-python.yml runs on v* tags. It builds wheels per CPython version (3.9 to 3.15, plus 3.14t and 3.15t) on manylinux and musllinux (x86-64 and ARM), macOS (Intel and ARM) and Windows (x64 and ARM). It also builds one abi3t wheel for later versions, plus an sdist. Publishing is PyPI trusted publishing, and it's off until the PYPI_PUBLISH repository variable is true and the release environment and the PyPI trusted publisher are set up. A manual run builds everything and publishes nothing. It's a separate file from ci: a release was neither signed nor reproducible #76's release.yml.
  • Why per-version wheels and not abi3: with the stable ABI, css("*") on the WHATWG spec (400,000 nodes) took 31 ms, against 22 ms built for the version. So abi3t is only the fallback.
  • README and ROADMAP: Python moves to done in the table and the M4 list.

Not here yet: source positions (the core has none until #8), XPath (#31), and clone() (the core's Document isn't Clone). This is part of #23, not all of it.

Tests

  • pytest bindings/python/tests: 15 tests, all passing on CPython 3.9, 3.14, 3.14t (GIL off, checked) and 3.15 (abi3t wheel). They cover parsing, text options, selectors, attributes, navigation, every edit including copies between documents, templates, fragments, limits, and 8 reader threads plus a writer sharing one document.
  • cargo fmt --check, cargo clippy -- -D warnings (with and without abi3t) and cargo deny check on the new crate. The root workspace checks are unchanged and pass.
  • bindings/python/bench.py against selectolax 1.0.2 on bench's 13 pages; numbers in the README.

Part of #23.

bindings/python is the parsec-html package, built with maturin and
PyO3, and imported as parsec_html (a `parsec` already exists on PyPI).
Its API is selectolax's: HTMLParser(html) with css, css_first, root,
head, body, text, and nodes with tag, attributes, attrs, html,
inner_html, navigation, iter, traverse, decompose, unwrap, replace_with,
insert_before/after/child and merge_text_nodes. Type stubs ship in the
wheel.

A parser keeps its document behind a RwLock, so one page can be read
from many threads and edited from any of them. The module declares that
it doesn't need the GIL, and parsing and css() let go of it while they
work. Nothing waits for the lock while attached to the interpreter, and
nothing makes a Python object while holding it, so a stop-the-world
pause or a finalizer can't deadlock it.

The release builds a wheel per CPython from 3.9 to 3.15 and for 3.13t,
3.14t and 3.15t, plus an abi3t wheel for later versions: the stable ABI
made creating 400,000 nodes take 31 ms against 22. Publishing uses PyPI
trusted publishing and only runs once PYPI_PUBLISH is set.
PyO3 0.30 refuses free-threaded 3.13, so it's out of the test matrix and
the release, and the manylinux builds name their interpreters rather
than finding them, since those images carry a 3.13t. ubuntu-latest is
now 26.04, which has no CPython 3.9 to install, so 3.9 tests on 24.04.
Two runs of bindings/python/bench.py at a load of 3: parsing the 13
pages takes 66 ms to selectolax's 106, and the 32 selectors 283 ms to
its 781.
@jamesconwaydev
jamesconwaydev enabled auto-merge (squash) November 18, 2026 23:55
@jamesconwaydev
jamesconwaydev merged commit 4cdcfed into main Nov 18, 2026
27 checks passed
@jamesconwaydev
jamesconwaydev deleted the bindings branch November 18, 2026 23:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant