Skip to content

GitLab

  • Projects
  • Groups
  • Snippets
  • Help
    • Loading...
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in / Register
pygolang pygolang
  • Project overview
    • Project overview
    • Details
    • Activity
    • Releases
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 0
    • Issues 0
    • List
    • Boards
    • Labels
    • Service Desk
    • Milestones
  • Merge requests 0
    • Merge requests 0
  • Operations
    • Operations
    • Incidents
  • Analytics
    • Analytics
    • Repository
    • Value Stream
  • Snippets
    • Snippets
  • Members
    • Members
  • Activity
  • Graph
  • Create a new issue
  • Commits
  • Issue Boards
Collapse sidebar
  • nexedi
  • pygolangpygolang
  • Merge requests
  • !28

Closed
Created Jun 18, 2024 by Kirill Smelkov@kirrOwner
  • Report abuse
Report abuse

golang: Fix `@func(cls) def name` not to set `name` in calling context

  • Overview 8
  • Commits 1
  • Changes 2

This is take 2 after 924a808c (golang: Fix @func(cls) def name not to override name in calling context). There we fixed it not to override name if name was already set, but for the case of unset name it was still set. The following example was thus not working correctly as builtin next was overridden:

class BitSync

@func(BitSync)
def next(): ...         # this was shadowing access to builtin next

def peek(seq):
    return next(...)    # here next was taken not from builtin, but
                        # from result of above shadowing

To solve the problem in the patch from 2019 I initially contemplated patching bytecode because python unconditionally does STORE_NAME after a function is defined with decorator:

In [2]: c = """
   ...: @fff
   ...: def ccc():
   ...:     return 1
   ...: """

In [3]: cc = compile(c, "file", "exec")

In [4]: dis(cc)
  2           0 LOAD_NAME                0 (fff)
              3 LOAD_CONST               0 (<code object ccc at 0x7fafe58d0130, file "file", line 2>)
              6 MAKE_FUNCTION            0
              9 CALL_FUNCTION            1
             12 STORE_NAME               1 (ccc)	<-- NOTE means: ccc = what fff() call returns
             15 LOAD_CONST               1 (None)
             18 RETURN_VALUE

However after hitting this problem for real again and taking a fresh look I found a way to arrange for the good end result without bytecode magic: if name is initially unset @func can install its own custom object, which, when overwritten by normal python codeflow of invoking STORE_NAME after decorator, unsets the attribute.

That works quite ok and the patch with the fix is small.

/cc @jerome

Assignee
Assign to
Reviewer
Request review from
None
Milestone
None
Assign milestone
Time tracking
Source branch: y/meth
GitLab Nexedi Edition | About GitLab | About Nexedi | 沪ICP备2021021310号-2 | 沪ICP备2021021310号-7