33
44
55class 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+ """
79
810 pass
911
1012
1113def is_boto3_client (obj ):
14+ """
15+ Function for understanding if the script is using boto3 or not
16+ """
1217 import sys
1318
14- # check boto3 module imprort
1519 boto3_module = sys .modules .get ("boto3" )
1620
1721 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
2022 try :
2123 from botocore .client import BaseClient
2224
2325 return isinstance (obj , BaseClient )
2426 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,
2727 return False
2828 return False
2929
@@ -47,45 +47,36 @@ def safe_deepcopy(obj: Any) -> Any:
4747
4848 try :
4949
50- # Try to use copy.deepcopy first
5150 return copy .deepcopy (obj )
5251 except (TypeError , AttributeError ) as e :
53- # If deepcopy fails, handle specific types manually
5452
55- # Handle dictionaries
5653 if isinstance (obj , dict ):
5754 new_obj = {}
5855
5956 for k , v in obj .items ():
6057 new_obj [k ] = safe_deepcopy (v )
6158 return new_obj
6259
63- # Handle lists
6460 elif isinstance (obj , list ):
6561 new_obj = []
6662
6763 for v in obj :
6864 new_obj .append (safe_deepcopy (v ))
6965 return new_obj
7066
71- # Handle tuples (immutable, but might contain mutable objects)
7267 elif isinstance (obj , tuple ):
7368 new_obj = tuple (safe_deepcopy (v ) for v in obj )
7469
7570 return new_obj
7671
77- # Handle frozensets (immutable, but might contain mutable objects)
7872 elif isinstance (obj , frozenset ):
7973 new_obj = frozenset (safe_deepcopy (v ) for v in obj )
8074 return new_obj
8175
8276 elif is_boto3_client (obj ):
8377 return obj
8478
85- # Handle objects with attributes
8679 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.
8980 try :
9081 return copy .copy (obj )
9182 except (TypeError , AttributeError ):
0 commit comments