/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to breezy/lazy_import.py

  • Committer: Jelmer Vernooij
  • Date: 2020-01-11 17:41:33 UTC
  • mto: This revision was merged to the branch mainline in revision 7440.
  • Revision ID: jelmer@jelmer.uk-20200111174133-ob2p0twwsmvw5ut7
Don't lazy-import errors.

Show diffs side-by-side

added added

removed removed

Lines of Context:
43
43
 
44
44
from __future__ import absolute_import
45
45
 
 
46
from .errors import BzrError, InternalBzrError
 
47
 
 
48
 
 
49
class ImportNameCollision(InternalBzrError):
 
50
 
 
51
    _fmt = ("Tried to import an object to the same name as"
 
52
            " an existing object. %(name)s")
 
53
 
 
54
    def __init__(self, name):
 
55
        BzrError.__init__(self)
 
56
        self.name = name
 
57
 
 
58
 
 
59
class IllegalUseOfScopeReplacer(InternalBzrError):
 
60
 
 
61
    _fmt = ("ScopeReplacer object %(name)r was used incorrectly:"
 
62
            " %(msg)s%(extra)s")
 
63
 
 
64
    def __init__(self, name, msg, extra=None):
 
65
        BzrError.__init__(self)
 
66
        self.name = name
 
67
        self.msg = msg
 
68
        if extra:
 
69
            self.extra = ': ' + str(extra)
 
70
        else:
 
71
            self.extra = ''
 
72
 
 
73
 
 
74
class InvalidImportLine(InternalBzrError):
 
75
 
 
76
    _fmt = "Not a valid import statement: %(msg)\n%(text)s"
 
77
 
 
78
    def __init__(self, text, msg):
 
79
        BzrError.__init__(self)
 
80
        self.text = text
 
81
        self.msg = msg
 
82
 
46
83
 
47
84
class ScopeReplacer(object):
48
85
    """A lazy object that will replace itself in the appropriate scope.
84
121
            scope = object.__getattribute__(self, '_scope')
85
122
            obj = factory(self, scope, name)
86
123
            if obj is self:
87
 
                raise errors.IllegalUseOfScopeReplacer(name, msg="Object tried"
88
 
                                                       " to replace itself, check it's not using its own scope.")
 
124
                raise IllegalUseOfScopeReplacer(
 
125
                    name, msg="Object tried"
 
126
                    " to replace itself, check it's not using its own scope.")
89
127
 
90
128
            # Check if another thread has jumped in while obj was generated.
91
129
            real_obj = object.__getattribute__(self, '_real_obj')
99
137
 
100
138
        # Raise if proxying is disabled as obj has already been generated.
101
139
        if not ScopeReplacer._should_proxy:
102
 
            raise errors.IllegalUseOfScopeReplacer(
 
140
            raise IllegalUseOfScopeReplacer(
103
141
                name, msg="Object already replaced, did you assign it"
104
142
                          " to another variable?")
105
143
        return real_obj
260
298
            elif line.startswith('from '):
261
299
                self._convert_from_str(line)
262
300
            else:
263
 
                raise errors.InvalidImportLine(line,
264
 
                                               "doesn't start with 'import ' or 'from '")
 
301
                raise InvalidImportLine(
 
302
                    line, "doesn't start with 'import ' or 'from '")
265
303
 
266
304
    def _convert_import_str(self, import_str):
267
305
        """This converts a import string into an import map.
286
324
                name = as_hunks[1].strip()
287
325
                module_path = as_hunks[0].strip().split('.')
288
326
                if name in self.imports:
289
 
                    raise errors.ImportNameCollision(name)
 
327
                    raise ImportNameCollision(name)
290
328
                if not module_path[0]:
291
329
                    raise ImportError(path)
292
330
                # No children available in 'import foo as bar'
345
383
            else:
346
384
                name = module = path
347
385
            if name in self.imports:
348
 
                raise errors.ImportNameCollision(name)
 
386
                raise ImportNameCollision(name)
349
387
            self.imports[name] = (from_module_path, module, {})
350
388
 
351
389
    def _canonicalize_import_text(self, text):
377
415
                else:
378
416
                    out.append(line.replace('(', '').replace(')', ''))
379
417
        if cur is not None:
380
 
            raise errors.InvalidImportLine(cur, 'Unmatched parenthesis')
 
418
            raise InvalidImportLine(cur, 'Unmatched parenthesis')
381
419
        return out
382
420
 
383
421
 
408
446
    # This is just a helper around ImportProcessor.lazy_import
409
447
    proc = ImportProcessor(lazy_import_class=lazy_import_class)
410
448
    return proc.lazy_import(scope, text)
411
 
 
412
 
 
413
 
# The only module that this module depends on is 'breezy.errors'. But it
414
 
# can actually be imported lazily, since we only need it if there is a
415
 
# problem.
416
 
 
417
 
lazy_import(globals(), """
418
 
from breezy import errors
419
 
""")