pyglove 0.5.0.dev202509100809__py3-none-any.whl → 0.5.0.dev202509120809__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of pyglove might be problematic. Click here for more details.

pyglove/core/__init__.py CHANGED
@@ -306,6 +306,7 @@ format = utils.format # pylint: disable=redefined-builtin
306
306
  print = utils.print # pylint: disable=redefined-builtin
307
307
  docstr = utils.docstr
308
308
  catch_errors = utils.catch_errors
309
+ match_error = utils.match_error
309
310
  timeit = utils.timeit
310
311
 
311
312
  contextual_override = utils.contextual_override
@@ -148,6 +148,7 @@ from pyglove.core.utils.docstr_utils import docstr
148
148
 
149
149
  # Handling exceptions.
150
150
  from pyglove.core.utils.error_utils import catch_errors
151
+ from pyglove.core.utils.error_utils import match_error
151
152
  from pyglove.core.utils.error_utils import CatchErrorsContext
152
153
  from pyglove.core.utils.error_utils import ErrorInfo
153
154
 
@@ -108,6 +108,64 @@ def catch_errors(
108
108
  Yields:
109
109
  A CatchErrorsContext object.
110
110
  """
111
+ errors = _parse_error_spec(errors)
112
+ context = CatchErrorsContext()
113
+ try:
114
+ yield context
115
+ except BaseException as e: # pylint: disable=broad-exception-caught
116
+ if match_error(e, errors):
117
+ context.error = e
118
+ if error_handler is not None:
119
+ error_handler(e)
120
+ else:
121
+ raise
122
+
123
+
124
+ def match_error(
125
+ error: BaseException,
126
+ errors: Union[
127
+ Union[Type[BaseException], Tuple[Type[BaseException], str]],
128
+ Sequence[Union[Type[BaseException], Tuple[Type[BaseException], str]]],
129
+ Dict[Type[BaseException], List[str]],
130
+ ],
131
+ ) -> bool:
132
+ """Returns True if the error matches the specification, .
133
+
134
+ Args:
135
+ error: The error to match.
136
+ errors: A sequence of exception types or tuples of exception type and error
137
+ messages (described in regular expression) as the desired exception types
138
+ to match.
139
+
140
+ Returns:
141
+ True if the error matches the specification, False otherwise.
142
+ """
143
+ error_mapping = _parse_error_spec(errors)
144
+ error_message = error.__class__.__name__ + ': ' + str(error)
145
+ for error_type, error_regexes in error_mapping.items():
146
+ if isinstance(error, error_type):
147
+ if not error_regexes:
148
+ return True
149
+ else:
150
+ for regex in error_regexes:
151
+ assert regex is not None
152
+ if not regex.startswith(('^', '.*')):
153
+ regex = '.*' + regex
154
+ if re.match(regex, error_message):
155
+ return True
156
+ return False
157
+
158
+
159
+ def _parse_error_spec(
160
+ errors: Union[
161
+ Union[Type[BaseException], Tuple[Type[BaseException], str]],
162
+ Sequence[Union[Type[BaseException], Tuple[Type[BaseException], str]]],
163
+ Dict[Type[BaseException], List[str]],
164
+ ]
165
+ ) -> Dict[Type[BaseException], List[str]]:
166
+ """Parses a sequence of error specifications into a dictionary."""
167
+ if isinstance(errors, dict):
168
+ return errors
111
169
  if not isinstance(errors, (tuple, list)):
112
170
  errors = [errors]
113
171
  elif (
@@ -136,28 +194,4 @@ def catch_errors(
136
194
  error_mapping[error_type] = []
137
195
  if regex is not None:
138
196
  error_mapping[error_type].append(regex)
139
-
140
- context = CatchErrorsContext()
141
- try:
142
- yield context
143
- except tuple(error_mapping.keys()) as e:
144
- error_message = e.__class__.__name__ + ': ' + str(e)
145
- found_match = False
146
- for error_type, error_regexes in error_mapping.items():
147
- if isinstance(e, error_type):
148
- if not error_regexes:
149
- found_match = True
150
- else:
151
- for regex in error_regexes:
152
- assert regex is not None
153
- if not regex.startswith(('^', '.*')):
154
- regex = '.*' + regex
155
- if re.match(regex, error_message):
156
- found_match = True
157
- break
158
- if found_match:
159
- context.error = e
160
- if error_handler is not None:
161
- error_handler(e)
162
- else:
163
- raise e
197
+ return error_mapping
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pyglove
3
- Version: 0.5.0.dev202509100809
3
+ Version: 0.5.0.dev202509120809
4
4
  Summary: PyGlove: A library for manipulating Python objects.
5
5
  Home-page: https://github.com/google/pyglove
6
6
  Author: PyGlove Authors
@@ -1,5 +1,5 @@
1
1
  pyglove/__init__.py,sha256=LP1HNk_VVWMHaakX3HZ0NeZ2c4lq2uJaRbalvT8haOg,1352
2
- pyglove/core/__init__.py,sha256=23XwyBxalafAnDAsJSSxTz56AtjBjq_d5SPCKCNdCoM,9668
2
+ pyglove/core/__init__.py,sha256=H4Yah3bYPrEqvuwRvhbA1MbQn7W6l2j5mmXkeAUNI30,9700
3
3
  pyglove/core/logging.py,sha256=pD5A6K8TOWoQdYZ1IgGHHd_vIcqtcWQ51Eep5Hswfwc,3305
4
4
  pyglove/core/logging_test.py,sha256=3z_c6wnxbqDbwUmSOAZzeDPXvzoweYL5QHUQVMJ5Xgo,1917
5
5
  pyglove/core/coding/__init__.py,sha256=tuHIg19ZchtkOQbdFVTVLkUpBa5f1eo66XtnKw3lcIU,1645
@@ -134,14 +134,14 @@ pyglove/core/typing/typed_missing.py,sha256=-l1omAu0jBZv5BnsFYXBqfvQwVBnmPh_X1wc
134
134
  pyglove/core/typing/typed_missing_test.py,sha256=TCNsb1SRpFaVdxYn2mB_yaLuja8w5Qn5NP7uGiZVBWs,2301
135
135
  pyglove/core/typing/value_specs.py,sha256=8E83QDZMb3lMXhgzfVNt9u6Bg3NPkvpjLXetjkps8UU,103263
136
136
  pyglove/core/typing/value_specs_test.py,sha256=eGXVxdduIM-oEaapJS9Kh7WSQHRUFegLIJ1GEzQkKHA,131017
137
- pyglove/core/utils/__init__.py,sha256=I2bRTzigU7qJVEATGlLUFkYzkiCBBCCEwrQyhsrRNmI,8602
137
+ pyglove/core/utils/__init__.py,sha256=2aw4n1kYG9xlX2tWI-H5i25cBuK1ME9Lmf-F31VlKEk,8657
138
138
  pyglove/core/utils/common_traits.py,sha256=PWxOgPhG5H60ZwfO8xNAEGRjFUqqDZQBWQYomOfvdy8,3640
139
139
  pyglove/core/utils/common_traits_test.py,sha256=DIuZB_1xfmeTVfWnGOguDQcDAM_iGgBOe8C-5CsIqBc,1122
140
140
  pyglove/core/utils/contextual.py,sha256=RxBQkDM2gB6QwZj_2oMels6oh-zQPGJJlinZbbqHuYQ,5148
141
141
  pyglove/core/utils/contextual_test.py,sha256=OOcthquVyAekTqt1RyYcEMHaockMIokpbv4pSf13Nzs,3252
142
142
  pyglove/core/utils/docstr_utils.py,sha256=5BY40kXozPKVGOB0eN8jy1P5_GHIzqFJ9FXAu_kzxaw,5119
143
143
  pyglove/core/utils/docstr_utils_test.py,sha256=TBHNwvhGyyoEs7dNOv5bW7h3B_y2smDyoAR9uPDiidI,4179
144
- pyglove/core/utils/error_utils.py,sha256=U9Ms3G1a_WUXaFgWccyPL7yOIf4k8Wi0nyXJ3yIaIfY,5264
144
+ pyglove/core/utils/error_utils.py,sha256=5if1LlouHMx9EkZt-rhlkDU53Qc-hmXgjhJHhhI125s,6392
145
145
  pyglove/core/utils/error_utils_test.py,sha256=gnsEwFmgDNNVNvuQyhSWai8GDfLoCuX_DMqYow9Q2l4,2547
146
146
  pyglove/core/utils/formatting.py,sha256=Wn4d933LQLhuMIfjdRJgpxOThCxBxQrkRBa6Z1-hL_I,15591
147
147
  pyglove/core/utils/formatting_test.py,sha256=hhg-nL6DyE5A2QA92ALHK5QtfAYKfPpTbBARF-IT1j0,14241
@@ -216,8 +216,8 @@ pyglove/ext/scalars/randoms.py,sha256=LkMIIx7lOq_lvJvVS3BrgWGuWl7Pi91-lA-O8x_gZs
216
216
  pyglove/ext/scalars/randoms_test.py,sha256=nEhiqarg8l_5EOucp59CYrpO2uKxS1pe0hmBdZUzRNM,2000
217
217
  pyglove/ext/scalars/step_wise.py,sha256=IDw3tuTpv0KVh7AN44W43zqm1-E0HWPUlytWOQC9w3Y,3789
218
218
  pyglove/ext/scalars/step_wise_test.py,sha256=TL1vJ19xVx2t5HKuyIzGoogF7N3Rm8YhLE6JF7i0iy8,2540
219
- pyglove-0.5.0.dev202509100809.dist-info/licenses/LICENSE,sha256=WNHhf_5RCaeuKWyq_K39vmp9F28LxKsB4SpomwSZ2L0,11357
220
- pyglove-0.5.0.dev202509100809.dist-info/METADATA,sha256=1F0uzEXLoiYGE7RVoGYlMQVGIH56hEEIRwlLr6Dq1zk,7089
221
- pyglove-0.5.0.dev202509100809.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
222
- pyglove-0.5.0.dev202509100809.dist-info/top_level.txt,sha256=wITzJSKcj8GZUkbq-MvUQnFadkiuAv_qv5qQMw0fIow,8
223
- pyglove-0.5.0.dev202509100809.dist-info/RECORD,,
219
+ pyglove-0.5.0.dev202509120809.dist-info/licenses/LICENSE,sha256=WNHhf_5RCaeuKWyq_K39vmp9F28LxKsB4SpomwSZ2L0,11357
220
+ pyglove-0.5.0.dev202509120809.dist-info/METADATA,sha256=zdKeS_0gKQBWo2LJ6QD7EEV62BOIdy7Scb0dWSO7iU4,7089
221
+ pyglove-0.5.0.dev202509120809.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
222
+ pyglove-0.5.0.dev202509120809.dist-info/top_level.txt,sha256=wITzJSKcj8GZUkbq-MvUQnFadkiuAv_qv5qQMw0fIow,8
223
+ pyglove-0.5.0.dev202509120809.dist-info/RECORD,,