compat.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. """Compatibility hacks and helpers."""
  2. import contextlib
  3. import sys
  4. async def windows_hot_reload_lifespan_hack():
  5. """[REF-3164] A hack to fix hot reload on Windows.
  6. Uvicorn has an issue stopping itself on Windows after detecting changes in
  7. the filesystem.
  8. This workaround repeatedly prints and flushes null characters to stderr,
  9. which seems to allow the uvicorn server to exit when the CTRL-C signal is
  10. sent from the reloader process.
  11. Don't ask me why this works, I discovered it by accident - masenf.
  12. """
  13. import asyncio
  14. import sys
  15. while True:
  16. sys.stderr.write("\0")
  17. sys.stderr.flush()
  18. await asyncio.sleep(0.5)
  19. @contextlib.contextmanager
  20. def pydantic_v1_patch():
  21. """A context manager that patches the Pydantic module to mimic v1 behaviour.
  22. Yields:
  23. None when the Pydantic module is patched.
  24. """
  25. patched_modules = [
  26. "pydantic",
  27. "pydantic.fields",
  28. "pydantic.errors",
  29. "pydantic.main",
  30. ]
  31. originals = {module: sys.modules.get(module) for module in patched_modules}
  32. try:
  33. import pydantic.v1 # type: ignore
  34. if pydantic.__version__.startswith("1."):
  35. # pydantic v1 is already installed
  36. yield
  37. return
  38. sys.modules["pydantic.fields"] = pydantic.v1.fields # type: ignore
  39. sys.modules["pydantic.main"] = pydantic.v1.main # type: ignore
  40. sys.modules["pydantic.errors"] = pydantic.v1.errors # type: ignore
  41. sys.modules["pydantic"] = pydantic.v1
  42. yield
  43. except (ImportError, AttributeError):
  44. # pydantic v1 is already installed
  45. yield
  46. finally:
  47. # Restore the original Pydantic module
  48. for k, original in originals.items():
  49. if k in sys.modules:
  50. if original:
  51. sys.modules[k] = original
  52. else:
  53. del sys.modules[k]
  54. with pydantic_v1_patch():
  55. import sqlmodel as sqlmodel