[issue26577] inspect.getclosurevars returns incorrect variable when using class member with the same name as other variable

Ryan Fox report at bugs.python.org
Thu Mar 17 03:40:30 EDT 2016


New submission from Ryan Fox:

If a variable 'x' exists in the global or local scope, and a function (also defined in the same scope as 'x', or lower) refers only to a member named 'x' of an object, inspect.getclosurevars will include a reference to the variable, rather than the member.

Okay, that's kind of confusing to describe, so here's a small example in code form:

import inspect

class Foo:
    x = int()

x = 1
f = Foo()
assert(f.x != x)

func = lambda: f.x == 0
assert(func())

cv = inspect.getclosurevars(func)
assert(cv.globals['f'] == f)
assert(cv.globals.get('x') != x) # <--- Assertion fails


It is expected that 'x' would not exist in cv.globals, since func does not refer to it. Also, there should be a 'f.x' included somewhere in the ClosureVariables object returned.

----------
components: Library (Lib)
messages: 261897
nosy: Ryan Fox
priority: normal
severity: normal
status: open
title: inspect.getclosurevars returns incorrect variable when using class member with the same name as other variable
versions: Python 3.3, Python 3.4, Python 3.5, Python 3.6

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue26577>
_______________________________________


More information about the Python-bugs-list mailing list