VirusTotal / vt-py

The official Python 3 client library for VirusTotal
https://virustotal.github.io/vt-py/
Apache License 2.0
531 stars 121 forks source link

Unable to save WhistleBlowerDict obj. (or an object with nested WhistleBlowerDict) to a pickle. #172

Closed Quodzik closed 9 months ago

Quodzik commented 9 months ago

When I'm saving a custom class instance to a pickle file, there's no error, file is being generated in the filesystem. I do this with: pickle.dump(proc_db, open('process_db_vted.pickle', 'wb')) I have also tried changing default arguments (protocol=pickle.HIGHEST_PROTOCOL and fix_imports=True).

When trying to load the pickle file with: proc_db = pickle.load(open('process_db_vted.pickle', 'rb')) I get:

    proc_db = pickle.load(open('process_db_vted.pickle', 'rb'))
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "\venv\Lib\site-packages\vt\object.py", line 39, in __setitem__
    self._on_change_callback()
    ^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'WhistleBlowerDict' object has no attribute '_on_change_callback'

please advise Regards, Q

mgmacias95 commented 9 months ago

Hello @Quodzik,

I have tried to reproduce your error but I've been unable to do it. This is the steps I've taken:

>>> import vt
>>> a = vt.Client('a')
>>> a
<vt.client.Client object at 0x1026d29d0>
>>> import pickle
>>> b = pickle.dumps(a)
>>> b
b'\x80\x04\x95\xb8\x00\x00\x00\x00\x00\x00\x00\x8c\tvt.client\x94\x8c\x06Client\x94\x93\x94)\x81\x94}\x94(\x8c\x05_host\x94\x8c\x1ahttps://www.virustotal.com\x94\x8c\x07_apikey\x94\x8c\x01a\x94\x8c\x06_agent\x94\x8c\x07unknown\x94\x8c\x08_session\x94N\x8c\n_trust_env\x94\x89\x8c\x08_timeout\x94M,\x01\x8c\x06_proxy\x94N\x8c\r_user_headers\x94N\x8c\x0b_verify_ssl\x94\x88ub.'
>>> c = pickle.loads(b)
>>> c
<vt.client.Client object at 0x1026d35d0>

Can you provide more accurate steps to reproduce the problem?

Thanks.

Quodzik commented 9 months ago

Hi @mgmacias95, thanks for your reply. That's exactly what I wanted to share, but I didn't have a change to do that. Here's a short script that generates an error, when I run it:

import pickle
import vt

md5hash = 'e60295d4d202496c1d689343bb58c9e0'

api_key = 'xxxxxxxxxxxxxx'
client = vt.Client(api_key)

vt_data = client.get_object(f'/files/{md5hash}')

#save vt_data to file
with open('vt_data.pickle', 'wb') as f:
    pickle.dump(vt_data, f)

#load vt_data from file
with open('vt_data.pickle', 'rb') as f:
    vt_data = pickle.load(f)

Thanks

mgmacias95 commented 9 months ago

Hello @Quodzik,

I have made a fix for this issue at #175. As soon as it's merged we'll release a new version with the fix.

Thanks!

Regards, Marta