-
-
Notifications
You must be signed in to change notification settings - Fork 690
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
Box children are not property removed, when iteration over _children #2117
Comments
Thanks for the report. In this case, I don't think this is a bug, although it's definitely a quirk of Python that won't necessarily be obvious. Firstly - as a quick aside, your code is referencing The issues you're having is that you're iterating over a list while deleting objects from the same list. Consider the first deletion pass in your example:
You go into this block of code with 3 children. You delete item 0. The list now has 2 elements. You delete item 1. Which item is being referenced? The original item 1 (child id There's a couple of ways around this problem. If the specific use case is "remove all children", Toga has a The second approach is to use the same loop, but iterate in reverse:
If you remove from the end of the list, the item order isn't ambiguous, because removing the last element of the list doesn't change the position of elements that come before it. Lastly, you can make the list you're iterating over unambiguous, and iterate over a copy of the list of children:
By taking a copy, you get a list of fixed length; when you remove children, the "live" list of children shrinks, but the copy doesn't, so the index references remain valid during iteration. By taking a copy, you're essentially making a distinction between "This is the list of elements I want to delete", and "this is the list of elements that are currently children of my box". The former is a static list; the latter changes as you delete elements. I hope that makes sense; unfortunately, there's not much we can do to protect against this from an API perspective because the conflict is occurring at the raw Python level. |
Thanks for the comment. I was aware about this quirk of python, but didn't connect it with this case. However something to correct for anybody reading this post in the future. The function |
I'm not sure what you mean by this - If you've got a case where this isn't working, that sounds like a bug. Can you give an example of a sequence of actions that is problematic? |
I'm using PyCharm and with Ctrl+Left Mouse clicking on
Could this be due to me getting an older version of toga through pip rather than using the repository itself? I have the toga version 0.3.1 |
Ah - that would do it. We've made a lot of improvements since the release of 0.3.1, as part of a comprehensive audit of functionality (which includes building a 100% coverage test suite). I forgot that the Toga-level "clear" implementation wasn't added until #1893. |
Is there a way to update using |
Any git repo can be added as a requirement; in this case, you need to install 2 packages:
I'm assuming, based on the screenshot in the original report, that you want the winforms backend; adjust the second requirement as appropriate if you want a different backend. |
Describe the bug
When iterating over _children from a toga.Box to remove all children, one child is still left.
Steps to reproduce
briefcase dev
using the following code in the app.pyremoveTwice = True
)Expected behavior
I would have expected to be able to remove all children objects at the same time.
Maybe there is another way should handle to remove children objects, I'm not aware.
Screenshots
Before Clicking 'Remove children':
![grafik](https://private-user-images.githubusercontent.com/49711043/267384608-e972239c-4567-452a-84ba-459beeaf9344.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3Mzk2NzcxNjcsIm5iZiI6MTczOTY3Njg2NywicGF0aCI6Ii80OTcxMTA0My8yNjczODQ2MDgtZTk3MjIzOWMtNDU2Ny00NTJhLTg0YmEtNDU5YmVlYWY5MzQ0LnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNTAyMTYlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjUwMjE2VDAzMzQyN1omWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTQ5ODQ5YWI5ZWM5ZmJlYmE2YmI5ZmQwMmM2NDU3NDVjNDNlMDFhMTUyMzIyOWU2MjUxOGM2OTEzMTY2YjFjMWMmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0In0.sWfiesT_65PcQi4ckYPIP9yG9HTbGlKCp0RWVVXMW3s)
After click:
![grafik](https://private-user-images.githubusercontent.com/49711043/267384352-8c635a44-c99e-4812-8bea-e35b0034c6f6.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3Mzk2NzcxNjcsIm5iZiI6MTczOTY3Njg2NywicGF0aCI6Ii80OTcxMTA0My8yNjczODQzNTItOGM2MzVhNDQtYzk5ZS00ODEyLThiZWEtZTM1YjAwMzRjNmY2LnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNTAyMTYlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjUwMjE2VDAzMzQyN1omWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTczZjQ4OTdkMWU5MTk0ZDg0NGE2ZTcxYzVlYzEyNjFjODA3MDI0NmIwNTRiOGRlYmIzZTAzNGNmMzQ2OGNkNzcmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0In0.wUAD9aIcq2OLtjGidUt2FKsyu3tEuGl-CReIkL6S_nQ)
Environment
Logs
Additional context
No response
The text was updated successfully, but these errors were encountered: