compat.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. import pydantic
  26. if pydantic.__version__.startswith("1."):
  27. # pydantic v1 is already installed
  28. yield
  29. return
  30. patched_modules = [
  31. "pydantic",
  32. "pydantic.fields",
  33. "pydantic.errors",
  34. "pydantic.main",
  35. ]
  36. originals = {module: sys.modules.get(module) for module in patched_modules}
  37. try:
  38. import pydantic.v1 # type: ignore
  39. sys.modules["pydantic.fields"] = pydantic.v1.fields # type: ignore
  40. sys.modules["pydantic.main"] = pydantic.v1.main # type: ignore
  41. sys.modules["pydantic.errors"] = pydantic.v1.errors # type: ignore
  42. sys.modules["pydantic"] = pydantic.v1
  43. yield
  44. except (ImportError, AttributeError):
  45. # pydantic v1 is already installed
  46. yield
  47. finally:
  48. # Restore the original Pydantic module
  49. for k, original in originals.items():
  50. if k in sys.modules:
  51. if original:
  52. sys.modules[k] = original
  53. else:
  54. del sys.modules[k]
  55. with pydantic_v1_patch():
  56. import sqlmodel as sqlmodel