pyobjc icon indicating copy to clipboard operation
pyobjc copied to clipboard

Remove objc._lazyimport, use module __getattr__ and __dir__ instead

Open ronaldoussoren opened this issue 6 years ago • 1 comments

This will require dropping Python 3.6 support, and will have to wait for the next major release.

PyObjC lazily loads data in the framework wrappers to reduce import time. This currently uses a "lazyimport" module that replaces the actual module in sys.modules. As of Python 3.7 it is possible to define __getattr__ and __dir__ at module scope instead.

This makes the code easier to understand and makes it easier to add extra code to framework wrappers.

ronaldoussoren avatar Feb 29 '20 10:02 ronaldoussoren

Interestingly it might be possible to implement this for python 3.6 as well, the following seems to work:

import os

Module = type(os)

class GetAttrModule (Module):
    def __getattribute__(self, name):
        try:
            return super().__getattribute__(name)
        except AttributeError:
            return self.__getattr__(name)

os.__getattr__ = lambda name: "<<%s>>"%(name,)
print(os.nosuchattribute)

ronaldoussoren avatar Apr 05 '20 15:04 ronaldoussoren