Files
synapse/synapse/__init__.py
T
Eric Eastwood 5676ce4ab9 Remove tox.ini (#20410)
As far as I can tell, no one uses it. @reivilibre also asked this in
[`#synapse-dev:matrix.org`](https://matrix.to/#/!i5D5LLct_DYG-4hQprLzrxdbZ580U9UB6AEgFnk6rZQ/$NrW3QmLEGgcjNqW-SaPZrS-EVwq1-9fO_Ds_1QIZX0E?via=element.io&via=matrix.org&via=starstruck.systems)
and
[`#element-backend-internal:matrix.org`](https://matrix.to/#/!SGNQGPGUwtcPBUotTL:matrix.org/$_rh3S79z4q1Q93SJ3ncQ_ZUzudks4Egan5dwutH2AKg?via=jki.re&via=element.io&via=matrix.org)
on 2026-02-20 with a couple of 🔥 emoji reactions in the public
channel.

As long as I've worked on Synapse, as far as I know, I've never used
this for local dev.

We no longer use this in CI.

Noticed the file again while working on
https://github.com/element-hq/synapse/pull/20359
2026-10-27 09:34:38 -05:00

108 lines
3.4 KiB
Python

#
# This file is licensed under the Affero General Public License (AGPL) version 3.
#
# Copyright 2023 The Matrix.org Foundation C.I.C.
# Copyright 2014-2016 OpenMarket Ltd
# Copyright (C) 2023 New Vector, Ltd
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# See the GNU Affero General Public License for more details:
# <https://www.gnu.org/licenses/agpl-3.0.html>.
#
# Originally licensed under the Apache License, Version 2.0:
# <http://www.apache.org/licenses/LICENSE-2.0>.
#
# [This file includes modifications made by New Vector Limited]
#
#
"""This is an implementation of a Matrix homeserver."""
import os
import sys
from typing import Any
from PIL import ImageFile
from synapse.util.rust import check_rust_lib_up_to_date
from synapse.util.stringutils import strtobool
# Allow truncated JPEG images to be thumbnailed.
ImageFile.LOAD_TRUNCATED_IMAGES = True
# Check that we're not running on an unsupported Python version.
#
# Note that we use an (unneeded) variable here so that pyupgrade doesn't nuke the
# if-statement completely.
py_version = sys.version_info
if py_version < (3, 11):
print("Synapse requires Python 3.11 or above.")
sys.exit(1)
# Allow using the asyncio reactor via env var.
if strtobool(os.environ.get("SYNAPSE_ASYNC_IO_REACTOR", "0")):
import asyncio
from twisted.internet import asyncioreactor
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
asyncioreactor.install(loop)
# Twisted and canonicaljson will fail to import when this file is executed to
# get the __version__ during a fresh install. That's OK and subsequent calls to
# actually start Synapse will import these libraries fine.
try:
from twisted.internet import protocol
from twisted.internet.protocol import Factory
from twisted.names.dns import DNSDatagramProtocol
protocol.Factory.noisy = False
Factory.noisy = False
DNSDatagramProtocol.noisy = False
except ImportError:
pass
# Teach canonicaljson how to serialise immutabledicts and JsonObjects.
try:
from canonicaljson import register_preserialisation_callback
from immutabledict import immutabledict
def _immutabledict_cb(d: immutabledict) -> dict[str, Any]:
try:
return d._dict
except Exception:
# Paranoia: fall back to a `dict()` call, in case a future version of
# immutabledict removes `_dict` from the implementation.
return dict(d)
register_preserialisation_callback(immutabledict, _immutabledict_cb)
# Teach canonicaljson how to serialise JsonObjects, which is just to
# convert them to dicts.
from synapse.synapse_rust.events import JsonObject # noqa: E402
register_preserialisation_callback(JsonObject, lambda obj: dict(obj))
except ImportError:
pass
import synapse.util # noqa: E402
__version__ = synapse.util.SYNAPSE_VERSION
# Introduced for usage with SyTest to check that log contexts are correctly handled when
# running a real homeserver. (our own unit tests apply the same patch unconditionally,
# in `tests/__init__.py`.)
if bool(os.environ.get("SYNAPSE_TEST_PATCH_LOG_CONTEXTS", False)):
from synapse.util.patch_inline_callbacks import do_patch
do_patch()
check_rust_lib_up_to_date()