Fix some py3k compatibility issues.
Avoid clash with Python threading variable.