<p dir="ltr">I think it would be nice for manipulating (e.g. optimizing, possibly with JIT-like analysis) and comparing regexes. It can also be useful as a teaching tool, e.g. exercises in optimizing and comparing regexes.</p>
<p dir="ltr">I think the discussion should be on python-ideas, though.</p>
<div class="gmail_quote">On Feb 14, 2016 2:01 PM, "Jonathan Goble" <<a href="mailto:jcgoble3@gmail.com">jcgoble3@gmail.com</a>> wrote:<br type="attribution"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">I'm new to Python's mailing lists, so please forgive me if I'm sending<br>
this to the wrong list. :)<br>
<br>
I filed <a href="http://bugs.python.org/issue26336" rel="noreferrer" target="_blank">http://bugs.python.org/issue26336</a> a few days ago, but now I<br>
think this list might be a better place to get discussion going.<br>
Basically, I'd like to see the bytecode of a compiled regex object<br>
exposed as a public (probably read-only) attribute of the object.<br>
<br>
Currently, although compiled in pure Python through modules<br>
sre_compile and sre_parse, the list of opcodes is then passed into C<br>
and copied into an array in a C struct, without being publicly exposed<br>
in any way. The only way for a user to get an internal representation<br>
of the regex is the re.DEBUG flag, which only produces an intermediate<br>
representation rather than the actual bytecode and only goes to<br>
stdout, which makes it useless for someone who wants to examine it<br>
programmatically.<br>
<br>
I'm sure others can think of other potential use cases for this, but<br>
one in particular would be that someone could write a debugger that<br>
can allow a user to step through a regex one opcode at a time to see<br>
exactly where it is failing. It would also perhaps be nice to have a<br>
public constructor for the regex object type, which would enable users<br>
to modify the bytecode and directly create a new regex object from it,<br>
similar to what is currently possible through the types.FunctionType<br>
and types.CodeType constructors.<br>
<br>
In addition to exposing the code in a public attribute, a helper<br>
module written in Python similar to the dis module (which is for<br>
Python's own bytecode) would be very helpful, allowing the code to be<br>
easily disassembled and examined at a higher level.<br>
<br>
Is this a good idea, or am I barking up the wrong tree? I think it's a<br>
great idea, but I'm open to being told this is a horrible idea. :) I<br>
welcome any and all comments both here and on the bug tracker.<br>
<br>
Jonathan Goble<br>
_______________________________________________<br>
Python-Dev mailing list<br>
<a href="mailto:Python-Dev@python.org">Python-Dev@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/python-dev" rel="noreferrer" target="_blank">https://mail.python.org/mailman/listinfo/python-dev</a><br>
Unsubscribe: <a href="https://mail.python.org/mailman/options/python-dev/leewangzhong%2Bpython%40gmail.com" rel="noreferrer" target="_blank">https://mail.python.org/mailman/options/python-dev/leewangzhong%2Bpython%40gmail.com</a><br>
</blockquote></div>