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.

classification
Title: using some_re.sub() often imports sre.__doc__
Type: Stage:
Components: Regular Expressions Versions: Python 2.4
process
Status: closed Resolution: wont fix
Dependencies: Superseder:
Assigned To: niemeyer Nosy List: georg.brandl, niemeyer, pterk, stevea_zope
Priority: normal Keywords:

Created on 2005-07-08 21:46 by stevea_zope, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (4)
msg60774 - (view) Author: Steve Alexander (stevea_zope) Date: 2005-07-08 21:46
Why is __doc__ imported from the builtin sre module
when a regular expression is substituted replacing a group?


$ python
Python 2.4.1 (#2, Mar 30 2005, 21:51:10)
[GCC 3.3.5 (Debian 1:3.3.5-8ubuntu2)] on linux2
Type "help", "copyright", "credits" or "license" for
more information.

>>> def import_hook(name, globals={}, locals={},
fromlist=[]):
...     print name, fromlist
...     return original_import(name, globals, locals,
fromlist)
...
>>> import __builtin__
>>> original_import = __builtin__.__import__
>>> __builtin__.__import__ = import_hook

>>> import re
re None
sre ('*',)
sys None
sre_compile None
_sre None
sys None
sre_constants ('*',)
sre_parse None
sys None
sre_constants ('*',)
sre_parse None
copy_reg None
sre ('__all__',)

>>> re1 = re.compile('foo...bar')
sre_parse None

>>> re1.sub('x', 'y')
'y'
>>> re1.sub('x', 'fooXXXbar')
'x'

>>> re2 = re.compile('foo(...)bar')
sre_parse None
>>> re2.sub('x', 'y')
'y'
>>> re2.sub('\\1', 'fooXXXbar')
sre ['__doc__']
'XXX'
msg60775 - (view) Author: Steve Alexander (stevea_zope) Date: 2005-07-08 21:52
Logged In: YES 
user_id=492001

This is significant for programs that use an expensive
import hook, and also use this kind of regex.

I'm working around this by making my import hook return
early for imports from sre.
msg60776 - (view) Author: Peter van Kampen (pterk) Date: 2005-07-24 12:27
Logged In: YES 
user_id=174455

Below is the full source of re.py. Does your problem go away
when you use sre directly instead? 

"""Minimal "re" compatibility wrapper.  See "sre" for
documentation."""

engine = "sre" # Some apps might use this undocumented variable

from sre import *
from sre import __all__
msg66692 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2008-05-11 23:15
pattern.sub() imports the SRE Python module (to call its subx()) with
PyImport_Import -- that C functions uses a dummy list ['__doc__'] to get
the correct module for dotted import paths.

The import of an additional module is gone now that re is really called re.
History
Date User Action Args
2022-04-11 14:56:12adminsetgithub: 42177
2008-05-11 23:15:30georg.brandlsetstatus: open -> closed
nosy: + georg.brandl
resolution: wont fix
messages: + msg66692
2005-07-08 21:46:21stevea_zopecreate