Skip to content

get_var_name reports wrong name on Python 3.11 #792

@Jimx-

Description

@Jimx-
class Test(Elaboratable):

    def __init__(self):
        self.i = Signal(4)

    def elaborate(self, platform):
        m = Module()

        a = Signal()
        print(a.name)

        b = Cat(self.i[i] & a for i in range(4))
        c = Signal(4)
        m.d.sync += c.eq(b)

        return m

Expected output is "a", but on Python 3.11, it prints "self". This happens when both self and the variable are cell vars (e.g., when they are used in a generator expression).

            name_index = int(code.co_code[index + 1])
            if sys.version_info >= (3, 11):
                name_index -= code.co_nlocals
            return code.co_cellvars[name_index]

The line name_index -= code.co_nlocals assumes that code.co_cellvars does not contains any local variable but this is not the case if self is also a cell var. The code object provides a _varname_from_oparg internal method and I wonder if it is a better choice for inferring variable names.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions