tests/test-demandimport.py
author Raphaël Gomès <rgomes@octobus.net>
Thu, 21 Oct 2021 14:03:33 +0200
branchstable
changeset 47900 f3e218ae3fd9
parent 46226 0826d684a1b5
child 48966 6000f5b25c9b
permissions -rw-r--r--
heptapod-ci: actually give pytest more time before timeout `HGTEST_TIMEOUT` is overridden by `HGTEST_SLOWTIMEOUT` for tests marked as slow, which `test-check-pytype.t` is. So this whole time the timeout was 1500s (or 25 minutes), which is unfortunately not long enough for a *lot* of the times it's run on the CI. Differential Revision: https://phab.mercurial-scm.org/D11717
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
33936
eddca62d9e64 tests: ensure demandimport test uses absolute_import
Augie Fackler <raf@durin42.com>
parents: 32486
diff changeset
     1
from __future__ import absolute_import, print_function
28948
16390f4cccf0 py3: make test-demandimport use print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 27535
diff changeset
     2
4631
e3afa670e484 demandimport: fix issue579 and add a test
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
     3
from mercurial import demandimport
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41521
diff changeset
     4
4631
e3afa670e484 demandimport: fix issue579 and add a test
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
     5
demandimport.enable()
e3afa670e484 demandimport: fix issue579 and add a test
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
     6
23643
2205d00b6d2b demandimport: blacklist distutils.msvc9compiler (issue4475)
Augie Fackler <raf@durin42.com>
parents: 21025
diff changeset
     7
import os
29872
4b50e1f922a0 tests: skip demandimport if disabled
timeless <timeless@mozdev.org>
parents: 28948
diff changeset
     8
import subprocess
4b50e1f922a0 tests: skip demandimport if disabled
timeless <timeless@mozdev.org>
parents: 28948
diff changeset
     9
import sys
41520
3e89736b98ce py3: conditionalize test-demandimport.py for Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41519
diff changeset
    10
import types
3e89736b98ce py3: conditionalize test-demandimport.py for Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41519
diff changeset
    11
3e89736b98ce py3: conditionalize test-demandimport.py for Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41519
diff changeset
    12
# Don't import pycompat because it has too many side-effects.
3e89736b98ce py3: conditionalize test-demandimport.py for Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41519
diff changeset
    13
ispy3 = sys.version_info[0] >= 3
29872
4b50e1f922a0 tests: skip demandimport if disabled
timeless <timeless@mozdev.org>
parents: 28948
diff changeset
    14
4b50e1f922a0 tests: skip demandimport if disabled
timeless <timeless@mozdev.org>
parents: 28948
diff changeset
    15
# Only run if demandimport is allowed
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41521
diff changeset
    16
if subprocess.call(
46226
0826d684a1b5 test: replace a many occurence of `python` with `$PYTHON`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 45211
diff changeset
    17
    [os.environ['PYTHON'], '%s/hghave' % os.environ['TESTDIR'], 'demandimport']
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41521
diff changeset
    18
):
29872
4b50e1f922a0 tests: skip demandimport if disabled
timeless <timeless@mozdev.org>
parents: 28948
diff changeset
    19
    sys.exit(80)
4b50e1f922a0 tests: skip demandimport if disabled
timeless <timeless@mozdev.org>
parents: 28948
diff changeset
    20
41519
dffd6a301570 py3: replace print() with assert in test-demandimport.py
Gregory Szorc <gregory.szorc@gmail.com>
parents: 38008
diff changeset
    21
# We rely on assert, which gets optimized out.
dffd6a301570 py3: replace print() with assert in test-demandimport.py
Gregory Szorc <gregory.szorc@gmail.com>
parents: 38008
diff changeset
    22
if sys.flags.optimize:
dffd6a301570 py3: replace print() with assert in test-demandimport.py
Gregory Szorc <gregory.szorc@gmail.com>
parents: 38008
diff changeset
    23
    sys.exit(80)
dffd6a301570 py3: replace print() with assert in test-demandimport.py
Gregory Szorc <gregory.szorc@gmail.com>
parents: 38008
diff changeset
    24
44117
c5e0a9b97b8a hgdemandimport: disable on Python 3.5
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43076
diff changeset
    25
# The demand importer doesn't work on Python 3.5.
c5e0a9b97b8a hgdemandimport: disable on Python 3.5
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43076
diff changeset
    26
if sys.version_info[0:2] == (3, 5):
c5e0a9b97b8a hgdemandimport: disable on Python 3.5
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43076
diff changeset
    27
    sys.exit(80)
c5e0a9b97b8a hgdemandimport: disable on Python 3.5
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43076
diff changeset
    28
41520
3e89736b98ce py3: conditionalize test-demandimport.py for Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41519
diff changeset
    29
if ispy3:
3e89736b98ce py3: conditionalize test-demandimport.py for Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41519
diff changeset
    30
    from importlib.util import _LazyModule
3e89736b98ce py3: conditionalize test-demandimport.py for Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41519
diff changeset
    31
3e89736b98ce py3: conditionalize test-demandimport.py for Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41519
diff changeset
    32
    try:
3e89736b98ce py3: conditionalize test-demandimport.py for Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41519
diff changeset
    33
        from importlib.util import _Module as moduletype
3e89736b98ce py3: conditionalize test-demandimport.py for Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41519
diff changeset
    34
    except ImportError:
3e89736b98ce py3: conditionalize test-demandimport.py for Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41519
diff changeset
    35
        moduletype = types.ModuleType
3e89736b98ce py3: conditionalize test-demandimport.py for Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41519
diff changeset
    36
else:
3e89736b98ce py3: conditionalize test-demandimport.py for Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41519
diff changeset
    37
    moduletype = types.ModuleType
3e89736b98ce py3: conditionalize test-demandimport.py for Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41519
diff changeset
    38
23643
2205d00b6d2b demandimport: blacklist distutils.msvc9compiler (issue4475)
Augie Fackler <raf@durin42.com>
parents: 21025
diff changeset
    39
if os.name != 'nt':
2205d00b6d2b demandimport: blacklist distutils.msvc9compiler (issue4475)
Augie Fackler <raf@durin42.com>
parents: 21025
diff changeset
    40
    try:
2205d00b6d2b demandimport: blacklist distutils.msvc9compiler (issue4475)
Augie Fackler <raf@durin42.com>
parents: 21025
diff changeset
    41
        import distutils.msvc9compiler
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41521
diff changeset
    42
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41521
diff changeset
    43
        print(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41521
diff changeset
    44
            'distutils.msvc9compiler needs to be an immediate '
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41521
diff changeset
    45
            'importerror on non-windows platforms'
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41521
diff changeset
    46
        )
23643
2205d00b6d2b demandimport: blacklist distutils.msvc9compiler (issue4475)
Augie Fackler <raf@durin42.com>
parents: 21025
diff changeset
    47
        distutils.msvc9compiler
2205d00b6d2b demandimport: blacklist distutils.msvc9compiler (issue4475)
Augie Fackler <raf@durin42.com>
parents: 21025
diff changeset
    48
    except ImportError:
2205d00b6d2b demandimport: blacklist distutils.msvc9compiler (issue4475)
Augie Fackler <raf@durin42.com>
parents: 21025
diff changeset
    49
        pass
2205d00b6d2b demandimport: blacklist distutils.msvc9compiler (issue4475)
Augie Fackler <raf@durin42.com>
parents: 21025
diff changeset
    50
4631
e3afa670e484 demandimport: fix issue579 and add a test
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    51
import re
e3afa670e484 demandimport: fix issue579 and add a test
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    52
e3afa670e484 demandimport: fix issue579 and add a test
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    53
rsub = re.sub
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41521
diff changeset
    54
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41521
diff changeset
    55
4631
e3afa670e484 demandimport: fix issue579 and add a test
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    56
def f(obj):
e3afa670e484 demandimport: fix issue579 and add a test
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    57
    l = repr(obj)
4802
3a4310e8fe72 test-demandimport: match upper-case hexadecimal
Patrick Mezard <pmezard@gmail.com>
parents: 4631
diff changeset
    58
    l = rsub("0x[0-9a-fA-F]+", "0x?", l)
4631
e3afa670e484 demandimport: fix issue579 and add a test
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    59
    l = rsub("from '.*'", "from '?'", l)
13083
c0290fc6b486 test-demandimport.py: PyPy support
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 12865
diff changeset
    60
    l = rsub("'<[a-z]*>'", "'<whatever>'", l)
4631
e3afa670e484 demandimport: fix issue579 and add a test
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    61
    return l
e3afa670e484 demandimport: fix issue579 and add a test
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    62
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41521
diff changeset
    63
36274
b39f0fdb0338 tests: actually check that HGDEMANDIMPORT=disable disables demandimport
Martin von Zweigbergk <martinvonz@google.com>
parents: 33936
diff changeset
    64
demandimport.disable()
b39f0fdb0338 tests: actually check that HGDEMANDIMPORT=disable disables demandimport
Martin von Zweigbergk <martinvonz@google.com>
parents: 33936
diff changeset
    65
