Returns the path to the flatc executable, caching the result.
()
| 259 | |
| 260 | |
| 261 | def _get_flatc_path() -> str: |
| 262 | """Returns the path to the flatc executable, caching the result.""" |
| 263 | global _flatc_cached_path, _flatc_exit_stack |
| 264 | # Double-checked locking: fast path avoids the lock once cached. |
| 265 | if _flatc_cached_path is not None: |
| 266 | return _flatc_cached_path |
| 267 | |
| 268 | with _flatc_lock: |
| 269 | if _flatc_cached_path is not None: |
| 270 | return _flatc_cached_path |
| 271 | |
| 272 | flatc_resource = importlib.resources.files(__package__).joinpath( |
| 273 | _FLATC_RESOURCE_NAME |
| 274 | ) |
| 275 | if flatc_resource.is_file(): |
| 276 | exit_stack = contextlib.ExitStack() |
| 277 | flatc_path = exit_stack.enter_context( |
| 278 | importlib.resources.as_file(flatc_resource) |
| 279 | ) |
| 280 | try: |
| 281 | current_mode = flatc_path.stat().st_mode |
| 282 | if not (current_mode & stat.S_IXUSR): |
| 283 | flatc_path.chmod( |
| 284 | current_mode | stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH |
| 285 | ) |
| 286 | except OSError: |
| 287 | pass |
| 288 | _flatc_exit_stack = exit_stack |
| 289 | # Clean up the extracted temp file on normal process exit. |
| 290 | atexit.register(exit_stack.close) |
| 291 | _flatc_cached_path = str(flatc_path) |
| 292 | else: |
| 293 | _flatc_cached_path = os.getenv("FLATC_EXECUTABLE", "flatc") |
| 294 | |
| 295 | return _flatc_cached_path |
| 296 | |
| 297 | |
| 298 | def _reset_flatc_cache_after_fork() -> None: |