| 1 | n/a | # |
|---|
| 2 | n/a | # Copyright (c) 2008-2012 Stefan Krah. All rights reserved. |
|---|
| 3 | n/a | # |
|---|
| 4 | n/a | # Redistribution and use in source and binary forms, with or without |
|---|
| 5 | n/a | # modification, are permitted provided that the following conditions |
|---|
| 6 | n/a | # are met: |
|---|
| 7 | n/a | # |
|---|
| 8 | n/a | # 1. Redistributions of source code must retain the above copyright |
|---|
| 9 | n/a | # notice, this list of conditions and the following disclaimer. |
|---|
| 10 | n/a | # |
|---|
| 11 | n/a | # 2. Redistributions in binary form must reproduce the above copyright |
|---|
| 12 | n/a | # notice, this list of conditions and the following disclaimer in the |
|---|
| 13 | n/a | # documentation and/or other materials provided with the distribution. |
|---|
| 14 | n/a | # |
|---|
| 15 | n/a | # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS "AS IS" AND |
|---|
| 16 | n/a | # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|---|
| 17 | n/a | # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
|---|
| 18 | n/a | # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
|---|
| 19 | n/a | # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
|---|
| 20 | n/a | # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
|---|
| 21 | n/a | # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
|---|
| 22 | n/a | # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
|---|
| 23 | n/a | # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
|---|
| 24 | n/a | # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
|---|
| 25 | n/a | # SUCH DAMAGE. |
|---|
| 26 | n/a | # |
|---|
| 27 | n/a | |
|---|
| 28 | n/a | |
|---|
| 29 | n/a | # Generate PEP-3101 format strings. |
|---|
| 30 | n/a | |
|---|
| 31 | n/a | |
|---|
| 32 | n/a | import os, sys, locale, random |
|---|
| 33 | n/a | import platform, subprocess |
|---|
| 34 | n/a | from test.support import import_fresh_module |
|---|
| 35 | n/a | from distutils.spawn import find_executable |
|---|
| 36 | n/a | |
|---|
| 37 | n/a | C = import_fresh_module('decimal', fresh=['_decimal']) |
|---|
| 38 | n/a | P = import_fresh_module('decimal', blocked=['_decimal']) |
|---|
| 39 | n/a | |
|---|
| 40 | n/a | |
|---|
| 41 | n/a | windows_lang_strings = [ |
|---|
| 42 | n/a | "chinese", "chinese-simplified", "chinese-traditional", "czech", "danish", |
|---|
| 43 | n/a | "dutch", "belgian", "english", "australian", "canadian", "english-nz", |
|---|
| 44 | n/a | "english-uk", "english-us", "finnish", "french", "french-belgian", |
|---|
| 45 | n/a | "french-canadian", "french-swiss", "german", "german-austrian", |
|---|
| 46 | n/a | "german-swiss", "greek", "hungarian", "icelandic", "italian", "italian-swiss", |
|---|
| 47 | n/a | "japanese", "korean", "norwegian", "norwegian-bokmal", "norwegian-nynorsk", |
|---|
| 48 | n/a | "polish", "portuguese", "portuguese-brazil", "russian", "slovak", "spanish", |
|---|
| 49 | n/a | "spanish-mexican", "spanish-modern", "swedish", "turkish", |
|---|
| 50 | n/a | ] |
|---|
| 51 | n/a | |
|---|
| 52 | n/a | preferred_encoding = { |
|---|
| 53 | n/a | 'cs_CZ': 'ISO8859-2', |
|---|
| 54 | n/a | 'cs_CZ.iso88592': 'ISO8859-2', |
|---|
| 55 | n/a | 'czech': 'ISO8859-2', |
|---|
| 56 | n/a | 'eesti': 'ISO8859-1', |
|---|
| 57 | n/a | 'estonian': 'ISO8859-1', |
|---|
| 58 | n/a | 'et_EE': 'ISO8859-15', |
|---|
| 59 | n/a | 'et_EE.ISO-8859-15': 'ISO8859-15', |
|---|
| 60 | n/a | 'et_EE.iso885915': 'ISO8859-15', |
|---|
| 61 | n/a | 'et_EE.iso88591': 'ISO8859-1', |
|---|
| 62 | n/a | 'fi_FI.iso88591': 'ISO8859-1', |
|---|
| 63 | n/a | 'fi_FI': 'ISO8859-15', |
|---|
| 64 | n/a | 'fi_FI@euro': 'ISO8859-15', |
|---|
| 65 | n/a | 'fi_FI.iso885915@euro': 'ISO8859-15', |
|---|
| 66 | n/a | 'finnish': 'ISO8859-1', |
|---|
| 67 | n/a | 'lv_LV': 'ISO8859-13', |
|---|
| 68 | n/a | 'lv_LV.iso885913': 'ISO8859-13', |
|---|
| 69 | n/a | 'nb_NO': 'ISO8859-1', |
|---|
| 70 | n/a | 'nb_NO.iso88591': 'ISO8859-1', |
|---|
| 71 | n/a | 'bokmal': 'ISO8859-1', |
|---|
| 72 | n/a | 'nn_NO': 'ISO8859-1', |
|---|
| 73 | n/a | 'nn_NO.iso88591': 'ISO8859-1', |
|---|
| 74 | n/a | 'no_NO': 'ISO8859-1', |
|---|
| 75 | n/a | 'norwegian': 'ISO8859-1', |
|---|
| 76 | n/a | 'nynorsk': 'ISO8859-1', |
|---|
| 77 | n/a | 'ru_RU': 'ISO8859-5', |
|---|
| 78 | n/a | 'ru_RU.iso88595': 'ISO8859-5', |
|---|
| 79 | n/a | 'russian': 'ISO8859-5', |
|---|
| 80 | n/a | 'ru_RU.KOI8-R': 'KOI8-R', |
|---|
| 81 | n/a | 'ru_RU.koi8r': 'KOI8-R', |
|---|
| 82 | n/a | 'ru_RU.CP1251': 'CP1251', |
|---|
| 83 | n/a | 'ru_RU.cp1251': 'CP1251', |
|---|
| 84 | n/a | 'sk_SK': 'ISO8859-2', |
|---|
| 85 | n/a | 'sk_SK.iso88592': 'ISO8859-2', |
|---|
| 86 | n/a | 'slovak': 'ISO8859-2', |
|---|
| 87 | n/a | 'sv_FI': 'ISO8859-1', |
|---|
| 88 | n/a | 'sv_FI.iso88591': 'ISO8859-1', |
|---|
| 89 | n/a | 'sv_FI@euro': 'ISO8859-15', |
|---|
| 90 | n/a | 'sv_FI.iso885915@euro': 'ISO8859-15', |
|---|
| 91 | n/a | 'uk_UA': 'KOI8-U', |
|---|
| 92 | n/a | 'uk_UA.koi8u': 'KOI8-U' |
|---|
| 93 | n/a | } |
|---|
| 94 | n/a | |
|---|
| 95 | n/a | integers = [ |
|---|
| 96 | n/a | "", |
|---|
| 97 | n/a | "1", |
|---|
| 98 | n/a | "12", |
|---|
| 99 | n/a | "123", |
|---|
| 100 | n/a | "1234", |
|---|
| 101 | n/a | "12345", |
|---|
| 102 | n/a | "123456", |
|---|
| 103 | n/a | "1234567", |
|---|
| 104 | n/a | "12345678", |
|---|
| 105 | n/a | "123456789", |
|---|
| 106 | n/a | "1234567890", |
|---|
| 107 | n/a | "12345678901", |
|---|
| 108 | n/a | "123456789012", |
|---|
| 109 | n/a | "1234567890123", |
|---|
| 110 | n/a | "12345678901234", |
|---|
| 111 | n/a | "123456789012345", |
|---|
| 112 | n/a | "1234567890123456", |
|---|
| 113 | n/a | "12345678901234567", |
|---|
| 114 | n/a | "123456789012345678", |
|---|
| 115 | n/a | "1234567890123456789", |
|---|
| 116 | n/a | "12345678901234567890", |
|---|
| 117 | n/a | "123456789012345678901", |
|---|
| 118 | n/a | "1234567890123456789012", |
|---|
| 119 | n/a | ] |
|---|
| 120 | n/a | |
|---|
| 121 | n/a | numbers = [ |
|---|
| 122 | n/a | "0", "-0", "+0", |
|---|
| 123 | n/a | "0.0", "-0.0", "+0.0", |
|---|
| 124 | n/a | "0e0", "-0e0", "+0e0", |
|---|
| 125 | n/a | ".0", "-.0", |
|---|
| 126 | n/a | ".1", "-.1", |
|---|
| 127 | n/a | "1.1", "-1.1", |
|---|
| 128 | n/a | "1e1", "-1e1" |
|---|
| 129 | n/a | ] |
|---|
| 130 | n/a | |
|---|
| 131 | n/a | # Get the list of available locales. |
|---|
| 132 | n/a | if platform.system() == 'Windows': |
|---|
| 133 | n/a | locale_list = windows_lang_strings |
|---|
| 134 | n/a | else: |
|---|
| 135 | n/a | locale_list = ['C'] |
|---|
| 136 | n/a | if os.path.isfile("/var/lib/locales/supported.d/local"): |
|---|
| 137 | n/a | # On Ubuntu, `locale -a` gives the wrong case for some locales, |
|---|
| 138 | n/a | # so we get the correct names directly: |
|---|
| 139 | n/a | with open("/var/lib/locales/supported.d/local") as f: |
|---|
| 140 | n/a | locale_list = [loc.split()[0] for loc in f.readlines() \ |
|---|
| 141 | n/a | if not loc.startswith('#')] |
|---|
| 142 | n/a | elif find_executable('locale'): |
|---|
| 143 | n/a | locale_list = subprocess.Popen(["locale", "-a"], |
|---|
| 144 | n/a | stdout=subprocess.PIPE).communicate()[0] |
|---|
| 145 | n/a | try: |
|---|
| 146 | n/a | locale_list = locale_list.decode() |
|---|
| 147 | n/a | except UnicodeDecodeError: |
|---|
| 148 | n/a | # Some distributions insist on using latin-1 characters |
|---|
| 149 | n/a | # in their locale names. |
|---|
| 150 | n/a | locale_list = locale_list.decode('latin-1') |
|---|
| 151 | n/a | locale_list = locale_list.split('\n') |
|---|
| 152 | n/a | try: |
|---|
| 153 | n/a | locale_list.remove('') |
|---|
| 154 | n/a | except ValueError: |
|---|
| 155 | n/a | pass |
|---|
| 156 | n/a | |
|---|
| 157 | n/a | # Debian |
|---|
| 158 | n/a | if os.path.isfile("/etc/locale.alias"): |
|---|
| 159 | n/a | with open("/etc/locale.alias") as f: |
|---|
| 160 | n/a | while 1: |
|---|
| 161 | n/a | try: |
|---|
| 162 | n/a | line = f.readline() |
|---|
| 163 | n/a | except UnicodeDecodeError: |
|---|
| 164 | n/a | continue |
|---|
| 165 | n/a | if line == "": |
|---|
| 166 | n/a | break |
|---|
| 167 | n/a | if line.startswith('#'): |
|---|
| 168 | n/a | continue |
|---|
| 169 | n/a | x = line.split() |
|---|
| 170 | n/a | if len(x) == 2: |
|---|
| 171 | n/a | if x[0] in locale_list: |
|---|
| 172 | n/a | locale_list.remove(x[0]) |
|---|
| 173 | n/a | |
|---|
| 174 | n/a | # FreeBSD |
|---|
| 175 | n/a | if platform.system() == 'FreeBSD': |
|---|
| 176 | n/a | # http://www.freebsd.org/cgi/query-pr.cgi?pr=142173 |
|---|
| 177 | n/a | # en_GB.US-ASCII has 163 as the currency symbol. |
|---|
| 178 | n/a | for loc in ['it_CH.ISO8859-1', 'it_CH.ISO8859-15', 'it_CH.UTF-8', |
|---|
| 179 | n/a | 'it_IT.ISO8859-1', 'it_IT.ISO8859-15', 'it_IT.UTF-8', |
|---|
| 180 | n/a | 'sl_SI.ISO8859-2', 'sl_SI.UTF-8', |
|---|
| 181 | n/a | 'en_GB.US-ASCII']: |
|---|
| 182 | n/a | try: |
|---|
| 183 | n/a | locale_list.remove(loc) |
|---|
| 184 | n/a | except ValueError: |
|---|
| 185 | n/a | pass |
|---|
| 186 | n/a | |
|---|
| 187 | n/a | # Print a testcase in the format of the IBM tests (for runtest.c): |
|---|
| 188 | n/a | def get_preferred_encoding(): |
|---|
| 189 | n/a | loc = locale.setlocale(locale.LC_CTYPE) |
|---|
| 190 | n/a | if loc in preferred_encoding: |
|---|
| 191 | n/a | return preferred_encoding[loc] |
|---|
| 192 | n/a | else: |
|---|
| 193 | n/a | return locale.getpreferredencoding() |
|---|
| 194 | n/a | |
|---|
| 195 | n/a | def printit(testno, s, fmt, encoding=None): |
|---|
| 196 | n/a | if not encoding: |
|---|
| 197 | n/a | encoding = get_preferred_encoding() |
|---|
| 198 | n/a | try: |
|---|
| 199 | n/a | result = format(P.Decimal(s), fmt) |
|---|
| 200 | n/a | fmt = str(fmt.encode(encoding))[2:-1] |
|---|
| 201 | n/a | result = str(result.encode(encoding))[2:-1] |
|---|
| 202 | n/a | if "'" in result: |
|---|
| 203 | n/a | sys.stdout.write("xfmt%d format %s '%s' -> \"%s\"\n" |
|---|
| 204 | n/a | % (testno, s, fmt, result)) |
|---|
| 205 | n/a | else: |
|---|
| 206 | n/a | sys.stdout.write("xfmt%d format %s '%s' -> '%s'\n" |
|---|
| 207 | n/a | % (testno, s, fmt, result)) |
|---|
| 208 | n/a | except Exception as err: |
|---|
| 209 | n/a | sys.stderr.write("%s %s %s\n" % (err, s, fmt)) |
|---|
| 210 | n/a | |
|---|
| 211 | n/a | |
|---|
| 212 | n/a | # Check if an integer can be converted to a valid fill character. |
|---|
| 213 | n/a | def check_fillchar(i): |
|---|
| 214 | n/a | try: |
|---|
| 215 | n/a | c = chr(i) |
|---|
| 216 | n/a | c.encode('utf-8').decode() |
|---|
| 217 | n/a | format(P.Decimal(0), c + '<19g') |
|---|
| 218 | n/a | return c |
|---|
| 219 | n/a | except: |
|---|
| 220 | n/a | return None |
|---|
| 221 | n/a | |
|---|
| 222 | n/a | # Generate all unicode characters that are accepted as |
|---|
| 223 | n/a | # fill characters by decimal.py. |
|---|
| 224 | n/a | def all_fillchars(): |
|---|
| 225 | n/a | for i in range(0, 0x110002): |
|---|
| 226 | n/a | c = check_fillchar(i) |
|---|
| 227 | n/a | if c: yield c |
|---|
| 228 | n/a | |
|---|
| 229 | n/a | # Return random fill character. |
|---|
| 230 | n/a | def rand_fillchar(): |
|---|
| 231 | n/a | while 1: |
|---|
| 232 | n/a | i = random.randrange(0, 0x110002) |
|---|
| 233 | n/a | c = check_fillchar(i) |
|---|
| 234 | n/a | if c: return c |
|---|
| 235 | n/a | |
|---|
| 236 | n/a | # Generate random format strings |
|---|
| 237 | n/a | # [[fill]align][sign][#][0][width][.precision][type] |
|---|
| 238 | n/a | def rand_format(fill, typespec='EeGgFfn%'): |
|---|
| 239 | n/a | active = sorted(random.sample(range(7), random.randrange(8))) |
|---|
| 240 | n/a | have_align = 0 |
|---|
| 241 | n/a | s = '' |
|---|
| 242 | n/a | for elem in active: |
|---|
| 243 | n/a | if elem == 0: # fill+align |
|---|
| 244 | n/a | s += fill |
|---|
| 245 | n/a | s += random.choice('<>=^') |
|---|
| 246 | n/a | have_align = 1 |
|---|
| 247 | n/a | elif elem == 1: # sign |
|---|
| 248 | n/a | s += random.choice('+- ') |
|---|
| 249 | n/a | elif elem == 2 and not have_align: # zeropad |
|---|
| 250 | n/a | s += '0' |
|---|
| 251 | n/a | elif elem == 3: # width |
|---|
| 252 | n/a | s += str(random.randrange(1, 100)) |
|---|
| 253 | n/a | elif elem == 4: # thousands separator |
|---|
| 254 | n/a | s += ',' |
|---|
| 255 | n/a | elif elem == 5: # prec |
|---|
| 256 | n/a | s += '.' |
|---|
| 257 | n/a | s += str(random.randrange(100)) |
|---|
| 258 | n/a | elif elem == 6: |
|---|
| 259 | n/a | if 4 in active: c = typespec.replace('n', '') |
|---|
| 260 | n/a | else: c = typespec |
|---|
| 261 | n/a | s += random.choice(c) |
|---|
| 262 | n/a | return s |
|---|
| 263 | n/a | |
|---|
| 264 | n/a | # Partially brute force all possible format strings containing a thousands |
|---|
| 265 | n/a | # separator. Fall back to random where the runtime would become excessive. |
|---|
| 266 | n/a | # [[fill]align][sign][#][0][width][,][.precision][type] |
|---|
| 267 | n/a | def all_format_sep(): |
|---|
| 268 | n/a | for align in ('', '<', '>', '=', '^'): |
|---|
| 269 | n/a | for fill in ('', 'x'): |
|---|
| 270 | n/a | if align == '': fill = '' |
|---|
| 271 | n/a | for sign in ('', '+', '-', ' '): |
|---|
| 272 | n/a | for zeropad in ('', '0'): |
|---|
| 273 | n/a | if align != '': zeropad = '' |
|---|
| 274 | n/a | for width in ['']+[str(y) for y in range(1, 15)]+['101']: |
|---|
| 275 | n/a | for prec in ['']+['.'+str(y) for y in range(15)]: |
|---|
| 276 | n/a | # for type in ('', 'E', 'e', 'G', 'g', 'F', 'f', '%'): |
|---|
| 277 | n/a | type = random.choice(('', 'E', 'e', 'G', 'g', 'F', 'f', '%')) |
|---|
| 278 | n/a | yield ''.join((fill, align, sign, zeropad, width, ',', prec, type)) |
|---|
| 279 | n/a | |
|---|
| 280 | n/a | # Partially brute force all possible format strings with an 'n' specifier. |
|---|
| 281 | n/a | # [[fill]align][sign][#][0][width][,][.precision][type] |
|---|
| 282 | n/a | def all_format_loc(): |
|---|
| 283 | n/a | for align in ('', '<', '>', '=', '^'): |
|---|
| 284 | n/a | for fill in ('', 'x'): |
|---|
| 285 | n/a | if align == '': fill = '' |
|---|
| 286 | n/a | for sign in ('', '+', '-', ' '): |
|---|
| 287 | n/a | for zeropad in ('', '0'): |
|---|
| 288 | n/a | if align != '': zeropad = '' |
|---|
| 289 | n/a | for width in ['']+[str(y) for y in range(1, 20)]+['101']: |
|---|
| 290 | n/a | for prec in ['']+['.'+str(y) for y in range(1, 20)]: |
|---|
| 291 | n/a | yield ''.join((fill, align, sign, zeropad, width, prec, 'n')) |
|---|
| 292 | n/a | |
|---|
| 293 | n/a | # Generate random format strings with a unicode fill character |
|---|
| 294 | n/a | # [[fill]align][sign][#][0][width][,][.precision][type] |
|---|
| 295 | n/a | def randfill(fill): |
|---|
| 296 | n/a | active = sorted(random.sample(range(5), random.randrange(6))) |
|---|
| 297 | n/a | s = '' |
|---|
| 298 | n/a | s += str(fill) |
|---|
| 299 | n/a | s += random.choice('<>=^') |
|---|
| 300 | n/a | for elem in active: |
|---|
| 301 | n/a | if elem == 0: # sign |
|---|
| 302 | n/a | s += random.choice('+- ') |
|---|
| 303 | n/a | elif elem == 1: # width |
|---|
| 304 | n/a | s += str(random.randrange(1, 100)) |
|---|
| 305 | n/a | elif elem == 2: # thousands separator |
|---|
| 306 | n/a | s += ',' |
|---|
| 307 | n/a | elif elem == 3: # prec |
|---|
| 308 | n/a | s += '.' |
|---|
| 309 | n/a | s += str(random.randrange(100)) |
|---|
| 310 | n/a | elif elem == 4: |
|---|
| 311 | n/a | if 2 in active: c = 'EeGgFf%' |
|---|
| 312 | n/a | else: c = 'EeGgFfn%' |
|---|
| 313 | n/a | s += random.choice(c) |
|---|
| 314 | n/a | return s |
|---|
| 315 | n/a | |
|---|
| 316 | n/a | # Generate random format strings with random locale setting |
|---|
| 317 | n/a | # [[fill]align][sign][#][0][width][,][.precision][type] |
|---|
| 318 | n/a | def rand_locale(): |
|---|
| 319 | n/a | try: |
|---|
| 320 | n/a | loc = random.choice(locale_list) |
|---|
| 321 | n/a | locale.setlocale(locale.LC_ALL, loc) |
|---|
| 322 | n/a | except locale.Error as err: |
|---|
| 323 | n/a | pass |
|---|
| 324 | n/a | active = sorted(random.sample(range(5), random.randrange(6))) |
|---|
| 325 | n/a | s = '' |
|---|
| 326 | n/a | have_align = 0 |
|---|
| 327 | n/a | for elem in active: |
|---|
| 328 | n/a | if elem == 0: # fill+align |
|---|
| 329 | n/a | s += chr(random.randrange(32, 128)) |
|---|
| 330 | n/a | s += random.choice('<>=^') |
|---|
| 331 | n/a | have_align = 1 |
|---|
| 332 | n/a | elif elem == 1: # sign |
|---|
| 333 | n/a | s += random.choice('+- ') |
|---|
| 334 | n/a | elif elem == 2 and not have_align: # zeropad |
|---|
| 335 | n/a | s += '0' |
|---|
| 336 | n/a | elif elem == 3: # width |
|---|
| 337 | n/a | s += str(random.randrange(1, 100)) |
|---|
| 338 | n/a | elif elem == 4: # prec |
|---|
| 339 | n/a | s += '.' |
|---|
| 340 | n/a | s += str(random.randrange(100)) |
|---|
| 341 | n/a | s += 'n' |
|---|
| 342 | n/a | return s |
|---|