Repository: huggingface/transformers
Pull request: Fix mutable default arguments and resource leaks

# What does this PR do?

Fixes mutable default arguments and unclosed file handles across several files.

**Mutable defaults** (can cause shared state across calls):
- `debug_utils.py`: `DebugUnderflowOverflow.__init__` `trace_batch_nums=[]` -> `None`
- `kernel_config.py`: `KernelConfig.__init__` `kernel_mapping={}` -> `None`
- `modeling_idefics.py`: `freeze_model`, `freeze_text_layers`, `freeze_vision_layers` `module_exceptions=[]` -> `()`
- `convert_usefulsensors_to_hf.py`: `_read_h5_weights` `weights={}` -> `None` (this dict is actively mutated via assignment at line 50)

**Resource leaks** (file handles not closed):
- `processing_utils.py`: `open(template_file).read()` in dict comprehension -> explicit loop with context manager
- `tokenization_myt5.py`: `json.load(open(vocab_file))` -> context manager
- `cli/serve.py`: `config_path.open().read()` -> context manager

## Who can review?

Anyone in the community is free to review the PR.

Changed files:
filename: src/transformers/debug_utils.py
patch: @@ -142,7 +142,9 @@ class DebugUnderflowOverflow:
             Whether to abort after a certain batch number has finished
     """
 
-    def __init__(self, model, max_frames_to_save=21, trace_batch_nums=[], abort_after_batch_num=None):
+    def __init__(self, model, max_frames_to_save=21, trace_batch_nums=None, abort_after_batch_num=None):
+        if trace_batch_nums is None:
+            trace_batch_nums = []
         self.model = model
         self.trace_batch_nums = trace_batch_nums
         self.abort_after_batch_num = abort_after_batch_num

filename: src/transformers/models/idefics/modeling_idefics.py
patch: @@ -156,7 +156,7 @@ def expand_inputs_for_generation(
     return input_ids, model_kwargs
 
 
-def freeze_model(model, module_exceptions=[]):
+def freeze_model(model, module_exceptions=()):
     mapping = {
         "LayerNorm": nn.LayerNorm,
         "Linear": nn.Linear,
@@ -932,11 +932,11 @@ def freeze_relevant_params(self, config=None):
         if config.freeze_vision_layers:
             freeze_model(self.vision_model, module_exceptions=config.freeze_vision_module_exceptions)
 
-    def freeze_text_layers(self, module_exceptions=[]):
+    def freeze_text_layers(self, module_exceptions=()):
         for module in [self.layers, self.norm]:
             freeze_model(module, module_exceptions=module_exceptions)
 
-    def freeze_vision_layers(self, module_exceptions=[]):
+    def freeze_vision_layers(self, module_exceptions=()):
         freeze_model(self.vision_model, module_exceptions=module_exceptions)
 
     @merge_with_config_defaults

filename: src/transformers/models/moonshine/convert_usefulsensors_to_hf.py
patch: @@ -32,7 +32,9 @@ def _get_weights(model_name):
     )
 
 
-def _read_h5_weights(group, current_key="", weights={}):
+def _read_h5_weights(group, current_key="", weights=None):
+    if weights is None:
+        weights = {}
     for key in group:
         full_key = f"{current_key}.{key}" if current_key else key
         if isinstance(group[key], h5py.Dataset):

filename: src/transformers/utils/kernel_config.py
patch: @@ -97,8 +97,8 @@ class KernelConfig(PushToHubMixin):
     Kernel configuration class. This class is used to configure the kernel mapping for a model.
     """
 
-    def __init__(self, kernel_mapping={}, use_local_kernel=False):
-        self.kernel_mapping = kernel_mapping
+    def __init__(self, kernel_mapping=None, use_local_kernel=False):
+        self.kernel_mapping = kernel_mapping if kernel_mapping is not None else {}
         self.registered_layer_names = {}
         self.use_local_kernel = use_local_kernel