a
    Ž¥»a  ã                   @   sÊ   d dl Z d dlmZmZ d dlmZmZ d dlmZ d dl	m
Z
 d dlmZmZmZmZmZ ddlmZmZmZ G d	d
„ d
eƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZeƒ ZG dd„ deƒZdS )é    N)ÚABCÚabstractmethod)Ú_getattributeÚ_Pickler)Úwhichmodule)Ú
ModuleType)ÚAnyÚListÚOptionalÚTupleÚDicté   )ÚdemangleÚget_mangle_prefixÚ
is_mangledc                   @   s   e Zd ZdZdS )ÚObjNotFoundErrorzHRaised when an importer cannot find an object by searching for its name.N©Ú__name__Ú
__module__Ú__qualname__Ú__doc__© r   r   úP/home/yuhw/wc/pochoir/venv/lib/python3.9/site-packages/torch/package/importer.pyr      s   r   c                   @   s   e Zd ZdZdS )ÚObjMismatchErrorz]Raised when an importer found a different object with the same name as the user-provided one.Nr   r   r   r   r   r      s   r   c                   @   sh   e Zd ZU dZeeef ed< eeedœdd„ƒZ	de
ee eeef dœdd	„Ze
eedœd
d„ZdS )ÚImporterad  Represents an environment to import modules from.

    By default, you can figure out what module an object belongs by checking
    __module__ and importing the result using __import__ or importlib.import_module.

    torch.package introduces module importers other than the default one.
    Each PackageImporter introduces a new namespace. Potentially a single
    name (e.g. 'foo.bar') is present in multiple namespaces.

    It supports two main operations:
        import_module: module_name -> module object
        get_name: object -> (parent module name, name of obj within module)

    The guarantee is that following round-trip will succeed or throw an ObjNotFoundError/ObjMisMatchError.
        module_name, obj_name = env.get_name(obj)
        module = env.import_module(module_name)
        obj2 = getattr(module, obj_name)
        assert obj1 is obj2
    Úmodules©Úmodule_nameÚreturnc                 C   s   dS )zvImport `module_name` from this environment.

        The contract is the same as for importlib.import_module.
        Nr   ©Úselfr   r   r   r   Úimport_module.   s    zImporter.import_moduleN©ÚobjÚnamer   c                    sV  ˆ du r`|r`t j t|ƒ¡du r`t|ddƒ}|dur`z|ƒ }t|tƒrJ|‰ W n ty^   Y n0 ˆ du rtt|ddƒ‰ ˆ du r‚|j‰ ˆ 	|ˆ ¡}t
|ƒ}zˆ |¡}t|ˆ ƒ\}}	W n2 tttfyä   t|› d|› dˆ › �ƒd‚Y n0 ||u rö|ˆ fS ‡ ‡fdd„}
|
|ƒ\}}}|
|ƒ\}}}d|› d	|› d
|› d|› d|› d|› d�}t|ƒ‚dS )am  Given an object, return a name that can be used to retrieve the
        object from this environment.

        Args:
            obj: An object to get the the module-environment-relative name for.
            name: If set, use this name instead of looking up __name__ or __qualname__ on `obj`.
                This is only here to match how Pickler handles __reduce__ functions that return a string,
                don't use otherwise.
        Returns:
            A tuple (parent_module_name, attr_name) that can be used to retrieve `obj` from this environment.
            Use it like:
                mod = importer.import_module(parent_module_name)
                obj = getattr(mod, attr_name)

        Raises:
            ObjNotFoundError: we couldn't retrieve `obj by name.
            ObjMisMatchError: we found a different object with the same name as `obj`.
        NÚ
