Skip to content
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

Resizing window of example app breaks layout and generates TypeError #191

Open
HalfWhitt opened this issue Nov 11, 2024 · 1 comment · May be fixed by beeware/rubicon-objc#540
Open

Resizing window of example app breaks layout and generates TypeError #191

HalfWhitt opened this issue Nov 11, 2024 · 1 comment · May be fixed by beeware/rubicon-objc#540
Labels
bug A crash or error in behavior.

Comments

@HalfWhitt
Copy link

HalfWhitt commented Nov 11, 2024

Describe the bug

Dragging the edges of the window to resize the example app (at least on macOS) generates repeated instances of this error:

Exception ignored on calling ctypes callback function <rubicon.objc.api.objc_method object at 0x10718c600>:
Traceback (most recent call last):
  File "/Users/charles/.pyenv/versions/toga_313/lib/python3.13/site-packages/rubicon/objc/api.py", line 339, in __call__
    result = self.py_method(py_self, *args)
  File "/Users/charles/toga_dev/toga/cocoa/src/toga_cocoa/widgets/canvas.py", line 41, in drawRect_
    self.interface.context._draw(self.impl, draw_context=context)
  File "/Users/charles/toga_dev/toga/core/src/toga/widgets/canvas.py", line 461, in _draw
    obj._draw(impl, **kwargs)
  File "/Users/charles/toga_dev/toga/core/src/toga/widgets/canvas.py", line 1070, in _draw
    obj._draw(impl, **sub_kwargs)
  File "/Users/charles/toga_dev/toga/core/src/toga/widgets/canvas.py", line 383, in _draw
    impl.write_text(
  File "/Users/charles/toga_dev/toga/cocoa/src/toga_cocoa/widgets/canvas.py", line 305, in write_text
    rs = self._render_string(line, font, **kwargs)
  File "/Users/charles/toga_dev/toga/cocoa/src/toga_cocoa/widgets/canvas.py", line 247, in _render_string
    textAttributes[NSFontAttributeName] = font.native
TypeError: 'ObjCInstance' object does not support item assignment

This happens when resizing either horizontally, vertically, or both. Additionally, reducing vertical size (not necessarily smaller than initial — any reduction from current) creates weird rendering overlaps.

Notably, this doesn't happen when first rendering the window and widget. And it doesn't always happen when resizing, either, only intermittently.

Steps to reproduce

  1. Go to the examples folder and run python -m chart
  2. Resize the window.

Expected behavior

No exception, and proper layout/rendering

Screenshots

image

Environment

  • Operating System: macOS
  • Python version: 3.13
  • Software versions:
    • Toga: current main branch
    • Travertino: both 0.3.0 and current main branch

Logs

No response

Additional context

No response

@HalfWhitt HalfWhitt added the bug A crash or error in behavior. label Nov 11, 2024
@freakboy3742
Copy link
Member

This is a weird one. It looks like it's deep in Rubicon.

The code in question is doing the following:

textAttributes = NSMutableDictionary.alloc().init()
textAttributes[NSFontAttributeName] = font.native

On a normal (successful) invocation, type(textAttributes) returns:

type[<ObjCMutableDictInstance: __NSDictionaryM at 0x130ada330: {}]

However, on the problem call, the same call returns:

type[<ObjCInstance: __NSDictionaryM at 0x12a2341a0: {}>]

So - it's not identifying that the MutableDict wrapper needs to be applied to the object returned by the constructor.

There's an immediate fix in converting all the use of subscript notation into setObject:forKey: calls; message passing works on any object. But that's not a very satisfying answer, as the underlying Rubicon issue still exists.

I wonder if this might be a niche manifestation of beeware/rubicon-objc#256 - if the ObjC memory address is being recycled before Python disposes of the object, that would result in the wrong object wrapper being applied, resulting in the __setitem__ handler not being available. It's only manifesting here because there are lots of calls to render strings, which means it's more likely to expose this sort of memory recycling issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A crash or error in behavior.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants