Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

# -*- encoding: utf-8 -*- 

""" 

Python 2 and 3 Compatibility 

 

TESTS: 

 

Test the functionality of ``six.with_metaclass`` and various issues 

which came up with it. Sage used to have a custom version of 

``with_metaclass``, but this is now fixed upstream. :: 

 

sage: from six import with_metaclass 

sage: class Meta(type): pass 

sage: class X(with_metaclass(Meta)): pass 

sage: type(X) is Meta 

True 

sage: issubclass(X, object) 

True 

sage: class Base(object): pass 

sage: class X(with_metaclass(Meta, Base)): pass 

sage: type(X) is Meta 

True 

sage: issubclass(X, Base) 

True 

sage: class Base2(object): pass 

sage: class X(with_metaclass(Meta, Base, Base2)): pass 

sage: type(X) is Meta 

True 

sage: issubclass(X, Base) 

True 

sage: issubclass(X, Base2) 

True 

sage: X.__mro__ == (X, Base, Base2, object) or X.__mro__ 

True 

 

Check that :trac:`18503` is fixed, i.e. that with_metaclass 

works with cdef'ed metaclasses:: 

 

sage: from sage.misc.classcall_metaclass import ClasscallMetaclass 

sage: class X(with_metaclass(ClasscallMetaclass)): pass 

sage: type(X) is ClasscallMetaclass 

True 

sage: X.__mro__ == (X, object) or X.__mro__ 

True 

 

Check a fix for :trac:`16074`:: 

 

sage: from sage.misc.inherit_comparison import InheritComparisonClasscallMetaclass 

sage: from sage.modules.with_basis.morphism import ModuleMorphismByLinearity 

sage: from sage.structure.unique_representation import UniqueRepresentation 

sage: class ExteriorAlgebraDifferential(with_metaclass( 

....: InheritComparisonClasscallMetaclass, 

....: ModuleMorphismByLinearity, UniqueRepresentation 

....: )): 

....: pass 

""" 

 

#***************************************************************************** 

# Copyright (C) 2017 Jeroen Demeyer <J.Demeyer@UGent.be> 

# 

# This program is free software: you can redistribute it and/or modify 

# it under the terms of the GNU General Public License as published by 

# the Free Software Foundation, either version 2 of the License, or 

# (at your option) any later version. 

# http://www.gnu.org/licenses/ 

#***************************************************************************** 

 

from __future__ import absolute_import 

 

from six import * 

 

 

def u(x): 

r""" 

Convert `x` to unicode, assuming UTF-8 encoding. 

 

Python2 behaviour: 

 

If input is unicode, returns the input. 

 

If input is str (assumed to be utf-8 encoded), convert to unicode. 

 

Python3 behaviour: 

 

If input is str, returns the input. 

 

If input is bytes (assumed to be utf-8 encoded), convert to unicode. 

 

EXAMPLES:: 

 

sage: from sage.misc.six import u 

sage: u("500 €") 

u'500 \u20ac' 

sage: u(u"500 \u20ac") 

u'500 \u20ac' 

""" 

if isinstance(x, text_type): # py2 unicode and py3 str 

return x 

if isinstance(x, bytes): 

return x.decode("utf-8") 

raise TypeError('input has no conversion to unicode')