test_sys refcount test checks that assigning None to a local variable n increases the references to None by exactly 1. However sys.settrace is set, then the frame object must be instantiated to be passed to the trace object. This increments the reference count to None again. Since the locals are not then removed from the frame object after the sys.settrace call, the number of references remains increased after the settrace function is exited. This problem can be avoided by making n a global.