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

Loaded datetimes with timezone offset cannot be pickled correctly #407

Closed
shezi opened this issue Oct 31, 2022 · 2 comments
Closed

Loaded datetimes with timezone offset cannot be pickled correctly #407

shezi opened this issue Oct 31, 2022 · 2 comments

Comments

@shezi
Copy link

shezi commented Oct 31, 2022

I've tried pickling an object loaded from a toml file that contains a datetime with a timezone offset.

Here is a minimal example:

>>> import toml, pickle
>>> toml_content = """
dt_with_tz = 2001-01-01 00:00:00+01:00
"""
>>> loaded_from_toml = toml.loads(toml_content)
>>> pickled = pickle.dumps(loaded_from_toml)
>>> print(pickle.loads(pickled))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: TomlTz.__init__() missing 1 required positional argument: 'toml_offset'

I have not looked deeper into the pickle data or how pickle accesses sub-objects. And yes, I know that this is a great combination, TOML+pickle.

@Skulblaka
Copy link

I'm encountering the same error message when trying to read a toml file with Ansible.

To install both ansible and toml I use a simple requirements.txt in a Dockerfile:

ansible==6.5.0
toml==0.10.2
FROM python:3-slim
COPY requirements.txt .
RUN pip install -r requirements.txt

With this file at filter_plugins/from_toml.py I define Ansible modules called to_toml and from_toml:

#!/usr/bin/python

DOCUMENTATION = '''
---
module: to_toml, from_toml

version_added: "2.8"

short_description: Converts Python data to TOML and TOML to Python data.

author:
    - "Samy Coenen ([email protected])"

'''

import toml

def to_toml(data):
    ''' Convert the value to TOML '''
    return toml.dumps(data)

def from_toml(data):
    ''' Convert TOML to Python data '''
    return toml.loads(data)

class FilterModule(object):
    ''' Ansible TOML jinja2 filters '''

    def filters(self):
        return {
            # toml
            'to_toml': to_toml,
            'from_toml': from_toml
        }

Then I try to read a toml file and set its content as a fact with the following tasks using my custom from_toml module:

- name: Slurp existing runner config again after registration
  slurp:
    src: "/etc/gitlab-runner/config.toml"
  register: config_file
  when:  runner_registration.changed

- set_fact:
    runner_config: "{{ config_file.content | b64decode | from_toml }}"
  when:  runner_registration.changed

The file I'm trying to read (/etc/gitlab-runner/config.toml) looks like this:

concurrent = 1
check_interval = 0

[session_server]
  session_timeout = 1800

[[runners]]
  name = "zenyatta"
  url = "[REDACTED]"
  id = 176
  token = "[REDACTED]"
  token_obtained_at = 2022-11-03T14:23:18Z
  token_expires_at = 0001-01-01T00:00:00Z
  executor = "shell"
  [runners.custom_build_dir]
  [runners.cache]
    [runners.cache.s3]
    [runners.cache.gcs]
    [runners.cache.azure]

I assume token_obtained_at and/or token_expires_at to be the culprit.
If desired I can build a repository reproducing the issue.

KosukeMizuno added a commit to KosukeMizuno/toml that referenced this issue Jan 17, 2023
@uiri uiri closed this as completed in 5706d31 Jan 17, 2023
@b-a-t
Copy link

b-a-t commented Mar 27, 2023

Funny, I hit the same issue with the Salt-stack and gitlab-runner/config.toml file. Well, as the parser underneath is the same...
The biggest question is - when we can expect the release of the library that addresses this issue(being there for more, than 6 years, thanks GitLab for exposing it).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants