This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author vstinner
Recipients abacabadabacaba, ezio.melotti, mrabarnett, vstinner
Date 2015-03-17.21:32:36
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1426627957.11.0.716091439999.issue23690@psf.upfronthosting.co.za>
In-reply-to
Content
> Aren't Python strings immutable?

Yes. But the re module supports more types than just str and bytes. For example, bytearray is also accepted:

>>> re.match(b'^abc', b'abc')
<_sre.SRE_Match object; span=(0, 3), match=b'abc'>
>>> re.match(b'^abc', bytearray(b'abc'))
<_sre.SRE_Match object; span=(0, 3), match=b'abc'>

> Also, match functions still permit execution of signal handlers, which can execute any Python code.

Correct, signal handlers are called. If you mutate the string currently used in the pattern matching, you can probably crash Python. I hope that nobody does such ugly things in Python signal handlers :-)

> If GIL is needed during matching, can it be released temporarily to permit thread switching?

It's possible to modify the _sre module to release the GIL in some cases. It's possible to release the GIL for immutables string, and keep the GIL for mutable strings. To do this, you have to audit the source code. First, ensure that no global variable is used. For example, the "state" must not be shared (it's ok, it's allocated on the stack, thread stacks are not shared).

If you start to release the GIL, you have to search for all functions which must be called with the GIL hold. For example, memory allocators, but also all functions manipulating Python objects. Hint: seach "PyObject*". For example, getslice() must be called with the GIL hold.

Since the GIL is a lock, you should benchmark to ensure that sequences of acquire/release the GIL doesn't kill performances with a single thread, and with multiple threads. Anyway, a benchmark will be needed.

To be clear: I'm *not* interested to optimize the _sre module to release the GIL (to support parallel executions).
History
Date User Action Args
2015-03-17 21:32:37vstinnersetrecipients: + vstinner, ezio.melotti, mrabarnett, abacabadabacaba
2015-03-17 21:32:37vstinnersetmessageid: <1426627957.11.0.716091439999.issue23690@psf.upfronthosting.co.za>
2015-03-17 21:32:37vstinnerlinkissue23690 messages
2015-03-17 21:32:36vstinnercreate