From f4f1b281eceb9f9c8e4724b4edb5717d3ccf1bd6 Mon Sep 17 00:00:00 2001 From: Richard Kuesters Date: Tue, 9 Oct 2018 16:23:03 -0300 Subject: [PATCH] wip; fixed bug on first node of each tree not propagating the no_conflict parameter --- CHANGELOG.rst | 6 ++++++ Makefile | 2 ++ src/xrtr.pyx | 16 +++++++--------- tests/test_xrtr.py | 20 ++++++++++++++++++++ 4 files changed, 35 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 0b3dc6d..4ee5754 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -2,6 +2,12 @@ Changelog ========= +v0.2.1 on 2018-10-09 +-------------------- + +* Fixed bug where the ``no_conflict`` flag where not being propagated to the ``add_method`` if a "non-conflicting method" was the first node being created in that tree. + + v0.2.0 on 2018-10-03 -------------------- diff --git a/Makefile b/Makefile index 36edc1a..e3639fc 100644 --- a/Makefile +++ b/Makefile @@ -33,6 +33,7 @@ clean: cleanpycache rm -rf ./src/*.egg-info rm -rf ./src/*.c rm -rf ./src/*.so + rm -rf ./wheelhouse test: clean python setup.py build_ext --force --inplace @@ -46,6 +47,7 @@ requirements-dev: pip-sync requirements-dev.txt docker-build: clean + mkdir ./wheelhouse python setup.py clean --all sdist docker pull quay.io/pypa/manylinux1_x86_64:latest docker pull quay.io/pypa/manylinux1_i686:latest diff --git a/src/xrtr.pyx b/src/xrtr.pyx index 9fb191f..e6498d6 100644 --- a/src/xrtr.pyx +++ b/src/xrtr.pyx @@ -34,7 +34,7 @@ cdef class RadixTreeNode: readonly bint index_zero_is_variable readonly bint index_zero_is_glob - def __cinit__(self, str path=None, object handler=None, list methods=None): + def __cinit__(self, str path=None, object handler=None, list methods=None, bint no_conflict=False): if path is None: self.path_len = 0 else: @@ -49,7 +49,8 @@ cdef class RadixTreeNode: self.index_zero_is_variable = 0 self.index_zero_is_glob = 0 - self.add_methods(methods, handler) + if handler is not None and methods is not None: + self.add_methods(methods, handler, no_conflict) def __repr__(self): return ( @@ -225,11 +226,8 @@ cdef class RadixTree: int i = 0 int n = len(path) int code = 0 - int j - int p - int m - RadixTreeNode root - RadixTreeNode child + int j, p, m + RadixTreeNode root, child root = self.root @@ -251,11 +249,11 @@ cdef class RadixTree: if p == n: p = _get_position(path.find(GLOB, i), n) if p == n: - root.insert_child(path[i], RadixTreeNode(path[i:], handler, methods)) + root.insert_child(path[i], RadixTreeNode(path[i:], handler, methods, no_conflict)) return code, None root = root.insert_child(path[i], RadixTreeNode(path[i:p])) - root.insert_child(GLOB, RadixTreeNode(path[p + 1:], handler, methods)) + root.insert_child(GLOB, RadixTreeNode(path[p + 1:], handler, methods, no_conflict)) return code, None root = root.insert_child(path[i], RadixTreeNode(path[i:p])) diff --git a/tests/test_xrtr.py b/tests/test_xrtr.py index 8b173d5..a0e992b 100644 --- a/tests/test_xrtr.py +++ b/tests/test_xrtr.py @@ -345,3 +345,23 @@ def test_duplicate_parameters(endpoint_factory): with pytest.raises(ValueError): another_tree.insert("|hello|.bar|world|*bar", endpoint_1, ["BAR"]) + + +def test_tree_middleware_first(endpoint_factory, middleware_factory): + endpoint_1 = endpoint_factory(1) + middleware_1 = middleware_factory(1) + + tree = RadixTree() + + tree.insert("/hello", middleware_1, ["BAR"], no_conflict=True) + tree.insert("/hello/world", endpoint_1, ["BAR"]) + + r = tree.get("/hello", "BAR") + assert r[0] is None + assert r[1] == [] + assert r[2] == {} + + r = tree.get("/hello/world", "BAR") + assert r[0] == endpoint_1 + assert r[1] == [middleware_1] + assert r[2] == {}