Does a '_sre.SRE_Pattern' have state,or is it thread-safe?

J

John Nagle

'_sre.SRE_Pattern' is what "re.compile" returns.

Is that a mutable object, with state that changes
during the parse, or is it an immutable constant? Can
two threads use the same '_sre.SRE_Pattern' at the same time?

(I'm writing something to find race conditions in existing code,
which is why all these obscure introspection-related questions.)

John Nagle
 
S

Stephen Hansen

'_sre.SRE_Pattern' is what "re.compile" returns.

Is that a mutable object, with state that changes
during the parse, or is it an immutable constant? Can
two threads use the same '_sre.SRE_Pattern' at the same time?

Ouch. I hope it is thread safe or I have some problems. But: since
compiled regular expressions are automatically cached and used when
one throws non compiled expressions into re, ISTM they have to be
thread safe. Or we'd have tons of problems.

--Stephen via iPad.
 
M

MRAB

John said:
'_sre.SRE_Pattern' is what "re.compile" returns.

Is that a mutable object, with state that changes
during the parse, or is it an immutable constant? Can
two threads use the same '_sre.SRE_Pattern' at the same time?

(I'm writing something to find race conditions in existing code,
which is why all these obscure introspection-related questions.)
A compiled pattern instance is immutable and threadsafe.

BTW, its methods don't release the GIL, so even if it wasn't threadsafe,
it would still be safe. :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top