3
3
4
4
5
5
class DeepCopyError (Exception ):
6
- """Custom exception raised when an object cannot be deep-copied."""
6
+ """
7
+ Custom exception raised when an object cannot be deep-copied.
8
+ """
7
9
8
10
pass
9
11
10
12
11
13
def is_boto3_client (obj ):
14
+ """
15
+ Function for understanding if the script is using boto3 or not
16
+ """
12
17
import sys
13
18
14
- # check boto3 module imprort
15
19
boto3_module = sys .modules .get ("boto3" )
16
20
17
21
if boto3_module :
18
- # boto3 use botocore client so here we import botocore
19
- # if boto3 was imported the botocore will be import automatically normally
20
22
try :
21
23
from botocore .client import BaseClient
22
24
23
25
return isinstance (obj , BaseClient )
24
26
except (AttributeError , ImportError ):
25
- # if the module is not imported, or the BaseClient class does not exist, return False
26
- # if custome module name is boto3, the BaseClient class does not exist,
27
27
return False
28
28
return False
29
29
@@ -47,45 +47,36 @@ def safe_deepcopy(obj: Any) -> Any:
47
47
48
48
try :
49
49
50
- # Try to use copy.deepcopy first
51
50
return copy .deepcopy (obj )
52
51
except (TypeError , AttributeError ) as e :
53
- # If deepcopy fails, handle specific types manually
54
52
55
- # Handle dictionaries
56
53
if isinstance (obj , dict ):
57
54
new_obj = {}
58
55
59
56
for k , v in obj .items ():
60
57
new_obj [k ] = safe_deepcopy (v )
61
58
return new_obj
62
59
63
- # Handle lists
64
60
elif isinstance (obj , list ):
65
61
new_obj = []
66
62
67
63
for v in obj :
68
64
new_obj .append (safe_deepcopy (v ))
69
65
return new_obj
70
66
71
- # Handle tuples (immutable, but might contain mutable objects)
72
67
elif isinstance (obj , tuple ):
73
68
new_obj = tuple (safe_deepcopy (v ) for v in obj )
74
69
75
70
return new_obj
76
71
77
- # Handle frozensets (immutable, but might contain mutable objects)
78
72
elif isinstance (obj , frozenset ):
79
73
new_obj = frozenset (safe_deepcopy (v ) for v in obj )
80
74
return new_obj
81
75
82
76
elif is_boto3_client (obj ):
83
77
return obj
84
78
85
- # Handle objects with attributes
86
79
else :
87
- # If an object cannot be deep copied, then the sub-properties of \
88
- # the object will not be analyzed and shallow copy will be used directly.
89
80
try :
90
81
return copy .copy (obj )
91
82
except (TypeError , AttributeError ):
0 commit comments