PyPcre 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pypcre-0.1.0/LICENSE +201 -0
- pypcre-0.1.0/PKG-INFO +237 -0
- pypcre-0.1.0/PyPcre.egg-info/PKG-INFO +237 -0
- pypcre-0.1.0/PyPcre.egg-info/SOURCES.txt +37 -0
- pypcre-0.1.0/PyPcre.egg-info/dependency_links.txt +1 -0
- pypcre-0.1.0/PyPcre.egg-info/top_level.txt +2 -0
- pypcre-0.1.0/README.md +212 -0
- pypcre-0.1.0/pcre/__init__.py +150 -0
- pypcre-0.1.0/pcre/cache.py +105 -0
- pypcre-0.1.0/pcre/flags.py +61 -0
- pypcre-0.1.0/pcre/pcre.py +699 -0
- pypcre-0.1.0/pcre/re_compat.py +321 -0
- pypcre-0.1.0/pcre/threads.py +159 -0
- pypcre-0.1.0/pcre_ext/cache.c +555 -0
- pypcre-0.1.0/pcre_ext/error.c +1467 -0
- pypcre-0.1.0/pcre_ext/flag.c +37 -0
- pypcre-0.1.0/pcre_ext/memory.c +230 -0
- pypcre-0.1.0/pcre_ext/pcre2.c +2257 -0
- pypcre-0.1.0/pcre_ext/util.c +194 -0
- pypcre-0.1.0/pyproject.toml +44 -0
- pypcre-0.1.0/setup.cfg +4 -0
- pypcre-0.1.0/setup.py +27 -0
- pypcre-0.1.0/tests/test_accuracy.py +101 -0
- pypcre-0.1.0/tests/test_api_parity.py +238 -0
- pypcre-0.1.0/tests/test_basic.py +161 -0
- pypcre-0.1.0/tests/test_bench_string.py +156 -0
- pypcre-0.1.0/tests/test_benchmark.py +401 -0
- pypcre-0.1.0/tests/test_bytes.py +58 -0
- pypcre-0.1.0/tests/test_cache.py +89 -0
- pypcre-0.1.0/tests/test_core.py +658 -0
- pypcre-0.1.0/tests/test_errors.py +68 -0
- pypcre-0.1.0/tests/test_jit.py +147 -0
- pypcre-0.1.0/tests/test_module.py +75 -0
- pypcre-0.1.0/tests/test_pattern.py +76 -0
- pypcre-0.1.0/tests/test_simd.py +15 -0
- pypcre-0.1.0/tests/test_threaded_backend.py +216 -0
- pypcre-0.1.0/tests/test_transformers_regex.py +51 -0
- pypcre-0.1.0/tests/test_utf8.py +116 -0
- pypcre-0.1.0/tests/test_version.py +22 -0
pypcre-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
pypcre-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: PyPcre
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Modern, GIL-friendly, Fast Python bindings for PCRE2 with auto caching and JIT of compiled patterns.
|
|
5
|
+
Author-email: ModelCloud <qubitium@modelcloud.ai>
|
|
6
|
+
License-Expression: Apache-2.0
|
|
7
|
+
Project-URL: Homepage, https://github.com/ModelCloud/pcre
|
|
8
|
+
Keywords: regex,pcre2,bindings
|
|
9
|
+
Classifier: Programming Language :: Python :: 3
|
|
10
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
11
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
16
|
+
Classifier: Programming Language :: Python :: Free Threading
|
|
17
|
+
Classifier: Programming Language :: Python :: Free Threading :: 3 - Stable
|
|
18
|
+
Classifier: Programming Language :: C
|
|
19
|
+
Classifier: Topic :: Software Development :: Libraries
|
|
20
|
+
Classifier: Topic :: Text Processing :: Linguistic
|
|
21
|
+
Requires-Python: >=3.9
|
|
22
|
+
Description-Content-Type: text/markdown
|
|
23
|
+
License-File: LICENSE
|
|
24
|
+
Dynamic: license-file
|
|
25
|
+
|
|
26
|
+
<!--
|
|
27
|
+
# SPDX-FileCopyrightText: 2025 ModelCloud.ai
|
|
28
|
+
# SPDX-FileCopyrightText: 2025 qubitium@modelcloud.ai
|
|
29
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
30
|
+
# Contact: qubitium@modelcloud.ai, x.com/qubitium
|
|
31
|
+
-->
|
|
32
|
+
|
|
33
|
+
# PyPcre (Python Pcre2 Binding)
|
|
34
|
+
|
|
35
|
+
Python bindings for the system PCRE2 library with a familiar `re`-style API.
|
|
36
|
+
|
|
37
|
+
## Installation
|
|
38
|
+
|
|
39
|
+
```bash
|
|
40
|
+
pip install PyPcre
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
The package links against the `libpcre2-8` variant already available on your
|
|
44
|
+
system. See [Building](#building) for manual build details.
|
|
45
|
+
|
|
46
|
+
## Usage
|
|
47
|
+
|
|
48
|
+
### Drop-in helpers
|
|
49
|
+
|
|
50
|
+
```python
|
|
51
|
+
from pcre import match, search, findall, compile, Flag
|
|
52
|
+
|
|
53
|
+
if match(r"(?P<word>\\w+)", "hello world"):
|
|
54
|
+
print("found word")
|
|
55
|
+
|
|
56
|
+
pattern = compile(rb"\d+", flags=Flag.MULTILINE)
|
|
57
|
+
numbers = pattern.findall(b"line 1\nline 22")
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
`pcre` mirrors the core helpers from Python’s standard library `re` module—
|
|
61
|
+
`match`, `search`, `fullmatch`, `finditer`, `findall`, and `compile`—while
|
|
62
|
+
exposing PCRE2’s extended flag set through the Pythonic `Flag` enum
|
|
63
|
+
(`Flag.CASELESS`, `Flag.MULTILINE`, `Flag.UTF`, ...).
|
|
64
|
+
|
|
65
|
+
### Stdlib `re` compatibility
|
|
66
|
+
|
|
67
|
+
- Module-level helpers and the `Pattern` class follow the same call shapes as
|
|
68
|
+
the standard library `re` module, including `pos`, `endpos`, and `flags`
|
|
69
|
+
behaviour.
|
|
70
|
+
- `Pattern` mirrors `re.Pattern` attributes like `.pattern`, `.groupindex`,
|
|
71
|
+
and `.groups`, while `Match` objects surface the familiar `.re`, `.string`,
|
|
72
|
+
`.pos`, `.endpos`, `.lastindex`, `.lastgroup`, `.regs`, and `.expand()` API.
|
|
73
|
+
- Substitution helpers enforce the same type rules as the standard library
|
|
74
|
+
`re` module: string patterns require string replacements, byte patterns
|
|
75
|
+
require bytes-like replacements, and callable replacements receive the
|
|
76
|
+
wrapped `Match`.
|
|
77
|
+
- `compile()` accepts native `Flag` values as well as compatible
|
|
78
|
+
`re.RegexFlag` members from the standard library. Supported stdlib flags
|
|
79
|
+
map 1:1 to PCRE2 options (`IGNORECASE→CASELESS`, `MULTILINE→MULTILINE`,
|
|
80
|
+
`DOTALL→DOTALL`, `VERBOSE→EXTENDED`); passing unsupported stdlib flags
|
|
81
|
+
raises a compatibility `ValueError` to prevent silent divergences.
|
|
82
|
+
- `pcre.escape()` delegates directly to `re.escape` for byte and text
|
|
83
|
+
patterns so escaping semantics remain identical.
|
|
84
|
+
|
|
85
|
+
### `regex` package compatibility
|
|
86
|
+
|
|
87
|
+
The [`regex`](https://pypi.org/project/regex/) package interprets
|
|
88
|
+
`\uXXXX` and `\UXXXXXXXX` escapes as UTF-8 code points, while PCRE2 expects
|
|
89
|
+
hexadecimal escapes to use the `\x{...}` form. Enable `Flag.COMPAT_UNICODE_ESCAPE` to
|
|
90
|
+
translate those escapes automatically when compiling patterns:
|
|
91
|
+
|
|
92
|
+
```python
|
|
93
|
+
from pcre import compile, Flag
|
|
94
|
+
|
|
95
|
+
pattern = compile(r"\\U0001F600", flags=Flag.COMPAT_UNICODE_ESCAPE)
|
|
96
|
+
assert pattern.pattern == r"\\x{0001F600}"
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
Set the default behaviour globally with `pcre.configure(compat_regex=True)`
|
|
100
|
+
so that subsequent calls to `compile()` and the module-level helpers apply
|
|
101
|
+
the conversion without repeating the flag.
|
|
102
|
+
|
|
103
|
+
### Automatic pattern caching
|
|
104
|
+
|
|
105
|
+
`pcre.compile()` caches the final `Pattern` wrapper for up to 128
|
|
106
|
+
unique `(pattern, flags)` pairs when the pattern object is hashable. This
|
|
107
|
+
keeps repeated calls to top-level helpers efficient without any extra work
|
|
108
|
+
from the caller. Adjust the capacity with `pcre.set_cache_limit(n)`—pass
|
|
109
|
+
`0` to disable caching completely or `None` for an unlimited cache—and
|
|
110
|
+
check the current limit with `pcre.get_cache_limit()`. The cache can be
|
|
111
|
+
emptied at any time with `pcre.clear_cache()` if your application needs to
|
|
112
|
+
release memory proactively.
|
|
113
|
+
|
|
114
|
+
Non-hashable patterns (for example, custom objects) bypass the cache and are
|
|
115
|
+
still compiled immediately.
|
|
116
|
+
|
|
117
|
+
### Text versus bytes defaults
|
|
118
|
+
|
|
119
|
+
String patterns follow the same defaults as Python’s `re` module,
|
|
120
|
+
automatically enabling the `Flag.UTF` and `Flag.UCP` options so Unicode
|
|
121
|
+
pattern and character semantics “just work.” Byte patterns remain raw by
|
|
122
|
+
default—neither option is activated—so you retain full control over
|
|
123
|
+
binary-oriented matching. Explicitly set `Flag.NO_UTF`/`Flag.NO_UCP` if you
|
|
124
|
+
need to opt out for strings, or add the UTF/UCP flags yourself when compiling
|
|
125
|
+
bytes.
|
|
126
|
+
|
|
127
|
+
### Working with compiled patterns
|
|
128
|
+
|
|
129
|
+
- `compile()` accepts either a pattern literal or an existing `Pattern`
|
|
130
|
+
instance, making it easy to mix compiled objects with the convenience
|
|
131
|
+
helpers.
|
|
132
|
+
- `Pattern.match/search/fullmatch/finditer/findall` accept optional
|
|
133
|
+
`pos`, `endpos`, and `options` arguments, mirroring the standard library
|
|
134
|
+
`re` module while letting you thread PCRE2 execution flags through
|
|
135
|
+
individual calls.
|
|
136
|
+
|
|
137
|
+
### Threaded execution
|
|
138
|
+
|
|
139
|
+
- `pcre.parallel_map()` fans out work across a shared thread pool for
|
|
140
|
+
`match`, `search`, `fullmatch`, and `findall`. The helper preserves the
|
|
141
|
+
order of the provided subjects and returns the same result objects you’d
|
|
142
|
+
normally receive from the `Pattern` methods.
|
|
143
|
+
- Threading is **opt-in by default** when Python runs without the GIL
|
|
144
|
+
(e.g. CPython with `-X gil=0` or `PYTHON_GIL=0`). When the GIL is active the default falls
|
|
145
|
+
back to sequential execution to avoid needless overhead.
|
|
146
|
+
- With auto threading enabled (`configure_threads(enabled=True)`), the pool
|
|
147
|
+
is only engaged when at least one subject is larger than the configured
|
|
148
|
+
threshold (60 kB by default). Smaller jobs run sequentially to avoid the
|
|
149
|
+
cost of thread hand-offs; adjust the boundary via
|
|
150
|
+
`configure_threads(threshold=...)`.
|
|
151
|
+
- Use `Flag.THREADS` to force threaded execution for a specific pattern or
|
|
152
|
+
`Flag.NO_THREADS` to lock it to sequential mode regardless of global
|
|
153
|
+
settings.
|
|
154
|
+
- `pcre.configure_thread_pool(max_workers=...)` controls the size of the
|
|
155
|
+
shared executor (capped to half the available CPUs); call it with
|
|
156
|
+
`preload=True` to spin the pool up eagerly, and `shutdown_thread_pool()`
|
|
157
|
+
to tear it down manually if needed.
|
|
158
|
+
|
|
159
|
+
### JIT control
|
|
160
|
+
|
|
161
|
+
Pcre’s JIT compiler is enabled by default for every compiled pattern. The
|
|
162
|
+
wrapper exposes two complementary ways to adjust that behaviour:
|
|
163
|
+
|
|
164
|
+
- Toggle the global default at runtime with `pcre.configure(jit=False)` to
|
|
165
|
+
turn JIT off (call `pcre.configure(jit=True)` to turn it back on).
|
|
166
|
+
- Override the default per pattern using the Python-only flags `Flag.JIT`
|
|
167
|
+
and `Flag.NO_JIT`:
|
|
168
|
+
|
|
169
|
+
```python
|
|
170
|
+
from pcre import compile, configure, Flag
|
|
171
|
+
|
|
172
|
+
configure(jit=False) # disable JIT globally
|
|
173
|
+
baseline = compile(r"expr") # JIT disabled
|
|
174
|
+
|
|
175
|
+
fast = compile(r"expr", flags=Flag.JIT) # force-enable for this pattern
|
|
176
|
+
slow = compile(r"expr", flags=Flag.NO_JIT) # force-disable for this pattern
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
## Building
|
|
180
|
+
|
|
181
|
+
The extension links against an existing PCRE2 installation (the `libpcre2-8`
|
|
182
|
+
variant). Install the development headers for your platform before building,
|
|
183
|
+
for example `apt install libpcre2-dev` on Debian/Ubuntu, `dnf install pcre2-devel`
|
|
184
|
+
on Fedora/RHEL derivatives, or `brew install pcre2` on macOS.
|
|
185
|
+
|
|
186
|
+
If the headers or library live in a non-standard location you can export one
|
|
187
|
+
or more of the following environment variables prior to invoking the build
|
|
188
|
+
(`pip install .`, `python -m build`, etc.):
|
|
189
|
+
|
|
190
|
+
- `PCRE2_ROOT`
|
|
191
|
+
- `PCRE2_INCLUDE_DIR`
|
|
192
|
+
- `PCRE2_LIBRARY_DIR`
|
|
193
|
+
- `PCRE2_LIBRARY_PATH` *(pathsep-separated directories or explicit library files to
|
|
194
|
+
prioritise when resolving `libpcre2-8`)*
|
|
195
|
+
- `PCRE2_LIBRARIES`
|
|
196
|
+
- `PCRE2_CFLAGS`
|
|
197
|
+
- `PCRE2_LDFLAGS`
|
|
198
|
+
|
|
199
|
+
When `pkg-config` is available the build will automatically pick up the
|
|
200
|
+
required include and link flags via `pkg-config --cflags/--libs libpcre2-8`.
|
|
201
|
+
Without `pkg-config`, the build script scans common installation prefixes for
|
|
202
|
+
Linux distributions (Debian, Ubuntu, Fedora/RHEL/CentOS, openSUSE, Alpine),
|
|
203
|
+
FreeBSD, macOS (including Homebrew), and Solaris to locate the headers and
|
|
204
|
+
libraries.
|
|
205
|
+
|
|
206
|
+
If your system ships `libpcre2-8` under `/usr` but you also maintain a
|
|
207
|
+
manually built copy under `/usr/local`, export `PCRE2_LIBRARY_PATH` (and, if
|
|
208
|
+
needed, a matching `PCRE2_INCLUDE_DIR`) so the build links against the desired
|
|
209
|
+
location.
|
|
210
|
+
|
|
211
|
+
# Notes
|
|
212
|
+
|
|
213
|
+
## Pattern cache
|
|
214
|
+
- `pcre.compile()` caches hashable `(pattern, flags)` pairs, keeping up to 128 entries.
|
|
215
|
+
- Use `pcre.clear_cache()` when you need to free the cache proactively.
|
|
216
|
+
- Non-hashable pattern objects skip the cache and are compiled each time.
|
|
217
|
+
|
|
218
|
+
## Default flags for text patterns
|
|
219
|
+
- String patterns enable `Flag.UTF` and `Flag.UCP` automatically so behaviour matches `re`.
|
|
220
|
+
- Byte patterns keep both flags disabled; opt in manually if Unicode semantics are desired.
|
|
221
|
+
- Explicitly supply `Flag.NO_UTF`/`Flag.NO_UCP` to override the defaults for strings.
|
|
222
|
+
|
|
223
|
+
## Additional usage notes
|
|
224
|
+
- All top-level helpers (`match`, `search`, `fullmatch`, `finditer`, `findall`) defer to the cached compiler.
|
|
225
|
+
- Compiled `Pattern` objects expose `.pattern`, `.flags`, `.jit`, and `.groupindex` for introspection.
|
|
226
|
+
- Execution helpers accept `pos`, `endpos`, and `options`, allowing you to thread PCRE2 execution flags per call.
|
|
227
|
+
|
|
228
|
+
## Memory allocation
|
|
229
|
+
- The extension selects the fastest available allocator at import time: it
|
|
230
|
+
prefers jemalloc, then tcmalloc, and finally falls back to the platform
|
|
231
|
+
`malloc`. Optional allocators are loaded via `dlopen`, so no additional
|
|
232
|
+
link flags are required when they are absent.
|
|
233
|
+
- All internal buffers (match data wrappers, JIT stack cache entries, error
|
|
234
|
+
formatting scratch space) use the chosen allocator; CPython’s `PyMem_*`
|
|
235
|
+
family is no longer used within the extension.
|
|
236
|
+
- Call `pcre_ext_c.get_allocator()` to inspect which backend is active at
|
|
237
|
+
runtime.
|
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: PyPcre
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Modern, GIL-friendly, Fast Python bindings for PCRE2 with auto caching and JIT of compiled patterns.
|
|
5
|
+
Author-email: ModelCloud <qubitium@modelcloud.ai>
|
|
6
|
+
License-Expression: Apache-2.0
|
|
7
|
+
Project-URL: Homepage, https://github.com/ModelCloud/pcre
|
|
8
|
+
Keywords: regex,pcre2,bindings
|
|
9
|
+
Classifier: Programming Language :: Python :: 3
|
|
10
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
11
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
16
|
+
Classifier: Programming Language :: Python :: Free Threading
|
|
17
|
+
Classifier: Programming Language :: Python :: Free Threading :: 3 - Stable
|
|
18
|
+
Classifier: Programming Language :: C
|
|
19
|
+
Classifier: Topic :: Software Development :: Libraries
|
|
20
|
+
Classifier: Topic :: Text Processing :: Linguistic
|
|
21
|
+
Requires-Python: >=3.9
|
|
22
|
+
Description-Content-Type: text/markdown
|
|
23
|
+
License-File: LICENSE
|
|
24
|
+
Dynamic: license-file
|
|
25
|
+
|
|
26
|
+
<!--
|
|
27
|
+
# SPDX-FileCopyrightText: 2025 ModelCloud.ai
|
|
28
|
+
# SPDX-FileCopyrightText: 2025 qubitium@modelcloud.ai
|
|
29
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
30
|
+
# Contact: qubitium@modelcloud.ai, x.com/qubitium
|
|
31
|
+
-->
|
|
32
|
+
|
|
33
|
+
# PyPcre (Python Pcre2 Binding)
|
|
34
|
+
|
|
35
|
+
Python bindings for the system PCRE2 library with a familiar `re`-style API.
|
|
36
|
+
|
|
37
|
+
## Installation
|
|
38
|
+
|
|
39
|
+
```bash
|
|
40
|
+
pip install PyPcre
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
The package links against the `libpcre2-8` variant already available on your
|
|
44
|
+
system. See [Building](#building) for manual build details.
|
|
45
|
+
|
|
46
|
+
## Usage
|
|
47
|
+
|
|
48
|
+
### Drop-in helpers
|
|
49
|
+
|
|
50
|
+
```python
|
|
51
|
+
from pcre import match, search, findall, compile, Flag
|
|
52
|
+
|
|
53
|
+
if match(r"(?P<word>\\w+)", "hello world"):
|
|
54
|
+
print("found word")
|
|
55
|
+
|
|
56
|
+
pattern = compile(rb"\d+", flags=Flag.MULTILINE)
|
|
57
|
+
numbers = pattern.findall(b"line 1\nline 22")
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
`pcre` mirrors the core helpers from Python’s standard library `re` module—
|
|
61
|
+
`match`, `search`, `fullmatch`, `finditer`, `findall`, and `compile`—while
|
|
62
|
+
exposing PCRE2’s extended flag set through the Pythonic `Flag` enum
|
|
63
|
+
(`Flag.CASELESS`, `Flag.MULTILINE`, `Flag.UTF`, ...).
|
|
64
|
+
|
|
65
|
+
### Stdlib `re` compatibility
|
|
66
|
+
|
|
67
|
+
- Module-level helpers and the `Pattern` class follow the same call shapes as
|
|
68
|
+
the standard library `re` module, including `pos`, `endpos`, and `flags`
|
|
69
|
+
behaviour.
|
|
70
|
+
- `Pattern` mirrors `re.Pattern` attributes like `.pattern`, `.groupindex`,
|
|
71
|
+
and `.groups`, while `Match` objects surface the familiar `.re`, `.string`,
|
|
72
|
+
`.pos`, `.endpos`, `.lastindex`, `.lastgroup`, `.regs`, and `.expand()` API.
|
|
73
|
+
- Substitution helpers enforce the same type rules as the standard library
|
|
74
|
+
`re` module: string patterns require string replacements, byte patterns
|
|
75
|
+
require bytes-like replacements, and callable replacements receive the
|
|
76
|
+
wrapped `Match`.
|
|
77
|
+
- `compile()` accepts native `Flag` values as well as compatible
|
|
78
|
+
`re.RegexFlag` members from the standard library. Supported stdlib flags
|
|
79
|
+
map 1:1 to PCRE2 options (`IGNORECASE→CASELESS`, `MULTILINE→MULTILINE`,
|
|
80
|
+
`DOTALL→DOTALL`, `VERBOSE→EXTENDED`); passing unsupported stdlib flags
|
|
81
|
+
raises a compatibility `ValueError` to prevent silent divergences.
|
|
82
|
+
- `pcre.escape()` delegates directly to `re.escape` for byte and text
|
|
83
|
+
patterns so escaping semantics remain identical.
|
|
84
|
+
|
|
85
|
+
### `regex` package compatibility
|
|
86
|
+
|
|
87
|
+
The [`regex`](https://pypi.org/project/regex/) package interprets
|
|
88
|
+
`\uXXXX` and `\UXXXXXXXX` escapes as UTF-8 code points, while PCRE2 expects
|
|
89
|
+
hexadecimal escapes to use the `\x{...}` form. Enable `Flag.COMPAT_UNICODE_ESCAPE` to
|
|
90
|
+
translate those escapes automatically when compiling patterns:
|
|
91
|
+
|
|
92
|
+
```python
|
|
93
|
+
from pcre import compile, Flag
|
|
94
|
+
|
|
95
|
+
pattern = compile(r"\\U0001F600", flags=Flag.COMPAT_UNICODE_ESCAPE)
|
|
96
|
+
assert pattern.pattern == r"\\x{0001F600}"
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
Set the default behaviour globally with `pcre.configure(compat_regex=True)`
|
|
100
|
+
so that subsequent calls to `compile()` and the module-level helpers apply
|
|
101
|
+
the conversion without repeating the flag.
|
|
102
|
+
|
|
103
|
+
### Automatic pattern caching
|
|
104
|
+
|
|
105
|
+
`pcre.compile()` caches the final `Pattern` wrapper for up to 128
|
|
106
|
+
unique `(pattern, flags)` pairs when the pattern object is hashable. This
|
|
107
|
+
keeps repeated calls to top-level helpers efficient without any extra work
|
|
108
|
+
from the caller. Adjust the capacity with `pcre.set_cache_limit(n)`—pass
|
|
109
|
+
`0` to disable caching completely or `None` for an unlimited cache—and
|
|
110
|
+
check the current limit with `pcre.get_cache_limit()`. The cache can be
|
|
111
|
+
emptied at any time with `pcre.clear_cache()` if your application needs to
|
|
112
|
+
release memory proactively.
|
|
113
|
+
|
|
114
|
+
Non-hashable patterns (for example, custom objects) bypass the cache and are
|
|
115
|
+
still compiled immediately.
|
|
116
|
+
|
|
117
|
+
### Text versus bytes defaults
|
|
118
|
+
|
|
119
|
+
String patterns follow the same defaults as Python’s `re` module,
|
|
120
|
+
automatically enabling the `Flag.UTF` and `Flag.UCP` options so Unicode
|
|
121
|
+
pattern and character semantics “just work.” Byte patterns remain raw by
|
|
122
|
+
default—neither option is activated—so you retain full control over
|
|
123
|
+
binary-oriented matching. Explicitly set `Flag.NO_UTF`/`Flag.NO_UCP` if you
|
|
124
|
+
need to opt out for strings, or add the UTF/UCP flags yourself when compiling
|
|
125
|
+
bytes.
|
|
126
|
+
|
|
127
|
+
### Working with compiled patterns
|
|
128
|
+
|
|
129
|
+
- `compile()` accepts either a pattern literal or an existing `Pattern`
|
|
130
|
+
instance, making it easy to mix compiled objects with the convenience
|
|
131
|
+
helpers.
|
|
132
|
+
- `Pattern.match/search/fullmatch/finditer/findall` accept optional
|
|
133
|
+
`pos`, `endpos`, and `options` arguments, mirroring the standard library
|
|
134
|
+
`re` module while letting you thread PCRE2 execution flags through
|
|
135
|
+
individual calls.
|
|
136
|
+
|
|
137
|
+
### Threaded execution
|
|
138
|
+
|
|
139
|
+
- `pcre.parallel_map()` fans out work across a shared thread pool for
|
|
140
|
+
`match`, `search`, `fullmatch`, and `findall`. The helper preserves the
|
|
141
|
+
order of the provided subjects and returns the same result objects you’d
|
|
142
|
+
normally receive from the `Pattern` methods.
|
|
143
|
+
- Threading is **opt-in by default** when Python runs without the GIL
|
|
144
|
+
(e.g. CPython with `-X gil=0` or `PYTHON_GIL=0`). When the GIL is active the default falls
|
|
145
|
+
back to sequential execution to avoid needless overhead.
|
|
146
|
+
- With auto threading enabled (`configure_threads(enabled=True)`), the pool
|
|
147
|
+
is only engaged when at least one subject is larger than the configured
|
|
148
|
+
threshold (60 kB by default). Smaller jobs run sequentially to avoid the
|
|
149
|
+
cost of thread hand-offs; adjust the boundary via
|
|
150
|
+
`configure_threads(threshold=...)`.
|
|
151
|
+
- Use `Flag.THREADS` to force threaded execution for a specific pattern or
|
|
152
|
+
`Flag.NO_THREADS` to lock it to sequential mode regardless of global
|
|
153
|
+
settings.
|
|
154
|
+
- `pcre.configure_thread_pool(max_workers=...)` controls the size of the
|
|
155
|
+
shared executor (capped to half the available CPUs); call it with
|
|
156
|
+
`preload=True` to spin the pool up eagerly, and `shutdown_thread_pool()`
|
|
157
|
+
to tear it down manually if needed.
|
|
158
|
+
|
|
159
|
+
### JIT control
|
|
160
|
+
|
|
161
|
+
Pcre’s JIT compiler is enabled by default for every compiled pattern. The
|
|
162
|
+
wrapper exposes two complementary ways to adjust that behaviour:
|
|
163
|
+
|
|
164
|
+
- Toggle the global default at runtime with `pcre.configure(jit=False)` to
|
|
165
|
+
turn JIT off (call `pcre.configure(jit=True)` to turn it back on).
|
|
166
|
+
- Override the default per pattern using the Python-only flags `Flag.JIT`
|
|
167
|
+
and `Flag.NO_JIT`:
|
|
168
|
+
|
|
169
|
+
```python
|
|
170
|
+
from pcre import compile, configure, Flag
|
|
171
|
+
|
|
172
|
+
configure(jit=False) # disable JIT globally
|
|
173
|
+
baseline = compile(r"expr") # JIT disabled
|
|
174
|
+
|
|
175
|
+
fast = compile(r"expr", flags=Flag.JIT) # force-enable for this pattern
|
|
176
|
+
slow = compile(r"expr", flags=Flag.NO_JIT) # force-disable for this pattern
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
## Building
|
|
180
|
+
|
|
181
|
+
The extension links against an existing PCRE2 installation (the `libpcre2-8`
|
|
182
|
+
variant). Install the development headers for your platform before building,
|
|
183
|
+
for example `apt install libpcre2-dev` on Debian/Ubuntu, `dnf install pcre2-devel`
|
|
184
|
+
on Fedora/RHEL derivatives, or `brew install pcre2` on macOS.
|
|
185
|
+
|
|
186
|
+
If the headers or library live in a non-standard location you can export one
|
|
187
|
+
or more of the following environment variables prior to invoking the build
|
|
188
|
+
(`pip install .`, `python -m build`, etc.):
|
|
189
|
+
|
|
190
|
+
- `PCRE2_ROOT`
|
|
191
|
+
- `PCRE2_INCLUDE_DIR`
|
|
192
|
+
- `PCRE2_LIBRARY_DIR`
|
|
193
|
+
- `PCRE2_LIBRARY_PATH` *(pathsep-separated directories or explicit library files to
|
|
194
|
+
prioritise when resolving `libpcre2-8`)*
|
|
195
|
+
- `PCRE2_LIBRARIES`
|
|
196
|
+
- `PCRE2_CFLAGS`
|
|
197
|
+
- `PCRE2_LDFLAGS`
|
|
198
|
+
|
|
199
|
+
When `pkg-config` is available the build will automatically pick up the
|
|
200
|
+
required include and link flags via `pkg-config --cflags/--libs libpcre2-8`.
|
|
201
|
+
Without `pkg-config`, the build script scans common installation prefixes for
|
|
202
|
+
Linux distributions (Debian, Ubuntu, Fedora/RHEL/CentOS, openSUSE, Alpine),
|
|
203
|
+
FreeBSD, macOS (including Homebrew), and Solaris to locate the headers and
|
|
204
|
+
libraries.
|
|
205
|
+
|
|
206
|
+
If your system ships `libpcre2-8` under `/usr` but you also maintain a
|
|
207
|
+
manually built copy under `/usr/local`, export `PCRE2_LIBRARY_PATH` (and, if
|
|
208
|
+
needed, a matching `PCRE2_INCLUDE_DIR`) so the build links against the desired
|
|
209
|
+
location.
|
|
210
|
+
|
|
211
|
+
# Notes
|
|
212
|
+
|
|
213
|
+
## Pattern cache
|
|
214
|
+
- `pcre.compile()` caches hashable `(pattern, flags)` pairs, keeping up to 128 entries.
|
|
215
|
+
- Use `pcre.clear_cache()` when you need to free the cache proactively.
|
|
216
|
+
- Non-hashable pattern objects skip the cache and are compiled each time.
|
|
217
|
+
|
|
218
|
+
## Default flags for text patterns
|
|
219
|
+
- String patterns enable `Flag.UTF` and `Flag.UCP` automatically so behaviour matches `re`.
|
|
220
|
+
- Byte patterns keep both flags disabled; opt in manually if Unicode semantics are desired.
|
|
221
|
+
- Explicitly supply `Flag.NO_UTF`/`Flag.NO_UCP` to override the defaults for strings.
|
|
222
|
+
|
|
223
|
+
## Additional usage notes
|
|
224
|
+
- All top-level helpers (`match`, `search`, `fullmatch`, `finditer`, `findall`) defer to the cached compiler.
|
|
225
|
+
- Compiled `Pattern` objects expose `.pattern`, `.flags`, `.jit`, and `.groupindex` for introspection.
|
|
226
|
+
- Execution helpers accept `pos`, `endpos`, and `options`, allowing you to thread PCRE2 execution flags per call.
|
|
227
|
+
|
|
228
|
+
## Memory allocation
|
|
229
|
+
- The extension selects the fastest available allocator at import time: it
|
|
230
|
+
prefers jemalloc, then tcmalloc, and finally falls back to the platform
|
|
231
|
+
`malloc`. Optional allocators are loaded via `dlopen`, so no additional
|
|
232
|
+
link flags are required when they are absent.
|
|
233
|
+
- All internal buffers (match data wrappers, JIT stack cache entries, error
|
|
234
|
+
formatting scratch space) use the chosen allocator; CPython’s `PyMem_*`
|
|
235
|
+
family is no longer used within the extension.
|
|
236
|
+
- Call `pcre_ext_c.get_allocator()` to inspect which backend is active at
|
|
237
|
+
runtime.
|