os.environ['HGDEMANDIMPORT'] = 'disable'
b39f0fdb0338 tests: actually check that HGDEMANDIMPORT=disable disables demandimport
Martin von Zweigbergk <martinvonz@google.com>
parents: 33936
diff changeset
    66
# this enable call should not actually enable demandimport!
b39f0fdb0338 tests: actually check that HGDEMANDIMPORT=disable disables demandimport
Martin von Zweigbergk <martinvonz@google.com>
parents: 33936
diff changeset
    67
demandimport.enable()
b39f0fdb0338 tests: actually check that HGDEMANDIMPORT=disable disables demandimport
Martin von Zweigbergk <martinvonz@google.com>
parents: 33936
diff changeset
    68
from mercurial import node
41519
dffd6a301570 py3: replace print() with assert in test-demandimport.py
Gregory Szorc <gregory.szorc@gmail.com>
parents: 38008
diff changeset
    69
dffd6a301570 py3: replace print() with assert in test-demandimport.py
Gregory Szorc <gregory.szorc@gmail.com>
parents: 38008
diff changeset
    70
# We use assert instead of a unittest test case because having imports inside
dffd6a301570 py3: replace print() with assert in test-demandimport.py
Gregory Szorc <gregory.szorc@gmail.com>
parents: 38008
diff changeset
    71
# functions changes behavior of the demand importer.
41520
3e89736b98ce py3: conditionalize test-demandimport.py for Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41519
diff changeset
    72
if ispy3:
3e89736b98ce py3: conditionalize test-demandimport.py for Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41519
diff changeset
    73
    assert not isinstance(node, _LazyModule)
3e89736b98ce py3: conditionalize test-demandimport.py for Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41519
diff changeset
    74
else:
3e89736b98ce py3: conditionalize test-demandimport.py for Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41519
diff changeset
    75
    assert f(node) == "<module 'mercurial.node' from '?'>", f(node)
41519
dffd6a301570 py3: replace print() with assert in test-demandimport.py
Gregory Szorc <gregory.szorc@gmail.com>
parents: 38008
diff changeset
    76
36274
b39f0fdb0338 tests: actually check that HGDEMANDIMPORT=disable disables demandimport
Martin von Zweigbergk <martinvonz@google.com>
parents: 33936
diff changeset
    77
# now enable it for real
b39f0fdb0338 tests: actually check that HGDEMANDIMPORT=disable disables demandimport
Martin von Zweigbergk <martinvonz@google.com>
parents: 33936
diff changeset
    78
del os.environ['HGDEMANDIMPORT']
b39f0fdb0338 tests: actually check that HGDEMANDIMPORT=disable disables demandimport
Martin von Zweigbergk <martinvonz@google.com>
parents: 33936
diff changeset
    79
demandimport.enable()
b39f0fdb0338 tests: actually check that HGDEMANDIMPORT=disable disables demandimport
Martin von Zweigbergk <martinvonz@google.com>
parents: 33936
diff changeset
    80
36284
c2c5f9f6fa21 tests: avoid referring to pvec in demandimport test
Martin von Zweigbergk <martinvonz@google.com>
parents: 36274
diff changeset
    81
# Test access to special attributes through demandmod proxy
41520
3e89736b98ce py3: conditionalize test-demandimport.py for Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41519
diff changeset
    82
assert 'mercurial.error' not in sys.modules
36284
c2c5f9f6fa21 tests: avoid referring to pvec in demandimport test
Martin von Zweigbergk <martinvonz@google.com>
parents: 36274
diff changeset
    83
from mercurial import error as errorproxy
41520
3e89736b98ce py3: conditionalize test-demandimport.py for Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41519
diff changeset
    84
3e89736b98ce py3: conditionalize test-demandimport.py for Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41519
diff changeset
    85
if ispy3:
3e89736b98ce py3: conditionalize test-demandimport.py for Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41519
diff changeset
    86
    # unsure why this isn't lazy.
3e89736b98ce py3: conditionalize test-demandimport.py for Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41519
diff changeset
    87
    assert not isinstance(f, _LazyModule)
3e89736b98ce py3: conditionalize test-demandimport.py for Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41519
diff changeset
    88
    assert f(errorproxy) == "<module 'mercurial.error' from '?'>", f(errorproxy)
3e89736b98ce py3: conditionalize test-demandimport.py for Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41519
diff changeset
    89
else:
3e89736b98ce py3: conditionalize test-demandimport.py for Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41519
diff changeset
    90
    assert f(errorproxy) == "<unloaded module 'error'>", f(errorproxy)
3e89736b98ce py3: conditionalize test-demandimport.py for Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41519
diff changeset
    91
