py3: conditionalize test-demandimport.py for Python 3

Authored by indygreg.

Description

py3: conditionalize test-demandimport.py for Python 3

The Python 3 lazy importer uses the LazyLoader that is part of
importlib.

On Python 3 and later, LazyLoader is implemented using a custom module
type that defines a getattribute which triggers module loading.

Furthermore, there are additional differences as well. For example,
it appears that Python 3 will return an existing sys.modules
entry instead of constructing a new module object.

This commit adds additional test coverage for lazy importing
behavior to cover the differences between Python 2 and 3. This
reveals that the test and some lazy import functionality is kinda
busted. For example, the test assumes "contextlib" will be lazy.
But in reality an import before it has already imported contextlib!

There's definitely room to improve the behavior of the demand
importer code, both for Python 2 and 3. But at least the test
passes on Python 3 now.

Differential Revision: https://phab.mercurial-scm.org/D5796

Details