Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Yaml.unsafe_load error

73 views
Skip to first unread message

Albert-Jan Roskam

unread,
Oct 19, 2022, 7:03:29 AM10/19/22
to
Hi,
I am trying to create a celery.schedules.crontab object from an external
yaml file. I can successfully create an instance from a dummy class "Bar",
but the crontab class seems call __setstate__ prior to __init__. I have no
idea how to solve this. Any ideas? See code below.
Thanks!
Albert-Jan

Python 3.6.8 (default, Nov 16 2020, 16:55:22)

[GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux

Type "help", "copyright", "credits" or "license" for more information.

>>> import yaml

>>> from celery.schedules import crontab

>>> crontab(hour=3, minute=0)

<crontab: 0 3 * * * (m/h/d/dM/MY)>

>>> yaml.unsafe_load('!!python/name:celery.schedules.crontab')

<class 'celery.schedules.crontab'>

>>> yaml.safe_load('celery.schedules.crontab:\n   hour: 3\n   minute:
0\n')

{'celery.schedules.crontab': {'hour': 3, 'minute': 0}}

>>> class Bar:

...     def __init__(self, x, y):

...         pass

...

>>> bar = yaml.unsafe_load('!!python/object:__main__.Bar\n   x: 42\n   y:
666')

>>> bar

<__main__.Bar object at 0x7f43b464bb38>

>>> bar.x

42

 

# what is the correct way for the next line?

>>> yaml.unsafe_load('!!python/object:celery.schedules.crontab\n   hour:
3\n   minute: 30')

Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

  File
"/home/albertjan@mycompany/envs/myenv/lib64/python3.6/site-packages/yaml/__init__.py",
line 182, in unsafe_load

    return load(stream, UnsafeLoader)

  File
"/home/albertjan@mycompany/envs/myenv/lib64/python3.6/site-packages/yaml/__init__.py",
line 114, in load

    return loader.get_single_data()

  File
"/home/albertjan@mycompany/envs/myenv/lib64/python3.6/site-packages/yaml/constructor.py",
line 51, in get_single_data

    return self.construct_document(node)

  File
"/home/albertjan@mycompany/envs/myenv/lib64/python3.6/site-packages/yaml/constructor.py",
line 60, in construct_document

    for dummy in generator:

  File
"/home/albertjan@mycompany/envs/myenv/lib64/python3.6/site-packages/yaml/constructor.py",
line 621, in construct_python_object

    self.set_python_instance_state(instance, state)

  File
"/home/albertjan@mycompany/envs/myenv/lib64/python3.6/site-packages/yaml/constructor.py",
line 727, in set_python_instance_state

    instance, state, unsafe=True)

  File
"/home/albertjan@mycompany/envs/myenv/lib64/python3.6/site-packages/yaml/constructor.py",
line 597, in set_python_instance_state

    instance.__setstate__(state)

  File
"/home/albertjan@mycompany/envs/myenv/lib/python3.6/site-packages/celery/schedules.py",
line 541, in __setstate__

    super().__init__(**state)

TypeError: __init__() got an unexpected keyword argument 'hour'

Albert-Jan Roskam

unread,
Oct 19, 2022, 2:01:37 PM10/19/22
to
On Oct 19, 2022 13:02, Albert-Jan Roskam <sjeik...@hotmail.com> wrote:

   Hi,
   I am trying to create a celery.schedules.crontab object from an
external
   yaml file. I can successfully create an instance from a dummy class
"Bar",
   but the crontab class seems call __setstate__ prior to __init__. I
have no
   idea how to solve this. Any ideas? See code below.
   Thanks!
   Albert-Jan

   

    

   # what is the correct way for the next line?

   >>> yaml.unsafe_load('!!python/object:celery.schedules.crontab\n  
hour:
   3\n   minute: 30')

  

====
Reading the source a bit more, me thinks it might be:
yaml.unsafe_load('!!python/object/apply:celery.schedules.crontab\nkwds:\n 
 hour: 3\n   minute: 30')
I did not yet test this, though.
0 new messages