~ubuntu-branches/debian/experimental/nuitka/experimental

« back to all changes in this revision

Viewing changes to nuitka/tree/ImportCache.py

  • Committer: Package Import Robot
  • Author(s): Kay Hayen
  • Date: 2015-04-06 17:20:44 UTC
  • mfrom: (1.1.37)
  • Revision ID: package-import@ubuntu.com-20150406172044-grhy9sk7g0whu2ag
Tags: 0.5.12+ds-1
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#     Copyright 2015, Kay Hayen, mailto:kay.hayen@gmail.com
2
 
#
3
 
#     Part of "Nuitka", an optimizing Python compiler that is compatible and
4
 
#     integrates with CPython, but also works on its own.
5
 
#
6
 
#     Licensed under the Apache License, Version 2.0 (the "License");
7
 
#     you may not use this file except in compliance with the License.
8
 
#     You may obtain a copy of the License at
9
 
#
10
 
#        http://www.apache.org/licenses/LICENSE-2.0
11
 
#
12
 
#     Unless required by applicable law or agreed to in writing, software
13
 
#     distributed under the License is distributed on an "AS IS" BASIS,
14
 
#     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 
#     See the License for the specific language governing permissions and
16
 
#     limitations under the License.
17
 
#
18
 
""" Import cache.
19
 
 
20
 
This is not about caching the search of modules in the file system, but about
21
 
maintaining a cache of module trees built.
22
 
 
23
 
It can happen that modules become unused, and then dropped from active modules,
24
 
and then later active again, via another import, and in this case, we should
25
 
not start anew.
26
 
"""
27
 
 
28
 
from logging import warning
29
 
 
30
 
from nuitka import Utils
31
 
 
32
 
imported_modules = {}
33
 
imported_by_name = {}
34
 
 
35
 
def addImportedModule(module_relpath, imported_module):
36
 
    if (module_relpath, "__main__") in imported_modules:
37
 
        warning("""\
38
 
Re-importing __main__ module via its filename duplicates the module.""")
39
 
 
40
 
    key = module_relpath, imported_module.getFullName()
41
 
 
42
 
    if key in imported_modules:
43
 
        assert imported_module is imported_modules[ key ], key
44
 
 
45
 
    imported_modules[ key ] = imported_module
46
 
    imported_by_name[ imported_module.getFullName() ] = imported_module
47
 
 
48
 
def isImportedModuleByPath(module_relpath):
49
 
    module_name = Utils.basename(module_relpath)
50
 
 
51
 
    if module_name.endswith(".py"):
52
 
        module_name = module_name[:-3]
53
 
 
54
 
    key = module_relpath, module_name
55
 
 
56
 
    return key in imported_modules
57
 
 
58
 
def isImportedModuleByName(full_name):
59
 
    return full_name in imported_by_name
60
 
 
61
 
def getImportedModuleByName(full_name):
62
 
    return imported_by_name[ full_name ]
63
 
 
64
 
def getImportedModuleByPath(module_relpath):
65
 
    module_name = Utils.basename(module_relpath)
66
 
 
67
 
    if module_name.endswith(".py"):
68
 
        module_name = module_name[:-3]
69
 
 
70
 
    key = module_relpath, module_name
71
 
 
72
 
    return imported_modules[ key ]