[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [HTCondor-users] list(ExprTree) infinite loop

On 19/11/2014 22:09, Brian Bockelman wrote:
It's a really interesting one; I'm not sure of the way to solve this.  Stumped the expert!

Basically, when you call list(classad.ExprTree("2+2")), the python starts putting together a list.  It notes that ExprTree provides "__getitem__" and begins building a list like this:

[classad.ExprTree("2+2")[0], classad.ExprTree("2+2")[1], ... ]

I think it continues to do that until an IndexError is thrown.

So, what's the problem here?

The expression tree is lazily-evaluated.  classad.ExprTree("2+2")[0] is equivalent to "(2+2)[0]".  This is a perfectly valid expression - which is obviously going to end in error when you evaluate it.  However, without evaluating it, we've got no way to know it'll end in tears.
Hmm. It doesn't seem the __getitem__ call is lazy for all types of ExprTree, because in the case of a list ExprTree, it appears to evaluate immediately:

>>> import classad
>>> a = classad.ExprTree('{"foo","bar"}')
>>> a[0]
>>> a[1]
>>> type(a[0])
<type 'str'>

It doesn't return expression {"foo","bar"}[0]

Also, 2[0] does not have any meaning:

>>> a = classad.ExprTree('2')
>>> a[0]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'long' object has no attribute '__getitem__'

So what is 2+(2[0]) or (2+2)[0] supposed to mean?

>>> a = classad.ExprTree('2+2')
>>> a[0]
2 + 2[0]

Is the issue that + is lazy, and so a+b is also lazy?