41519
dffd6a301570 py3: replace print() with assert in test-demandimport.py
Gregory Szorc <gregory.szorc@gmail.com>
parents: 38008
diff changeset
    92
doc = ' '.join(errorproxy.__doc__.split()[:3])
dffd6a301570 py3: replace print() with assert in test-demandimport.py
Gregory Szorc <gregory.szorc@gmail.com>
parents: 38008
diff changeset
    93
assert doc == 'Mercurial exceptions. This', doc
dffd6a301570 py3: replace print() with assert in test-demandimport.py
Gregory Szorc <gregory.szorc@gmail.com>
parents: 38008
diff changeset
    94
assert errorproxy.__name__ == 'mercurial.error', errorproxy.__name__
dffd6a301570 py3: replace print() with assert in test-demandimport.py
Gregory Szorc <gregory.szorc@gmail.com>
parents: 38008
diff changeset
    95
36284
c2c5f9f6fa21 tests: avoid referring to pvec in demandimport test
Martin von Zweigbergk <martinvonz@google.com>
parents: 36274
diff changeset
    96
# __name__ must be accessible via __dict__ so the relative imports can be
c2c5f9f6fa21 tests: avoid referring to pvec in demandimport test
Martin von Zweigbergk <martinvonz@google.com>
parents: 36274
diff changeset
    97
# resolved
41519
dffd6a301570 py3: replace print() with assert in test-demandimport.py
Gregory Szorc <gregory.szorc@gmail.com>
parents: 38008
diff changeset
    98
name = errorproxy.__dict__['__name__']
dffd6a301570 py3: replace print() with assert in test-demandimport.py
Gregory Szorc <gregory.szorc@gmail.com>
parents: 38008
diff changeset
    99
assert name == 'mercurial.error', name
dffd6a301570 py3: replace print() with assert in test-demandimport.py
Gregory Szorc <gregory.szorc@gmail.com>
parents: 38008
diff changeset
   100
41520
3e89736b98ce py3: conditionalize test-demandimport.py for Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41519
diff changeset
   101
if ispy3:
3e89736b98ce py3: conditionalize test-demandimport.py for Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41519
diff changeset
   102
    assert not isinstance(errorproxy, _LazyModule)
3e89736b98ce py3: conditionalize test-demandimport.py for Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41519
diff changeset
   103
    assert f(errorproxy) == "<module 'mercurial.error' from '?'>", f(errorproxy)
3e89736b98ce py3: conditionalize test-demandimport.py for Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41519
diff changeset
   104
else:
3e89736b98ce py3: conditionalize test-demandimport.py for Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41519
diff changeset
   105
    assert f(errorproxy) == "<proxied module 'error'>", f(errorproxy)
36284
c2c5f9f6fa21 tests: avoid referring to pvec in demandimport test
Martin von Zweigbergk <martinvonz@google.com>
parents: 36274
diff changeset
   106
4631
e3afa670e484 demandimport: fix issue579 and add a test
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   107
import os
e3afa670e484 demandimport: fix issue579 and add a test
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   108
41520
3e89736b98ce py3: conditionalize test-demandimport.py for Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41519
diff changeset
   109
if ispy3:
3e89736b98ce py3: conditionalize test-demandimport.py for Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41519
diff changeset
   110
    assert not isinstance(os, _LazyModule)
3e89736b98ce py3: conditionalize test-demandimport.py for Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41519
diff changeset
   111
    assert f(os) == "<module 'os' from '?'>", f(os)
3e89736b98ce py3: conditionalize test-demandimport.py for Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41519
diff changeset
   112
else:
3e89736b98ce py3: conditionalize test-demandimport.py for Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41519
diff changeset
   113
    assert f(os) == "<unloaded module 'os'>", f(os)
3e89736b98ce py3: conditionalize test-demandimport.py for Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41519
diff changeset
   114
41519
dffd6a301570 py3: replace print() with assert in test-demandimport.py
Gregory Szorc <gregory.szorc@gmail.com>
parents: 38008
diff changeset
   115
assert f(os.system) == '<built-in function system>', f(os.system)
dffd6a301570 py3: replace print() with assert in test-demandimport.py
Gregory Szorc <gregory.szorc@gmail.com>
parents: 38008
diff changeset
   116
assert f(os) == "<module 'os' from '?'>", f(os)
4631
e3afa670e484 demandimport: fix issue579 and add a test
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   117
41520
3e89736b98ce py3: conditionalize test-demandimport.py for Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41519
diff changeset
   118
assert 'mercurial.utils.procutil' not in sys.modules
38008
1d0610fdd63b tests: migrate demandimport.py away from deprecated `util` module symbols
Matt Harbison <matt_harbison@yahoo.com>
parents: 36284
diff changeset
   119
