Skip to content

Fix issue2183 #2186

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 19, 2016
Merged

Fix issue2183 #2186

merged 2 commits into from
Jul 19, 2016

Conversation

0xc0170
Copy link
Contributor

@0xc0170 0xc0170 commented Jul 18, 2016

@bogdanm @theotherjimmy Please review. This is a fix for backward compatibility as our tools use for instance if is in Exporter.TARGETS . I found one more solution to this via metaclass, but was less clear to me. Any suggestions welcome.

Tested with the same command as reported, plus some others (uvision4,5 and iar as all of them introduced TARGETS as property):
tools\project.py -c -m NUCLEO_L432KC -p 0 -i uvision5

cc @svastm

@svastm
Copy link
Contributor

svastm commented Jul 18, 2016

Looks ok to me but after a quick test,

class ExporterTargetsProperty(object):
    def __init__(self, func):
        self.func = func
    def __get__(self, inst, cls):
        print("[PROP] inst: " + str(inst) + ", cls: " + str(cls))
        return self.func(cls)

class ClassTest(object):
    @ExporterTargetsProperty
    def property_test(cls):
        print("[FUNC] cls: " + str(cls))
        return [1, 2, 3]

# Call from class
if 1 in ClassTest.property_test:
    print("OK")

# Call from instance    
instance_test = ClassTest()
if 1 in instance_test.property_test:
    print("OK")
[PROP] inst: None, cls: <class '__main__.ClassTest'>
[FUNC] cls: <class '__main__.ClassTest'>
OK
[PROP] inst: <__main__.ClassTest object at 0x025CD490>, cls: <class '__main__.ClassTest'>
[FUNC] cls: <class '__main__.ClassTest'>
OK

the TARGETS() take as parameter cls instead of self when called from instance and class.

To be more consistent, the method should be

@ExporterTargetsProperty
def TARGETS(cls):
        if not hasattr(cls, "_TARGETS_SUPPORTED"):
            cls._TARGETS_SUPPORTED = []
            for target in TARGET_NAMES:
                try:
                    if (ProGenDef('iar').is_supported(str(TARGET_MAP[target])) or
                        ProGenDef('iar').is_supported(TARGET_MAP[target].progen['target'])):
                        cls._TARGETS_SUPPORTED.append(target)
                except AttributeError:
                    # target is not supported yet
                    continue
        return cls._TARGETS_SUPPORTED

@0xc0170
Copy link
Contributor Author

0xc0170 commented Jul 18, 2016

@svastm cls shall be used for a class method (reason: PEP 8). I don't think @property is. When you look at python examples (this one is for python 3) https://docs.python.org/3/library/functions.html#property, @property has self as an argument there as well.

@svastm
Copy link
Contributor

svastm commented Jul 18, 2016

@property don't but @ExporterTargetsProperty call func on <class '__main__.ClassTest'> and not on <__main__.ClassTest object at 0x0251D510>. So a method decorated by @ExporterTargetsProperty can be called class method.

@0xc0170
Copy link
Contributor Author

0xc0170 commented Jul 19, 2016

@svastm Updated ;)

@bogdanm
Copy link
Contributor

bogdanm commented Jul 19, 2016

LGTM. It looks like it fixes the reported issue, so it's good to go.

@bogdanm bogdanm merged commit 474915c into ARMmbed:master Jul 19, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants