From 55951c3025cf088487fdd91af800b4e92ae8f892 Mon Sep 17 00:00:00 2001 From: rdb Date: Tue, 2 Jun 2020 20:23:38 +0200 Subject: [PATCH] pgraph: fix has_tags() after clearing Python tags Fixes #936 --- panda/src/pgraph/pandaNode_ext.cxx | 6 ++++++ tests/pgraph/test_nodepath.py | 29 +++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/panda/src/pgraph/pandaNode_ext.cxx b/panda/src/pgraph/pandaNode_ext.cxx index bc6427b1d0..d4071ff4c6 100644 --- a/panda/src/pgraph/pandaNode_ext.cxx +++ b/panda/src/pgraph/pandaNode_ext.cxx @@ -165,6 +165,12 @@ clear_python_tag(PyObject *key) { if (PyDict_GetItem(dict, key) != nullptr) { PyDict_DelItem(dict, key); } + + if (PyDict_Size(dict) == 0 && Py_REFCNT(dict) == 1) { + // This was the last tag, and do_get_python_tags() made sure we have a + // unique reference to the tags, so clear the tag object. + _this->_python_tag_data.clear(); + } } /** diff --git a/tests/pgraph/test_nodepath.py b/tests/pgraph/test_nodepath.py index 7dcbd2662b..43e43313e1 100644 --- a/tests/pgraph/test_nodepath.py +++ b/tests/pgraph/test_nodepath.py @@ -151,6 +151,35 @@ def test_nodepath_python_tags(): assert rc1 == rc2 +def test_nodepath_clear_python_tag(): + from panda3d.core import NodePath + + path = NodePath("node") + assert not path.has_python_tag("a") + assert not path.has_python_tag("b") + assert not path.node().has_tags() + + path.set_python_tag("a", "value") + assert path.has_python_tag("a") + assert not path.has_python_tag("b") + assert path.node().has_tags() + + path.set_python_tag("b", "value") + assert path.has_python_tag("a") + assert path.has_python_tag("b") + assert path.node().has_tags() + + path.clear_python_tag("a") + assert not path.has_python_tag("a") + assert path.has_python_tag("b") + assert path.node().has_tags() + + path.clear_python_tag("b") + assert not path.has_python_tag("a") + assert not path.has_python_tag("b") + assert not path.node().has_tags() + + def test_nodepath_replace_texture(): from panda3d.core import NodePath, Texture