Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

using some_re.sub() often imports sre.__doc__ #42177

Closed
steveazope mannequin opened this issue Jul 8, 2005 · 4 comments
Closed

using some_re.sub() often imports sre.__doc__ #42177

steveazope mannequin opened this issue Jul 8, 2005 · 4 comments

Comments

@steveazope
Copy link
Mannequin

steveazope mannequin commented Jul 8, 2005

BPO 1234985
Nosy @birkenfeld

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2008-05-11.23:15:30.436>
created_at = <Date 2005-07-08.21:46:21.000>
labels = ['expert-regex']
title = 'using some_re.sub() often imports sre.__doc__'
updated_at = <Date 2008-05-11.23:15:30.404>
user = 'https://bugs.python.org/steveazope'

bugs.python.org fields:

activity = <Date 2008-05-11.23:15:30.404>
actor = 'georg.brandl'
assignee = 'niemeyer'
closed = True
closed_date = <Date 2008-05-11.23:15:30.436>
closer = 'georg.brandl'
components = ['Regular Expressions']
creation = <Date 2005-07-08.21:46:21.000>
creator = 'stevea_zope'
dependencies = []
files = []
hgrepos = []
issue_num = 1234985
keywords = []
message_count = 4.0
messages = ['60774', '60775', '60776', '66692']
nosy_count = 4.0
nosy_names = ['georg.brandl', 'niemeyer', 'stevea_zope', 'pterk']
pr_nums = []
priority = 'normal'
resolution = 'wont fix'
stage = None
status = 'closed'
superseder = None
type = None
url = 'https://bugs.python.org/issue1234985'
versions = ['Python 2.4']

@steveazope
Copy link
Mannequin Author

steveazope mannequin commented Jul 8, 2005

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'

@steveazope steveazope mannequin assigned niemeyer Jul 8, 2005
@steveazope steveazope mannequin added the topic-regex label Jul 8, 2005
@steveazope steveazope mannequin assigned niemeyer Jul 8, 2005
@steveazope steveazope mannequin added the topic-regex label Jul 8, 2005
@steveazope
Copy link
Mannequin Author

steveazope mannequin commented Jul 8, 2005

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.

@pterk
Copy link
Mannequin

pterk mannequin commented Jul 24, 2005

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__

@birkenfeld
Copy link
Member

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.

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant