fix some test_weakref tests to not rely on ref-counting (closes #22267)
merge 3.4 (#22267)
allow test to work on implementations not using ref-counting (closes #22265)
merge 3.4 (#22265)