view tests/test-demandimport.py @ 44763:94f4f2ec7dee stable

packaging: support building Inno installer with PyOxidizer We want to start distributing Mercurial on Python 3 on Windows. PyOxidizer will be our vehicle for achieving that. This commit implements basic support for producing Inno installers using PyOxidizer. While it is an eventual goal of PyOxidizer to produce installers, those features aren't yet implemented. So our strategy for producing Mercurial installers is similar to what we've been doing with py2exe: invoke a build system to produce files then stage those files into a directory so they can be turned into an installer. We had to make significant alterations to the pyoxidizer.bzl config file to get it to produce the files that we desire for a Windows install. This meant differentiating the build targets so we can target Windows specifically. We've added a new module to hgpackaging to deal with interacting with PyOxidizer. It is similar to pyexe: we invoke a build process then copy files to a staging directory. Ideally these extra files would be defined in pyoxidizer.bzl. But I don't think it is worth doing at this time, as PyOxidizer's config files are lacking some features to make this turnkey. The rest of the change is introducing a variant of the Inno installer code that invokes PyOxidizer instead of py2exe. Comparing the Python 2.7 based Inno installers with this one, the following changes were observed: * No lib/*.{pyd, dll} files * No Microsoft.VC90.CRT.manifest * No msvc{m,p,r}90.dll files * python27.dll replaced with python37.dll * Add vcruntime140.dll file The disappearance of the .pyd and .dll files is acceptable, as PyOxidizer has embedded these in hg.exe and loads them from memory. The disappearance of the *90* files is acceptable because those provide the Visual C++ 9 runtime, as required by Python 2.7. Similarly, the appearance of vcruntime140.dll is a requirement of Python 3.7. Differential Revision: https://phab.mercurial-scm.org/D8473
author Gregory Szorc <gregory.szorc@gmail.com>
date Thu, 23 Apr 2020 18:06:02 -0700
parents f81c17ec303c
children fc8299319ffe
line wrap: on
line source

from __future__ import absolute_import, print_function

from mercurial import demandimport

demandimport.enable()

import os
import subprocess
import sys
import types

# Don't import pycompat because it has too many side-effects.
ispy3 = sys.version_info[0] >= 3

# Only run if demandimport is allowed
if subprocess.call(
    ['python', '%s/hghave' % os.environ['TESTDIR'], 'demandimport']
):
    sys.exit(80)

# We rely on assert, which gets optimized out.
if sys.flags.optimize:
    sys.exit(80)

# The demand importer doesn't work on Python 3.5.
if sys.version_info[0:2] == (3, 5):
    sys.exit(80)

if ispy3:
    from importlib.util import _LazyModule

    try:
        from importlib.util import _Module as moduletype
    except ImportError:
        moduletype = types.ModuleType
else:
    moduletype = types.ModuleType

if os.name != 'nt':
    try:
        import distutils.msvc9compiler

        print(
            'distutils.msvc9compiler needs to be an immediate '
            'importerror on non-windows platforms'
        )
        distutils.msvc9compiler
    except ImportError:
        pass

import re

rsub = re.sub


def f(obj):
    l = repr(obj)
    l = rsub("0x[0-9a-fA-F]+", "0x?", l)
    l = rsub("from '.*'", "from '?'", l)
    l = rsub("'<[a-z]*>'", "'<whatever>'", l)
    return l


demandimport.disable()
os.environ['HGDEMANDIMPORT'] = 'disable'
# this enable call should not actually enable demandimport!
demandimport.enable()
from mercurial import node

# We use assert instead of a unittest test case because having imports inside
# functions changes behavior of the demand importer.
if ispy3:
    assert not isinstance(node, _LazyModule)
else:
    assert f(node) == "<module 'mercurial.node' from '?'>", f(node)

# now enable it for real
del os.environ['HGDEMANDIMPORT']
demandimport.enable()

# Test access to special attributes through demandmod proxy
assert 'mercurial.error' not in sys.modules
from mercurial import error as errorproxy

if ispy3:
    # unsure why this isn't lazy.
    assert not isinstance(f, _LazyModule)
    assert f(errorproxy) == "<module 'mercurial.error' from '?'>", f(errorproxy)
else:
    assert f(errorproxy) == "<unloaded module 'error'>", f(errorproxy)

doc = ' '.join(errorproxy.__doc__.split()[:3])
assert doc == 'Mercurial exceptions. This', doc
assert errorproxy.__name__ == 'mercurial.error', errorproxy.__name__

# __name__ must be accessible via __dict__ so the relative imports can be
# resolved
name = errorproxy.__dict__['__name__']
assert name == 'mercurial.error', name

if ispy3:
    assert not isinstance(errorproxy, _LazyModule)
    assert f(errorproxy) == "<module 'mercurial.error' from '?'>", f(errorproxy)
else:
    assert f(errorproxy) == "<proxied module 'error'>", f(errorproxy)

import os

if ispy3:
    assert not isinstance(os, _LazyModule)
    assert f(os) == "<module 'os' from '?'>", f(os)
else:
    assert f(os) == "<unloaded module 'os'>", f(os)

assert f(os.system) == '<built-in function system>', f(os.system)
assert f(os) == "<module 'os' from '?'>", f(os)

assert 'mercurial.utils.procutil' not in sys.modules
from mercurial.utils import procutil

if ispy3:
    assert isinstance(procutil, _LazyModule)
    assert f(procutil) == "<module 'mercurial.utils.procutil' from '?'>", f(
        procutil
    )
else:
    assert f(procutil) == "<unloaded module 'procutil'>", f(procutil)

assert f(procutil.system) == '<function system at 0x?>', f(procutil.system)
assert procutil.__class__ == moduletype, procutil.__class__
assert f(procutil) == "<module 'mercurial.utils.procutil' from '?'>", f(
    procutil
)
assert f(procutil.system) == '<function system at 0x?>', f(procutil.system)

assert 'mercurial.hgweb' not in sys.modules
from mercurial import hgweb

if ispy3:
    assert isinstance(hgweb, _LazyModule)
    assert f(hgweb) == "<module 'mercurial.hgweb' from '?'>", f(hgweb)
    assert isinstance(hgweb.hgweb_mod, _LazyModule)
    assert (
        f(hgweb.hgweb_mod) == "<module 'mercurial.hgweb.hgweb_mod' from '?'>"
    ), f(hgweb.hgweb_mod)
else:
    assert f(hgweb) == "<unloaded module 'hgweb'>", f(hgweb)
    assert f(hgweb.hgweb_mod) == "<unloaded module 'hgweb_mod'>", f(
        hgweb.hgweb_mod
    )

assert f(hgweb) == "<module 'mercurial.hgweb' from '?'>", f(hgweb)

import re as fred

if ispy3:
    assert not isinstance(fred, _LazyModule)
    assert f(fred) == "<module 're' from '?'>"
else:
    assert f(fred) == "<unloaded module 're'>", f(fred)

import re as remod

if ispy3:
    assert not isinstance(remod, _LazyModule)
    assert f(remod) == "<module 're' from '?'>"
else:
    assert f(remod) == "<unloaded module 're'>", f(remod)

import sys as re

if ispy3:
    assert not isinstance(re, _LazyModule)
    assert f(re) == "<module 'sys' (built-in)>"
else:
    assert f(re) == "<unloaded module 'sys'>", f(re)

if ispy3:
    assert not isinstance(fred, _LazyModule)
    assert f(fred) == "<module 're' from '?'>", f(fred)
else:
    assert f(fred) == "<unloaded module 're'>", f(fred)

assert f(fred.sub) == '<function sub at 0x?>', f(fred.sub)

if ispy3:
    assert not isinstance(fred, _LazyModule)
    assert f(fred) == "<module 're' from '?'>", f(fred)
else:
    assert f(fred) == "<proxied module 're'>", f(fred)

remod.escape  # use remod
assert f(remod) == "<module 're' from '?'>", f(remod)

if ispy3:
    assert not isinstance(re, _LazyModule)
    assert f(re) == "<module 'sys' (built-in)>"
    assert f(type(re.stderr)) == "<class '_io.TextIOWrapper'>", f(
        type(re.stderr)
    )
    assert f(re) == "<module 'sys' (built-in)>"
else:
    assert f(re) == "<unloaded module 'sys'>", f(re)
    assert f(re.stderr) == "<open file '<whatever>', mode 'w' at 0x?>", f(
        re.stderr
    )
    assert f(re) == "<proxied module 'sys'>", f(re)

assert 'telnetlib' not in sys.modules
import telnetlib

if ispy3:
    assert isinstance(telnetlib, _LazyModule)
    assert f(telnetlib) == "<module 'telnetlib' from '?'>"
else:
    assert f(telnetlib) == "<unloaded module 'telnetlib'>", f(telnetlib)

try:
    from telnetlib import unknownattr

    assert False, (
        'no demandmod should be created for attribute of non-package '
        'module:\ntelnetlib.unknownattr = %s' % f(unknownattr)
    )
except ImportError as inst:
    assert rsub(r"'", '', str(inst)).startswith(
        'cannot import name unknownattr'
    )

from mercurial import util

# Unlike the import statement, __import__() function should not raise
# ImportError even if fromlist has an unknown item
# (see Python/import.c:import_module_level() and ensure_fromlist())
assert 'zipfile' not in sys.modules
zipfileimp = __import__('zipfile', globals(), locals(), ['unknownattr'])
assert f(zipfileimp) == "<module 'zipfile' from '?'>", f(zipfileimp)
assert not util.safehasattr(zipfileimp, 'unknownattr')