From de13ace4e4fd7704d0b343526715ffc331440e14 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Thu, 3 Nov 2022 20:40:20 +0100 Subject: [PATCH 1/3] feat: speed up creating Variant objects This is only a 1.5% speed up but we create a lot of these --- src/dbus_fast/signature.pxd | 2 +- src/dbus_fast/signature.py | 29 +++++++++++++---------------- 2 files changed, 14 insertions(+), 17 deletions(-) diff --git a/src/dbus_fast/signature.pxd b/src/dbus_fast/signature.pxd index 3202de65..a37e2110 100644 --- a/src/dbus_fast/signature.pxd +++ b/src/dbus_fast/signature.pxd @@ -19,5 +19,5 @@ cdef class SignatureTree: cdef class Variant: cdef public object type - cdef public str signature + cdef public object signature cdef public object value diff --git a/src/dbus_fast/signature.py b/src/dbus_fast/signature.py index a282210d..2c79ec6b 100644 --- a/src/dbus_fast/signature.py +++ b/src/dbus_fast/signature.py @@ -405,31 +405,28 @@ def __init__( """Init a new Variant.""" if type(signature) is SignatureTree: signature_tree = signature + self.signature = signature_tree.signature + self.type = signature_tree.types[0] elif type(signature) is SignatureType: - signature_type = signature - signature_str = signature.signature signature_tree = None + self.signature = signature.signature + self.type = signature elif type(signature) is str: signature_tree = get_signature_tree(signature) + self.signature = signature + self.type = signature_tree.types[0] else: raise TypeError( "signature must be a SignatureTree, SignatureType, or a string" ) - - if signature_tree: - if verify and len(signature_tree.types) != 1: - raise ValueError( - "variants must have a signature for a single complete type" - ) - signature_str = signature_tree.signature - signature_type = signature_tree.types[0] - - if verify: - signature_type.verify(value) - - self.type = signature_type - self.signature = signature_str self.value = value + if verify: + if signature_tree: + if len(signature_tree.types) != 1: + raise ValueError( + "variants must have a signature for a single complete type" + ) + self.type.verify(value) def __eq__(self, other: Any) -> bool: if type(other) is Variant: From 87dd4dbb07dc9f464793d955f5bfc7cff336d7f5 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Thu, 3 Nov 2022 21:09:05 +0100 Subject: [PATCH 2/3] feat: add missing cover --- tests/test_signature.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/tests/test_signature.py b/tests/test_signature.py index 6eab88e3..c8d7fe09 100644 --- a/tests/test_signature.py +++ b/tests/test_signature.py @@ -220,3 +220,14 @@ def test_invalid_variants(): with pytest.raises(SignatureBodyMismatchError): tree.verify([con]) + + +def test_variant_signature_type(): + tree = SignatureTree("as") + var = Variant(tree.types[0], ["foo", "bar"]) + assert var.type == tree.types[0] + assert var.value == ["foo", "bar"] + assert var.signature == "as" + + with pytest.raises(SignatureBodyMismatchError): + Variant(tree.types[0], "wrong") From 3d87051ddd00a02108e3873884cdfad51348776e Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Thu, 3 Nov 2022 21:40:06 +0100 Subject: [PATCH 3/3] fix: cleanup --- src/dbus_fast/signature.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/dbus_fast/signature.py b/src/dbus_fast/signature.py index 2c79ec6b..1c7040ac 100644 --- a/src/dbus_fast/signature.py +++ b/src/dbus_fast/signature.py @@ -421,11 +421,10 @@ def __init__( ) self.value = value if verify: - if signature_tree: - if len(signature_tree.types) != 1: - raise ValueError( - "variants must have a signature for a single complete type" - ) + if signature_tree and len(signature_tree.types) != 1: + raise ValueError( + "variants must have a signature for a single complete type" + ) self.type.verify(value) def __eq__(self, other: Any) -> bool: