Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: speed up creating Variant objects #144

Merged
merged 4 commits into from
Nov 3, 2022
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
2 changes: 1 addition & 1 deletion src/dbus_fast/signature.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -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
24 changes: 10 additions & 14 deletions src/dbus_fast/signature.py
Original file line number Diff line number Diff line change
Expand Up @@ -405,31 +405,27 @@ 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:
self.value = value
if verify:
if signature_tree 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
self.type.verify(value)

def __eq__(self, other: Any) -> bool:
if type(other) is Variant:
Expand Down
11 changes: 11 additions & 0 deletions tests/test_signature.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")