from mercurial.utils import procutil
4631
e3afa670e484 demandimport: fix issue579 and add a test
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   120
41520
3e89736b98ce py3: conditionalize test-demandimport.py for Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41519
diff changeset
   121
if ispy3:
3e89736b98ce py3: conditionalize test-demandimport.py for Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41519
diff changeset
   122
    assert isinstance(procutil, _LazyModule)
3e89736b98ce py3: conditionalize test-demandimport.py for Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41519
diff changeset
   123
    assert f(procutil) == "<module 'mercurial.utils.procutil' from '?'>", f(
3e89736b98ce py3: conditionalize test-demandimport.py for Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41519
diff changeset
   124
        procutil
3e89736b98ce py3: conditionalize test-demandimport.py for Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41519
diff changeset
   125
    )
3e89736b98ce py3: conditionalize test-demandimport.py for Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41519
diff changeset
   126
else:
3e89736b98ce py3: conditionalize test-demandimport.py for Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41519
diff changeset
   127
    assert f(procutil) == "<unloaded module 'procutil'>", f(procutil)
3e89736b98ce py3: conditionalize test-demandimport.py for Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41519
diff changeset
   128
41519
dffd6a301570 py3: replace print() with assert in test-demandimport.py
Gregory Szorc <gregory.szorc@gmail.com>
parents: 38008
diff changeset
   129
assert f(procutil.system) == '<function system at 0x?>', f(procutil.system)
41520
3e89736b98ce py3: conditionalize test-demandimport.py for Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41519
diff changeset
   130
assert procutil.__class__ == moduletype, procutil.__class__
41519
dffd6a301570 py3: replace print() with assert in test-demandimport.py
Gregory Szorc <gregory.szorc@gmail.com>
parents: 38008
diff changeset
   131
assert f(procutil) == "<module 'mercurial.utils.procutil' from '?'>", f(
dffd6a301570 py3: replace print() with assert in test-demandimport.py
Gregory Szorc <gregory.szorc@gmail.com>
parents: 38008
diff changeset
   132
    procutil
dffd6a301570 py3: replace print() with assert in test-demandimport.py
Gregory Szorc <gregory.szorc@gmail.com>
parents: 38008
diff changeset
   133
)
dffd6a301570 py3: replace print() with assert in test-demandimport.py
Gregory Szorc <gregory.szorc@gmail.com>
parents: 38008
diff changeset
   134
assert f(procutil.system) == '<function system at 0x?>', f(procutil.system)
4631
e3afa670e484 demandimport: fix issue579 and add a test
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   135
41520
3e89736b98ce py3: conditionalize test-demandimport.py for Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41519
diff changeset
   136
assert 'mercurial.hgweb' not in sys.modules
27535
0d0f4070f6d7 test-demandimport: ensure that relative imports are deferred
Bryan O'Sullivan <bos@serpentine.com>
parents: 23643
diff changeset
   137
from mercurial import hgweb
41520
3e89736b98ce py3: conditionalize test-demandimport.py for Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41519
diff changeset
   138
3e89736b98ce py3: conditionalize test-demandimport.py for Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41519
diff changeset
   139
if ispy3:
44118
f81c17ec303c hgdemandimport: apply lazy module loading to sys.meta_path finders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 44117
diff changeset
   140
    assert isinstance(hgweb, _LazyModule)
41520
3e89736b98ce py3: conditionalize test-demandimport.py for Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41519
diff changeset
   141
    assert f(hgweb) == "<module 'mercurial.hgweb' from '?'>", f(hgweb)
3e89736b98ce py3: conditionalize test-demandimport.py for Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41519
diff changeset
   142
    assert isinstance(hgweb.hgweb_mod, _LazyModule)
3e89736b98ce py3: conditionalize test-demandimport.py for Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41519
diff changeset
   143
    assert (
3e89736b98ce py3: conditionalize test-demandimport.py for Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41519
diff changeset
   144
        f(hgweb.hgweb_mod) == "<module 'mercurial.hgweb.hgweb_mod' from '?'>"
3e89736b98ce py3: conditionalize test-demandimport.py for Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41519
diff changeset
   145
    ), f(hgweb.hgweb_mod)
3e89736b98ce py3: conditionalize test-demandimport.py for Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41519
diff changeset
   146
else:
3e89736b98ce py3: conditionalize test-demandimport.py for Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41519
diff changeset
   147
    assert f(hgweb) == "<unloaded module 'hgweb'>", f(hgweb)
