Comments (8)
The class declaration itself is working fine, the issue here is with our current implementation of send_super
. The issue is actually very similar to a problem that can happen when using Python's super
incorrectly, as in this SO question. Our send_super
does the Objective-C equivalent of super(self.__class__, self).method()
, which causes infinite recursion for the reason explained on SO. The fix is also the same - we need to change send_super
to accept a class above which to start searching for the method, instead of always starting at self
's superclass.
from rubicon-objc.
Ah well I'm super happy the python side sets up the objects correctly then! That's actually incredibly valuable to me to know.
So this is a generic python problem, huh? I did not know that. This reveals my relative newness to python, still.
So in our case would: super().init() work if you know that super is a python-side class? Or does our __getattr__ implementation interfere with python's proxy 'super' instance that is returned?
EDIT: yeah, no that doesn't work because the instance is actually ObjCInstance, duh. Oh well. I give up. :)
from rubicon-objc.
Sorry, my answer may have been a bit short. Python's super
isn't involved here at all. However, the issue with our send_super
is similar to a problem that can happen with Python's super
, that's why I mentioned it. I'll try to explain a bit.
When you call an Objective-C method normally ([self method]
), the runtime looks for an implementation of the method in self
's class. If there is none, it looks in the superclass of self
's class, then in the superclass of that, etc.
When you call a superclass method ([super method]
), the behavior is a bit different, because the call ignores the method's implementation in the current class (and any subclasses). To do this, the runtime starts searching for implementations in the superclass of the current class, rather than starting at self
's class.
In the case of super
, it's important that the starting class is remembered when the method is defined. The current send_super
implementation doesn't do this correctly - instead, it determines the starting class dynamically based on self
's class, which means that the starting class changes depending on self
's runtime class. This is what causes the infinite recursion. Here's what happens when you call MyB.alloc().init()
with our buggy implementation of send_super
:
self
is an object of classMyB
.- When
self.init()
is called, the runtime searches for aninit
method inself.objc_class
, i. e.MyB
. That method exists, so it is called. MyB.init
callssend_super(self, "init")
. Rubicon asks the runtime to callself.init()
and start method lookup inself.objc_class.superclass
, i. e.MyA
. That method exists, so it is called.MyA.init
callssend_super(self, "init")
again. The same thing happens as before,self.objc_class.superclass
is stillMyA
, soMyA.init
is called again. This continues forever.
And here's what should happen:
self
is an object of classMyB
.- When
self.init()
is called, the runtime searches for aninit
method inself.objc_class
, i. e.MyB
. That method exists, so it is called. MyB.init
callssend_super(MyB, self, "init")
. Rubicon asks the runtime to callself.init()
and start method lookup inMyB.superclass
, i. e.MyA
. That method exists, so it is called.MyA.init
callssend_super(MyA, self, "init")
. Rubicon asks the runtime to callself.init()
and start method lookup inMyA.superclass
, i. e.NSObject
. That method exists, so it is called.NSObject.init
does its thing. No infinite recursion!
This issue is a bit difficult to explain, hence the long explanation. I hope it's somewhat understandable - if you have any questions, please ask!
from rubicon-objc.
Oh man you are awesome, dgelessus. So rarely do I interact with people on github that are so communicative and great. Thanks for this explanation.
I think I intuitively understood what you are explaining as I read through the runtime.py code and eventually "got" it.
But seeing it all laid down clearly surely elevates the discussion.
runtime starts searching for implementations in the superclass of the current class,
How exactly is it implemented in objective-c? From my quick reading -- basically the compiler generates an objc_super structure at compile time right there in the method when it sees the 'super' keyword -- so the super keyword always goes withe the concrete compile time class implementation the method finds itself in. (Which expains weirdnesses like [super class] not returning what you expect. It still returns [self class] such that [super class] == [self class]).
Clever!
We can't easily do the same in Python.. or can we?
Anyway -- I'd like very much to volunteer to fix this at some point (in addition to my promise to make ObjCBlock and Block and ObjCBlockInstance one and the same class that does everything).
How would we go about doing this properly? I guess I need to read the runtime.py code more and maybe something will pop into my head as an obvious solution. I feel like it may be possible to do it with minimal overhead.
If objc can do it by issuing a struct at compile-time that goes with a method -- surely we can do it at runtime somehow!
from rubicon-objc.
Update: I am reading the code now. Actually the 'super' obvious (pun intended) solution would just be to have send_super accept a new keyword argument in addition to the argtypes= and restype= keywords.. namely, 'objcsuper=' or somesuch, which instructs it where to start searching on the obj-c side for the superclass. objcsuper= should be an object of type ObjCClass, right? (Or perhaps the lower-level pointer type you get from get_class(name) )??
I am thinking that pretty much reproduces the 'automatic' functionality the obj-c compiler gives obj-c programmers, just we have to do it manually in python.
If the caller omits the arg, then the usual broken thing happens. If they add the arg, then the correct behavior occurs.
What do you think?
from rubicon-objc.
Ok, added a pull request, #108
Note: Quick and dirty but at least correct (I think!) approach. Let me know!
from rubicon-objc.
Oh man you are awesome
Thank you :)
How exactly is it implemented in objective-c? From my quick reading -- basically the compiler generates an objc_super structure at compile time right there in the method when it sees the 'super' keyword -- so the super keyword always goes withe the concrete compile time class implementation the method finds itself in.
Yep, that is correct. When you call a method on super
, the compiler generates a call to objc_msgSendSuper
instead of objc_msgSend
. The first argument to objc_msgSendSuper
is a struct objc_super *
, and that struct contains two fields: receiver
(the object to call the method on) and super_class
(the class at which to start searching for the method). super_class
is set at compile time to the superclass of the current class.
Which expains weirdnesses like [super class] not returning what you expect.
Yeah, it would be nice if [super class]
returned the superclass of the current class or something, but it actually does the same as [self class]
. By calling [super class]
you're saying "call [self class]
and ignore any implementation in this class", so unless your class defines its own class
method, it won't make any difference.
Ok, added a pull request
Wow, that was quick! Will have a look.
from rubicon-objc.
Addressed in #108 — closing. Thanks for the help and everything with this!
from rubicon-objc.
Related Issues (20)
- Race condition when populating the ObjCClass method/attribute cache
- Update CI configuration to test release artefacts
- Improve correspondence between ObjectiveC objects and Python wrappers HOT 17
- New caching logic may inadvertently release objects that are still needed HOT 5
- ctypes.ArgumentError message is split into individual characters
- Warning about deprecated `pkg_resources` API
- Incompatibility with `from __future__ import annotations` HOT 1
- Incompatibility with Python 3.12.0b1 HOT 4
- Scroll blocks eventloop for ios HOT 3
- Can't invoke Protocol methods that collide with object properties HOT 2
- "ImportError: No module named 'rubicon'" if trying to bundle script via py2app. HOT 1
- Sphinx 7.2 dropped support for Python 3.8
- Importing rubicon.objs hangs on pypy HOT 1
- Normalize contribution docs with Briefcase/Toga
- Duplicate elements in repr of `ObjCInstance` HOT 7
- ctypes_patch breaks with Python 3.13.0a6 HOT 2
- Event loop stalls in network test
- Change the implementation of ObjCStrInstance HOT 2
- Partial method argument order HOT 5
- Improve the error message of partial methods HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from rubicon-objc.