1cdef union Spam: 2 int i 3 char c 4 float *p[42] 5 6cdef Spam spam, ham 7 8cdef void eggs_i(Spam s): 9 cdef int j 10 j = s.i 11 s.i = j 12 13cdef void eggs_c(Spam s): 14 cdef char c 15 c = s.c 16 s.c = c 17 18cdef void eggs_p(Spam s): 19 cdef float *p 20 p = s.p[0] 21 s.p[0] = p 22 23spam = ham 24 25 26def test_i(): 27 """ 28 >>> test_i() 29 """ 30 spam.i = 1 31 eggs_i(spam) 32 33 34def test_c(): 35 """ 36 >>> test_c() 37 """ 38 spam.c = c'a' 39 eggs_c(spam) 40 41 42def test_p(): 43 """ 44 >>> test_p() 45 """ 46 cdef float f 47 spam.p[0] = &f 48 eggs_p(spam) 49 50 51cdef union AllCharptr: 52 char* s1 53 char* s2 54 char* s3 55 56 57def test_charptr_to_py(): 58 """ 59 >>> result = test_charptr_to_py() 60 >>> len(result) 61 3 62 >>> result['s1'] == b'abc' 63 True 64 >>> result['s2'] == b'abc' 65 True 66 >>> result['s3'] == b'abc' 67 True 68 """ 69 cdef AllCharptr u 70 u.s1 = b"abc" 71 return u 72 73 74cdef union SafeMix: 75 char c 76 unsigned char uc 77 signed char sc 78 short w 79 int i 80 long l 81 size_t z 82 float f 83 double d 84 85 86def test_safe_type_mix_from_to_py(v): 87 """ 88 >>> test_safe_type_mix_from_to_py({'l': 32, 'c': 32}) 89 Traceback (most recent call last): 90 ValueError: More than one union attribute passed: 'c' and 'l' 91 92 >>> result = test_safe_type_mix_from_to_py({'c': 32}) 93 >>> sorted(result) 94 ['c', 'd', 'f', 'i', 'l', 'sc', 'uc', 'w', 'z'] 95 >>> result['c'] 96 32 97 >>> result['z'] != 0 98 True 99 100 >>> result = test_safe_type_mix_from_to_py({'uc': 32}) 101 >>> len(result) 102 9 103 >>> result['uc'] 104 32 105 106 >>> result = test_safe_type_mix_from_to_py({'l': 100}) 107 >>> result['l'] 108 100 109 110 >>> result = test_safe_type_mix_from_to_py({'z': 0}) 111 >>> result['z'] 112 0 113 >>> result['i'] 114 0 115 >>> result['l'] 116 0 117 118 >>> result = test_safe_type_mix_from_to_py({'d': 2**52 - 1}) 119 >>> result['d'] 120 4503599627370495.0 121 >>> result['z'] != 0 122 True 123 """ 124 cdef SafeMix u = v 125 return u 126