a
    Ad                     @   s   d dl Z d dlZd dlZd dlZd dlZd dlZd dlZd dlmZ e	ej
drXej
jnej
jZdd ZG dd de jZedZed	Zd
d Zdd Zdd ZdddZG dd dZejZee_dadS )    N)errorsTypedStoragec                  G   s   t j|  }|S N)_codecsencode)argsout r	   4/var/www/html/stable-diffusion-webui/modules/safe.pyr      s    
r   c                   @   s    e Zd ZdZdd Zdd ZdS )RestrictedUnpicklerNc                 C   s:   |d dksJ zt ddW S  ty4   t   Y S 0 d S )Nr   storageT)	_internal)r   	TypeError)selfsaved_idr	   r	   r
   persistent_load   s
    z#RestrictedUnpickler.persistent_loadc                 C   sV  | j d ur"|  ||}|d ur"|S |dkr<|dkr<tt|S |dkrX|dv rXttj|S |dkrr|dv rrtt|S |dkr|dv rttjjj|S |d	kr|d
v rttj	j
|S |dkr|dv rtt|S |dkr|dkrtS |dkr|dkrdd l}|jjS |dkr$|dkr$dd l}|jjjS |dkr<|dkr<tS td| d| dd S )NcollectionsOrderedDictztorch._utils)_rebuild_tensor_v2_rebuild_parameter!_rebuild_device_tensor_from_numpytorch)FloatStorageHalfStorage
IntStorageLongStorageDoubleStorageByteStoragefloat32BFloat16Storageztorch.nn.modules.container)ParameterDictznumpy.core.multiarray)scalar_reconstructnumpy)dtypendarrayr   r   zpytorch_lightning.callbacksmodel_checkpointr   z,pytorch_lightning.callbacks.model_checkpointModelCheckpoint__builtin__setzglobal '/z' is forbidden)extra_handlergetattrr   r   _utilsnnmodules	containerr#   core
multiarrayr   pytorch_lightning.callbacks	callbacksr&   ,pytorch_lightning.callbacks.model_checkpointr'   r)   	Exception)r   modulenamerespytorch_lightningr	   r	   r
   
find_class"   s6    




zRestrictedUnpickler.find_class)__name__
__module____qualname__r+   r   r;   r	   r	   r	   r
   r      s   r   z*^([^/]+)/((data/\d+)|version|(data\.pkl))$z^([^/]+)/data\.pkl$c                 C   s.   |D ]$}t |rqtd|  d| qd S )Nzbad file inside z: )allowed_zip_names_rematchr6   )filenamenamesr8   r	   r	   r
   check_zip_filenamesG   s    
rC   c                 C   s2  zt | }t| |  dd | D }t|dkrHtd|  t|dkrbtd|  ||d &}t|}||_|	  W d    n1 s0    Y  W d    n1 s0    Y  W nf t j
y,   t| d4}t|}||_tdD ]}|	  qW d    n1 s0    Y  Y n0 d S )	Nc                 S   s   g | ]}t |r|qS r	   )data_pkl_rer@   ).0fr	   r	   r
   
<listcomp>W       zcheck_pt.<locals>.<listcomp>r   zdata.pkl not found in    zMultiple data.pkl found in rb   )zipfileZipFilerC   namelistlenr6   openr   r+   load
BadZipfilerange)rA   r+   zZdata_pkl_filenamesfile	unpickler_r	   r	   r
   check_ptO   s$    HrX   c                 O   s   t | g|R dti|S )Nr+   )load_with_extraglobal_extra_handler)rA   r   kwargsr	   r	   r
   rQ   k   s    rQ   c                 O   s   ddl m} z|jjs t| | W nT tjyN   tjd|  ddd Y dS  t	yv   tjd|  ddd Y dS 0 t
| g|R i |S )	a  
    this function is intended to be used by extensions that want to load models with
    some extra classes in them that the usual unpickler would find suspicious.

    Use the extra_handler argument to specify a function that takes module and field name as text,
    and returns that field's value:

    ```python
    def extra(module, name):
        if module == 'collections' and name == 'OrderedDict':
            return collections.OrderedDict

        return None

    safe.load_with_extra('model.pt', extra_handler=extra)
    ```

    The alternative to this is just to use safe.unsafe_torch_load('model.pt'), which as the name implies is
    definitely unsafe.
    r   )sharedz"Error verifying pickled file from z
-----> !!!! The file is most likely corrupted !!!! <-----
You can skip this check with --disable-safe-unpickle commandline argument, but that is not going to help you.

T)exc_infoNz
The file may be malicious, so the program is not going to read it.
You can skip this check with --disable-safe-unpickle commandline argument.

)r/   r\   cmd_optsdisable_safe_unpicklerX   pickleUnpicklingErrorr   reportr6   unsafe_torch_load)rA   r+   r   r[   r\   r	   r	   r
   rY   o   s"    

rY   c                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )	Extraa  
    A class for temporarily setting the global handler for when you can't explicitly call load_with_extra
    (because it's not your code making the torch.load call). The intended use is like this:

```
import torch
from modules import safe

def handler(module, name):
    if module == 'torch' and name in ['float64', 'float16']:
        return getattr(torch, name)

    return None

with safe.Extra(handler):
    x = torch.load('model.pt')
```
    c                 C   s
   || _ d S r   )handler)r   re   r	   r	   r
   __init__   s    zExtra.__init__c                 C   s   t d u sJ d| ja d S )Nzalready inside an Extra() block)rZ   re   )r   r	   r	   r
   	__enter__   s    zExtra.__enter__c                 C   s   d a d S r   )rZ   )r   exc_typeexc_valexc_tbr	   r	   r
   __exit__   s    zExtra.__exit__N)r<   r=   r>   __doc__rf   rg   rk   r	   r	   r	   r
   rd      s   rd   )N)r`   r   r   r#   r   rL   rer/   r   hasattrr   r   Z_TypedStorager   	Unpicklerr   compiler?   rD   rC   rX   rQ   rY   rd   rc   rZ   r	   r	   r	   r
   <module>   s(   -


0#