cdgriffith / Box

Python dictionaries with advanced dot notation access
https://github.com/cdgriffith/Box/wiki
MIT License
2.61k stars 106 forks source link

Segfault (infinite recursion?) when loading YAML schema into Box with `box_dots=True` #265

Open brynpickering opened 9 months ago

brynpickering commented 9 months ago

I have a YAML schema (see here) which I want to be able to get the flattened dict keys for - something that Box is well placed to provide with its dot notation.

However, I get a segfault when loading the YAML file with:

from box import Box
schema = Box.from_yaml("path/to/schema.yaml", default_box=True, box_dots=True)

It loads successfully with:

from box import Box
schema = Box.from_yaml("path/to/schema.yaml")

But then I lose the ability to inspect the flattened keys (schema.keys(dotted=True)).

cdgriffith commented 9 months ago

Confirmed with that file the behavior, with both ruamel.yaml and pyyaml as backends, so is from something in my code side looping. Thanks for raising the issue will look into!

paulschroeder-tomtom commented 6 months ago

Anything new on this one? I have a similar problem but with some JSON:

Box.from_yaml(resp.text, box_dots=True)
Traceback (most recent call last):
  File ".../pydevd_asyncio_utils.py", line 117, in _exec_async_code
    result = func()
  File "<input>", line 1, in <module>
  File "box/box.py", line 1050, in box.box.Box.from_yaml
  File "box/box.py", line 291, in box.box.Box.__init__
  File "box/box.py", line 671, in box.box.Box.__setitem__
  File "box/box.py", line 574, in box.box.Box.__convert_and_store
  File "box/box.py", line 291, in box.box.Box.__init__
  File "box/box.py", line 671, in box.box.Box.__setitem__
  File "box/box.py", line 583, in box.box.Box.__convert_and_store
  File "box/box_list.py", line 52, in box.box_list.BoxList.__init__
  File "box/box_list.py", line 110, in box.box_list.BoxList.append
  File "box/box_list.py", line 100, in box.box_list.BoxList._convert
  File "box/box.py", line 291, in box.box.Box.__init__
  File "box/box.py", line 671, in box.box.Box.__setitem__
  File "box/box.py", line 574, in box.box.Box.__convert_and_store
  File "box/box.py", line 291, in box.box.Box.__init__
  File "box/box.py", line 671, in box.box.Box.__setitem__
  File "box/box.py", line 574, in box.box.Box.__convert_and_store
  File "box/box.py", line 291, in box.box.Box.__init__
  File "box/box.py", line 664, in box.box.Box.__setitem__
box.exceptions.BoxKeyError: "'<class 'box.box.Box'>' object has no attribute tenant"
paulschroeder-tomtom commented 6 months ago

I am pretty sure Box chokes on / or . in the... keys maybe?

...
metadata:
  labels:
    kubed.appscode.com/origin.cluster: unicorn
...
Traceback (most recent call last):
  File "/home/schroederp/.local/share/JetBrains/IntelliJIdea2024.1/python/helpers/pydev/pydevconsole.py", line 364, in runcode
    coro = func()
  File "<input>", line 1, in <module>
  File "box/box.py", line 303, in box.box.Box.__init__
  File "box/box.py", line 671, in box.box.Box.__setitem__
  File "box/box.py", line 574, in box.box.Box.__convert_and_store
  File "box/box.py", line 291, in box.box.Box.__init__
  File "box/box.py", line 671, in box.box.Box.__setitem__
  File "box/box.py", line 574, in box.box.Box.__convert_and_store
  File "box/box.py", line 291, in box.box.Box.__init__
  File "box/box.py", line 664, in box.box.Box.__setitem__
box.exceptions.BoxKeyError: "'<class 'box.box.Box'>' object has no attribute kubed"
cdgriffith commented 4 months ago

@paulschroeder-tomtom good find, yes.

If there are keys with . or [] in them, box dots will treat them as if they should break them apart.

For example in @brynpickering YAML has keys like '^[^_^\d][\w]*$': {} that it will fail on.

I don't see any what Box could really support those cases, but should at least have a check on box creation if something is going to break it like that and give a clear error.

paulschroeder-tomtom commented 4 months ago

Ok, I sad but then it is like that. WIll you create an improve on the error message?