rapydscript-ns 0.8.4 → 0.9.0

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.
Files changed (132) hide show
  1. package/.agignore +1 -1
  2. package/.github/workflows/ci.yml +38 -38
  3. package/=template.pyj +5 -5
  4. package/CHANGELOG.md +18 -0
  5. package/HACKING.md +103 -103
  6. package/LICENSE +24 -24
  7. package/README.md +715 -169
  8. package/TODO.md +9 -2
  9. package/add-toc-to-readme +2 -2
  10. package/bin/export +75 -75
  11. package/bin/rapydscript +70 -70
  12. package/bin/web-repl-export +102 -102
  13. package/build +2 -2
  14. package/language-service/index.js +36 -27
  15. package/package.json +1 -1
  16. package/publish.py +37 -37
  17. package/release/baselib-plain-pretty.js +2358 -168
  18. package/release/baselib-plain-ugly.js +73 -3
  19. package/release/compiler.js +6282 -3092
  20. package/release/signatures.json +31 -30
  21. package/session.vim +4 -4
  22. package/setup.cfg +2 -2
  23. package/src/ast.pyj +1 -0
  24. package/src/baselib-builtins.pyj +340 -2
  25. package/src/baselib-bytes.pyj +664 -0
  26. package/src/baselib-errors.pyj +1 -1
  27. package/src/baselib-internal.pyj +267 -60
  28. package/src/baselib-itertools.pyj +110 -97
  29. package/src/baselib-str.pyj +22 -4
  30. package/src/compiler.pyj +36 -36
  31. package/src/errors.pyj +30 -30
  32. package/src/lib/abc.pyj +317 -0
  33. package/src/lib/aes.pyj +646 -646
  34. package/src/lib/copy.pyj +120 -120
  35. package/src/lib/dataclasses.pyj +532 -0
  36. package/src/lib/elementmaker.pyj +83 -83
  37. package/src/lib/encodings.pyj +126 -126
  38. package/src/lib/enum.pyj +125 -0
  39. package/src/lib/gettext.pyj +569 -569
  40. package/src/lib/itertools.pyj +580 -580
  41. package/src/lib/math.pyj +193 -193
  42. package/src/lib/operator.pyj +11 -11
  43. package/src/lib/pythonize.pyj +20 -20
  44. package/src/lib/random.pyj +118 -118
  45. package/src/lib/re.pyj +504 -470
  46. package/src/lib/react.pyj +74 -74
  47. package/src/lib/traceback.pyj +63 -63
  48. package/src/lib/typing.pyj +577 -0
  49. package/src/lib/uuid.pyj +77 -77
  50. package/src/monaco-language-service/builtins.js +14 -4
  51. package/src/monaco-language-service/diagnostics.js +19 -20
  52. package/src/monaco-language-service/dts.js +550 -550
  53. package/src/output/classes.pyj +62 -26
  54. package/src/output/comments.pyj +45 -45
  55. package/src/output/exceptions.pyj +201 -201
  56. package/src/output/functions.pyj +78 -5
  57. package/src/output/jsx.pyj +164 -164
  58. package/src/output/loops.pyj +5 -2
  59. package/src/output/operators.pyj +100 -34
  60. package/src/output/treeshake.pyj +182 -182
  61. package/src/output/utils.pyj +72 -72
  62. package/src/parse.pyj +80 -16
  63. package/src/string_interpolation.pyj +72 -72
  64. package/src/tokenizer.pyj +9 -4
  65. package/src/unicode_aliases.pyj +576 -576
  66. package/src/utils.pyj +192 -192
  67. package/test/_import_one.pyj +37 -37
  68. package/test/_import_two/__init__.pyj +11 -11
  69. package/test/_import_two/level2/deep.pyj +4 -4
  70. package/test/_import_two/other.pyj +6 -6
  71. package/test/_import_two/sub.pyj +13 -13
  72. package/test/abc.pyj +291 -0
  73. package/test/aes_vectors.pyj +421 -421
  74. package/test/annotations.pyj +80 -80
  75. package/test/arithmetic_nostrict.pyj +88 -0
  76. package/test/arithmetic_types.pyj +169 -0
  77. package/test/baselib.pyj +91 -0
  78. package/test/bytes.pyj +467 -0
  79. package/test/classes.pyj +1 -0
  80. package/test/comparison_ops.pyj +173 -0
  81. package/test/dataclasses.pyj +253 -0
  82. package/test/decorators.pyj +77 -77
  83. package/test/docstrings.pyj +39 -39
  84. package/test/elementmaker_test.pyj +45 -45
  85. package/test/enum.pyj +134 -0
  86. package/test/eval_exec.pyj +56 -0
  87. package/test/format.pyj +148 -0
  88. package/test/functions.pyj +151 -151
  89. package/test/generators.pyj +41 -41
  90. package/test/generic.pyj +370 -370
  91. package/test/imports.pyj +72 -72
  92. package/test/internationalization.pyj +73 -73
  93. package/test/lint.pyj +164 -164
  94. package/test/loops.pyj +85 -85
  95. package/test/numpy.pyj +734 -734
  96. package/test/object.pyj +64 -0
  97. package/test/omit_function_metadata.pyj +20 -20
  98. package/test/python_compat.pyj +17 -15
  99. package/test/python_features.pyj +70 -15
  100. package/test/regexp.pyj +83 -55
  101. package/test/repl.pyj +121 -121
  102. package/test/scoped_flags.pyj +76 -76
  103. package/test/tuples.pyj +96 -0
  104. package/test/typing.pyj +469 -0
  105. package/test/unit/index.js +116 -7
  106. package/test/unit/language-service-dts.js +543 -543
  107. package/test/unit/language-service-hover.js +455 -455
  108. package/test/unit/language-service.js +84 -0
  109. package/test/unit/web-repl.js +804 -1
  110. package/test/vars_locals_globals.pyj +94 -0
  111. package/tools/cli.js +558 -547
  112. package/tools/compile.js +224 -219
  113. package/tools/completer.js +131 -131
  114. package/tools/embedded_compiler.js +262 -251
  115. package/tools/gettext.js +185 -185
  116. package/tools/ini.js +65 -65
  117. package/tools/lint.js +16 -19
  118. package/tools/msgfmt.js +187 -187
  119. package/tools/repl.js +223 -223
  120. package/tools/test.js +118 -118
  121. package/tools/utils.js +128 -128
  122. package/tools/web_repl.js +95 -95
  123. package/try +41 -41
  124. package/web-repl/env.js +196 -196
  125. package/web-repl/index.html +163 -163
  126. package/web-repl/main.js +252 -252
  127. package/web-repl/prism.css +139 -139
  128. package/web-repl/prism.js +113 -113
  129. package/web-repl/rapydscript.js +224 -224
  130. package/web-repl/sha1.js +25 -25
  131. package/PYTHON_DIFFERENCES_REPORT.md +0 -291
  132. package/PYTHON_FEATURE_COVERAGE.md +0 -200