3e89736b98ce py3: conditionalize test-demandimport.py for Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41519
diff changeset
   148
    assert f(hgweb.hgweb_mod) == "<unloaded module 'hgweb_mod'>", f(
3e89736b98ce py3: conditionalize test-demandimport.py for Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41519
diff changeset
   149
        hgweb.hgweb_mod
3e89736b98ce py3: conditionalize test-demandimport.py for Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41519
diff changeset
   150
    )
3e89736b98ce py3: conditionalize test-demandimport.py for Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41519
diff changeset
   151
41519
dffd6a301570 py3: replace print() with assert in test-demandimport.py
Gregory Szorc <gregory.szorc@gmail.com>
parents: 38008
diff changeset
   152
assert f(hgweb) == "<module 'mercurial.hgweb' from '?'>", f(hgweb)
27535
0d0f4070f6d7 test-demandimport: ensure that relative imports are deferred
Bryan O'Sullivan <bos@serpentine.com>
parents: 23643
diff changeset
   153
4631
e3afa670e484 demandimport: fix issue579 and add a test
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   154
import re as fred
41520
3e89736b98ce py3: conditionalize test-demandimport.py for Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41519
diff changeset
   155
3e89736b98ce py3: conditionalize test-demandimport.py for Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41519
diff changeset
   156
if ispy3:
3e89736b98ce py3: conditionalize test-demandimport.py for Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41519
diff changeset
   157
    assert not isinstance(fred, _LazyModule)
3e89736b98ce py3: conditionalize test-demandimport.py for Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41519
diff changeset
   158
    assert f(fred) == "<module 're' from '?'>"
3e89736b98ce py3: conditionalize test-demandimport.py for Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41519
diff changeset
   159
else:
3e89736b98ce py3: conditionalize test-demandimport.py for Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41519
diff changeset
   160
    assert f(fred) == "<unloaded module 're'>", f(fred)
4631
e3afa670e484 demandimport: fix issue579 and add a test
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   161
32485
252d2260c74e demandimport: look for 'mod' suffix as alternative name for module reference
Yuya Nishihara <yuya@tcha.org>
parents: 30261
diff changeset
   162
import re as remod
41520
3e89736b98ce py3: conditionalize test-demandimport.py for Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41519
diff changeset
   163
3e89736b98ce py3: conditionalize test-demandimport.py for Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41519
diff changeset
   164
if ispy3:
3e89736b98ce py3: conditionalize test-demandimport.py for Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41519
diff changeset
   165
    assert not isinstance(remod, _LazyModule)
3e89736b98ce py3: conditionalize test-demandimport.py for Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41519
diff changeset
   166
    assert f(remod) == "<module 're' from '?'>"
3e89736b98ce py3: conditionalize test-demandimport.py for Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41519
diff changeset
   167
else:
3e89736b98ce py3: conditionalize test-demandimport.py for Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41519
diff changeset
   168
    assert f(remod) == "<unloaded module 're'>", f(remod)
32485
252d2260c74e demandimport: look for 'mod' suffix as alternative name for module reference
Yuya Nishihara <yuya@tcha.org>
parents: 30261
diff changeset
   169
4631
e3afa670e484 demandimport: fix issue579 and add a test
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   170
import sys as re
41520
3e89736b98ce py3: conditionalize test-demandimport.py for Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41519
diff changeset
   171
3e89736b98ce py3: conditionalize test-demandimport.py for Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41519
diff changeset
   172
if ispy3:
3e89736b98ce py3: conditionalize test-demandimport.py for Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41519
diff changeset
   173
    assert not isinstance(re, _LazyModule)
3e89736b98ce py3: conditionalize test-demandimport.py for Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41519
diff changeset
   174
    assert f(re) == "<module 'sys' (built-in)>"
3e89736b98ce py3: conditionalize test-demandimport.py for Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41519
diff changeset
   175
else:
3e89736b98ce py3: conditionalize test-demandimport.py for Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41519
diff changeset
   176
    assert f(re) == "<unloaded module 'sys'>", f(re)
4631
e3afa670e484 demandimport: fix issue579 and add a test
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   177
41520
3e89736b98ce py3: conditionalize test-demandimport.py for Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41519
diff changeset
   178
if ispy3:
3e89736b98ce py3: conditionalize test-demandimport.py for Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41519
diff changeset
   179
    assert not isinstance(fred, _LazyModule)
3e89736b98ce py3: conditionalize test-demandimport.py for Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41519
diff changeset
   180
    assert f(fred) == "<module 're' from '?'>", f(fred)
3e89736b98ce py3: conditionalize test-demandimport.py for Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41519
diff changeset
   181
else:
3e89736b98ce py3: conditionalize test-demandimport.py for Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41519
diff changeset
   182
    assert f(fred) == "<unloaded module 're'>", f(fred)
3e89736b98ce py3: conditionalize test-demandimport.py for Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41519
diff changeset
   183