__reduce__r   z was not found as Ú.c                    sP   ˆ d usJ ‚ˆ  | ˆ ¡}t|ƒ}|r,t|ƒnd}|rBdt|ƒ› �nd}|||fS )Nzthe current Python environmentzthe importer for z'sys_importer')r   r   r   )r#   r   Zis_mangled_ÚlocationZimporter_name©r$   r    r   r   Úget_obj_infol   s    ÿ
ýÿýz'Importer.get_name.<locals>.get_obj_infoz

The object provided is from 'z', which is coming from z.
However, when we import 'z', it's coming from z@.
To fix this, make sure this 'PackageExporter's importer lists z before )r   ÚdispatchÚgetÚtypeÚgetattrÚ
isinstanceÚstrÚ	Exceptionr   r   r   r!   r   ÚImportErrorÚKeyErrorÚAttributeErrorr   r   )r    r#   r$   ÚreduceÚrvZorig_module_namer   ÚmoduleÚobj2Ú_r)   Zobj_module_nameZobj_locationZobj_importer_nameZobj2_module_nameZobj2_locationZobj2_importer_nameÚmsgr   r(   r   Úget_name6   sT     

ÿþÿþþüüÿzImporter.get_namec              	   C   s‚   t |ddƒ}|dur|S | j ¡  ¡ D ]V\}}|dks&|dks&|du rHq&z t||ƒd |u rf|W   S W q& tyz   Y q&0 q&dS )a  Find the module name an object belongs to.

        This should be considered internal for end-users, but developers of
        an importer can override it to customize the behavior.

        Taken from pickle.py, but modified to exclude the search into sys.modules
        r   NÚ__main__Z__mp_main__r   )r-   r   ÚcopyÚitemsr   r3   )r    r#   r$   r   r6   r   r   r   r   ‡   s"    ÿþýzImporter.whichmodule)N)r   r   r   r   r   r/   r   Ú__annotations__r   r!   r   r
   r   r:   r   r   r   r   r   r      s   
 Qr   c                   @   s0   e Zd ZdZedœdd„Zeeedœdd„ZdS )	Ú_SysImporterz;An importer that implements the default behavior of Python.)r   c                 C   s
   t  |¡S ©N)Ú	importlibr!   r   r   r   r   r!   ¨   s    z_SysImporter.import_moduler"   c                 C   s
   t ||ƒS r@   )Ú_pickle_whichmodule)r    r#   r$   r   r   r   r   «   s    z_SysImporter.whichmoduleN)r   r   r   r   r/   r!   r   r   r   r   r   r   r?   ¥   s   r?   c                   @   s:   e Zd ZdZdd„ Zeedœdd„Zeeedœdd	„Z	d
S )ÚOrderedImporterz—A compound importer that takes a list of importers and tries them one at a time.

    The first importer in the list that returns a result "wins".
    c                 G   s   t |ƒ| _d S r@   )ÚlistÚ
_importers)r    Úargsr   r   r   Ú__init__¸   s    zOrderedImporter.__init__r   c                 C   s|   d }| j D ]V}t|tƒs&t|› d�ƒ‚z| |¡W   S  ty^ } z|}W Y d }~q
d }~0 0 q
|d urp|‚nt|ƒ‚d S )NzP is not a Importer. All importers in OrderedImporter must inherit from Importer.)rE   r.   r   Ú	TypeErrorr!   ÚModuleNotFoundError)r    r   Zlast_errÚimporterÚerrr   r   r   r!   »   s    

ÿzOrderedImporter.import_moduler"   c                 C   s,   | j D ] }| ||¡}|dkr|  S qdS )Nr;   )rE   r   )r    r#   r$   rJ   r   r   r   r   r   Í   s
    

zOrderedImporter.whichmoduleN)
r   r   r   r   rG   r/   r   r!   r   r   r   r   r   r   rC   ²   s   rC   )rA   Úabcr   r   Úpickler   r   r   rB   Útypesr   Útypingr   r	   r
   r   r   Z	_manglingr   r   r   r0   r   r   r   r?   Zsys_importerrC   r   r   r   r   Ú<module>   s    
