rapydscript-ns 0.9.2 → 0.9.3

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 (151) 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 +19 -0
  5. package/HACKING.md +103 -103
  6. package/LICENSE +24 -24
  7. package/PYTHON_GAPS.md +420 -0
  8. package/README.md +153 -29
  9. package/TODO.md +16 -118
  10. package/add-toc-to-readme +2 -2
  11. package/bin/export +75 -75
  12. package/bin/rapydscript +70 -70
  13. package/bin/web-repl-export +102 -102
  14. package/build +2 -2
  15. package/language-service/index.js +237 -8
  16. package/memory/project_string_impl.md +43 -0
  17. package/package.json +1 -1
  18. package/publish.py +37 -37
  19. package/release/baselib-plain-pretty.js +248 -38
  20. package/release/baselib-plain-ugly.js +8 -8
  21. package/release/compiler.js +778 -277
  22. package/release/signatures.json +30 -30
  23. package/session.vim +4 -4
  24. package/setup.cfg +2 -2
  25. package/src/ast.pyj +4 -1
  26. package/src/baselib-builtins.pyj +56 -2
  27. package/src/baselib-containers.pyj +2 -0
  28. package/src/baselib-errors.pyj +7 -3
  29. package/src/baselib-internal.pyj +51 -6
  30. package/src/baselib-str.pyj +5 -3
  31. package/src/compiler.pyj +36 -36
  32. package/src/errors.pyj +30 -30
  33. package/src/lib/aes.pyj +646 -646
  34. package/src/lib/asyncio.pyj +534 -0
  35. package/src/lib/base64.pyj +399 -0
  36. package/src/lib/bisect.pyj +73 -0
  37. package/src/lib/collections.pyj +1 -1
  38. package/src/lib/copy.pyj +120 -120
  39. package/src/lib/csv.pyj +494 -0
  40. package/src/lib/elementmaker.pyj +83 -83
  41. package/src/lib/encodings.pyj +126 -126
  42. package/src/lib/gettext.pyj +569 -569
  43. package/src/lib/heapq.pyj +98 -0
  44. package/src/lib/html.pyj +382 -0
  45. package/src/lib/http/__init__.pyj +98 -0
  46. package/src/lib/http/client.pyj +304 -0
  47. package/src/lib/http/cookies.pyj +236 -0
  48. package/src/lib/itertools.pyj +580 -580
  49. package/src/lib/logging.pyj +672 -0
  50. package/src/lib/math.pyj +193 -193
  51. package/src/lib/operator.pyj +11 -11
  52. package/src/lib/pythonize.pyj +20 -20
  53. package/src/lib/random.pyj +118 -118
  54. package/src/lib/react.pyj +74 -74
  55. package/src/lib/string.pyj +357 -0
  56. package/src/lib/textwrap.pyj +329 -0
  57. package/src/lib/traceback.pyj +63 -63
  58. package/src/lib/urllib/__init__.pyj +14 -0
  59. package/src/lib/urllib/error.pyj +66 -0
  60. package/src/lib/urllib/parse.pyj +475 -0
  61. package/src/lib/urllib/request.pyj +86 -0
  62. package/src/lib/uuid.pyj +77 -77
  63. package/src/monaco-language-service/analyzer.js +5 -2
  64. package/src/monaco-language-service/completions.js +26 -0
  65. package/src/monaco-language-service/diagnostics.js +202 -3
  66. package/src/monaco-language-service/dts.js +550 -550
  67. package/src/monaco-language-service/scope.js +1 -0
  68. package/src/output/comments.pyj +45 -45
  69. package/src/output/exceptions.pyj +201 -201
  70. package/src/output/functions.pyj +152 -6
  71. package/src/output/jsx.pyj +164 -164
  72. package/src/output/loops.pyj +17 -2
  73. package/src/output/modules.pyj +1 -1
  74. package/src/output/operators.pyj +15 -0
  75. package/src/output/stream.pyj +0 -1
  76. package/src/output/treeshake.pyj +182 -182
  77. package/src/output/utils.pyj +72 -72
  78. package/src/parse.pyj +80 -17
  79. package/src/string_interpolation.pyj +72 -72
  80. package/src/tokenizer.pyj +1 -1
  81. package/src/unicode_aliases.pyj +576 -576
  82. package/src/utils.pyj +192 -192
  83. package/test/_import_one.pyj +37 -37
  84. package/test/_import_two/__init__.pyj +11 -11
  85. package/test/_import_two/level2/deep.pyj +4 -4
  86. package/test/_import_two/other.pyj +6 -6
  87. package/test/_import_two/sub.pyj +13 -13
  88. package/test/aes_vectors.pyj +421 -421
  89. package/test/annotations.pyj +80 -80
  90. package/test/async_generators.pyj +144 -0
  91. package/test/asyncio.pyj +307 -0
  92. package/test/base64.pyj +202 -0
  93. package/test/bisect.pyj +178 -0
  94. package/test/csv.pyj +405 -0
  95. package/test/decorators.pyj +77 -77
  96. package/test/docstrings.pyj +39 -39
  97. package/test/elementmaker_test.pyj +45 -45
  98. package/test/float_special.pyj +64 -0
  99. package/test/functions.pyj +151 -151
  100. package/test/generators.pyj +41 -41
  101. package/test/generic.pyj +370 -370
  102. package/test/heapq.pyj +174 -0
  103. package/test/html.pyj +212 -0
  104. package/test/http.pyj +259 -0
  105. package/test/imports.pyj +79 -72
  106. package/test/internationalization.pyj +73 -73
  107. package/test/lint.pyj +164 -164
  108. package/test/logging.pyj +356 -0
  109. package/test/long.pyj +130 -0
  110. package/test/loops.pyj +85 -85
  111. package/test/numpy.pyj +734 -734
  112. package/test/parenthesized_with.pyj +141 -0
  113. package/test/python_compat.pyj +3 -5
  114. package/test/python_modulo.pyj +76 -0
  115. package/test/python_modulo_off.pyj +21 -0
  116. package/test/repl.pyj +121 -121
  117. package/test/scoped_flags.pyj +76 -76
  118. package/test/str.pyj +14 -0
  119. package/test/string.pyj +245 -0
  120. package/test/textwrap.pyj +172 -0
  121. package/test/type_display.pyj +48 -0
  122. package/test/type_enforcement.pyj +164 -0
  123. package/test/unit/index.js +14 -6
  124. package/test/unit/language-service-completions.js +119 -0
  125. package/test/unit/language-service-dts.js +543 -543
  126. package/test/unit/language-service-hover.js +455 -455
  127. package/test/unit/language-service-scope.js +32 -0
  128. package/test/unit/language-service.js +127 -3
  129. package/test/unit/run-language-service.js +17 -3
  130. package/test/unit/web-repl.js +2094 -29
  131. package/test/urllib.pyj +193 -0
  132. package/tools/compile.js +1 -1
  133. package/tools/compiler.d.ts +367 -367
  134. package/tools/completer.js +131 -131
  135. package/tools/embedded_compiler.js +7 -7
  136. package/tools/gettext.js +185 -185
  137. package/tools/ini.js +65 -65
  138. package/tools/msgfmt.js +187 -187
  139. package/tools/repl.js +223 -223
  140. package/tools/test.js +118 -118
  141. package/tools/utils.js +128 -128
  142. package/tools/web_repl.js +95 -95
  143. package/try +41 -41
  144. package/web-repl/env.js +196 -196
  145. package/web-repl/index.html +163 -163
  146. package/web-repl/main.js +1 -1
  147. package/web-repl/prism.css +139 -139
  148. package/web-repl/prism.js +113 -113
  149. package/web-repl/rapydscript.js +224 -224
  150. package/web-repl/sha1.js +25 -25
  151. package/test/omit_function_metadata.pyj +0 -20
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)