2012/8/23 Stefan Behnel <stefan...@behnel.de>:
> Vitja Makarov, 22.08.2012 22:34:
>> 2012/8/23 Stefan Behnel:
>>> Vitja Makarov, 22.08.2012 22:11:
>>>> I've found regression:
>>>>
>>>> https://sage.math.washington.edu:8091/hudson/job/cython-devel-tests-pyregr/
>>>
>>> Interesting. It's a Py2 list comprehension in a class body that's failing 
>>> here:
>>>
>>> """
>>>  class TestHelpSubparsersOrdering(HelpTestCase):
>>>      subparsers_signatures = [Sig(name=name)
>>>                               for name in ('a', 'b', 'c', 'd', 'e')]
>>>  """
>>>
>>> I wonder why "name" isn't declared as a variable yet at the point where it
>>> is being looked up in the function call.
>>
>>     def lookup_relative(self, name, pos):
>>         if name == "name":
>>             print name
>>             from ipdb import set_trace; set_trace()
>>         entry = self.lookup_here(name)
>>         if entry is not None and entry.pos[1:] <= pos[1:]: # Lookup fails 
>> here
>>             return entry
>>         if self.outer_scope:
>>             return self.outer_scope.lookup_relative(name, pos)
>>         return None
>>
>>
>> What is that comparison for?
>
> Ah, yes, it is wrong in this context. It was meant to prevent names defined
> further down in the class body from being considered assignments to the
> name being looked up. Class bodies are not function bodies, assignments in
> them do not make a name "local". As long as it's not assigned, it's not
> defined and must be looked up in the outer scope.
>

Do you remember this ticket #671

If there is assignment in the class body we first lookup in the class
dict and then in globals.

B = 0
def foo():
    B = 1
    class Foo():
        A = B
        B = B
    class Bar():
        A = B
    print Foo.A, Foo.B, Bar.A
foo()

prints "0 0 1"

> I think comprehensions are actually the only case where a name is used in
> the source before its declaration. It should work in all other cases.
>
> I had considered solving this problem with the flow control analysis
> information, but I can't see how that helps me to figure out if an entry is
> already assigned (i.e. declared) at a given point in the class body.
>
> Any idea?
>

What would you do with maybe assigned case?

> Actually, I even wonder if it is a good idea to look up the name directly
> in the class scope - we may want to inject a local comprehension scope
> here, as for Py3 comprehensions and genexprs, and just switch the
> fall-through of the declarations on or off based on the code semantics.
>

-- 
vitja.
_______________________________________________
cython-devel mailing list
cython-devel@python.org
http://mail.python.org/mailman/listinfo/cython-devel

Reply via email to