Time out a regular expression in Python 2.6.4?
Jonathan Gardner
jgardner at jonathangardner.net
Mon Feb 15 18:55:05 EST 2010
On Feb 15, 7:59 am, Steve Holden <st... at holdenweb.com> wrote:
> pyt... at bdurham.com wrote:
> > Is there any way to time out a regular expression in Python 2.6.4?
>
> > Motiviation: Our application allows users to enter regular expressions
> > as validation criteria. If a user enters a pathological regular
> > expression, we would like to timeout the evaluation of this expression
> > after a short period of time.
>
> Python itself does not contain any mechanism to terminate an operation
> if it takes too much time.
>
> One approach would be to run the regex in a subprocess, and apply
> process limits to terminate that subprocess if it ran too long.
>
> This group being what it is you are likely to receive other, better
> suggestions too.
>
I'm not sure how exactly the re module is implemented, but since I
assume a great chunk is in C code, you may get away with a single
process and multiple threads. One thread will watch the process, or
have a timer event set to go off at a certain point. The other will
actually run the regex and get killed by the timer process if it
doesn't finish in time.
More information about the Python-list
mailing list