Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

missing global names inside a class body (callingexec with aChainMap) #121306

Open
Labels
interpreter-core(Objects, Python, Grammar, and Parser dirs)triagedThe issue has been accepted as valid by a triager.type-featureA feature request or enhancement
@CNSeniorious000

Description

@CNSeniorious000

Bug report

Bug description:

I need to implement layered context running python code, so I chooseChainMap. But Python needs everyglobals passed toexec to be adict, so I mixins it intoChainMap.

This patch runs well as far, but in a class it can't get the value from the outside.

I think this may because in the class context CPython uses some function likePyDict_Get instead of__getitem__, so the values didn't get copied into the class's globals context.

This bug caused this code fails:

fromcollectionsimportChainMapclassChainMap(ChainMap,dict):# globals must be a real dictpasssource="""a = 1class A:    print(a)"""exec(source,ChainMap())# this line raises NameError
Other reproduction approaches

This is reproduceable inpyodide (a wasm port of CPython 3.12.1), so you can run the code above by just clicking one of the following link:

  • Stackblitz - you can edit code and the result will sync instantly
  • Python Online - this is faster but just has a console instead of an IDE

CPython versions tested on:

3.12

Operating systems tested on:

Linux, Windows

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    interpreter-core(Objects, Python, Grammar, and Parser dirs)triagedThe issue has been accepted as valid by a triager.type-featureA feature request or enhancement

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp