tests/test-demandimport.py
author Martin von Zweigbergk <martinvonz@google.com>
Fri, 16 Feb 2018 11:30:18 -0800
changeset 36237 b39f0fdb0338
parent 33918 eddca62d9e64
child 36251 c2c5f9f6fa21
permissions -rw-r--r--
tests: actually check that HGDEMANDIMPORT=disable disables demandimport At the point in the test where we were checking that the 'node' got eagerly loaded, it had already been loaded (via the pvec module), so our check wasn't doing anything (i.e. the test would pass even if you removed the line that set HGDEMANDIMPORT=disable). Let's move this test earlier so it tests what it was meant to test. Differential Revision: https://phab.mercurial-scm.org/D2286
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
33918
eddca62d9e64 tests: ensure demandimport test uses absolute_import
Augie Fackler <raf@durin42.com>
parents: 32448
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
e3afa670e484 demandimport: fix issue579 and add a test
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
     4
demandimport.enable()
e3afa670e484 demandimport: fix issue579 and add a test
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
     5
23643
2205d00b6d2b demandimport: blacklist distutils.msvc9compiler (issue4475)
Augie Fackler <raf@durin42.com>
parents: 21025
diff changeset
     6
import os
29868
4b50e1f922a0 tests: skip demandimport if disabled
timeless <timeless@mozdev.org>
parents: 28948
diff changeset
     7
import subprocess
4b50e1f922a0 tests: skip demandimport if disabled
timeless <timeless@mozdev.org>
parents: 28948
diff changeset
     8
import sys
4b50e1f922a0 tests: skip demandimport if disabled
timeless <timeless@mozdev.org>
parents: 28948
diff changeset
     9
4b50e1f922a0 tests: skip demandimport if disabled
timeless <timeless@mozdev.org>
parents: 28948
diff changeset
    10
# Only run if demandimport is allowed
4b50e1f922a0 tests: skip demandimport if disabled
timeless <timeless@mozdev.org>
parents: 28948
diff changeset
    11
if subprocess.call(['python', '%s/hghave' % os.environ['TESTDIR'],
4b50e1f922a0 tests: skip demandimport if disabled
timeless <timeless@mozdev.org>
parents: 28948
diff changeset
    12
                    'demandimport']):
4b50e1f922a0 tests: skip demandimport if disabled
timeless <timeless@mozdev.org>
parents: 28948
diff changeset
    13
    sys.exit(80)
4b50e1f922a0 tests: skip demandimport if disabled
timeless <timeless@mozdev.org>
parents: 28948
diff changeset
    14
23643
2205d00b6d2b demandimport: blacklist distutils.msvc9compiler (issue4475)
Augie Fackler <raf@durin42.com>
parents: 21025
diff changeset
    15
if os.name != 'nt':
2205d00b6d2b demandimport: blacklist distutils.msvc9compiler (issue4475)
Augie Fackler <raf@durin42.com>
parents: 21025
diff changeset
    16
    try:
2205d00b6d2b demandimport: blacklist distutils.msvc9compiler (issue4475)
Augie Fackler <raf@durin42.com>
parents: 21025
diff changeset
    17
        import distutils.msvc9compiler
28948
16390f4cccf0 py3: make test-demandimport use print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 27535
diff changeset
    18
        print('distutils.msvc9compiler needs to be an immediate '
16390f4cccf0 py3: make test-demandimport use print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 27535
diff changeset
    19
              'importerror on non-windows platforms')
23643
2205d00b6d2b demandimport: blacklist distutils.msvc9compiler (issue4475)
Augie Fackler <raf@durin42.com>
parents: 21025
diff changeset
    20
        distutils.msvc9compiler
2205d00b6d2b demandimport: blacklist distutils.msvc9compiler (issue4475)
Augie Fackler <raf@durin42.com>
parents: 21025
diff changeset
    21
    except ImportError:
2205d00b6d2b demandimport: blacklist distutils.msvc9compiler (issue4475)
Augie Fackler <raf@durin42.com>
parents: 21025
diff changeset
    22
        pass
2205d00b6d2b demandimport: blacklist distutils.msvc9compiler (issue4475)
Augie Fackler <raf@durin42.com>
parents: 21025
diff changeset
    23
4631
e3afa670e484 demandimport: fix issue579 and add a test
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    24
import re
e3afa670e484 demandimport: fix issue579 and add a test
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    25
e3afa670e484 demandimport: fix issue579 and add a test
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    26
rsub = re.sub
e3afa670e484 demandimport: fix issue579 and add a test
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    27
def f(obj):
e3afa670e484 demandimport: fix issue579 and add a test
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    28
    l = repr(obj)
