|
5 | 5 | import os |
6 | 6 | import string |
7 | 7 | import sys |
8 | | - |
9 | | -import six |
| 8 | +from io import StringIO |
10 | 9 |
|
11 | 10 | __all__ = [ |
12 | 11 | 'Template', |
|
43 | 42 | } |
44 | 43 | } |
45 | 44 |
|
46 | | -try: |
47 | | - dict |
48 | | -except NameError: |
49 | | - from UserDict import UserDict |
50 | | - |
51 | | - class dict(UserDict): |
52 | | - |
53 | | - def __init__(self): |
54 | | - self.data = {} |
55 | | -try: |
56 | | - operator.__gt__ |
57 | | -except AttributeError: |
58 | | - operator.__gt__ = lambda a, b: a > b |
59 | | - operator.__lt__ = lambda a, b: a < b |
60 | | - operator.__ge__ = lambda a, b: a >= b |
61 | | - operator.__le__ = lambda a, b: a <= b |
62 | | - operator.__eq__ = lambda a, b: a == b |
63 | | - operator.__ne__ = lambda a, b: a != b |
64 | | - operator.mod = lambda a, b: a % b |
65 | | -try: |
66 | | - basestring |
67 | | - |
68 | | - def is_string(s): |
69 | | - return isinstance(s, basestring) |
70 | | -except NameError: |
71 | | - def is_string(s): |
72 | | - return isinstance(s, type('')) |
73 | | - |
74 | 45 | ############################################################################### |
75 | 46 | # Public interface |
76 | 47 | ############################################################################### |
@@ -113,19 +84,19 @@ class TemplateError(Exception): |
113 | 84 |
|
114 | 85 |
|
115 | 86 | class TemplateExecutionError(TemplateError): |
116 | | - def __init__(self, element, exc_info): |
117 | | - cause, value, traceback = exc_info |
118 | | - self.__cause__ = value |
| 87 | + "This exception will always have a __cause__ attached." |
| 88 | + def __init__(self, element): |
119 | 89 | self.element = element |
120 | 90 | self.start, self.end, self.filename = (element.start, element.end, |
121 | 91 | element.filename) |
122 | | - self.msg = "Error in template '%s' at position " \ |
123 | | - "%d-%d in expression: %s\n%s: %s" % \ |
124 | | - (self.filename, self.start, self.end, |
125 | | - element.my_text(), cause.__name__, value) |
126 | 92 |
|
127 | 93 | def __str__(self): |
128 | | - return self.msg |
| 94 | + return "Error in template '%s' at position " \ |
| 95 | + "%d-%d in expression: %s\n%s: %s" % \ |
| 96 | + (self.filename, self.start, self.end, |
| 97 | + self.element.my_text(), |
| 98 | + self.__cause__.__class__.__name__, |
| 99 | + self.__cause__) |
129 | 100 |
|
130 | 101 |
|
131 | 102 | class TemplateSyntaxError(TemplateError): |
@@ -207,14 +178,14 @@ def load_template(self, name): |
207 | 178 | return template |
208 | 179 |
|
209 | 180 |
|
210 | | -class StoppableStream(six.StringIO): |
| 181 | +class StoppableStream(StringIO): |
211 | 182 | def __init__(self, buf=''): |
212 | 183 | self.stop = False |
213 | | - six.StringIO.__init__(self, buf) |
| 184 | + StringIO.__init__(self, buf) |
214 | 185 |
|
215 | 186 | def write(self, s): |
216 | 187 | if not self.stop: |
217 | | - six.StringIO.write(self, s) |
| 188 | + StringIO.write(self, s) |
218 | 189 |
|
219 | 190 |
|
220 | 191 | ############################################################################### |
@@ -356,11 +327,9 @@ def evaluate(self, *args): |
356 | 327 | return self.evaluate_raw(*args) |
357 | 328 | except TemplateExecutionError: |
358 | 329 | raise |
359 | | - except: |
360 | | - exc_info = sys.exc_info() |
361 | | - six.reraise(TemplateExecutionError, |
362 | | - TemplateExecutionError(self, exc_info), exc_info[2]) |
363 | | - |
| 330 | + except Exception as e: |
| 331 | + _type, value, _tb = sys.exc_info() |
| 332 | + raise TemplateExecutionError(self) from e |
364 | 333 |
|
365 | 334 | class Text(_Element): |
366 | 335 | PLAIN = re.compile( |
@@ -621,7 +590,7 @@ def calculate(self, current_object, loader, top_namespace): |
621 | 590 | # If list make sure index is an integer |
622 | 591 | if isinstance( |
623 | 592 | result, list) and not isinstance( |
624 | | - array_index, six.integer_types): |
| 593 | + array_index, int): |
625 | 594 | raise ValueError( |
626 | 595 | "expected integer for array index, got '%s'" % |
627 | 596 | (array_index)) |
@@ -714,8 +683,7 @@ def parse(self): |
714 | 683 | self.require_match(self.END, ']') |
715 | 684 |
|
716 | 685 | def calculate(self, namespace, loader): |
717 | | - result = self.index.calculate(namespace, loader) |
718 | | - return result |
| 686 | + return self.index.calculate(namespace, loader) |
719 | 687 |
|
720 | 688 | class AlternateValue(_Element): |
721 | 689 | START = re.compile(r'\|(.*)$', re.S) |
@@ -757,10 +725,10 @@ def evaluate_raw(self, stream, namespace, loader): |
757 | 725 | value = '' |
758 | 726 | else: |
759 | 727 | value = self.my_text() |
760 | | - if is_string(value): |
| 728 | + if isinstance(value, str): |
761 | 729 | stream.write(value) |
762 | 730 | else: |
763 | | - stream.write(six.text_type(value)) |
| 731 | + stream.write(str(value)) |
764 | 732 |
|
765 | 733 |
|
766 | 734 | class Null: |
|
0 commit comments