package/test/generic.pyj CHANGED
@@ -1,370 +1,370 @@
1
- # globals: assrt, ρσ_last_exception
2
-
3
- import traceback
4
-
5
- def throw_test(code):
6
- assrt.throws(def():
7
- RapydScript.parse(code, {'filename':code}).body[0]
8
- , RapydScript.SyntaxError)
9
-
10
- # unary operators
11
- assrt.equal(-(1), -1)
12
- assrt.equal(-(-1), 1)
13
- assrt.equal(+(+1), 1)
14
-
15
- # arithmetic
16
- assrt.equal(3**4, Math.pow(3, 4))
17
- assrt.equal(100**-2, Math.pow(100, -2))
18
- assrt.equal(2*5**2*3, 150)
19
- assrt.equal(-2**2, -4)
20
- assrt.equal((-2)**2, 4)
21
- assrt.equal(pow(2, 2, 2), 0)
22
- assrt.equal(100 // 3, 33)
23
- assrt.equal(-100 // 3, -34)
24
- a = 100
25
- a //= 3
26
- assrt.equal(a, 33)
27
- assrt.equal(0b11, 3)
28
- assrt.ok(42 / "x" is NaN)
29
- assrt.ok(42 is not NaN)
30
- assrt.ok(NaN is parseInt('asd', 10))
31
- assrt.ok('NaN' is not 42 / 'x')
32
-
33
- # comparisons
34
- assrt.ok(3<5<7)
35
- assrt.equal(-1 < 0 == 1 < 0, False)
36
-
37
- # Empty tuple
38
- assrt.deepEqual((), [])
39
-
40
- # Conditional operators
41
- assrt.equal(1 if True else 2, 1)
42
- assrt.equal(1
43
- if True else 2, 1)
44
- assrt.deepEqual([x for x in ("asd" if True else "xyz") if x], 'asd'.split(''))
45
- assrt.deepEqual((1, [x for x in [2] if x]), [1, [2]])
46
-
47
- # Comprehensions
48
- assrt.deepEqual([a+1 for a in [1,2,3] if a > 1], [3, 4])
49
- assrt.deepEqual({a+1:a+2 for a in [1,2,3] if a > 1}, {3:4, 4:5})
50
- assrt.deepEqual({a+1 for a in [1,2,3] if a > 1}, set([3, 4]))
51
- assrt.deepEqual([(a+1, 1) for a in [1,2,3] if a > 1], [[3, 1], [4, 1]])
52
-
53
- # Destructuring assignment
54
- a, (b, (c, d)), e = [1, [2, [3, 4]], 5]
55
- assrt.deepEqual([a, b, c, d, e], [1, 2, 3, 4, 5])
56
- for x, (y, z) in [ [1, [2, 3]] ]:
57
- assrt.deepEqual([x, y, z], [1, 2, 3])
58
- assrt.deepEqual([ [x, y, z, w] for ((x, y), (z, w)) in [ [[1, 2], [3, 4]] ] ],
59
- [[1, 2, 3, 4]])
60
- assrt.deepEqual([ [x, y, z] for x, (y, z) in [ [1, [2, 3] ] ]],
61
- [[1, 2, 3]])
62
- (a, b) = [1, 2]
63
- assrt.deepEqual([a, b], [1, 2])
64
- (a, (b, c)) = [1, [2, 3]]
65
- assrt.deepEqual([a, b, c], [1, 2, 3])
66
-
67
- # Chained assignment
68
- a = b = 11
69
- assrt.deepEqual([a, b], [11, 11])
70
- def inc():
71
- nonlocal a
72
- a += 1
73
- return a
74
- a = b = inc()
75
- assrt.deepEqual([a, b], [12, 12])
76
- a, b = c, d = 1, 2
77
- assrt.deepEqual([a, b, c, d], [1, 2, 1, 2])
78
- (a, b) = [c, d] = 1, 2
79
- assrt.deepEqual([a, b, c, d], [1, 2, 1, 2])
80
- a, (x, b) = c, d = [1, [2, 3]]
81
- assrt.deepEqual([a, x, b, c, d], [1, 2, 3, 1, [2, 3]])
82
- a, b = [c, d] = [e, f] = [1, 2]
83
- assrt.deepEqual([a, b, c, d, e, f], [1, 2, 1, 2, 1, 2])
84
- a = b, c = 1, 2
85
- assrt.deepEqual([a, b, c], [[1,2], 1, 2])
86
-
87
- throw_test('a += b += 1')
88
- throw_test('a += 1, 2 += b')
89
- throw_test('a += [1, 2] += b')
90
- throw_test('function = 1')
91
- throw_test('def function():\n pass')
92
- throw_test('class function:\n pass')
93
- throw_test('while 1:\npass')
94
- throw_test('def f():\n while 1:\n pass')
95
- throw_test('1 1')
96
- throw_test('z = x y')
97
- throw_test('while a = 1: pass')
98
- throw_test('while 1\n pass')
99
- throw_test('for a in [1]\n pass')
100
-
101
- # object literals
102
- {1:
103
- 1
104
- } # Check that arbitrary indentation is allowed after : for an object literal key
105
-
106
- # strict equality
107
- assrt(1 == 1) # number vs number: ok
108
- assrt(True == True) # boolean vs boolean: ok
109
- assrt(not (1 == True)) # number vs boolean: NEVER equal
110
- assrt(1 != True) # number vs boolean: NEVER equal
111
- assrt(not ("" == False)) # string vs boolean: NEVER equal
112
- assrt(not ("0" == 0)) # string vs integer: NEVER equal
113
- assrt(not ("" == 0)) # string vs integer: NEVER equal
114
- assrt(bool(1) == True) # boolean conversion
115
- assrt(bool("") == False) # boolean conversion
116
- assrt(v'1 == true') # javascript override
117
- assrt(not v'(1 != true)') # javascript override
118
- assrt(v'String("test")' == "test") # this should do string conversion rather than creating a string object
119
- assrt(String("test") != "test") # this should create a string object
120
-
121
-
122
- # raw JavaScript
123
- v'var def = {}' # ability to bypass reserved keywords in declaration
124
- v'def'.item = 3 # ability to use reserved keywords
125
- assrt(v'def.item' == 3)
126
-
127
- n = 5
128
- assrt.equal(v"""
129
- (function() {
130
- var s = 0
131
- for (var i=0; i<=n; i++) {
132
- s += i
133
- }
134
- return s
135
- })()
136
- """, (def():
137
- s = 0
138
- for i in range(n+1):
139
- s += i
140
- return s
141
- )()) # shared scoping and equivalent capability
142
- a = []
143
- for v'var i = 0; i < 1; i++':
144
- a[i] = 1 # noqa: undef
145
- assrt.deepEqual(a, [1])
146
-
147
- # String literals
148
- a = '\u00ad'
149
- assrt.equal(a.charCodeAt(0), 0xad)
150
- # String literal concatenation
151
-
152
- assrt.equal('1' '2', '12')
153
- assrt.equal('1'
154
- '2' '3', '123')
155
- assrt.equal(u'a', 'a')
156
- assrt.equal('a' * 5, 'aaaaa')
157
- assrt.ok(isinstance('a', str))
158
-
159
- # Various bits of miscellaneous code that caused parser infinite loops and other breakage
160
- throw_test('for a in b:\\n 1+1')
161
-
162
- def localvars_in_comprehension():
163
- return {k:i for i, k in enumerate([1,2])}
164
- assrt.deepEqual(localvars_in_comprehension(), {'1':0, '2':1})
165
-
166
- # Numbers
167
- assrt.equal(1e2, 100)
168
- assrt.equal(-1e2, -100)
169
- assrt.equal(1e+2, 100)
170
- assrt.equal(1E-1, 0.1)
171
-
172
- # Equality operators
173
-
174
- class D:
175
-
176
- def __init__(self, data):
177
- self.data = data
178
-
179
- def __eq__(self, other):
180
- return self.data == other.data
181
-
182
- assrt.ok(D(1) == D(1))
183
- assrt.ok(D(2) != D(1))
184
- assrt.ok(D(1) == {'data':1})
185
- assrt.ok({'data':1} == D(1))
186
-
187
- arr = []
188
- {} == arr.append(2)
189
- assrt.ok(arr.length is 1)
190
-
191
- call_count = 0
192
- def cc():
193
- nonlocal call_count
194
- call_count += 1
195
- return [1]
196
- cc()[-1]
197
- assrt.equal(call_count, 1)
198
-
199
- # Test the del operator
200
-
201
- # JS object
202
- deleteme = {1:1}
203
- assrt.ok(Object.keys(deleteme).indexOf('1') is not -1)
204
- del deleteme[1]
205
- assrt.ok(Object.keys(deleteme).indexOf('1') is -1)
206
-
207
- # list
208
- deleteme = [1, 2]
209
- del deleteme[0]
210
- assrt.equal(deleteme.length, 1)
211
- assrt.deepEqual(deleteme, [2])
212
- deleteme = [1, 2, 3]
213
- del deleteme[::2]
214
- assrt.deepEqual(deleteme, [2])
215
- deleteme = [1, 2, 3]
216
- del deleteme[::1]
217
- assrt.equal(deleteme.length, 0)
218
- deleteme = [1, 2, 3]
219
- del deleteme[1:2]
220
- assrt.deepEqual(deleteme, [1, 3])
221
-
222
- # global
223
- del deleteme
224
-
225
- # python dict
226
- from __python__ import dict_literals, overload_getitem
227
- deleteme = {1:1, 2:2}
228
- assrt.ok(1 in deleteme)
229
- del deleteme[1]
230
- assrt.ok(1 not in deleteme)
231
- assrt.equal(len(deleteme), 1)
232
-
233
- # Asserts
234
-
235
- assrt.throws(def():
236
- assert 0
237
- , AssertionError
238
- )
239
- assrt.throws(def():
240
- assert 0, 'xdx'
241
- , /xdx/
242
- )
243
-
244
- # Exceptions
245
-
246
- class MyException(Exception):
247
-
248
- def __init__(self, message, val):
249
- Exception.__init__(self, message)
250
- self.val = val
251
-
252
- try:
253
- raise MyException('xxx', 3)
254
- except MyException as e:
255
- assrt.equal(e.name, 'MyException')
256
- assrt.equal(e.message, 'xxx')
257
- assrt.equal(e.val, 3)
258
- assrt.equal(e.toString(), 'MyException: xxx')
259
- assrt.ok(v'e instanceof MyException')
260
- assrt.ok(v'e instanceof Error')
261
-
262
- try:
263
- raise Error('eee')
264
- except Exception:
265
- # Test that we can catch JS errors and that local variables in the except block are declared
266
- xxlocalvar = 1
267
- assrt.equal(xxlocalvar, 1)
268
-
269
- def errf():
270
- raise MyException()
271
-
272
- try:
273
- errf()
274
- except MyException as e:
275
- fe = traceback.format_exception()
276
- assrt.ok(str.strip(fe[-2]).startsWith('at errf'))
277
-
278
-
279
- def stackf():
280
- return traceback.format_stack()
281
- assrt.ok(str.strip(stackf()[-1]).startsWith('at stackf'))
282
-
283
-
284
- def else_without_finally(fail):
285
- ans = []
286
- try:
287
- if fail:
288
- raise Exception('a')
289
- ans.push('ok')
290
- except:
291
- ans.append('ex')
292
- else:
293
- ans.append('el')
294
- return ans
295
-
296
- def else_with_finally(fail):
297
- ans = []
298
- try:
299
- if fail:
300
- raise Exception('a')
301
- ans.push('ok')
302
- except:
303
- ans.push('ex')
304
- else:
305
- ans.push('el')
306
- finally:
307
- ans.push('fi')
308
- return ans
309
-
310
- def exc_in_else():
311
- ans = []
312
- try:
313
- try:
314
- ans.push('ok')
315
- except:
316
- pass
317
- else:
318
- raise Exception('1')
319
- finally:
320
- ans.push('fi')
321
- except:
322
- ans.push('ex')
323
- return ans
324
-
325
- assrt.deepEqual(else_without_finally(), ['ok', 'el'])
326
- assrt.deepEqual(else_without_finally(True), ['ex'])
327
- assrt.deepEqual(else_with_finally(), ['ok', 'el', 'fi'])
328
- assrt.deepEqual(else_with_finally(True), ['ex', 'fi'])
329
- assrt.deepEqual(exc_in_else(), ['ok', 'fi', 'ex'])
330
-
331
- # Existential operator
332
-
333
- assrt.ok(not gfsgs?)
334
- assrt.equal(gfsgs ? 1, 1)
335
- assrt.equal(gfsgs?.b, undefined)
336
- assrt.equal(gfsgs?(), undefined) # noqa: undef
337
- undef = undefined
338
- assrt.ok(not undef?)
339
- undef = None
340
- assrt.ok(not undef?)
341
- undef = 0
342
- assrt.ok(undef?)
343
- assrt.equal(undef?(), undefined)
344
- assrt.equal(undef?.xxx?(), undefined)
345
- assrt.equal(undef.a, undefined)
346
- assrt.equal(undef[1], undefined)
347
- assrt.equal(undef ? 1, 0)
348
- ml = 1, '''
349
- '''
350
- assrt.deepEqual(ml, [1, '\n'])
351
-
352
-
353
- # localvars in conditions
354
-
355
- gv = 1
356
- (def ():
357
- if (gv = 2):
358
- pass
359
- assrt.equal(gv, 2)
360
- )()
361
- assrt.equal(gv, 1)
362
-
363
- assrt.ok(1 not in [2, 3])
364
- assrt.ok(1 in [1, 2])
365
- assrt.ok("1" + "2" in ["12", "x"])
366
- assrt.ok("1" + "2" not in ["1", "2"])
367
- if "1" + "2" not in ["1", "2"]:
368
- assrt.ok(1)
369
- else:
370
- assrt.ok(0)
1
+ # globals: assrt, ρσ_last_exception
2
+
3
+ import traceback
4
+
5
+ def throw_test(code):
6
+ assrt.throws(def():
7
+ RapydScript.parse(code, {'filename':code}).body[0]
8
+ , RapydScript.SyntaxError)
9
+
10
+ # unary operators
11
+ assrt.equal(-(1), -1)
12
+ assrt.equal(-(-1), 1)
13
+ assrt.equal(+(+1), 1)
14
+
15
+ # arithmetic
16
+ assrt.equal(3**4, Math.pow(3, 4))
17
+ assrt.equal(100**-2, Math.pow(100, -2))
18
+ assrt.equal(2*5**2*3, 150)
19
+ assrt.equal(-2**2, -4)
20
+ assrt.equal((-2)**2, 4)
21
+ assrt.equal(pow(2, 2, 2), 0)
22
+ assrt.equal(100 // 3, 33)
23
+ assrt.equal(-100 // 3, -34)
24
+ a = 100
25
+ a //= 3
26
+ assrt.equal(a, 33)
27
+ assrt.equal(0b11, 3)
28
+ assrt.ok(42 / "x" is NaN)
29
+ assrt.ok(42 is not NaN)
30
+ assrt.ok(NaN is parseInt('asd', 10))
31
+ assrt.ok('NaN' is not 42 / 'x')
32
+
33
+ # comparisons
34
+ assrt.ok(3<5<7)
35
+ assrt.equal(-1 < 0 == 1 < 0, False)
36
+
37
+ # Empty tuple
38
+ assrt.deepEqual((), [])
39
+
40
+ # Conditional operators
41
+ assrt.equal(1 if True else 2, 1)
42
+ assrt.equal(1
43
+ if True else 2, 1)
44
+ assrt.deepEqual([x for x in ("asd" if True else "xyz") if x], 'asd'.split(''))
45
+ assrt.deepEqual((1, [x for x in [2] if x]), [1, [2]])
46
+
47
+ # Comprehensions
48
+ assrt.deepEqual([a+1 for a in [1,2,3] if a > 1], [3, 4])
49
+ assrt.deepEqual({a+1:a+2 for a in [1,2,3] if a > 1}, {3:4, 4:5})
50
+ assrt.deepEqual({a+1 for a in [1,2,3] if a > 1}, set([3, 4]))
51
+ assrt.deepEqual([(a+1, 1) for a in [1,2,3] if a > 1], [[3, 1], [4, 1]])
52
+
53
+ # Destructuring assignment
54
+ a, (b, (c, d)), e = [1, [2, [3, 4]], 5]
55
+ assrt.deepEqual([a, b, c, d, e], [1, 2, 3, 4, 5])
56
+ for x, (y, z) in [ [1, [2, 3]] ]:
57
+ assrt.deepEqual([x, y, z], [1, 2, 3])
58
+ assrt.deepEqual([ [x, y, z, w] for ((x, y), (z, w)) in [ [[1, 2], [3, 4]] ] ],
59
+ [[1, 2, 3, 4]])
60
+ assrt.deepEqual([ [x, y, z] for x, (y, z) in [ [1, [2, 3] ] ]],
61
+ [[1, 2, 3]])
62
+ (a, b) = [1, 2]
63
+ assrt.deepEqual([a, b], [1, 2])
64
+ (a, (b, c)) = [1, [2, 3]]
65
+ assrt.deepEqual([a, b, c], [1, 2, 3])
66
+
67
+ # Chained assignment
68
+ a = b = 11
69
+ assrt.deepEqual([a, b], [11, 11])
70
+ def inc():
71
+ nonlocal a
72
+ a += 1
73
+ return a
74
+ a = b = inc()
75
+ assrt.deepEqual([a, b], [12, 12])
76
+ a, b = c, d = 1, 2
77
+ assrt.deepEqual([a, b, c, d], [1, 2, 1, 2])
78
+ (a, b) = [c, d] = 1, 2
79
+ assrt.deepEqual([a, b, c, d], [1, 2, 1, 2])
80
+ a, (x, b) = c, d = [1, [2, 3]]
81
+ assrt.deepEqual([a, x, b, c, d], [1, 2, 3, 1, [2, 3]])
82
+ a, b = [c, d] = [e, f] = [1, 2]
83
+ assrt.deepEqual([a, b, c, d, e, f], [1, 2, 1, 2, 1, 2])
84
+ a = b, c = 1, 2
85
+ assrt.deepEqual([a, b, c], [[1,2], 1, 2])
86
+
87
+ throw_test('a += b += 1')
88
+ throw_test('a += 1, 2 += b')
89
+ throw_test('a += [1, 2] += b')
90
+ throw_test('function = 1')
91
+ throw_test('def function():\n pass')
92
+ throw_test('class function:\n pass')
93
+ throw_test('while 1:\npass')
94
+ throw_test('def f():\n while 1:\n pass')
95
+ throw_test('1 1')
96
+ throw_test('z = x y')
97
+ throw_test('while a = 1: pass')
98
+ throw_test('while 1\n pass')
99
+ throw_test('for a in [1]\n pass')
100
+
101
+ # object literals
102
+ {1:
103
+ 1
104
+ } # Check that arbitrary indentation is allowed after : for an object literal key
105
+
106
+ # strict equality
107
+ assrt(1 == 1) # number vs number: ok
108
+ assrt(True == True) # boolean vs boolean: ok
109
+ assrt(not (1 == True)) # number vs boolean: NEVER equal
110
+ assrt(1 != True) # number vs boolean: NEVER equal
111
+ assrt(not ("" == False)) # string vs boolean: NEVER equal
112
+ assrt(not ("0" == 0)) # string vs integer: NEVER equal
113
+ assrt(not ("" == 0)) # string vs integer: NEVER equal
114
+ assrt(bool(1) == True) # boolean conversion
115
+ assrt(bool("") == False) # boolean conversion
116
+ assrt(v'1 == true') # javascript override
117
+ assrt(not v'(1 != true)') # javascript override
118
+ assrt(v'String("test")' == "test") # this should do string conversion rather than creating a string object
119
+ assrt(String("test") != "test") # this should create a string object
120
+
121
+
122
+ # raw JavaScript
123
+ v'var def = {}' # ability to bypass reserved keywords in declaration
124
+ v'def'.item = 3 # ability to use reserved keywords
125
+ assrt(v'def.item' == 3)
126
+
127
+ n = 5
128
+ assrt.equal(v"""
129
+ (function() {
130
+ var s = 0
131
+ for (var i=0; i<=n; i++) {
132
+ s += i
133
+ }
134
+ return s
135
+ })()
136
+ """, (def():
137
+ s = 0
138
+ for i in range(n+1):
139
+ s += i
140
+ return s
141
+ )()) # shared scoping and equivalent capability
142
+ a = []
143
+ for v'var i = 0; i < 1; i++':
144
+ a[i] = 1 # noqa: undef
145
+ assrt.deepEqual(a, [1])
146
+
147
+ # String literals
148
+ a = '\u00ad'
149
+ assrt.equal(a.charCodeAt(0), 0xad)
150
+ # String literal concatenation
151
+
152
+ assrt.equal('1' '2', '12')
153
+ assrt.equal('1'
154
+ '2' '3', '123')
155
+ assrt.equal(u'a', 'a')
156
+ assrt.equal('a' * 5, 'aaaaa')
157
+ assrt.ok(isinstance('a', str))
158
+
159
+ # Various bits of miscellaneous code that caused parser infinite loops and other breakage
160
+ throw_test('for a in b:\\n 1+1')
161
+
162
+ def localvars_in_comprehension():
163
+ return {k:i for i, k in enumerate([1,2])}
164
+ assrt.deepEqual(localvars_in_comprehension(), {'1':0, '2':1})
165
+
166
+ # Numbers
167
+ assrt.equal(1e2, 100)
168
+ assrt.equal(-1e2, -100)
169
+ assrt.equal(1e+2, 100)
170
+ assrt.equal(1E-1, 0.1)
171
+
172
+ # Equality operators
173
+
174
+ class D:
175
+
176
+ def __init__(self, data):
177
+ self.data = data
178
+
179
+ def __eq__(self, other):
180
+ return self.data == other.data
181
+
182
+ assrt.ok(D(1) == D(1))
183
+ assrt.ok(D(2) != D(1))
184
+ assrt.ok(D(1) == {'data':1})
185
+ assrt.ok({'data':1} == D(1))
186
+
187
+ arr = []
188
+ {} == arr.append(2)
189
+ assrt.ok(arr.length is 1)
190
+
191
+ call_count = 0
192
+ def cc():
193
+ nonlocal call_count
194
+ call_count += 1
195
+ return [1]
196
+ cc()[-1]
197
+ assrt.equal(call_count, 1)
198
+
199
+ # Test the del operator
200
+
201
+ # JS object
202
+ deleteme = {1:1}
203
+ assrt.ok(Object.keys(deleteme).indexOf('1') is not -1)
204
+ del deleteme[1]
205
+ assrt.ok(Object.keys(deleteme).indexOf('1') is -1)
206
+
207
+ # list
208
+ deleteme = [1, 2]
209
+ del deleteme[0]
210
+ assrt.equal(deleteme.length, 1)
211
+ assrt.deepEqual(deleteme, [2])
212
+ deleteme = [1, 2, 3]
213
+ del deleteme[::2]
214
+ assrt.deepEqual(deleteme, [2])
215
+ deleteme = [1, 2, 3]
216
+ del deleteme[::1]
217
+ assrt.equal(deleteme.length, 0)
218
+ deleteme = [1, 2, 3]
219
+ del deleteme[1:2]
220
+ assrt.deepEqual(deleteme, [1, 3])
221
+
222
+ # global
223
+ del deleteme
224
+
225
+ # python dict
226
+ from __python__ import dict_literals, overload_getitem
227
+ deleteme = {1:1, 2:2}
228
+ assrt.ok(1 in deleteme)
229
+ del deleteme[1]
230
+ assrt.ok(1 not in deleteme)
231
+ assrt.equal(len(deleteme), 1)
232
+
233
+ # Asserts
234
+
235
+ assrt.throws(def():
236
+ assert 0
237
+ , AssertionError
238
+ )
239
+ assrt.throws(def():
240
+ assert 0, 'xdx'
241
+ , /xdx/
242
+ )
243
+
244
+ # Exceptions
245
+
246
+ class MyException(Exception):
247
+
248
+ def __init__(self, message, val):
249
+ Exception.__init__(self, message)
250
+ self.val = val
251
+
252
+ try:
253
+ raise MyException('xxx', 3)
254
+ except MyException as e:
255
+ assrt.equal(e.name, 'MyException')
256
+ assrt.equal(e.message, 'xxx')
257
+ assrt.equal(e.val, 3)
258
+ assrt.equal(e.toString(), 'MyException: xxx')
259
+ assrt.ok(v'e instanceof MyException')
260
+ assrt.ok(v'e instanceof Error')
261
+
262
+ try:
263
+ raise Error('eee')
264
+ except Exception:
265
+ # Test that we can catch JS errors and that local variables in the except block are declared
266
+ xxlocalvar = 1
267
+ assrt.equal(xxlocalvar, 1)
268
+
269
+ def errf():
270
+ raise MyException()
271
+
272
+ try:
273
+ errf()
274
+ except MyException as e:
275
+ fe = traceback.format_exception()
276
+ assrt.ok(str.strip(fe[-2]).startsWith('at errf'))
277
+
278
+
279
+ def stackf():
280
+ return traceback.format_stack()
281
+ assrt.ok(str.strip(stackf()[-1]).startsWith('at stackf'))
282
+
283
+
284
+ def else_without_finally(fail):
285
+ ans = []
286
+ try:
287
+ if fail:
288
+ raise Exception('a')
289
+ ans.push('ok')
290
+ except:
291
+ ans.append('ex')
292
+ else:
293
+ ans.append('el')
294
+ return ans
295
+
296
+ def else_with_finally(fail):
297
+ ans = []
298
+ try:
299
+ if fail:
300
+ raise Exception('a')
301
+ ans.push('ok')
302
+ except:
303
+ ans.push('ex')
304
+ else:
305
+ ans.push('el')
306
+ finally:
307
+ ans.push('fi')
308
+ return ans
309
+
310
+ def exc_in_else():
311
+ ans = []
312
+ try:
313
+ try:
314
+ ans.push('ok')
315
+ except:
316
+ pass
317
+ else:
318
+ raise Exception('1')
319
+ finally:
320
+ ans.push('fi')
321
+ except:
322
+ ans.push('ex')
323
+ return ans
324
+
325
+ assrt.deepEqual(else_without_finally(), ['ok', 'el'])
326
+ assrt.deepEqual(else_without_finally(True), ['ex'])
327
+ assrt.deepEqual(else_with_finally(), ['ok', 'el', 'fi'])
328
+ assrt.deepEqual(else_with_finally(True), ['ex', 'fi'])
329
+ assrt.deepEqual(exc_in_else(), ['ok', 'fi', 'ex'])
330
+
331
+ # Existential operator
332
+
333
+ assrt.ok(not gfsgs?)
334
+ assrt.equal(gfsgs ? 1, 1)
335
+ assrt.equal(gfsgs?.b, undefined)
336
+ assrt.equal(gfsgs?(), undefined) # noqa: undef
337
+ undef = undefined
338
+ assrt.ok(not undef?)
339
+ undef = None
340
+ assrt.ok(not undef?)
341
+ undef = 0
342
+ assrt.ok(undef?)
343
+ assrt.equal(undef?(), undefined)
344
+ assrt.equal(undef?.xxx?(), undefined)
345
+ assrt.equal(undef.a, undefined)
346
+ assrt.equal(undef[1], undefined)
347
+ assrt.equal(undef ? 1, 0)
348
+ ml = 1, '''
349
+ '''
350
+ assrt.deepEqual(ml, [1, '\n'])
351
+
352
+
353
+ # localvars in conditions
354
+
355
+ gv = 1
356
+ (def ():
357
+ if (gv = 2):
358
+ pass
359
+ assrt.equal(gv, 2)
360
+ )()
361
+ assrt.equal(gv, 1)
362
+
363
+ assrt.ok(1 not in [2, 3])
364
+ assrt.ok(1 in [1, 2])
365
+ assrt.ok("1" + "2" in ["12", "x"])
366
+ assrt.ok("1" + "2" not in ["1", "2"])
367
+ if "1" + "2" not in ["1", "2"]:
368
+ assrt.ok(1)
369
+ else:
370
+ assrt.ok(0)