microsoft / microsoft/playwright-python
[Bug]: RegExp values are not serialized by evaluate(), and returned RegExps leak internal protocol JSON
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 15k
- Forks
- 1.2k
- Avg merge
- 55m
- Merged PRs (30d)
- 4
Description
Version
1.62.0
Steps to reproduce
import re
from playwright.sync_api import sync_playwright
with sync_playwright() as p:
browser = p.chromium.launch()
page = browser.new_page()
# 1. Passing a compiled pattern in
print(page.evaluate("(v) => [typeof v, String(v)]", re.compile(r"a\d+", re.I)))
# 2. Getting a RegExp back out
print(page.evaluate("() => /a\\d+/gi"))
# 3. Nested in a structure
print(page.evaluate("() => ({x: [/foo/m]})"))
browser.close()
Expected behavior
['object', '/a\\d+/i'], and the value is a realRegExpinside the page.re.compile('a\\d+', re.IGNORECASE){'x': [re.compile('foo', re.MULTILINE)]}
This is what playwright-dotnet does today. EvaluateArgumentValueConverter.cs serializes Regex (line 138) and parses "r" back into a Regex (line 351).
Actual behavior
['undefined', 'undefined'], the argument is silently dropped{'r': {'p': 'a\\d+', 'f': 'gi'}}{'x': [{'r': {'p': 'foo', 'f': 'm'}}]}
Two separate problems, both silent:
serialize_value()inplaywright/_impl/_js_handle.pyhas no branch forre.Pattern, so a compiled pattern falls through to{"v": "undefined"}.parse_value()has no branch for"r", so it hits the barereturn valueat the end and hands back the raw wire format. There is no way to tell from the outside that this dict is not the actual result.
The protocol supports this in both directions and the driver already implements both sides ({r: {p, f}} on serialize, new RegExp(v.r.p, v.r.f) on parse), so this is only missing on the Python side.
Additional context
Happy to send a PR, I have one ready. The serialize direction can reuse the existing escape_regex_flags() in _str_utils.py, which already maps re.IGNORECASE|DOTALL|MULTILINE to i/s/m for locators and route matching. The parse direction needs the inverse.
Two things worth an opinion before I do:
- JavaScript flags with no
reequivalent (g,y,d,u,v). I would drop them rather than raise, since/foo/gis very common and raising would be worse than today's behavior. Note that playwright-dotnet'sFromInlineFlagsthrows on these, which looks like a separate bug over there. - A JS pattern that is not valid Python
resyntax, for example/(?<name>x)/(Python spells it(?P<name>x)). With this change that would raise fromre.compileinstead of returning the dict. I think raising is acceptable since the dict was never usable anyway, but let me know if you would rather it degrade some other way.
Environment
- Operating System: macOS 26.3.1
- CPU: arm64
- Browser: Chromium
- Python Version: 3.14.0
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start in playwright/_impl/_js_handle.py, inspecting serialize_value() and parse_value(), then review escape_regex_flags() in _str_utils.py for existing flag handling. Reproduce the three evaluate() cases from the issue and add coverage for compiled patterns, returned RegExps, and nested values. Done means Python regex values serialize correctly and returned JavaScript RegExps become re.Pattern objects rather than protocol dictionaries.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- api
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Active
- Clarity
- Clearly specified
- Newbie friendliness
- 72/100