Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

regex - Does re.compile() or any given Python library call throw an exception?

I can't tell from the Python documentation whether the re.compile(x) function may throw an exception (assuming you pass in a string). I imagine there is something that could be considered an invalid regular expression. The larger question is, where do I go to find if a given Python library call may throw exception(s) and what those are?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Well, re.compile certainly may:

>>> import re
>>> re.compile('he(lo')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:Python25lib
e.py", line 180, in compile
    return _compile(pattern, flags)
  File "C:Python25lib
e.py", line 233, in _compile
    raise error, v # invalid expression
sre_constants.error: unbalanced parenthesis

The documentation does support this, in a roundabout way - check the bottom of the "Module Contents" page for (brief) description of the error exception.

Unfortunately, I don't have any answer to the general question. I suppose the documentation for the various modules varies in quality and thoroughness. If there were particular modules you were interested in, you might be able to decompile them (if written in Python) or even look at the source, if they're in the standard library.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...