This is an archive of the discontinued LLVM Phabricator instance.

Python 3 - Fix some portability issues with class / instance attributes
ClosedPublic

Authored by zturner on Nov 6 2015, 10:24 AM.

Details

Summary
TBH I'm honestly not sure what the problem was before, or why this fixes
it.  But what I can tell from debugging is that under Py3,
`sortMethodsUsing` is treated as a class attribute, but it was being
accessed as `self.sortMethodsUsing`.  What the full implications of this
are I don't quite know, but the symptom here was that the value we were
assigning to it -- the global lambda `cmp_` -- was being treated as a
bound method instead of a global method.  So it was expecting to be called
with a `self` argument, and I got an exception saying the method expected 3
arguments but received 2.

Why exactly this happens in Python 3, and why exactly this *doesn't*
happen in Python 2, even though the syntax was identical in both cases
are not clear to me.

Diff Detail

Event Timeline

zturner updated this revision to Diff 39555.Nov 6 2015, 10:24 AM
zturner retitled this revision from to Python 3 - Fix some portability issues with class / instance attributes.
zturner updated this object.
zturner added reviewers: tfiala, labath, tberghammer.
zturner added a subscriber: lldb-commits.
tfiala accepted this revision.Nov 6 2015, 10:28 AM
tfiala edited edge metadata.

LGTM. We just weren't using the reversed_cmp() code it seems?

This revision is now accepted and ready to land.Nov 6 2015, 10:28 AM

Derp, seems I uploaded this review twice. Now we have split threads. Anyway, there were a couple issues. The issue that was blocking me was related to the instance attributes. Specifically we were writing this:

testMethodPrefix = 'test'
suiteClass = suite.TestSuite
sortTestMethodsUsing = cmp_
_top_level_dir = None

instead of this:

def __init__(self):
    self.testMethodPrefix = 'test'
    self.sortTestMethodsUsing = cmp_
    self.testMethodPrefix = 'test'
    self._top_level_dir = None

But we were still accessing the variables as self.sortTestMethodsUsing. I'm not sure how this works in 2.7, but for whatever reason, it doesn't work in 3.x. sortTestMethodsUsing is treated as a bound method here (i.e. instance method), so it expects a self argument.

The other issues with reversed_cmp_ were still valid issues that I just fixed at the same time since they were related, but the main one was the instance / class attribute incompatibility.

zturner closed this revision.Nov 6 2015, 1:40 PM