File size: 1,939 Bytes
ff505fe
 
8e1f5bc
 
ff505fe
 
 
 
 
3e6ab4d
1db4430
ff505fe
8e1f5bc
ff505fe
 
1db4430
ff505fe
 
 
 
 
 
 
1db4430
8e1f5bc
 
ff505fe
 
c9dd489
 
1db4430
c9dd489
 
 
1db4430
c9dd489
 
 
 
 
 
8e1f5bc
c9dd489
1db4430
c9dd489
 
 
1db4430
c9dd489
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
from __future__ import annotations

import os
from pathlib import Path
import asyncio.base_events as base_events
from typing import Any


def patch_asyncio_cleanup_warning() -> None:
    """Patches asyncio EventLoop __del__ method to ignore harmless file descriptor cleanup warnings in notebook/interactive runs."""
    # Skip patching when the runtime does not expose the cleanup hook.
    original_del = getattr(base_events.BaseEventLoop, "__del__", None)
    if original_del is None or getattr(original_del, "_innerspace_patched", False):
        return

    # Preserve normal cleanup while ignoring the known invalid-fd warning.
    def patched_del(self: Any) -> None:
        try:
            original_del(self)
        except ValueError as exc:
            if str(exc) != "Invalid file descriptor: -1":
                raise

    # Mark the patched function so repeated imports stay idempotent.
    setattr(patched_del, "_innerspace_patched", True)
    setattr(base_events.BaseEventLoop, "__del__", patched_del)


def load_env() -> None:
    """Loads environment variables from .env if present in the current or parent directory."""
    # Search the project folder before its parent workspace.
    for path in [Path(".env"), Path("../.env")]:
        if path.is_file():
            try:
                # Parse simple KEY=value lines without adding a dependency.
                with open(path, encoding="utf-8") as f:
                    for line in f:
                        line = line.strip()
                        if line and not line.startswith("#") and "=" in line:
                            k, v = line.split("=", 1)
                            os.environ.setdefault(k.strip(), v.strip().strip("'\""))
                break
            except Exception:
                # Ignore malformed local env files during app startup.
                pass


# Load local secrets before model or Modal clients are used.
load_env()