|
1 | 1 | import sys |
2 | 2 | import inspect |
| 3 | +from itertools import zip_longest |
3 | 4 |
|
4 | | -PY2 = sys.version_info[0] == 2 |
5 | 5 |
|
| 6 | +text_type = str |
| 7 | +string_type = str |
6 | 8 |
|
7 | | -def with_metaclass(meta, *bases): |
8 | | - # Taken from flask/six. |
9 | | - class metaclass(meta): |
10 | | - def __new__(cls, name, this_bases, d): |
11 | | - return meta(name, bases, d) |
12 | | - return type.__new__(metaclass, 'temporary_class', (), {}) |
13 | 9 |
|
| 10 | +def with_str_method(cls): |
| 11 | + # In python3, we don't need to do anything, we return a str type. |
| 12 | + return cls |
14 | 13 |
|
15 | | -if PY2: |
16 | | - text_type = unicode |
17 | | - string_type = basestring |
18 | | - from itertools import izip_longest as zip_longest |
| 14 | +def with_repr_method(cls): |
| 15 | + return cls |
19 | 16 |
|
20 | | - def with_str_method(cls): |
21 | | - """Class decorator that handles __str__ compat between py2 and py3.""" |
22 | | - # In python2, the __str__ should be __unicode__ |
23 | | - # and __str__ should return bytes. |
24 | | - cls.__unicode__ = cls.__str__ |
25 | | - def __str__(self): |
26 | | - return self.__unicode__().encode('utf-8') |
27 | | - cls.__str__ = __str__ |
28 | | - return cls |
29 | | - |
30 | | - def with_repr_method(cls): |
31 | | - """Class decorator that handle __repr__ with py2 and py3.""" |
32 | | - # This is almost the same thing as with_str_method *except* |
33 | | - # it uses the unicode_escape encoding. This also means we need to be |
34 | | - # careful encoding the input multiple times, so we only encode |
35 | | - # if we get a unicode type. |
36 | | - original_repr_method = cls.__repr__ |
37 | | - def __repr__(self): |
38 | | - original_repr = original_repr_method(self) |
39 | | - if isinstance(original_repr, text_type): |
40 | | - original_repr = original_repr.encode('unicode_escape') |
41 | | - return original_repr |
42 | | - cls.__repr__ = __repr__ |
43 | | - return cls |
44 | | - |
45 | | - def get_methods(cls): |
46 | | - for name, method in inspect.getmembers(cls, |
47 | | - predicate=inspect.ismethod): |
48 | | - yield name, method |
49 | | - |
50 | | -else: |
51 | | - text_type = str |
52 | | - string_type = str |
53 | | - from itertools import zip_longest |
54 | | - |
55 | | - def with_str_method(cls): |
56 | | - # In python3, we don't need to do anything, we return a str type. |
57 | | - return cls |
58 | | - |
59 | | - def with_repr_method(cls): |
60 | | - return cls |
61 | | - |
62 | | - def get_methods(cls): |
63 | | - for name, method in inspect.getmembers(cls, |
64 | | - predicate=inspect.isfunction): |
65 | | - yield name, method |
| 17 | +def get_methods(cls): |
| 18 | + for name, method in inspect.getmembers(cls, predicate=inspect.isfunction): |
| 19 | + yield name, method |
0 commit comments