tests/test-demandimport.py
author Martin von Zweigbergk <martinvonz@google.com>
Fri, 16 Feb 2018 11:33:56 -0800
changeset 36251 c2c5f9f6fa21
parent 36237 b39f0fdb0338
child 37964 1d0610fdd63b
permissions -rw-r--r--
tests: avoid referring to pvec in demandimport test Nothing else currently uses pvec, so we may want to delete it (but Augie Fackler says he may want to use it). To enable deletion, this patch replaces it by the error module in the demandimport test (any module works). However, since the error module had already been loaded at this point in the test (via the util module), I moved it earlier in the test so it's still not loaded (although I'm not sure if that's even relevant to the test). Differential Revision: https://phab.mercurial-scm.org/D2287
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
36251
c2c5f9f6fa21 tests: avoid referring to pvec in demandimport test
Martin von Zweigbergk <martinvonz@google.com>
parents: 36237
diff changeset
    44
# Test access to special attributes through demandmod proxy
c2c5f9f6fa21 tests: avoid referring to pvec in demandimport test
Martin von Zweigbergk <martinvonz@google.com>
parents: 36237
diff changeset
    45
from mercurial import error as errorproxy
c2c5f9f6fa21 tests: avoid referring to pvec in demandimport test
Martin von Zweigbergk <martinvonz@google.com>
parents: 36237
diff changeset
    46
print("errorproxy =", f(errorproxy))
c2c5f9f6fa21 tests: avoid referring to pvec in demandimport test
Martin von Zweigbergk <martinvonz@google.com>
parents: 36237
diff changeset
    47
print("errorproxy.__doc__ = %r"
c2c5f9f6fa21 tests: avoid referring to pvec in demandimport test
Martin von Zweigbergk <martinvonz@google.com>
parents: 36237
diff changeset
    48
      % (' '.join(errorproxy.__doc__.split()[:3]) + ' ...'))
c2c5f9f6fa21 tests: avoid referring to pvec in demandimport test
Martin von Zweigbergk <martinvonz@google.com>
parents: 36237
diff changeset
    49
print("errorproxy.__name__ = %r" % errorproxy.__name__)
c2c5f9f6fa21 tests: avoid referring to pvec in demandimport test
Martin von Zweigbergk <martinvonz@google.com>
parents: 36237
diff changeset
    50
# __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: 36237
diff changeset
    51
# resolved
c2c5f9f6fa21 tests: avoid referring to pvec in demandimport test
Martin von Zweigbergk <martinvonz@google.com>
parents: 36237
diff changeset
    52
print("errorproxy.__dict__['__name__'] = %r" % errorproxy.__dict__['__name__'])
c2c5f9f6fa21 tests: avoid referring to pvec in demandimport test
Martin von Zweigbergk <martinvonz@google.com>
parents: 36237
diff changeset
    53
print("errorproxy =", f(errorproxy))
c2c5f9f6fa21 tests: avoid referring to pvec in demandimport test
Martin von Zweigbergk <martinvonz@google.com>
parents: 36237
diff changeset
    54
4631
e3afa670e484 demandimport: fix issue579 and add a test
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    55
import os
e3afa670e484 demandimport: fix issue579 and add a test
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    56
28948
16390f4cccf0 py3: make test-demandimport use print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 27535
diff changeset
    57
print("os =", f(os))
16390f4cccf0 py3: make test-demandimport use print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 27535
diff changeset
    58
print("os.system =", f(os.system))
16390f4cccf0 py3: make test-demandimport use print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 27535
diff changeset
    59
print("os =", f(os))
4631
e3afa670e484 demandimport: fix issue579 and add a test
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    60
e3afa670e484 demandimport: fix issue579 and add a test
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    61
from mercurial import util
e3afa670e484 demandimport: fix issue579 and add a test
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    62
28948
16390f4cccf0 py3: make test-demandimport use print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 27535
diff changeset
    63
print("util =", f(util))
16390f4cccf0 py3: make test-demandimport use print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 27535
diff changeset
    64
print("util.system =", f(util.system))
16390f4cccf0 py3: make test-demandimport use print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 27535
diff changeset
    65
print("util =", f(util))
16390f4cccf0 py3: make test-demandimport use print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 27535
diff changeset
    66
print("util.system =", f(util.system))
4631
e3afa670e484 demandimport: fix issue579 and add a test
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    67
27535
0d0f4070f6d7 test-demandimport: ensure that relative imports are deferred
Bryan O'Sullivan <bos@serpentine.com>
parents: 23643
diff changeset
    68
from mercurial import hgweb
28948
16390f4cccf0 py3: make test-demandimport use print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 27535
diff changeset
    69
print("hgweb =", f(hgweb))
16390f4cccf0 py3: make test-demandimport use print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 27535
diff changeset
    70
print("hgweb_mod =", f(hgweb.hgweb_mod))
16390f4cccf0 py3: make test-demandimport use print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 27535
diff changeset
    71
print("hgweb =", f(hgweb))
27535
0d0f4070f6d7 test-demandimport: ensure that relative imports are deferred
Bryan O'Sullivan <bos@serpentine.com>
parents: 23643
diff changeset
    72
4631
e3afa670e484 demandimport: fix issue579 and add a test
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    73
import re as fred
28948
16390f4cccf0 py3: make test-demandimport use print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 27535
diff changeset
    74
print("fred =", f(fred))
4631
e3afa670e484 demandimport: fix issue579 and add a test
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    75
32447
252d2260c74e demandimport: look for 'mod' suffix as alternative name for module reference
Yuya Nishihara <yuya@tcha.org>
parents: 30647
diff changeset
    76
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
    77
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
    78
4631
e3afa670e484 demandimport: fix issue579 and add a test
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    79
import sys as re
28948
16390f4cccf0 py3: make test-demandimport use print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 27535
diff changeset
    80
print("re =", f(re))
4631
e3afa670e484 demandimport: fix issue579 and add a test
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    81
28948
16390f4cccf0 py3: make test-demandimport use print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 27535
diff changeset
    82
print("fred =", f(fred))
16390f4cccf0 py3: make test-demandimport use print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 27535
diff changeset
    83
print("fred.sub =", f(fred.sub))
16390f4cccf0 py3: make test-demandimport use print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 27535
diff changeset
    84
print("fred =", f(fred))
4631
e3afa670e484 demandimport: fix issue579 and add a test
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    85
32447
252d2260c74e demandimport: look for 'mod' suffix as alternative name for module reference
Yuya Nishihara <yuya@tcha.org>
parents: 30647
diff changeset
    86
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
    87
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
    88
28948
16390f4cccf0 py3: make test-demandimport use print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 27535
diff changeset
    89
print("re =", f(re))
16390f4cccf0 py3: make test-demandimport use print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 27535
diff changeset
    90
print("re.stderr =", f(re.stderr))
16390f4cccf0 py3: make test-demandimport use print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 27535
diff changeset
    91
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
    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'))