microsoft / microsoft/playwright-python

[Bug]: RegExp values are not serialized by evaluate(), and returned RegExps leak internal protocol JSON

Đang mở
#3,188 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Ngôn ngữ chính
Python
Star
15k
Fork
1.2k
Merge trung bình
55 phút
Pull request đã merge (30 ngày)
4

Mô tả

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
  1. ['object', '/a\\d+/i'], and the value is a real RegExp inside the page.
  2. re.compile('a\\d+', re.IGNORECASE)
  3. {'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
  1. ['undefined', 'undefined'], the argument is silently dropped
  2. {'r': {'p': 'a\\d+', 'f': 'gi'}}
  3. {'x': [{'r': {'p': 'foo', 'f': 'm'}}]}

Two separate problems, both silent:

  • serialize_value() in playwright/_impl/_js_handle.py has no branch for re.Pattern, so a compiled pattern falls through to {"v": "undefined"}.
  • parse_value() has no branch for "r", so it hits the bare return value at 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:

  1. JavaScript flags with no re equivalent (g, y, d, u, v). I would drop them rather than raise, since /foo/g is very common and raising would be worse than today's behavior. Note that playwright-dotnet's FromInlineFlags throws on these, which looks like a separate bug over there.
  2. A JS pattern that is not valid Python re syntax, for example /(?<name>x)/ (Python spells it (?P<name>x)). With this change that would raise from re.compile instead 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

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Bắt đầu trong playwright/_impl/_js_handle.py bằng cách kiểm tra serialize_value() và parse_value(), sau đó xem lại escape_regex_flags() trong _str_utils.py để kiểm tra cách xử lý flag hiện có. Tái hiện ba trường hợp evaluate() từ issue và bổ sung coverage cho các pattern đã biên dịch, các RegExps JavaScript được trả về và các giá trị lồng nhau. Công việc được xem là hoàn tất khi các giá trị regex của Python được serialize chính xác và các RegExps JavaScript được trả về trở thành các đối tượng re.Pattern thay vì các dictionary của protocol.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
python
Lĩnh vực
api
Loại issue
Lỗi
Độ khó
3/5
Thời gian dự kiến
1-2 ngày
Mức độ hoạt động
Sôi nổi
Độ rõ ràng
Đặc tả rõ ràng
Mức phù hợp với người mới
72/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.