last day (15 days later) » 

11:54 AM
1
A: Truly Private Variables in Python 3

Tcllsomething I like to do, though it's not exactly 100% private, is to use closures in methods to R/W normally inaccessible attributes as member_descriptor objects: def privateNS(): class MyObject(object): __slots__ = ['private'] # name doesn't matter def __new__(cls, value): ...

 
Um, wow. That is cool!
In my answer, I found a way to see where the call is coming from. The problem was that inspect seems to be able to see though anything. :(
 
Oh wait, I misread your answer, so while mine completely hides the attribute, yours actually restricts access! nice job!
 
Thanks! Sadly, inspect can see through it... But it is as close as I think you can get. If you find a way to stop inspect, though... ...that would be great!
 
actually, it looks like my method does just that already, at least through getattr_static as it seems to check the class or instance __dict__ for the attribute (I'm using __slots__ so only the class has __dict__, and the private attribute is unlinked from it with del). but my problem is I inject the attribute's descriptor into the method closures.
also, the issue with yours is that getattr_static uses type mechanics (base-level functionality) to break into your class and bypass your __getattribute__ method (if you look at inspect.py, you can see what goes on, since most of the standard library is written in poor quality python code), and that's why banning __dict__ doesn't work from you. ;)
now that I've gotten chat to trigger (finally), it seems both of our methods could benefit from using __getattribute__ in a metaclass to control access... though I'm confused as to how to set that up without a backdoor (access to the mechanics).
 
 
3 hours later…
3:05 PM
...because if it works from inside the class, it also works from the outside...
Probably
My first thought is to monkey-patch parts of the inspect library.
But that is a stupid idea because someone could just make something called inspect2 or something.
It also feels hacky.
So you found a way to stop inspect, but the value can be found by other means.
What if we ban that with my ban code?
Also, with metaclasses: they could work...
...but I’m not sure how they could help because like you said we could just look at the metaclass’ attributes.
The problem with inspect is that it was made to go where most code can’t, however your way to block inspect looks very promising.
 
3:48 PM
So maybe we combine the two?
I can’t seem to wrap my head around all of this stuff now, let me think for a bit...
 
4:15 PM
@Tcll So, quick question. I can’t seem to remember where __slots__ stores its values and if inspect can see through it if we block it with my ban script.
Where do __slots__ values go?
 
__slots__ is an iterable that defines names for member_descriptor objects, but the __slots__ variable is only used once during the class creation, where the member_descriptor objects created are stored in the class __dict__
so yeah, type.__new__() is what uses __slots__
member descriptors can't be created other than through __slots__ btw, but we could define another naming scheme that could define private-scope variables... but the problem there is creating descriptors that can't access their value but in a private context.
also, one important detail about __slots__ classes (if you haven't read up already) is that they use a read-only mappingproxy for a __dict__, and as such, you can't add new attributes during runtime, the benefit of which is that instances use much less memory.
 
5:14 PM
The problem is that we can’t monkey-patch type. Also, monkey-patching type seems like it wouldn’t end well.
@Tcll Wait, __slots__ saves its values in a __dict__? I though the __dict__ went away when you used __slots__ Is the mapping saved in __dict__ or somewhere else?
 
only instance, not class ;)
the class __dict__ is what contains the member_descriptor objects created from __slots__
__slots__ is nothing more than an iterator of strings, the member_descriptor objects are created within type.__new__ and bound to the class before being thrown in the class __dict__ which is why they still work even though del removes them
here, do this in a shell:

>>> class A(object): __slots__ = ['name']
>>> A.name
>>> A.name.__class__
A.__dict__ just holds that descriptor
note that a bound descriptor is an object instance containing __get__ and __set__
it could be possible to make a read-only descriptor, but as long as it inherits type it'll be hackable
one place I could think to look would be ctypes (or _ctypes), since we're breaking the barrier into C code there
if I knew how to write my own member_descriptor implementation in C that could actually define and reference private data, I could integrate that into a metaclass that uses __private__ similar to __slots__
thing is I'd have to figure out how to grab the context in C
 
5:41 PM
...which sound really fun! (Sarcastic)
We could ban the class’ __dict__ with a metaclass and only allow instances of that class to access it. The only problem is that you could just enter the metaclass.
 
yeah, it has to reference from somewhere sadly -.-
 
What if there was a way to override the class’ __getattribute__ in the same class?
Wait, what about @classmethod?
...and then use your method to block inspect!
 
we'd need the instance though, because you can think of descriptors as dictionaries holding the instance as the key to the value
 
Oh...
We can’t override both the instance’s and the class’ __getattribute__, can we?
 
yeah possibly, but unless we have our own metaclass (breaking type access), we can't control privacy
note that's our own metaclass in C
 
5:54 PM
Sadly, I don’t know C.
Wait, say we make a @classmethod __getattribute__ that blocks class level access outside of the class with my script, and then directly add a __getattribute__ function to the instances __dict__ to prevent overwriting of both methods in the class definition?
Would that work? It does metaclass-like stuff inside of the class.
I didn’t mean to submit that.
 
that's technically just a wrapper though
 
Great
 
yeah I'm stumped currently without testing
 
I can test later, but not now.
 
hey, find me on forums or such and I could probably get a collaborative project going ;)
 
6:02 PM
GitHub?
Email seems less productive.
 
it's really just for a better position, this place isn't exactly the best place to talk about this.
 
I guess. Could we use GitHub issues instead?
 
I'm meaning somewhere less public that we'll have more control over... eg Discord
 
Oh. Why does it have to be private?
 
mainly so we can have a larger conversation, this place and somewhat GH issues are for others to reference upon.
 
6:17 PM
Okay...
 
6:50 PM
GTG, I can talk in a few hours.
 
 
4 hours later…
10:57 PM
I'm back.
 
not to be rude or anything, but I think we've stated all we can here, I don't have any more information to provide on my answer at this moment, but I will likely update when the time comes. remember this is for everyone to follow up on, so we want to keep this as short as possible. please get in touch with me on a platform we can freely chat on (you can find me in quite a number of places). ;)
 

last day (15 days later) »