I tried to address most of the above in the more detailed write-up I linked to. I didn't want to spam out a message that was too long, and the link provided a good way to get syntax highlighting etc.
The code that is available in the linked github repository now treats lookups and sets in fastlocals the same way it does for global and module level namespaces. I changed the behavior so that it does not affect variables stored in a dictionary. If you have one of these "cloaking" variables in a dict, you will get the actual variable before __getself__ or __setself__ is called. This makes it behave the same way as it would in say a list. Only looking up a variable by name causes these methods to get triggered.
My current implementation does not handle slots (or closures) but that I have looked into it, but I just didn't prioritize that work. I do not believe it will not be too hard to add though. I did a little bit of "magic" (inside the new lookup) to ensure that self does not have __getself__ called on it if it is used within its own methods. That keeps the code reading the same as existing python code.
As for type, it responds to whatever py object is loaded. If a __getself__ is called then type will see the result that is returned. I have added a few (preliminary implemented) "builtins" to my demo that help address this. One is getcloaked, which will return the underlying variable if there is one, otherwise it just returns the normal python variable. This reads: type(x) => result of x.__getself__ type(getcloaked('x')) => the cloaking type i.e. the history variable in one example
I have also added a setcloaked to ignore __setself__ and re-bind the name, cloaksset which returns if a variable implements __setself__, cloaksget which does the same for get, and iscloaking which returns if a variable implements any cloaking behavior.
On Tue, Jun 25, 2019 at 7:08 PM Andrew Barnert firstname.lastname@example.org wrote:
On Jun 25, 2019, at 14:00, nate lust email@example.com wrote:
This message is related to two previous threads, but was a sufficiently
evolved to warrant a new topic.
I am proposing that two new magic methods be added to python that will
control assignment and loading of class
instances. This means that if an instance is bound to a variable name,
any attempts to rebind that name will
result in a call to the __setself__ (name negotiable) of the instance
already bound to that name. Likewise
when a class instance bound to a name is loaded by the interpreter, if
present, the __getself__ method of that
instance will be called and its result will be returned instead. I have
been internally calling these cloaking
variables as they "cloak" the underlying instance, parallelling the idea
of shadowing. Feel free to suggest
Does this still have the same issues as your previous version, like sometimes working for locals and sometimes not, affecting values stored in dicts that aren’t namespaces, not working for namespaces that aren’t dicts (slots classes, custom modules, etc.), type doing the wrong thing, and so on?