Skip to content

Conversation

@provinzkraut
Copy link
Contributor

Implement #860.

from . import inspect
from . import structs
from ._version import __version__
from ._utils import RegexPatternProtocol
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to publish this?

static bool
ensure_is_re_pattern_compatible (PyObject *val, const char *param) {
if (PyUnicode_CheckExact(val)) return true;
if (PyObject_HasAttrString(val, "pattern") && PyObject_HasAttrString(val, "search")) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This check is currently very rudimentary. How strict do we want to be with this? Another option would be to make the RegexPatternProtocol runtime checkable and then do an isinstance check here. That would certainly be more robust, but is it desirable?

return cls(**kwargs)


class RegexPatternProtocol(typing.Protocol):
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've kept the required API as minimal as possible here.

string: str,
pos: int = 0,
endpos: int = sys.maxsize,
) -> object | None: ...
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Returning object | None reflects how it's being used internally. This allows greater flexibility, since regex engines mustn't also comply with the re.Match protocol, and we're not doing anything with the match anyway

@provinzkraut
Copy link
Contributor Author

provinzkraut commented Nov 11, 2025

Should not be merged before #899.

@provinzkraut provinzkraut force-pushed the add-3rd-party-regex-support branch from 2dcea3f to b979819 Compare November 25, 2025 12:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

1 participant