41519
dffd6a301570 py3: replace print() with assert in test-demandimport.py
Gregory Szorc <gregory.szorc@gmail.com>
parents: 38008
diff changeset
   184
assert f(fred.sub) == '<function sub at 0x?>', f(fred.sub)
41520
3e89736b98ce py3: conditionalize test-demandimport.py for Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41519
diff changeset
   185
3e89736b98ce py3: conditionalize test-demandimport.py for Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41519
diff changeset
   186
if ispy3:
3e89736b98ce py3: conditionalize test-demandimport.py for Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41519
diff changeset
   187
    assert not isinstance(fred, _LazyModule)
3e89736b98ce py3: conditionalize test-demandimport.py for Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41519
diff changeset
   188
    assert f(fred) == "<module 're' from '?'>", f(fred)
3e89736b98ce py3: conditionalize test-demandimport.py for Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41519
diff changeset
   189
else:
3e89736b98ce py3: conditionalize test-demandimport.py for Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41519
diff changeset
   190
    assert f(fred) == "<proxied module 're'>", f(fred)
4631
e3afa670e484 demandimport: fix issue579 and add a test
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   191
32485
252d2260c74e demandimport: look for 'mod' suffix as alternative name for module reference
Yuya Nishihara <yuya@tcha.org>
parents: 30261
diff changeset
   192
remod.escape  # use remod
41519
dffd6a301570 py3: replace print() with assert in test-demandimport.py
Gregory Szorc <gregory.szorc@gmail.com>
parents: 38008
diff changeset
   193
assert f(remod) == "<module 're' from '?'>", f(remod)
32485
252d2260c74e demandimport: look for 'mod' suffix as alternative name for module reference
Yuya Nishihara <yuya@tcha.org>
parents: 30261
diff changeset
   194
41520
3e89736b98ce py3: conditionalize test-demandimport.py for Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41519
diff changeset
   195
if ispy3:
3e89736b98ce py3: conditionalize test-demandimport.py for Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41519
diff changeset
   196
    assert not isinstance(re, _LazyModule)
3e89736b98ce py3: conditionalize test-demandimport.py for Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41519
diff changeset
   197
    assert f(re) == "<module 'sys' (built-in)>"
3e89736b98ce py3: conditionalize test-demandimport.py for Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41519
diff changeset
   198
    assert f(type(re.stderr)) == "<class '_io.TextIOWrapper'>", f(
3e89736b98ce py3: conditionalize test-demandimport.py for Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41519
diff changeset
   199
        type(re.stderr)
3e89736b98ce py3: conditionalize test-demandimport.py for Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41519
diff changeset
   200
    )
3e89736b98ce py3: conditionalize test-demandimport.py for Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41519
diff changeset
   201
    assert f(re) == "<module 'sys' (built-in)>"
3e89736b98ce py3: conditionalize test-demandimport.py for Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41519
diff changeset
   202
else:
3e89736b98ce py3: conditionalize test-demandimport.py for Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41519
diff changeset
   203
    assert f(re) == "<unloaded module 'sys'>", f(re)
3e89736b98ce py3: conditionalize test-demandimport.py for Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41519
diff changeset
   204
    assert f(re.stderr) == "<open file '<whatever>', mode 'w' at 0x?>", f(
3e89736b98ce py3: conditionalize test-demandimport.py for Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41519
diff changeset
   205
        re.stderr
3e89736b98ce py3: conditionalize test-demandimport.py for Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41519
diff changeset
   206
    )
3e89736b98ce py3: conditionalize test-demandimport.py for Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41519
diff changeset
   207
    assert f(re) == "<proxied module 'sys'>", f(re)
21025
54af51c18c4c demandimport: make it possible to disable by setting HGDEMANDIMPORT=disable
Mads Kiilerich <madski@unity3d.com>
parents: 13083
diff changeset
   208
41521
30248d6bc057 tests: use unimported modules in test-demandimport.py
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41520
diff changeset
   209
assert 'telnetlib' not in sys.modules
30248d6bc057 tests: use unimported modules in test-demandimport.py
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41520
diff changeset
   210
import telnetlib
41520
3e89736b98ce py3: conditionalize test-demandimport.py for Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41519
diff changeset
   211
3e89736b98ce py3: conditionalize test-demandimport.py for Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41519
diff changeset
   212
if ispy3:
44118
f81c17ec303c hgdemandimport: apply lazy module loading to sys.meta_path finders
Gregory Szorc <gregory.szorc@gmail.com>
parents: 44117
diff changeset
   213
    assert isinstance(telnetlib, _LazyModule)