4802
3a4310e8fe72 test-demandimport: match upper-case hexadecimal
Patrick Mezard <pmezard@gmail.com>
parents: 4631
diff changeset
    29
    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
    30
    l = rsub("from '.*'", "from '?'", l)
13083
c0290fc6b486 test-demandimport.py: PyPy support
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 12865
diff changeset
    31
    l = rsub("'<[a-z]*>'", "'<whatever>'", l)
4631
e3afa670e484 demandimport: fix issue579 and add a test
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    32
    return l
e3afa670e484 demandimport: fix issue579 and add a test
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    33
36237
b39f0fdb0338 tests: actually check that HGDEMANDIMPORT=disable disables demandimport
Martin von Zweigbergk <martinvonz@google.com>
parents: 33918
diff changeset
    34
demandimport.disable()
b39f0fdb0338 tests: actually check that HGDEMANDIMPORT=disable disables demandimport
Martin von Zweigbergk <martinvonz@google.com>
parents: 33918
diff changeset
    35
os.environ['HGDEMANDIMPORT'] = 'disable'
b39f0fdb0338 tests: actually check that HGDEMANDIMPORT=disable disables demandimport
Martin von Zweigbergk <martinvonz@google.com>
parents: 33918
diff changeset
    36
# this enable call should not actually enable demandimport!
b39f0fdb0338 tests: actually check that HGDEMANDIMPORT=disable disables demandimport
Martin von Zweigbergk <martinvonz@google.com>
parents: 33918
diff changeset
    37
demandimport.enable()
b39f0fdb0338 tests: actually check that HGDEMANDIMPORT=disable disables demandimport
Martin von Zweigbergk <martinvonz@google.com>
parents: 33918
diff changeset
    38
from mercurial import node
b39f0fdb0338 tests: actually check that HGDEMANDIMPORT=disable disables demandimport
Martin von Zweigbergk <martinvonz@google.com>
parents: 33918
diff changeset
    39
print("node =", f(node))
b39f0fdb0338 tests: actually check that HGDEMANDIMPORT=disable disables demandimport
Martin von Zweigbergk <martinvonz@google.com>
parents: 33918
diff changeset
    40
# now enable it for real
b39f0fdb0338 tests: actually check that HGDEMANDIMPORT=disable disables demandimport
Martin von Zweigbergk <martinvonz@google.com>
parents: 33918
diff changeset
    41
del os.environ['HGDEMANDIMPORT']
b39f0fdb0338 tests: actually check that HGDEMANDIMPORT=disable disables demandimport
Martin von Zweigbergk <martinvonz@google.com>
parents: 33918
diff changeset
    42
demandimport.enable()
b39f0fdb0338 tests: actually check that HGDEMANDIMPORT=disable disables demandimport
Martin von Zweigbergk <martinvonz@google.com>
parents: 33918
diff changeset
    43
4631
e3afa670e484 demandimport: fix issue579 and add a test
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    44
import os
e3afa670e484 demandimport: fix issue579 and add a test
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    45
28948
16390f4cccf0 py3: make test-demandimport use print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 27535
diff changeset
    46
print("os =", f(os))
16390f4cccf0 py3: make test-demandimport use print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 27535
diff changeset
    47
print("os.system =", f(os.system))
16390f4cccf0 py3: make test-demandimport use print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 27535
diff changeset
    48
print("os =", f(os))
4631
e3afa670e484 demandimport: fix issue579 and add a test
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    49
e3afa670e484 demandimport: fix issue579 and add a test
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    50
from mercurial import util
e3afa670e484 demandimport: fix issue579 and add a test
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    51
28948
16390f4cccf0 py3: make test-demandimport use print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 27535
diff changeset
    52
print("util =", f(util))
16390f4cccf0 py3: make test-demandimport use print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 27535
diff changeset
    53
print("util.system =", f(util.system))
16390f4cccf0 py3: make test-demandimport use print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 27535
diff changeset
    54
print("util =", f(util))
16390f4cccf0 py3: make test-demandimport use print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 27535
diff changeset
    55
print("util.system =", f(util.system))
4631
e3afa670e484 demandimport: fix issue579 and add a test
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    56
27535
0d0f4070f6d7 test-demandimport: ensure that relative imports are deferred
Bryan O'Sullivan <bos@serpentine.com>
parents: 23643
diff changeset
    57
from mercurial import hgweb
28948
16390f4cccf0 py3: make test-demandimport use print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 27535
diff changeset
    58
