Fix tests

This fixes most of the tests in PY3. However resource warnings and increased refcounts are not fixed in this PR.