41521
30248d6bc057 tests: use unimported modules in test-demandimport.py
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41520
diff changeset
   214
    assert f(telnetlib) == "<module 'telnetlib' from '?'>"
41520
3e89736b98ce py3: conditionalize test-demandimport.py for Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41519
diff changeset
   215
else:
41521
30248d6bc057 tests: use unimported modules in test-demandimport.py
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41520
diff changeset
   216
    assert f(telnetlib) == "<unloaded module 'telnetlib'>", f(telnetlib)
41520
3e89736b98ce py3: conditionalize test-demandimport.py for Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41519
diff changeset
   217
30024
26a4e46af2bc demandimport: error out early on missing attribute of non package (issue5373)
Yuya Nishihara <yuya@tcha.org>
parents: 29985
diff changeset
   218
try:
41521
30248d6bc057 tests: use unimported modules in test-demandimport.py
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41520
diff changeset
   219
    from telnetlib import unknownattr
41519
dffd6a301570 py3: replace print() with assert in test-demandimport.py
Gregory Szorc <gregory.szorc@gmail.com>
parents: 38008
diff changeset
   220
dffd6a301570 py3: replace print() with assert in test-demandimport.py
Gregory Szorc <gregory.szorc@gmail.com>
parents: 38008
diff changeset
   221
    assert False, (
dffd6a301570 py3: replace print() with assert in test-demandimport.py
Gregory Szorc <gregory.szorc@gmail.com>
parents: 38008
diff changeset
   222
        'no demandmod should be created for attribute of non-package '
41521
30248d6bc057 tests: use unimported modules in test-demandimport.py
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41520
diff changeset
   223
        'module:\ntelnetlib.unknownattr = %s' % f(unknownattr)
41519
dffd6a301570 py3: replace print() with assert in test-demandimport.py
Gregory Szorc <gregory.szorc@gmail.com>
parents: 38008
diff changeset
   224
    )
30024
26a4e46af2bc demandimport: error out early on missing attribute of non package (issue5373)
Yuya Nishihara <yuya@tcha.org>
parents: 29985
diff changeset
   225
except ImportError as inst:
41520
3e89736b98ce py3: conditionalize test-demandimport.py for Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41519
diff changeset
   226
    assert rsub(r"'", '', str(inst)).startswith(
3e89736b98ce py3: conditionalize test-demandimport.py for Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41519
diff changeset
   227
        'cannot import name unknownattr'
3e89736b98ce py3: conditionalize test-demandimport.py for Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41519
diff changeset
   228
    )
30261
1914db1b7d9e demandimport: do not raise ImportError for unknown item in fromlist
Yuya Nishihara <yuya@tcha.org>
parents: 30024
diff changeset
   229
38008
1d0610fdd63b tests: migrate demandimport.py away from deprecated `util` module symbols
Matt Harbison <matt_harbison@yahoo.com>
parents: 36284
diff changeset
   230
from mercurial import util
1d0610fdd63b tests: migrate demandimport.py away from deprecated `util` module symbols
Matt Harbison <matt_harbison@yahoo.com>
parents: 36284
diff changeset
   231
30261
1914db1b7d9e demandimport: do not raise ImportError for unknown item in fromlist
Yuya Nishihara <yuya@tcha.org>
parents: 30024
diff changeset
   232
# Unlike the import statement, __import__() function should not raise
1914db1b7d9e demandimport: do not raise ImportError for unknown item in fromlist
Yuya Nishihara <yuya@tcha.org>
parents: 30024
diff changeset
   233
# ImportError even if fromlist has an unknown item
1914db1b7d9e demandimport: do not raise ImportError for unknown item in fromlist
Yuya Nishihara <yuya@tcha.org>
parents: 30024
diff changeset
   234
# (see Python/import.c:import_module_level() and ensure_fromlist())
45211
fc8299319ffe tests: fix test-demandimport.py on Python 3.9
Manuel Jacob <me@manueljacob.de>
parents: 44118
diff changeset
   235
assert 'ftplib' not in sys.modules
fc8299319ffe tests: fix test-demandimport.py on Python 3.9
Manuel Jacob <me@manueljacob.de>
parents: 44118
diff changeset
   236
zipfileimp = __import__('ftplib', globals(), locals(), ['unknownattr'])
fc8299319ffe tests: fix test-demandimport.py on Python 3.9
Manuel Jacob <me@manueljacob.de>
parents: 44118
diff changeset
   237
assert f(zipfileimp) == "<module 'ftplib' from '?'>", f(zipfileimp)
41521
30248d6bc057 tests: use unimported modules in test-demandimport.py
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41520
diff changeset
   238
assert not util.safehasattr(zipfileimp, 'unknownattr')