print("hgweb =", f(hgweb))
16390f4cccf0 py3: make test-demandimport use print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 27535
diff changeset
    59
print("hgweb_mod =", f(hgweb.hgweb_mod))
16390f4cccf0 py3: make test-demandimport use print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 27535
diff changeset
    60
print("hgweb =", f(hgweb))
27535
0d0f4070f6d7 test-demandimport: ensure that relative imports are deferred
Bryan O'Sullivan <bos@serpentine.com>
parents: 23643
diff changeset
    61
4631
e3afa670e484 demandimport: fix issue579 and add a test
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    62
import re as fred
28948
16390f4cccf0 py3: make test-demandimport use print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 27535
diff changeset
    63
print("fred =", f(fred))
4631
e3afa670e484 demandimport: fix issue579 and add a test
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    64
32447
252d2260c74e demandimport: look for 'mod' suffix as alternative name for module reference
Yuya Nishihara <yuya@tcha.org>
parents: 30647
diff changeset
    65
import re as remod
252d2260c74e demandimport: look for 'mod' suffix as alternative name for module reference
Yuya Nishihara <yuya@tcha.org>
parents: 30647
diff changeset
    66
print("remod =", f(remod))
252d2260c74e demandimport: look for 'mod' suffix as alternative name for module reference
Yuya Nishihara <yuya@tcha.org>
parents: 30647
diff changeset
    67
4631
e3afa670e484 demandimport: fix issue579 and add a test
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    68
import sys as re
28948
16390f4cccf0 py3: make test-demandimport use print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 27535
diff changeset
    69
print("re =", f(re))
4631
e3afa670e484 demandimport: fix issue579 and add a test
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    70
28948
16390f4cccf0 py3: make test-demandimport use print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 27535
diff changeset
    71
print("fred =", f(fred))
16390f4cccf0 py3: make test-demandimport use print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 27535
diff changeset
    72
print("fred.sub =", f(fred.sub))
16390f4cccf0 py3: make test-demandimport use print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 27535
diff changeset
    73
print("fred =", f(fred))
4631
e3afa670e484 demandimport: fix issue579 and add a test
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    74
32447
252d2260c74e demandimport: look for 'mod' suffix as alternative name for module reference
Yuya Nishihara <yuya@tcha.org>
parents: 30647
diff changeset
    75
remod.escape  # use remod
252d2260c74e demandimport: look for 'mod' suffix as alternative name for module reference
Yuya Nishihara <yuya@tcha.org>
parents: 30647
diff changeset
    76
print("remod =", f(remod))
252d2260c74e demandimport: look for 'mod' suffix as alternative name for module reference
Yuya Nishihara <yuya@tcha.org>
parents: 30647
diff changeset
    77
28948
16390f4cccf0 py3: make test-demandimport use print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 27535
diff changeset
    78
print("re =", f(re))
16390f4cccf0 py3: make test-demandimport use print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 27535
diff changeset
    79
print("re.stderr =", f(re.stderr))
16390f4cccf0 py3: make test-demandimport use print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 27535
diff changeset
    80
print("re =", f(re))
21025
54af51c18c4c demandimport: make it possible to disable by setting HGDEMANDIMPORT=disable
Mads Kiilerich <madski@unity3d.com>
parents: 13083
diff changeset
    81
32448
91a2ec8e7fa0 demandimport: stop overriding __getattribute__()
Yuya Nishihara <yuya@tcha.org>
parents: 32447
diff changeset
    82
# Test access to special attributes through demandmod proxy
91a2ec8e7fa0 demandimport: stop overriding __getattribute__()
Yuya Nishihara <yuya@tcha.org>
parents: 32447
diff changeset
    83
from mercurial import pvec as pvecproxy
91a2ec8e7fa0 demandimport: stop overriding __getattribute__()
Yuya Nishihara <yuya@tcha.org>
parents: 32447
diff changeset
    84
print("pvecproxy =", f(pvecproxy))
91a2ec8e7fa0 demandimport: stop overriding __getattribute__()
Yuya Nishihara <yuya@tcha.org>
parents: 32447
diff changeset
    85
print("pvecproxy.__doc__ = %r"
91a2ec8e7fa0 demandimport: stop overriding __getattribute__()
Yuya Nishihara <yuya@tcha.org>
parents: 32447
diff changeset
    86
      % (' '.join(pvecproxy.__doc__.split()[:3]) + ' ...'))
91a2ec8e7fa0 demandimport: stop overriding __getattribute__()
Yuya Nishihara <yuya@tcha.org>
parents: 32447
diff changeset
    87
print("pvecproxy.__name__ = %r" % pvecproxy.__name__)
91a2ec8e7fa0 demandimport: stop overriding __getattribute__()
Yuya Nishihara <yuya@tcha.org>
parents: 32447
diff changeset
    88
# __name__ must be accessible via __dict__ so the relative imports can be
91a2ec8e7fa0 demandimport: stop overriding __getattribute__()
Yuya Nishihara <yuya@tcha.org>
parents: 32447
diff changeset
    89
# resolved
91a2ec8e7fa0 demandimport: stop overriding __getattribute__()
Yuya Nishihara <yuya@tcha.org>
parents: 32447
diff changeset
    90
print("pvecproxy.__dict__['__name__'] = %r" % pvecproxy.__dict__['__name__'])
91a2ec8e7fa0 demandimport: stop overriding __getattribute__()
Yuya Nishihara <yuya@tcha.org>
parents: 32447
diff changeset
    91
print("pvecproxy =", f(pvecproxy))
91a2ec8e7fa0 demandimport: stop overriding __getattribute__()
Yuya Nishihara <yuya@tcha.org>
parents: 32447
diff changeset
    92
30022
26a4e46af2bc demandimport: error out early on missing attribute of non package (issue5373)
Yuya Nishihara <yuya@tcha.org>
parents: 29981
diff changeset
    93
import contextlib
26a4e46af2bc demandimport: error out early on missing attribute of non package (issue5373)
Yuya Nishihara <yuya@tcha.org>
parents: 29981
diff changeset
    94
print("contextlib =", f(contextlib))
26a4e46af2bc demandimport: error out early on missing attribute of non package (issue5373)
Yuya Nishihara <yuya@tcha.org>
parents: 29981
diff changeset
    95
try:
26a4e46af2bc demandimport: error out early on missing attribute of non package (issue5373)
Yuya Nishihara <yuya@tcha.org>
parents: 29981
diff changeset
    96
    from contextlib import unknownattr
26a4e46af2bc demandimport: error out early on missing attribute of non package (issue5373)
Yuya Nishihara <yuya@tcha.org>
parents: 29981
diff changeset
    97
    print('no demandmod should be created for attribute of non-package '
26a4e46af2bc demandimport: error out early on missing attribute of non package (issue5373)
Yuya Nishihara <yuya@tcha.org>
parents: 29981
diff changeset
    98
          'module:\ncontextlib.unknownattr =', f(unknownattr))
26a4e46af2bc demandimport: error out early on missing attribute of non package (issue5373)
Yuya Nishihara <yuya@tcha.org>
parents: 29981
diff changeset
    99
except ImportError as inst:
30647
1914db1b7d9e demandimport: do not raise ImportError for unknown item in fromlist
Yuya Nishihara <yuya@tcha.org>
parents: 30022
diff changeset
   100
    print('contextlib.unknownattr = ImportError: %s'
1914db1b7d9e demandimport: do not raise ImportError for unknown item in fromlist
Yuya Nishihara <yuya@tcha.org>
parents: 30022
diff changeset
   101
          % rsub(r"'", '', str(inst)))
1914db1b7d9e demandimport: do not raise ImportError for unknown item in fromlist
Yuya Nishihara <yuya@tcha.org>
parents: 30022
diff changeset
   102
1914db1b7d9e demandimport: do not raise ImportError for unknown item in fromlist
Yuya Nishihara <yuya@tcha.org>
parents: 30022
diff changeset
   103
# 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: 30022
diff changeset
   104
# 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: 30022
diff changeset
   105
# (see Python/import.c:import_module_level() and ensure_fromlist())
1914db1b7d9e demandimport: do not raise ImportError for unknown item in fromlist
Yuya Nishihara <yuya@tcha.org>
parents: 30022
diff changeset
   106
contextlibimp = __import__('contextlib', globals(), locals(), ['unknownattr'])
1914db1b7d9e demandimport: do not raise ImportError for unknown item in fromlist
Yuya Nishihara <yuya@tcha.org>
parents: 30022
diff changeset
   107
print("__import__('contextlib', ..., ['unknownattr']) =", f(contextlibimp))
1914db1b7d9e demandimport: do not raise ImportError for unknown item in fromlist
Yuya Nishihara <yuya@tcha.org>
parents: 30022
diff changeset
   108
print("hasattr(contextlibimp, 'unknownattr') =",
1914db1b7d9e demandimport: do not raise ImportError for unknown item in fromlist
Yuya Nishihara <yuya@tcha.org>
parents: 30022
diff changeset
   109
      util.safehasattr(contextlibimp, 'unknownattr'))