/usr/local/lib64/python3.6/site-packages/torch/package/__pycache__
Edit: /usr/local/lib64/python3.6/site-packages/torch/package/__pycache__/importer.cpython-36.pyc (7111B)
3
Eg @ s d dl Z d dlmZmZ d dlmZmZ d dlmZ d dl m
Z
d dlmZm
Z
mZmZmZ ddlmZmZmZ G d d
d
eZG dd deZG d
d deZG dd deZe ZG dd deZdS ) N)ABCabstractmethod)
_getattribute_Pickler)whichmodule)
ModuleType)AnyListOptionalTupleDict )demangleget_mangle_prefix
is_mangledc @ s e Zd ZdZdS )ObjNotFoundErrorzHRaised when an importer cannot find an object by searching for its name.N)__name__
__module____qualname____doc__ r r B/usr/local/lib64/python3.6/site-packages/torch/package/importer.pyr s r c @ s e Zd ZdZdS )ObjMismatchErrorz]Raised when an importer found a different object with the same name as the user-provided one.N)r r r r r r r r r s r c @ sd e Zd ZU dZeeef eeedddZ de
ee eeef dddZ
e
eedd d
ZdS )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
)module_namereturnc C s dS )zvImport `module_name` from this environment.
The contract is the same as for importlib.import_module.
Nr )selfr r r r
import_module. s zImporter.import_moduleN)objnamer c sZ dkrb|rbt jjt|dkrbt|dd}|dk rby| }t|trJ| W n tk
r` Y nX dkrvt|dd dkr|j j | }t
|}yj|}t| \}} W n4 t
ttfk
r t| d| d dY nX ||kr| fS fdd}
|
|\}}}
|
|\}}}d| d | d
| d| d|
d
| d
}t|dS )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 k st j| }t|}|r,t|nd}|rBdt| nd}|||fS )Nzthe current Python environmentzthe importer for z'sys_importer')AssertionErrorr r r )r r Zis_mangled_locationZ
importer_name)r r r r get_obj_infol s z'Importer.get_name.
.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 dispatchgettypegetattr
isinstancestr Exceptionr r r r r ImportErrorKeyErrorAttributeErrorr r )r r r reducervZorig_module_namer moduleobj2_r$ Zobj_module_nameZobj_locationZobj_importer_nameZobj2_module_nameZ
obj2_locationZobj2_importer_namemsgr )r r r get_name6 s:
*zImporter.get_namec C s t |dd}|dk r|S xd| jj j D ]R\}}|dks(|dks(|dkrJq(yt||d |krb|S W q( tk
rx Y q(X q(W dS )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____mp_main__r )r( modulescopyitemsr r. )r r r r r1 r r r r s
zImporter.whichmodule)N)r r r r r r* r r8 r r r r
r r5 r r r r r r s
Qr c @ s0 e Zd ZdZedddZeeedddZdS ) _SysImporterz;An importer that implements the default behavior of Python.)r c C s
t j|S )N) importlibr )r r r r r r s z_SysImporter.import_module)r r r c C s
t ||S )N)_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 Zd ZdZdd ZeedddZeeeddd Z d
S )OrderedImporterzA 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 )N)list
_importers)r argsr r r __init__ s zOrderedImporter.__init__)r r c C sx d }xX| j D ]N}t|ts(t| dy
|j|S tk
rX } z
|}W Y d d }~X qX qW |d k rl|nt|d S )NzP is not a Importer. All importers in OrderedImporter must inherit from Importer.)r@ r) r TypeErrorr ModuleNotFoundError)r r Zlast_errimportererrr r r r s
zOrderedImporter.import_module)r r r c C s, x&| j D ]}|j||}|dkr|S qW dS )Nr6 )r@ r )r r r rE r r r r r s
zOrderedImporter.whichmoduleN)
r r r r rB r* r r r r r r r r r> s r> )r<