@@ -977,3 +977,47 @@ def fire_callbacks(self, *args: P.args, **kwargs: P.kwargs) -> None:
977
977
logger .exception (
978
978
"Exception in callback for %s registered with gpu trace" , self .name
979
979
)
980
+
981
+
982
+ # IMPORT_MAPPING and NAME_MAPPING are adapted from https://github.com/python/cpython/blob/main/Lib/_compat_pickle.py
983
+ # for use in the weights_only Unpickler.
984
+
985
+ IMPORT_MAPPING = {
986
+ "__builtin__" : "builtins" ,
987
+ "copy_reg" : "copyreg" ,
988
+ "Queue" : "queue" ,
989
+ "repr" : "reprlib" ,
990
+ "_abcoll" : "collections.abc" ,
991
+ # Non-mutual mappings.
992
+ "UserDict" : "collections" ,
993
+ "UserList" : "collections" ,
994
+ "UserString" : "collections" ,
995
+ "whichdb" : "dbm" ,
996
+ "StringIO" : "io" ,
997
+ "cStringIO" : "io" ,
998
+ }
999
+
1000
+
1001
+ # This contains rename rules that are easy to handle. We ignore the more
1002
+ # complex stuff (e.g. mapping the names in the urllib and types modules).
1003
+ # These rules should be run before import names are fixed.
1004
+ NAME_MAPPING = {
1005
+ ("__builtin__" , "xrange" ): ("builtins" , "range" ),
1006
+ ("__builtin__" , "reduce" ): ("functools" , "reduce" ),
1007
+ ("__builtin__" , "intern" ): ("sys" , "intern" ),
1008
+ ("__builtin__" , "unichr" ): ("builtins" , "chr" ),
1009
+ ("__builtin__" , "unicode" ): ("builtins" , "str" ),
1010
+ ("__builtin__" , "long" ): ("builtins" , "int" ),
1011
+ ("itertools" , "izip" ): ("builtins" , "zip" ),
1012
+ ("itertools" , "imap" ): ("builtins" , "map" ),
1013
+ ("itertools" , "ifilter" ): ("builtins" , "filter" ),
1014
+ ("itertools" , "ifilterfalse" ): ("itertools" , "filterfalse" ),
1015
+ ("itertools" , "izip_longest" ): ("itertools" , "zip_longest" ),
1016
+ ("UserDict" , "IterableUserDict" ): ("collections" , "UserDict" ),
1017
+ ("UserList" , "UserList" ): ("collections" , "UserList" ),
1018
+ ("UserString" , "UserString" ): ("collections" , "UserString" ),
1019
+ # Non-mutual mappings.
1020
+ ("__builtin__" , "basestring" ): ("builtins" , "str" ),
1021
+ ("exceptions" , "StandardError" ): ("builtins" , "Exception" ),
1022
+ ("UserDict" , "UserDict" ): ("collections" , "UserDict" ),
1023
+ }
0 commit comments