Fix some python3 incompatibility issues
Add a test case