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

Appending a list to a list causes recursion #251

Closed
d00m514y3r opened this issue Apr 1, 2023 · 4 comments
Closed

Appending a list to a list causes recursion #251

d00m514y3r opened this issue Apr 1, 2023 · 4 comments
Labels

Comments

@d00m514y3r
Copy link

reproduce:

from box import Box
a = Box({"list_of_dicts": [[{"example1": 1}]]})
a.list_of_dicts.append([{"example2": 2}])
print(a)
# {'list_of_dicts': [[{'example1': 1}], [...]]}
# expected output
# {'list_of_dicts': [[{'example1': 1}], [{'example2': 2}]]}

Using python 3.10.9 and python-box 6.0.2

@cdgriffith cdgriffith added the bug label Apr 3, 2023
@cdgriffith
Copy link
Owner

This also happens on Python 3.11 and box 7.

What's weird is that if you debug that line a.list_of_dicts.append([{"example2": 2}]) it works as expected. So no easy way to even see where that is happening...

@d00m514y3r
Copy link
Author

Also if you do it like this it works for some reason:

a.list_of_dicts.append(BoxList([{"example2": 2}]))

@cdgriffith
Copy link
Owner

The issue comes down to reuse of id in python.

print(id([{"example2": 2}]))
print(id([{"example2": 1}]))
print(id([{"2": 1}]))
print(id([{"5": "2", "2": "4"}]))
1977990054720
1977990054720
1977990054720
1977990054720

Because as soon as the life of one object is over, that id can be reused. The id was being used as a recursion safety check. If you say, assign one to a variable so it is still alive, it will work.

from box import Box
a = Box({"list_of_dicts": [[{"example1": 1}]]})
b = [{"example2": 2}]
a.list_of_dicts.append(b)
assert a['list_of_dicts'][1] == [{"example2": 2}],  a['list_of_dicts'][1]

So what was happening is the object [[{"example1": 1}]] was given an id, say 1, and then moved into the new list as a new object, Say object with id 2.

That means the first object could be cleared from memory now that it's in a new space.

Object [{"example2": 2}] comes along and goes "oh hey, look at that, memory space 1 is free. I'll use that!"

Then BoxList goes (incorrectly), "Ah ha! This is 1, the same object we used to create the BoxList from in the beginning! It's self referencing! Just link me to myself."

Know the cause now. Just need to figure out best approach to go forward.

cdgriffith added a commit that referenced this issue Aug 26, 2023
* Adding #255 defer ipython import for large import speed improvements (thanks to Eric Prestat)
* Adding testing for Python 3.12
* Fixing #253 merge_update box list merge types not populated to sub dictionaries (thanks to lei wang)
* Fixing #257 Two test failures due to arguments having incorrect types (thanks to Michał Górny)
* Fixing stub files to match latest code signatures
* Removing #251 support for circular references in lists (thanks to d00m514y3r)

---------

Co-authored-by: Eric Prestat <eric.prestat@gmail.com>
@cdgriffith
Copy link
Owner

Decided best course of action is to just fully remove support for self references inside of lists, as it is a rare and dangerous use-case anyways (says the guy who doesn't want to figure out how to support it.) Updated in 7.1.0

CNSeniorious000 added a commit to CNSeniorious000/Box that referenced this issue Sep 18, 2023
CNSeniorious000 added a commit to CNSeniorious000/Box that referenced this issue Sep 18, 2023
cdgriffith added a commit that referenced this issue Jun 12, 2024
* Adding #266 support for accessing nested items in BoxList using numpy-style tuple indexing (thanks to Bit0r)
* Adding tests and Cython releases for Python 3.12
* Fixing #251 support for circular references in lists (thanks to Muspi Merol)
* Fixing #261 altering all `__repr__` methods so that subclassing will output the correct class name (thanks to Gabriel Tkacz)
* Fixing #267 Fix type 'int' not iterable (thanks to YISH)

---------

Co-authored-by: Bit0r <nie_wang@outlook.com>
Co-authored-by: Muspi Merol <me@promplate.dev>
Co-authored-by: Gabriel Tkacz <55806524+gtkacz@users.noreply.github.com>
Co-authored-by: Gabriel Tkacz <gabriel.tkacz@gscap.com.br>
Co-authored-by: YISH <mokeyish@hotmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants