mpy-coverage 1.0.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,14 @@
1
+ """MicroPython code coverage toolchain."""
2
+
3
+ from mpy_coverage._version import __version__ # noqa: F401
4
+
5
+
6
+ def __getattr__(name):
7
+ if name in ("merge_coverage_data", "run_report"):
8
+ from mpy_coverage.report import merge_coverage_data, run_report
9
+
10
+ return {"merge_coverage_data": merge_coverage_data, "run_report": run_report}[name]
11
+ raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
12
+
13
+
14
+ __all__ = ["merge_coverage_data", "run_report", "__version__"]
@@ -0,0 +1,3 @@
1
+ from mpy_coverage.cli import main
2
+
3
+ main()
File without changes
@@ -0,0 +1,460 @@
1
+ """
2
+ Process raw qstr file and output qstr data with length, hash and data bytes.
3
+
4
+ This script works with Python 3.3+.
5
+ """
6
+
7
+ import re
8
+ import sys
9
+
10
+ bytes_cons = bytes
11
+
12
+ # fmt: off
13
+ codepoint2name = {
14
+ 198: "AElig", 193: "Aacute", 194: "Acirc", 192: "Agrave", 913: "Alpha", 197: "Aring", 195: "Atilde",
15
+ 196: "Auml", 914: "Beta", 199: "Ccedil", 935: "Chi", 8225: "Dagger", 916: "Delta", 208: "ETH",
16
+ 201: "Eacute", 202: "Ecirc", 200: "Egrave", 917: "Epsilon", 919: "Eta", 203: "Euml", 915: "Gamma",
17
+ 205: "Iacute", 206: "Icirc", 204: "Igrave", 921: "Iota", 207: "Iuml", 922: "Kappa", 923: "Lambda",
18
+ 924: "Mu", 209: "Ntilde", 925: "Nu", 338: "OElig", 211: "Oacute", 212: "Ocirc", 210: "Ograve",
19
+ 937: "Omega", 927: "Omicron", 216: "Oslash", 213: "Otilde", 214: "Ouml", 934: "Phi", 928: "Pi",
20
+ 8243: "Prime", 936: "Psi", 929: "Rho", 352: "Scaron", 931: "Sigma", 222: "THORN", 932: "Tau",
21
+ 920: "Theta", 218: "Uacute", 219: "Ucirc", 217: "Ugrave", 933: "Upsilon", 220: "Uuml", 926: "Xi",
22
+ 221: "Yacute", 376: "Yuml", 918: "Zeta", 225: "aacute", 226: "acirc", 180: "acute", 230: "aelig",
23
+ 224: "agrave", 8501: "alefsym", 945: "alpha", 38: "amp", 8743: "and", 8736: "ang", 229: "aring",
24
+ 8776: "asymp", 227: "atilde", 228: "auml", 8222: "bdquo", 946: "beta", 166: "brvbar", 8226: "bull",
25
+ 8745: "cap", 231: "ccedil", 184: "cedil", 162: "cent", 967: "chi", 710: "circ", 9827: "clubs",
26
+ 8773: "cong", 169: "copy", 8629: "crarr", 8746: "cup", 164: "curren", 8659: "dArr", 8224: "dagger",
27
+ 8595: "darr", 176: "deg", 948: "delta", 9830: "diams", 247: "divide", 233: "eacute", 234: "ecirc",
28
+ 232: "egrave", 8709: "empty", 8195: "emsp", 8194: "ensp", 949: "epsilon", 8801: "equiv", 951: "eta",
29
+ 240: "eth", 235: "euml", 8364: "euro", 8707: "exist", 402: "fnof", 8704: "forall", 189: "frac12",
30
+ 188: "frac14", 190: "frac34", 8260: "frasl", 947: "gamma", 8805: "ge", 62: "gt", 8660: "hArr",
31
+ 8596: "harr", 9829: "hearts", 8230: "hellip", 237: "iacute", 238: "icirc", 161: "iexcl", 236: "igrave",
32
+ 8465: "image", 8734: "infin", 8747: "int", 953: "iota", 191: "iquest", 8712: "isin", 239: "iuml",
33
+ 954: "kappa", 8656: "lArr", 955: "lambda", 9001: "lang", 171: "laquo", 8592: "larr", 8968: "lceil",
34
+ 8220: "ldquo", 8804: "le", 8970: "lfloor", 8727: "lowast", 9674: "loz", 8206: "lrm", 8249: "lsaquo",
35
+ 8216: "lsquo", 60: "lt", 175: "macr", 8212: "mdash", 181: "micro", 183: "middot", 8722: "minus",
36
+ 956: "mu", 8711: "nabla", 160: "nbsp", 8211: "ndash", 8800: "ne", 8715: "ni", 172: "not", 8713: "notin",
37
+ 8836: "nsub", 241: "ntilde", 957: "nu", 243: "oacute", 244: "ocirc", 339: "oelig", 242: "ograve",
38
+ 8254: "oline", 969: "omega", 959: "omicron", 8853: "oplus", 8744: "or", 170: "ordf", 186: "ordm",
39
+ 248: "oslash", 245: "otilde", 8855: "otimes", 246: "ouml", 182: "para", 8706: "part", 8240: "permil",
40
+ 8869: "perp", 966: "phi", 960: "pi", 982: "piv", 177: "plusmn", 163: "pound", 8242: "prime",
41
+ 8719: "prod", 8733: "prop", 968: "psi", 34: "quot", 8658: "rArr", 8730: "radic", 9002: "rang",
42
+ 187: "raquo", 8594: "rarr", 8969: "rceil", 8221: "rdquo", 8476: "real", 174: "reg", 8971: "rfloor",
43
+ 961: "rho", 8207: "rlm", 8250: "rsaquo", 8217: "rsquo", 8218: "sbquo", 353: "scaron", 8901: "sdot",
44
+ 167: "sect", 173: "shy", 963: "sigma", 962: "sigmaf", 8764: "sim", 9824: "spades", 8834: "sub",
45
+ 8838: "sube", 8721: "sum", 8835: "sup", 185: "sup1", 178: "sup2", 179: "sup3", 8839: "supe",
46
+ 223: "szlig", 964: "tau", 8756: "there4", 952: "theta", 977: "thetasym", 8201: "thinsp", 254: "thorn",
47
+ 732: "tilde", 215: "times", 8482: "trade", 8657: "uArr", 250: "uacute", 8593: "uarr", 251: "ucirc",
48
+ 249: "ugrave", 168: "uml", 978: "upsih", 965: "upsilon", 252: "uuml", 8472: "weierp", 958: "xi",
49
+ 253: "yacute", 165: "yen", 255: "yuml", 950: "zeta", 8205: "zwj", 8204: "zwnj"
50
+ }
51
+ # fmt: on
52
+
53
+ codepoint2name[ord("-")] = "hyphen"
54
+
55
+ # add some custom names to map characters that aren't in HTML
56
+ codepoint2name[ord(" ")] = "space"
57
+ codepoint2name[ord("'")] = "squot"
58
+ codepoint2name[ord(",")] = "comma"
59
+ codepoint2name[ord(".")] = "dot"
60
+ codepoint2name[ord(":")] = "colon"
61
+ codepoint2name[ord(";")] = "semicolon"
62
+ codepoint2name[ord("/")] = "slash"
63
+ codepoint2name[ord("%")] = "percent"
64
+ codepoint2name[ord("#")] = "hash"
65
+ codepoint2name[ord("(")] = "paren_open"
66
+ codepoint2name[ord(")")] = "paren_close"
67
+ codepoint2name[ord("[")] = "bracket_open"
68
+ codepoint2name[ord("]")] = "bracket_close"
69
+ codepoint2name[ord("{")] = "brace_open"
70
+ codepoint2name[ord("}")] = "brace_close"
71
+ codepoint2name[ord("*")] = "star"
72
+ codepoint2name[ord("!")] = "bang"
73
+ codepoint2name[ord("\\")] = "backslash"
74
+ codepoint2name[ord("+")] = "plus"
75
+ codepoint2name[ord("$")] = "dollar"
76
+ codepoint2name[ord("=")] = "equals"
77
+ codepoint2name[ord("?")] = "question"
78
+ codepoint2name[ord("@")] = "at_sign"
79
+ codepoint2name[ord("^")] = "caret"
80
+ codepoint2name[ord("|")] = "pipe"
81
+ codepoint2name[ord("~")] = "tilde"
82
+
83
+ # static qstrs, these must maintain a specific order for .mpy compatibility
84
+ # See QSTR_LAST_STATIC at the top of py/persistentcode.c
85
+
86
+ static_qstr_list = [
87
+ "",
88
+ "__dir__", # Put __dir__ after empty qstr for builtin dir() to work
89
+ "\n",
90
+ " ",
91
+ "*",
92
+ "/",
93
+ "<module>",
94
+ "_",
95
+ "__call__",
96
+ "__class__",
97
+ "__delitem__",
98
+ "__enter__",
99
+ "__exit__",
100
+ "__getattr__",
101
+ "__getitem__",
102
+ "__hash__",
103
+ "__init__",
104
+ "__int__",
105
+ "__iter__",
106
+ "__len__",
107
+ "__main__",
108
+ "__module__",
109
+ "__name__",
110
+ "__new__",
111
+ "__next__",
112
+ "__qualname__",
113
+ "__repr__",
114
+ "__setitem__",
115
+ "__str__",
116
+ "ArithmeticError",
117
+ "AssertionError",
118
+ "AttributeError",
119
+ "BaseException",
120
+ "EOFError",
121
+ "Ellipsis",
122
+ "Exception",
123
+ "GeneratorExit",
124
+ "ImportError",
125
+ "IndentationError",
126
+ "IndexError",
127
+ "KeyError",
128
+ "KeyboardInterrupt",
129
+ "LookupError",
130
+ "MemoryError",
131
+ "NameError",
132
+ "NoneType",
133
+ "NotImplementedError",
134
+ "OSError",
135
+ "OverflowError",
136
+ "RuntimeError",
137
+ "StopIteration",
138
+ "SyntaxError",
139
+ "SystemExit",
140
+ "TypeError",
141
+ "ValueError",
142
+ "ZeroDivisionError",
143
+ "abs",
144
+ "all",
145
+ "any",
146
+ "append",
147
+ "args",
148
+ "bool",
149
+ "builtins",
150
+ "bytearray",
151
+ "bytecode",
152
+ "bytes",
153
+ "callable",
154
+ "chr",
155
+ "classmethod",
156
+ "clear",
157
+ "close",
158
+ "const",
159
+ "copy",
160
+ "count",
161
+ "dict",
162
+ "dir",
163
+ "divmod",
164
+ "end",
165
+ "endswith",
166
+ "eval",
167
+ "exec",
168
+ "extend",
169
+ "find",
170
+ "format",
171
+ "from_bytes",
172
+ "get",
173
+ "getattr",
174
+ "globals",
175
+ "hasattr",
176
+ "hash",
177
+ "id",
178
+ "index",
179
+ "insert",
180
+ "int",
181
+ "isalpha",
182
+ "isdigit",
183
+ "isinstance",
184
+ "islower",
185
+ "isspace",
186
+ "issubclass",
187
+ "isupper",
188
+ "items",
189
+ "iter",
190
+ "join",
191
+ "key",
192
+ "keys",
193
+ "len",
194
+ "list",
195
+ "little",
196
+ "locals",
197
+ "lower",
198
+ "lstrip",
199
+ "main",
200
+ "map",
201
+ "micropython",
202
+ "next",
203
+ "object",
204
+ "open",
205
+ "ord",
206
+ "pop",
207
+ "popitem",
208
+ "pow",
209
+ "print",
210
+ "range",
211
+ "read",
212
+ "readinto",
213
+ "readline",
214
+ "remove",
215
+ "replace",
216
+ "repr",
217
+ "reverse",
218
+ "rfind",
219
+ "rindex",
220
+ "round",
221
+ "rsplit",
222
+ "rstrip",
223
+ "self",
224
+ "send",
225
+ "sep",
226
+ "set",
227
+ "setattr",
228
+ "setdefault",
229
+ "sort",
230
+ "sorted",
231
+ "split",
232
+ "start",
233
+ "startswith",
234
+ "staticmethod",
235
+ "step",
236
+ "stop",
237
+ "str",
238
+ "strip",
239
+ "sum",
240
+ "super",
241
+ "throw",
242
+ "to_bytes",
243
+ "tuple",
244
+ "type",
245
+ "update",
246
+ "upper",
247
+ "utf-8",
248
+ "value",
249
+ "values",
250
+ "write",
251
+ "zip",
252
+ ]
253
+
254
+ # Additional QSTRs that must have index <255 because they are stored as `byte` values.
255
+ # These are not part of the .mpy compatibility list, but we place them in the
256
+ # fixed unsorted pool (i.e. QDEF0) to ensure their indices are small.
257
+ unsorted_qstr_list = {
258
+ # From py/objtype.c: used in the `mp_binary_op_method_name` and `mp_unary_op_method_name` tables.
259
+ "__bool__",
260
+ "__pos__",
261
+ "__neg__",
262
+ "__invert__",
263
+ "__abs__",
264
+ "__float__",
265
+ "__complex__",
266
+ "__sizeof__",
267
+ "__lt__",
268
+ "__gt__",
269
+ "__eq__",
270
+ "__le__",
271
+ "__ge__",
272
+ "__ne__",
273
+ "__contains__",
274
+ "__iadd__",
275
+ "__isub__",
276
+ "__imul__",
277
+ "__imatmul__",
278
+ "__ifloordiv__",
279
+ "__itruediv__",
280
+ "__imod__",
281
+ "__ipow__",
282
+ "__ior__",
283
+ "__ixor__",
284
+ "__iand__",
285
+ "__ilshift__",
286
+ "__irshift__",
287
+ "__add__",
288
+ "__sub__",
289
+ "__mul__",
290
+ "__matmul__",
291
+ "__floordiv__",
292
+ "__truediv__",
293
+ "__mod__",
294
+ "__divmod__",
295
+ "__pow__",
296
+ "__or__",
297
+ "__xor__",
298
+ "__and__",
299
+ "__lshift__",
300
+ "__rshift__",
301
+ "__radd__",
302
+ "__rsub__",
303
+ "__rmul__",
304
+ "__rmatmul__",
305
+ "__rfloordiv__",
306
+ "__rtruediv__",
307
+ "__rmod__",
308
+ "__rpow__",
309
+ "__ror__",
310
+ "__rxor__",
311
+ "__rand__",
312
+ "__rlshift__",
313
+ "__rrshift__",
314
+ "__get__",
315
+ "__set__",
316
+ "__delete__",
317
+ # From py/scope.c: used in `scope_simple_name_table` table.
318
+ # Note: "<module>" is already in `static_qstr_list`.
319
+ "<lambda>",
320
+ "<listcomp>",
321
+ "<dictcomp>",
322
+ "<setcomp>",
323
+ "<genexpr>",
324
+ }
325
+
326
+ # Matches any string that needs no escaping (alphanum + _ only)
327
+ RE_NO_ESCAPE = re.compile(r"^[a-zA-Z0-9_]$")
328
+
329
+
330
+ # this must match the equivalent function in qstr.c
331
+ def compute_hash(qstr, bytes_hash):
332
+ hash = 5381
333
+ for b in qstr:
334
+ hash = (hash * 33) ^ b
335
+ # Make sure that valid hash is never zero, zero means "hash not computed"
336
+ # if bytes_hash is zero, assume a 16-bit mask (to match qstr.c)
337
+ return (hash & ((1 << (8 * (bytes_hash or 2))) - 1)) or 1
338
+
339
+
340
+ def qstr_escape(qst):
341
+ def esc_char(c):
342
+ if RE_NO_ESCAPE.match(c):
343
+ return c
344
+ c = ord(c)
345
+ try:
346
+ name = codepoint2name[c]
347
+ except KeyError:
348
+ name = "0x%02x" % c
349
+ return "_" + name + "_"
350
+
351
+ return "".join(map(esc_char, qst))
352
+
353
+
354
+ static_qstr_list_ident = list(map(qstr_escape, static_qstr_list))
355
+
356
+
357
+ def parse_input_headers(infiles):
358
+ qcfgs = {}
359
+ qstrs = {}
360
+
361
+ # read the qstrs in from the input files
362
+ for infile in infiles:
363
+ with open(infile, "rt") as f:
364
+ for line in f:
365
+ line = line.strip()
366
+
367
+ # is this a config line?
368
+ match = re.match(r"^QCFG\((.+), (.+)\)", line)
369
+ if match:
370
+ value = match.group(2)
371
+ if value[0] == "(" and value[-1] == ")":
372
+ # strip parenthesis from config value
373
+ value = value[1:-1]
374
+ qcfgs[match.group(1)] = value
375
+ continue
376
+
377
+ # is this a QSTR line?
378
+ match = re.match(r"^Q\((.*)\)$", line)
379
+ if not match:
380
+ continue
381
+
382
+ # get the qstr value
383
+ qstr = match.group(1)
384
+
385
+ # special cases to specify control characters
386
+ if qstr == "\\n":
387
+ qstr = "\n"
388
+ elif qstr == "\\r\\n":
389
+ qstr = "\r\n"
390
+
391
+ # work out the corresponding qstr name
392
+ ident = qstr_escape(qstr)
393
+
394
+ # don't add duplicates
395
+ if ident in static_qstr_list_ident:
396
+ continue
397
+ if ident in qstrs:
398
+ continue
399
+
400
+ qstrs[ident] = (ident, qstr)
401
+
402
+ if not qcfgs:
403
+ sys.stderr.write("ERROR: Empty preprocessor output - check for errors above\n")
404
+ sys.exit(1)
405
+
406
+ return qcfgs, qstrs
407
+
408
+
409
+ def escape_bytes(qstr, qbytes):
410
+ if all(32 <= ord(c) <= 126 and c != "\\" and c != '"' for c in qstr):
411
+ # qstr is all printable ASCII so render it as-is (for easier debugging)
412
+ return qstr
413
+ else:
414
+ # qstr contains non-printable codes so render entire thing as hex pairs
415
+ return "".join(("\\x%02x" % b) for b in qbytes)
416
+
417
+
418
+ def make_bytes(cfg_bytes_len, cfg_bytes_hash, qstr):
419
+ qbytes = bytes_cons(qstr, "utf8")
420
+ qlen = len(qbytes)
421
+ qhash = compute_hash(qbytes, cfg_bytes_hash)
422
+ if qlen >= (1 << (8 * cfg_bytes_len)):
423
+ print("qstr is too long:", qstr)
424
+ assert False
425
+ qdata = escape_bytes(qstr, qbytes)
426
+ return '%d, %d, "%s"' % (qhash, qlen, qdata)
427
+
428
+
429
+ def print_qstr_data(qcfgs, qstrs):
430
+ # get config variables
431
+ cfg_bytes_len = int(qcfgs["BYTES_IN_LEN"])
432
+ cfg_bytes_hash = int(qcfgs["BYTES_IN_HASH"])
433
+
434
+ # print out the starter of the generated C header file
435
+ print("// This file was automatically generated by makeqstrdata.py")
436
+ print("")
437
+
438
+ # add NULL qstr with no hash or data
439
+ print('QDEF0(MP_QSTRnull, 0, 0, "")')
440
+
441
+ # add static qstrs to the first unsorted pool
442
+ for qstr in static_qstr_list:
443
+ qbytes = make_bytes(cfg_bytes_len, cfg_bytes_hash, qstr)
444
+ print("QDEF0(MP_QSTR_%s, %s)" % (qstr_escape(qstr), qbytes))
445
+
446
+ # add remaining qstrs to the sorted (by value) pool (unless they're in
447
+ # unsorted_qstr_list, in which case add them to the unsorted pool)
448
+ for ident, qstr in sorted(qstrs.values(), key=lambda x: x[1]):
449
+ qbytes = make_bytes(cfg_bytes_len, cfg_bytes_hash, qstr)
450
+ pool = 0 if qstr in unsorted_qstr_list else 1
451
+ print("QDEF%d(MP_QSTR_%s, %s)" % (pool, ident, qbytes))
452
+
453
+
454
+ def do_work(infiles):
455
+ qcfgs, qstrs = parse_input_headers(infiles)
456
+ print_qstr_data(qcfgs, qstrs)
457
+
458
+
459
+ if __name__ == "__main__":
460
+ do_work(sys.argv[1:])