Skip to content

Commit

Permalink
fix(object): Load a pickled vt object
Browse files Browse the repository at this point in the history
  • Loading branch information
mgmacias95 committed Dec 19, 2023
1 parent 87ab687 commit 8fbb999
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
7 changes: 7 additions & 0 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import datetime
import io
import json
import pickle

import pytest
from vt import APIError
Expand Down Expand Up @@ -70,6 +71,12 @@ def test_object_date_attrs():
assert obj.foo_date == datetime.datetime(1970, 1, 1, 0, 0, 0)


def test_object_pickle():
obj = Object("dummy")
obj.whatever = {'1': '2'}
pickle.loads(pickle.dumps(obj))


def test_object_to_dict():
obj = Object.from_dict({
"type": "dummy_type",
Expand Down
9 changes: 5 additions & 4 deletions vt/object.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

"""Defines a VT object and other helper classes."""

import collections
import datetime
import functools
import re
Expand All @@ -21,7 +22,7 @@
__all__ = ["Object"]


class WhistleBlowerDict(dict):
class WhistleBlowerDict(collections.UserDict):
"""Helper class for detecting changes in a dictionary.
This class wraps a standard Python dictionary and calls the provided callback
Expand Down Expand Up @@ -141,7 +142,7 @@ def __init__(
self._modified_data = {}
self._error = None

def __on_attr_change(self, attr: str) -> None:
def _on_attr_change(self, attr: str) -> None:
if hasattr(self, "_modified_attrs"):
self._modified_attrs.append(attr)

Expand All @@ -156,12 +157,12 @@ def __getattribute__(self, attr: str) -> typing.Any:
def __setattr__(self, attr: str, value: typing.Any) -> None:
if isinstance(value, dict):
value = WhistleBlowerDict(
value, functools.partial(self.__on_attr_change, attr)
value, functools.partial(self._on_attr_change, attr)
)
elif isinstance(value, datetime.datetime):
value = int(datetime.datetime.timestamp(value))
if attr not in self.__dict__ or value != self.__dict__[attr]:
self.__on_attr_change(attr)
self._on_attr_change(attr)
super().__setattr__(attr, value)

def __repr__(self) -> str:
Expand Down

0 comments on commit 8fbb999

Please sign in to comment.