Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.

Commit 743325a

Browse files
ShadowJonathanHalf-Shot
authored andcommitted
Add experimental support for PyPy. (#9123)
* Adds proper dependencies. * Minor fixes in database layer.
1 parent 6dd4e71 commit 743325a

File tree

4 files changed

+15
-8
lines changed

4 files changed

+15
-8
lines changed

changelog.d/9123.misc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add experimental support for running Synapse with PyPy.

synapse/python_dependencies.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,12 @@
8686

8787
CONDITIONAL_REQUIREMENTS = {
8888
"matrix-synapse-ldap3": ["matrix-synapse-ldap3>=0.1"],
89-
# we use execute_values with the fetch param, which arrived in psycopg 2.8.
90-
"postgres": ["psycopg2>=2.8"],
89+
"postgres": [
90+
# we use execute_values with the fetch param, which arrived in psycopg 2.8.
91+
"psycopg2>=2.8 ; platform_python_implementation != 'PyPy'",
92+
"psycopg2cffi>=2.8 ; platform_python_implementation == 'PyPy'",
93+
"psycopg2cffi-compat==1.1 ; platform_python_implementation == 'PyPy'",
94+
],
9195
# ACME support is required to provision TLS certificates from authorities
9296
# that use the protocol, such as Let's Encrypt.
9397
"acme": [

synapse/storage/engines/__init__.py

+2-6
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
15-
import platform
1615

1716
from ._base import BaseDatabaseEngine, IncorrectDatabaseSetup
1817
from .postgres import PostgresEngine
@@ -28,11 +27,8 @@ def create_engine(database_config) -> BaseDatabaseEngine:
2827
return Sqlite3Engine(sqlite3, database_config)
2928

3029
if name == "psycopg2":
31-
# pypy requires psycopg2cffi rather than psycopg2
32-
if platform.python_implementation() == "PyPy":
33-
import psycopg2cffi as psycopg2 # type: ignore
34-
else:
35-
import psycopg2 # type: ignore
30+
# Note that psycopg2cffi-compat provides the psycopg2 module on pypy.
31+
import psycopg2 # type: ignore
3632

3733
return PostgresEngine(psycopg2, database_config)
3834

synapse/storage/engines/sqlite.py

+6
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
15+
import platform
1516
import struct
1617
import threading
1718
import typing
@@ -30,6 +31,11 @@ def __init__(self, database_module, database_config):
3031
database = database_config.get("args", {}).get("database")
3132
self._is_in_memory = database in (None, ":memory:",)
3233

34+
if platform.python_implementation() == "PyPy":
35+
# pypy's sqlite3 module doesn't handle bytearrays, convert them
36+
# back to bytes.
37+
database_module.register_adapter(bytearray, lambda array: bytes(array))
38+
3339
# The current max state_group, or None if we haven't looked
3440
# in the DB yet.
3541
self._current_state_group_id = None

0 commit comments

Comments
 (0)