Skip to content

locally imported modules are unaccessible in lambdas in pdb #76987

Description

@YuriKanivetsky
BPO 32806
Nosy @nitishch

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 = None
created_at = <Date 2018-02-09.09:47:46.402>
labels = ['type-bug']
title = 'locally imported modules are unaccessible in lambdas in pdb'
updated_at = <Date 2018-02-11.20:46:57.026>
user = 'https://bugs.python.org/YuriKanivetsky'

bugs.python.org fields:

activity = <Date 2018-02-11.20:46:57.026>
actor = 'nitishch'
assignee = 'none'
closed = False
closed_date = None
closer = None
components = []
creation = <Date 2018-02-09.09:47:46.402>
creator = 'Yuri Kanivetsky'
dependencies = []
files = []
hgrepos = []
issue_num = 32806
keywords = []
message_count = 3.0
messages = ['311871', '312010', '312012']
nosy_count = 2.0
nosy_names = ['nitishch', 'Yuri Kanivetsky']
pr_nums = []
priority = 'normal'
resolution = None
stage = None
status = 'open'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue32806'
versions = ['Python 3.6']

Activity

  1. YuriKanivetsky commented on Feb 9, 2018

    YuriKanivetskymannequin
    MannequinAuthor

    Consider the following script:

        # import pdb; pdb.set_trace()
        # import re
        def f():
            import re
            print((lambda: re.findall('a', 'aaa'))())
            import pdb; pdb.set_trace()
            print('test')
        f()

    When you run it and try to evaluate (lambda: re.findall('a', 'aaa'))(), you get:

    ['a', 'a', 'a']
    > /home/yuri/_/1.py(7)f()
    -> print('test')
    (Pdb) (lambda: re.findall('a', 'aaa'))()
    *** NameError: name 're' is not defined
    (Pdb) import re
    (Pdb) (lambda: re.findall('a', 'aaa'))()
    *** NameError: name 're' is not defined
    (Pdb) 
    

    With the commented out breakpoint it works:

    > /home/yuri/_/a.py(3)<module>()
    -> def f():
    (Pdb) import re
    (Pdb) (lambda: re.findall('a', 'aaa'))()
    ['a', 'a', 'a']
    (Pdb) 
    

    Also it works with uncommented global import and second breakpoint:

    ['a', 'a', 'a']
    > /srv/http/sl/makosh/a.py(7)f()
    -> print('test')
    (Pdb) (lambda: re.findall('a', 'aaa'))()
    ['a', 'a', 'a']
    (Pdb) 
    

    From what I can see the issue occurs when there's no re in globals argument here: https://github.com/python/cpython/blob/v3.6.4/Lib/pdb.py#L376

    I've run into it when trying to grep some object's attribute names, like:

    !list(filter(lambda x: re.search('class', x), dir(__name__)))
    
  2. nitishch commented on Feb 11, 2018

    nitishchmannequin
    Mannequin

    This can be traced back to the following issue:

    >>> c = compile("(lambda: re.findall('a', 'aaa'))()", "<stdin>", "single")
    >>> import re as rea
    >>> exec(c, None, {'re': rea})
    NameError: name 're' is not defined.

    Seeing disassembly of the compiled code, it used LOAD_GLOBAL to get re. This seems to be the problem.

  3. nitishch commented on Feb 11, 2018

    nitishchmannequin
    Mannequin

    Sorry. I didn't finish my last message. Is the behaviour described in that message a bug? If yes, that would explain the original behaviour. If no, why not?

  4. transferred this issue fromon Apr 10, 2022
  5. added
    stdlibStandard Library Python modules in the Lib/ directory
    on Nov 23, 2023
  6. gaogaotiantian commented on May 6, 2024

    @gaogaotiantian
    Member

    Fixed by #111094

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions