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

use memoryview in py3 #3303

Merged
merged 2 commits into from
May 30, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions synapse/storage/keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from synapse.util.caches.descriptors import cachedInlineCallbacks

from twisted.internet import defer
import six

import OpenSSL
from signedjson.key import decode_verify_key_bytes
Expand All @@ -26,6 +27,13 @@

logger = logging.getLogger(__name__)

# py2 sqlite has buffer hardcoded as only binary type, so we must use it,
# despite being deprecated and removed in favor of memoryview
if six.PY2:
db_binary_type = buffer
else:
db_binary_type = memoryview


class KeyStore(SQLBaseStore):
"""Persistence for signature verification keys and tls X.509 certificates
Expand Down Expand Up @@ -72,7 +80,7 @@ def store_server_certificate(self, server_name, from_server, time_now_ms,
values={
"from_server": from_server,
"ts_added_ms": time_now_ms,
"tls_certificate": buffer(tls_certificate_bytes),
"tls_certificate": db_binary_type(tls_certificate_bytes),
},
desc="store_server_certificate",
)
Expand Down Expand Up @@ -135,7 +143,7 @@ def _txn(txn):
values={
"from_server": from_server,
"ts_added_ms": time_now_ms,
"verify_key": buffer(verify_key.encode()),
"verify_key": db_binary_type(verify_key.encode()),
},
)
txn.call_after(
Expand Down Expand Up @@ -172,7 +180,7 @@ def store_server_keys_json(self, server_name, key_id, from_server,
"from_server": from_server,
"ts_added_ms": ts_now_ms,
"ts_valid_until_ms": ts_expires_ms,
"key_json": buffer(key_json_bytes),
"key_json": db_binary_type(key_json_bytes),
},
desc="store_server_keys_json",
)
Expand Down
12 changes: 10 additions & 2 deletions synapse/storage/signatures.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,21 @@
# limitations under the License.

from twisted.internet import defer
import six

from ._base import SQLBaseStore

from unpaddedbase64 import encode_base64
from synapse.crypto.event_signing import compute_event_reference_hash
from synapse.util.caches.descriptors import cached, cachedList

# py2 sqlite has buffer hardcoded as only binary type, so we must use it,
# despite being deprecated and removed in favor of memoryview
if six.PY2:
db_binary_type = buffer
else:
db_binary_type = memoryview


class SignatureWorkerStore(SQLBaseStore):
@cached()
Expand Down Expand Up @@ -56,7 +64,7 @@ def add_event_hashes(self, event_ids):
for e_id, h in hashes.items()
}

defer.returnValue(hashes.items())
defer.returnValue(list(hashes.items()))

def _get_event_reference_hashes_txn(self, txn, event_id):
"""Get all the hashes for a given PDU.
Expand Down Expand Up @@ -91,7 +99,7 @@ def _store_event_reference_hashes_txn(self, txn, events):
vals.append({
"event_id": event.event_id,
"algorithm": ref_alg,
"hash": buffer(ref_hash_bytes),
"hash": db_binary_type(ref_hash_bytes),
})

self._simple_insert_many_txn(
Expand Down
10 changes: 9 additions & 1 deletion synapse/storage/transactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from synapse.util.caches.descriptors import cached

from twisted.internet import defer
import six

from canonicaljson import encode_canonical_json

Expand All @@ -25,6 +26,13 @@
import logging
import simplejson as json

# py2 sqlite has buffer hardcoded as only binary type, so we must use it,
# despite being deprecated and removed in favor of memoryview
if six.PY2:
db_binary_type = buffer
else:
db_binary_type = memoryview

logger = logging.getLogger(__name__)


Expand Down Expand Up @@ -110,7 +118,7 @@ def set_received_txn_response(self, transaction_id, origin, code,
"transaction_id": transaction_id,
"origin": origin,
"response_code": code,
"response_json": buffer(encode_canonical_json(response_dict)),
"response_json": db_binary_type(encode_canonical_json(response_dict)),
"ts": self._clock.time_msec(),
},
or_ignore=True,
Expand Down