rapydscript-ns 0.9.0 → 0.9.2
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.
- package/.agignore +1 -1
- package/.github/workflows/ci.yml +38 -38
- package/=template.pyj +5 -5
- package/CHANGELOG.md +10 -0
- package/HACKING.md +103 -103
- package/LICENSE +24 -24
- package/README.md +7 -6
- package/TODO.md +116 -1
- package/add-toc-to-readme +2 -2
- package/bin/export +75 -75
- package/bin/rapydscript +70 -70
- package/bin/web-repl-export +102 -102
- package/build +2 -2
- package/language-service/index.js +9 -9
- package/language-service/language-service.d.ts +1 -1
- package/package.json +6 -2
- package/publish.py +37 -37
- package/release/compiler.js +246 -231
- package/release/signatures.json +23 -23
- package/session.vim +4 -4
- package/setup.cfg +2 -2
- package/src/compiler.pyj +36 -36
- package/src/errors.pyj +30 -30
- package/src/lib/aes.pyj +646 -646
- package/src/lib/contextlib.pyj +379 -0
- package/src/lib/copy.pyj +120 -120
- package/src/lib/datetime.pyj +712 -0
- package/src/lib/elementmaker.pyj +83 -83
- package/src/lib/encodings.pyj +126 -126
- package/src/lib/gettext.pyj +569 -569
- package/src/lib/io.pyj +500 -0
- package/src/lib/itertools.pyj +580 -580
- package/src/lib/json.pyj +227 -0
- package/src/lib/math.pyj +193 -193
- package/src/lib/operator.pyj +11 -11
- package/src/lib/pythonize.pyj +20 -20
- package/src/lib/random.pyj +118 -118
- package/src/lib/react.pyj +74 -74
- package/src/lib/traceback.pyj +63 -63
- package/src/lib/uuid.pyj +77 -77
- package/src/monaco-language-service/diagnostics.js +4 -4
- package/src/monaco-language-service/dts.js +550 -550
- package/src/monaco-language-service/index.js +2 -2
- package/src/output/comments.pyj +45 -45
- package/src/output/exceptions.pyj +201 -201
- package/src/output/jsx.pyj +164 -164
- package/src/output/loops.pyj +9 -0
- package/src/output/treeshake.pyj +182 -182
- package/src/output/utils.pyj +72 -72
- package/src/string_interpolation.pyj +72 -72
- package/src/tokenizer.pyj +1 -1
- package/src/unicode_aliases.pyj +576 -576
- package/src/utils.pyj +192 -192
- package/test/_import_one.pyj +37 -37
- package/test/_import_two/__init__.pyj +11 -11
- package/test/_import_two/level2/deep.pyj +4 -4
- package/test/_import_two/other.pyj +6 -6
- package/test/_import_two/sub.pyj +13 -13
- package/test/aes_vectors.pyj +421 -421
- package/test/annotations.pyj +80 -80
- package/test/contextlib.pyj +362 -0
- package/test/datetime.pyj +500 -0
- package/test/debugger_stmt.pyj +41 -0
- package/test/decorators.pyj +77 -77
- package/test/docstrings.pyj +39 -39
- package/test/elementmaker_test.pyj +45 -45
- package/test/functions.pyj +151 -151
- package/test/generators.pyj +41 -41
- package/test/generic.pyj +370 -370
- package/test/imports.pyj +72 -72
- package/test/internationalization.pyj +73 -73
- package/test/io.pyj +316 -0
- package/test/json.pyj +196 -0
- package/test/lint.pyj +164 -164
- package/test/loops.pyj +85 -85
- package/test/numpy.pyj +734 -734
- package/test/omit_function_metadata.pyj +20 -20
- package/test/repl.pyj +121 -121
- package/test/scoped_flags.pyj +76 -76
- package/test/unit/index.js +66 -0
- package/test/unit/language-service-dts.js +543 -543
- package/test/unit/language-service-hover.js +455 -455
- package/test/unit/language-service.js +1 -1
- package/test/unit/web-repl.js +533 -0
- package/tools/compiler.d.ts +367 -0
- package/tools/completer.js +131 -131
- package/tools/gettext.js +185 -185
- package/tools/ini.js +65 -65
- package/tools/msgfmt.js +187 -187
- package/tools/repl.js +223 -223
- package/tools/test.js +118 -118
- package/tools/utils.js +128 -128
- package/tools/web_repl.js +95 -95
- package/try +41 -41
- package/web-repl/env.js +196 -196
- package/web-repl/index.html +163 -163
- package/web-repl/prism.css +139 -139
- package/web-repl/prism.js +113 -113
- package/web-repl/rapydscript.js +224 -224
- package/web-repl/sha1.js +25 -25
package/test/imports.pyj
CHANGED
|
@@ -1,72 +1,72 @@
|
|
|
1
|
-
# globals:test_path, GLOBAL_SYMBOL, assrt
|
|
2
|
-
from _import_one import toplevel_var, toplevel_func as tf, TopLevel, true_var, false_var, test_other
|
|
3
|
-
from _import_two import (toplevel_var2,
|
|
4
|
-
toplevel_func2, TopLevel2 as TL2)
|
|
5
|
-
|
|
6
|
-
def AClass(x):
|
|
7
|
-
return this
|
|
8
|
-
|
|
9
|
-
eq = assrt.equal
|
|
10
|
-
# Test import of top-level variables and callables
|
|
11
|
-
eq(toplevel_var, 'foo')
|
|
12
|
-
eq(tf('x'), 'xtoplevel')
|
|
13
|
-
eq(toplevel_var2, 'foo2')
|
|
14
|
-
eq(toplevel_func2('x'), 'xtoplevel2')
|
|
15
|
-
eq(false_var, undefined)
|
|
16
|
-
eq(test_other, 'other')
|
|
17
|
-
|
|
18
|
-
# Test import of top-level vars in a conditional
|
|
19
|
-
eq('true', true_var)
|
|
20
|
-
|
|
21
|
-
# Test plain imports
|
|
22
|
-
import _import_one
|
|
23
|
-
eq(_import_one.toplevel_var, toplevel_var)
|
|
24
|
-
eq(_import_one.toplevel_func('x'), tf('x'))
|
|
25
|
-
|
|
26
|
-
# Test recognition of imported classes
|
|
27
|
-
tl = TopLevel('1')
|
|
28
|
-
eq(tl.a, '1')
|
|
29
|
-
tl2 = TL2('x')
|
|
30
|
-
eq(tl2.a, 'x')
|
|
31
|
-
|
|
32
|
-
# Test access to submodules via plain imports
|
|
33
|
-
import _import_two.sub, _import_two.sub as ts
|
|
34
|
-
eq('sub', _import_two.sub.sub_var)
|
|
35
|
-
eq('sub', ts.sub_var)
|
|
36
|
-
eq('sub', _import_two.sub.sub_func())
|
|
37
|
-
|
|
38
|
-
# Test deep import
|
|
39
|
-
from _import_two.level2.deep import deep_var
|
|
40
|
-
eq('deep', deep_var)
|
|
41
|
-
|
|
42
|
-
# Test that class accessed via plain import is
|
|
43
|
-
# recognized
|
|
44
|
-
s = _import_two.sub.Sub(1)
|
|
45
|
-
eq(s.a, 1)
|
|
46
|
-
s2 = ts.Sub(1)
|
|
47
|
-
eq(s2.a, 1)
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
# Test that a class imported into an inner scope is not recognized as a class
|
|
51
|
-
# outside that scope
|
|
52
|
-
def inner():
|
|
53
|
-
from _import_one import AClass
|
|
54
|
-
a = AClass(1)
|
|
55
|
-
eq(a.a, 1)
|
|
56
|
-
|
|
57
|
-
inner()
|
|
58
|
-
b = AClass(1)
|
|
59
|
-
eq(b, this)
|
|
60
|
-
|
|
61
|
-
# Test global symbol declared in other module
|
|
62
|
-
eq(GLOBAL_SYMBOL, 'i am global')
|
|
63
|
-
|
|
64
|
-
# Import errors happen during parsing, so we cannot test them directly as they would
|
|
65
|
-
# prevent this file from being parsed.
|
|
66
|
-
|
|
67
|
-
assrt.throws(def():
|
|
68
|
-
RapydScript.parse('from _import_one import not_exported', {'basedir':test_path}).body[0]
|
|
69
|
-
, /not exported/)
|
|
70
|
-
assrt.throws(def():
|
|
71
|
-
RapydScript.parse('import xxxx', {'basedir':test_path}).body[0]
|
|
72
|
-
, /doesn't exist/)
|
|
1
|
+
# globals:test_path, GLOBAL_SYMBOL, assrt
|
|
2
|
+
from _import_one import toplevel_var, toplevel_func as tf, TopLevel, true_var, false_var, test_other
|
|
3
|
+
from _import_two import (toplevel_var2,
|
|
4
|
+
toplevel_func2, TopLevel2 as TL2)
|
|
5
|
+
|
|
6
|
+
def AClass(x):
|
|
7
|
+
return this
|
|
8
|
+
|
|
9
|
+
eq = assrt.equal
|
|
10
|
+
# Test import of top-level variables and callables
|
|
11
|
+
eq(toplevel_var, 'foo')
|
|
12
|
+
eq(tf('x'), 'xtoplevel')
|
|
13
|
+
eq(toplevel_var2, 'foo2')
|
|
14
|
+
eq(toplevel_func2('x'), 'xtoplevel2')
|
|
15
|
+
eq(false_var, undefined)
|
|
16
|
+
eq(test_other, 'other')
|
|
17
|
+
|
|
18
|
+
# Test import of top-level vars in a conditional
|
|
19
|
+
eq('true', true_var)
|
|
20
|
+
|
|
21
|
+
# Test plain imports
|
|
22
|
+
import _import_one
|
|
23
|
+
eq(_import_one.toplevel_var, toplevel_var)
|
|
24
|
+
eq(_import_one.toplevel_func('x'), tf('x'))
|
|
25
|
+
|
|
26
|
+
# Test recognition of imported classes
|
|
27
|
+
tl = TopLevel('1')
|
|
28
|
+
eq(tl.a, '1')
|
|
29
|
+
tl2 = TL2('x')
|
|
30
|
+
eq(tl2.a, 'x')
|
|
31
|
+
|
|
32
|
+
# Test access to submodules via plain imports
|
|
33
|
+
import _import_two.sub, _import_two.sub as ts
|
|
34
|
+
eq('sub', _import_two.sub.sub_var)
|
|
35
|
+
eq('sub', ts.sub_var)
|
|
36
|
+
eq('sub', _import_two.sub.sub_func())
|
|
37
|
+
|
|
38
|
+
# Test deep import
|
|
39
|
+
from _import_two.level2.deep import deep_var
|
|
40
|
+
eq('deep', deep_var)
|
|
41
|
+
|
|
42
|
+
# Test that class accessed via plain import is
|
|
43
|
+
# recognized
|
|
44
|
+
s = _import_two.sub.Sub(1)
|
|
45
|
+
eq(s.a, 1)
|
|
46
|
+
s2 = ts.Sub(1)
|
|
47
|
+
eq(s2.a, 1)
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
# Test that a class imported into an inner scope is not recognized as a class
|
|
51
|
+
# outside that scope
|
|
52
|
+
def inner():
|
|
53
|
+
from _import_one import AClass
|
|
54
|
+
a = AClass(1)
|
|
55
|
+
eq(a.a, 1)
|
|
56
|
+
|
|
57
|
+
inner()
|
|
58
|
+
b = AClass(1)
|
|
59
|
+
eq(b, this)
|
|
60
|
+
|
|
61
|
+
# Test global symbol declared in other module
|
|
62
|
+
eq(GLOBAL_SYMBOL, 'i am global')
|
|
63
|
+
|
|
64
|
+
# Import errors happen during parsing, so we cannot test them directly as they would
|
|
65
|
+
# prevent this file from being parsed.
|
|
66
|
+
|
|
67
|
+
assrt.throws(def():
|
|
68
|
+
RapydScript.parse('from _import_one import not_exported', {'basedir':test_path}).body[0]
|
|
69
|
+
, /not exported/)
|
|
70
|
+
assrt.throws(def():
|
|
71
|
+
RapydScript.parse('import xxxx', {'basedir':test_path}).body[0]
|
|
72
|
+
, /doesn't exist/)
|
|
@@ -1,73 +1,73 @@
|
|
|
1
|
-
# vim:fileencoding=utf-8
|
|
2
|
-
# License: BSD Copyright: 2015, Kovid Goyal <kovid at kovidgoyal.net>
|
|
3
|
-
|
|
4
|
-
from gettext import gettext as _, ngettext, install
|
|
5
|
-
|
|
6
|
-
g = require('../tools/gettext.js')
|
|
7
|
-
|
|
8
|
-
def gettext(code):
|
|
9
|
-
ans = {}
|
|
10
|
-
g.gettext(ans, code, '<test>')
|
|
11
|
-
return ans
|
|
12
|
-
|
|
13
|
-
def test_string(code, *args):
|
|
14
|
-
catalog = gettext(code)
|
|
15
|
-
assrt.equal(len(catalog), len(args))
|
|
16
|
-
for msgid, q in zip(Object.keys(catalog), args):
|
|
17
|
-
assrt.equal(g.entry_to_string(msgid, catalog[msgid]), q)
|
|
18
|
-
|
|
19
|
-
test_string('a = _("one")', '#: <test>:1\nmsgid "one"\nmsgstr ""')
|
|
20
|
-
test_string('a = _("one")\nb = gettext("one")', '#: <test>:1\n#: <test>:2\nmsgid "one"\nmsgstr ""')
|
|
21
|
-
test_string('''a = _("""one
|
|
22
|
-
two""")''', '#: <test>:1\nmsgid "one\\ntwo"\nmsgstr ""')
|
|
23
|
-
test_string('a = _("{}one")', '#: <test>:1\n#, python-brace-format\nmsgid "{}one"\nmsgstr ""')
|
|
24
|
-
test_string('a = _("{one}")', '#: <test>:1\n#, python-brace-format\nmsgid "{one}"\nmsgstr ""')
|
|
25
|
-
test_string('ngettext("one", "two", 1)', '#: <test>:1\nmsgid "one"\nmsgid_plural "two"\nmsgstr[0] ""\nmsgstr[1] ""')
|
|
26
|
-
test_string('''_('o"ne')''', '#: <test>:1\nmsgid "o\\"ne"\nmsgstr ""')
|
|
27
|
-
|
|
28
|
-
m = require('../tools/msgfmt.js')
|
|
29
|
-
catalog = m.parse(r'''
|
|
30
|
-
msgid ""
|
|
31
|
-
msgstr ""
|
|
32
|
-
"Language: en\n"
|
|
33
|
-
"Plural-Forms: nplurals=2; \n"
|
|
34
|
-
|
|
35
|
-
#, fuzzy
|
|
36
|
-
msgid "one\n"
|
|
37
|
-
"continued"
|
|
38
|
-
msgstr "ON"
|
|
39
|
-
"E"
|
|
40
|
-
|
|
41
|
-
msgid "two"
|
|
42
|
-
msgid_plural "three"
|
|
43
|
-
msgstr[0] "a"
|
|
44
|
-
"bc"
|
|
45
|
-
msgstr[1] "def"
|
|
46
|
-
|
|
47
|
-
msgid "test \"quote\" escape"
|
|
48
|
-
msgstr "good"
|
|
49
|
-
''')
|
|
50
|
-
|
|
51
|
-
assrt.equal(2, catalog['nplurals'])
|
|
52
|
-
assrt.equal('en', catalog['language'])
|
|
53
|
-
assrt.equal(catalog['entries'].length, 3)
|
|
54
|
-
item = catalog['entries'][0]
|
|
55
|
-
assrt.equal(item['msgid'], 'one\ncontinued')
|
|
56
|
-
assrt.deepEqual(item['msgstr'], v"['ONE']")
|
|
57
|
-
assrt.ok(item['fuzzy'], 'item not fuzzy')
|
|
58
|
-
item = catalog['entries'][1]
|
|
59
|
-
assrt.equal(item['msgid'], 'two')
|
|
60
|
-
assrt.deepEqual(item['msgstr'], v"['abc', 'def']")
|
|
61
|
-
assrt.ok(not item['fuzzy'], 'item not fuzzy')
|
|
62
|
-
item = catalog['entries'][2]
|
|
63
|
-
assrt.equal(item['msgid'], 'test "quote" escape')
|
|
64
|
-
assrt.deepEqual(item['msgstr'], v"['good']")
|
|
65
|
-
|
|
66
|
-
install({'entries': {
|
|
67
|
-
'one':['ONE'],
|
|
68
|
-
'two':['1', '2'],
|
|
69
|
-
}})
|
|
70
|
-
|
|
71
|
-
assrt.equal(_('one'), 'ONE')
|
|
72
|
-
assrt.equal(ngettext('two', 'xxx', 1), '1')
|
|
73
|
-
assrt.equal(ngettext('two', 'xxx', 100), '2')
|
|
1
|
+
# vim:fileencoding=utf-8
|
|
2
|
+
# License: BSD Copyright: 2015, Kovid Goyal <kovid at kovidgoyal.net>
|
|
3
|
+
|
|
4
|
+
from gettext import gettext as _, ngettext, install
|
|
5
|
+
|
|
6
|
+
g = require('../tools/gettext.js')
|
|
7
|
+
|
|
8
|
+
def gettext(code):
|
|
9
|
+
ans = {}
|
|
10
|
+
g.gettext(ans, code, '<test>')
|
|
11
|
+
return ans
|
|
12
|
+
|
|
13
|
+
def test_string(code, *args):
|
|
14
|
+
catalog = gettext(code)
|
|
15
|
+
assrt.equal(len(catalog), len(args))
|
|
16
|
+
for msgid, q in zip(Object.keys(catalog), args):
|
|
17
|
+
assrt.equal(g.entry_to_string(msgid, catalog[msgid]), q)
|
|
18
|
+
|
|
19
|
+
test_string('a = _("one")', '#: <test>:1\nmsgid "one"\nmsgstr ""')
|
|
20
|
+
test_string('a = _("one")\nb = gettext("one")', '#: <test>:1\n#: <test>:2\nmsgid "one"\nmsgstr ""')
|
|
21
|
+
test_string('''a = _("""one
|
|
22
|
+
two""")''', '#: <test>:1\nmsgid "one\\ntwo"\nmsgstr ""')
|
|
23
|
+
test_string('a = _("{}one")', '#: <test>:1\n#, python-brace-format\nmsgid "{}one"\nmsgstr ""')
|
|
24
|
+
test_string('a = _("{one}")', '#: <test>:1\n#, python-brace-format\nmsgid "{one}"\nmsgstr ""')
|
|
25
|
+
test_string('ngettext("one", "two", 1)', '#: <test>:1\nmsgid "one"\nmsgid_plural "two"\nmsgstr[0] ""\nmsgstr[1] ""')
|
|
26
|
+
test_string('''_('o"ne')''', '#: <test>:1\nmsgid "o\\"ne"\nmsgstr ""')
|
|
27
|
+
|
|
28
|
+
m = require('../tools/msgfmt.js')
|
|
29
|
+
catalog = m.parse(r'''
|
|
30
|
+
msgid ""
|
|
31
|
+
msgstr ""
|
|
32
|
+
"Language: en\n"
|
|
33
|
+
"Plural-Forms: nplurals=2; \n"
|
|
34
|
+
|
|
35
|
+
#, fuzzy
|
|
36
|
+
msgid "one\n"
|
|
37
|
+
"continued"
|
|
38
|
+
msgstr "ON"
|
|
39
|
+
"E"
|
|
40
|
+
|
|
41
|
+
msgid "two"
|
|
42
|
+
msgid_plural "three"
|
|
43
|
+
msgstr[0] "a"
|
|
44
|
+
"bc"
|
|
45
|
+
msgstr[1] "def"
|
|
46
|
+
|
|
47
|
+
msgid "test \"quote\" escape"
|
|
48
|
+
msgstr "good"
|
|
49
|
+
''')
|
|
50
|
+
|
|
51
|
+
assrt.equal(2, catalog['nplurals'])
|
|
52
|
+
assrt.equal('en', catalog['language'])
|
|
53
|
+
assrt.equal(catalog['entries'].length, 3)
|
|
54
|
+
item = catalog['entries'][0]
|
|
55
|
+
assrt.equal(item['msgid'], 'one\ncontinued')
|
|
56
|
+
assrt.deepEqual(item['msgstr'], v"['ONE']")
|
|
57
|
+
assrt.ok(item['fuzzy'], 'item not fuzzy')
|
|
58
|
+
item = catalog['entries'][1]
|
|
59
|
+
assrt.equal(item['msgid'], 'two')
|
|
60
|
+
assrt.deepEqual(item['msgstr'], v"['abc', 'def']")
|
|
61
|
+
assrt.ok(not item['fuzzy'], 'item not fuzzy')
|
|
62
|
+
item = catalog['entries'][2]
|
|
63
|
+
assrt.equal(item['msgid'], 'test "quote" escape')
|
|
64
|
+
assrt.deepEqual(item['msgstr'], v"['good']")
|
|
65
|
+
|
|
66
|
+
install({'entries': {
|
|
67
|
+
'one':['ONE'],
|
|
68
|
+
'two':['1', '2'],
|
|
69
|
+
}})
|
|
70
|
+
|
|
71
|
+
assrt.equal(_('one'), 'ONE')
|
|
72
|
+
assrt.equal(ngettext('two', 'xxx', 1), '1')
|
|
73
|
+
assrt.equal(ngettext('two', 'xxx', 100), '2')
|
package/test/io.pyj
ADDED
|
@@ -0,0 +1,316 @@
|
|
|
1
|
+
# globals: assrt
|
|
2
|
+
# vim:fileencoding=utf-8
|
|
3
|
+
#
|
|
4
|
+
# io.pyj
|
|
5
|
+
# Tests for the io standard library module (StringIO and BytesIO).
|
|
6
|
+
|
|
7
|
+
from io import StringIO, BytesIO, UnsupportedOperation
|
|
8
|
+
|
|
9
|
+
ae = assrt.equal
|
|
10
|
+
ade = assrt.deepEqual
|
|
11
|
+
ok = assrt.ok
|
|
12
|
+
throws = assrt.throws
|
|
13
|
+
|
|
14
|
+
# ── 1. StringIO — construction ────────────────────────────────────────────────
|
|
15
|
+
|
|
16
|
+
_sio = StringIO()
|
|
17
|
+
ae(_sio.getvalue(), '')
|
|
18
|
+
ae(_sio.tell(), 0)
|
|
19
|
+
ok(not _sio.closed)
|
|
20
|
+
|
|
21
|
+
_sio2 = StringIO('hello')
|
|
22
|
+
ae(_sio2.getvalue(), 'hello')
|
|
23
|
+
ae(_sio2.tell(), 0)
|
|
24
|
+
|
|
25
|
+
# ── 2. StringIO — read ────────────────────────────────────────────────────────
|
|
26
|
+
|
|
27
|
+
_s = StringIO('hello world')
|
|
28
|
+
ae(_s.read(5), 'hello')
|
|
29
|
+
ae(_s.read(1), ' ')
|
|
30
|
+
ae(_s.read(), 'world')
|
|
31
|
+
ae(_s.read(), '') # at end
|
|
32
|
+
|
|
33
|
+
_s.seek(0)
|
|
34
|
+
ae(_s.read(), 'hello world')
|
|
35
|
+
|
|
36
|
+
# read with size=0
|
|
37
|
+
_s.seek(0)
|
|
38
|
+
ae(_s.read(0), '')
|
|
39
|
+
ae(_s.tell(), 0)
|
|
40
|
+
|
|
41
|
+
# ── 3. StringIO — readline ────────────────────────────────────────────────────
|
|
42
|
+
|
|
43
|
+
_ml = StringIO('line1\nline2\nline3')
|
|
44
|
+
ae(_ml.readline(), 'line1\n')
|
|
45
|
+
ae(_ml.readline(), 'line2\n')
|
|
46
|
+
ae(_ml.readline(), 'line3')
|
|
47
|
+
ae(_ml.readline(), '') # past end
|
|
48
|
+
|
|
49
|
+
# readline with size limit
|
|
50
|
+
_ml2 = StringIO('abcdef\nghijkl')
|
|
51
|
+
ae(_ml2.readline(3), 'abc')
|
|
52
|
+
ae(_ml2.readline(100), 'def\n')
|
|
53
|
+
|
|
54
|
+
# ── 4. StringIO — readlines ───────────────────────────────────────────────────
|
|
55
|
+
|
|
56
|
+
_rls = StringIO('a\nb\nc\n')
|
|
57
|
+
ade(_rls.readlines(), ['a\n', 'b\n', 'c\n'])
|
|
58
|
+
|
|
59
|
+
_rls2 = StringIO('x\ny\nz')
|
|
60
|
+
ade(_rls2.readlines(), ['x\n', 'y\n', 'z'])
|
|
61
|
+
|
|
62
|
+
# hint stops early (total >= hint)
|
|
63
|
+
_hint = StringIO('aa\nbb\ncc\n')
|
|
64
|
+
_hlines = _hint.readlines(hint=4)
|
|
65
|
+
ok(_hlines.length >= 1)
|
|
66
|
+
ok(_hlines.length <= 3)
|
|
67
|
+
|
|
68
|
+
# ── 5. StringIO — write ───────────────────────────────────────────────────────
|
|
69
|
+
|
|
70
|
+
_w = StringIO()
|
|
71
|
+
_w.write('hello')
|
|
72
|
+
_w.write(' ')
|
|
73
|
+
_w.write('world')
|
|
74
|
+
ae(_w.getvalue(), 'hello world')
|
|
75
|
+
ae(_w.tell(), 11)
|
|
76
|
+
|
|
77
|
+
# write returns the number of chars written
|
|
78
|
+
_w2 = StringIO()
|
|
79
|
+
ae(_w2.write('abc'), 3)
|
|
80
|
+
ae(_w2.write(''), 0)
|
|
81
|
+
|
|
82
|
+
# overwrite in the middle
|
|
83
|
+
_ow = StringIO('hello world')
|
|
84
|
+
_ow.seek(6)
|
|
85
|
+
_ow.write('Python')
|
|
86
|
+
ae(_ow.getvalue(), 'hello Python')
|
|
87
|
+
|
|
88
|
+
# ── 6. StringIO — writelines ──────────────────────────────────────────────────
|
|
89
|
+
|
|
90
|
+
_wl = StringIO()
|
|
91
|
+
_wl.writelines(['one', ' ', 'two'])
|
|
92
|
+
ae(_wl.getvalue(), 'one two')
|
|
93
|
+
|
|
94
|
+
# ── 7. StringIO — seek / tell ─────────────────────────────────────────────────
|
|
95
|
+
|
|
96
|
+
_sk = StringIO('abcde')
|
|
97
|
+
ae(_sk.seek(2), 2)
|
|
98
|
+
ae(_sk.tell(), 2)
|
|
99
|
+
ae(_sk.read(2), 'cd')
|
|
100
|
+
|
|
101
|
+
# whence=1 (relative)
|
|
102
|
+
_sk.seek(0)
|
|
103
|
+
_sk.seek(2, 1)
|
|
104
|
+
ae(_sk.tell(), 2)
|
|
105
|
+
|
|
106
|
+
# whence=2 (from end)
|
|
107
|
+
_sk.seek(-2, 2)
|
|
108
|
+
ae(_sk.tell(), 3)
|
|
109
|
+
ae(_sk.read(), 'de')
|
|
110
|
+
|
|
111
|
+
# seek before start clamps to 0
|
|
112
|
+
_sk.seek(-99)
|
|
113
|
+
ae(_sk.tell(), 0)
|
|
114
|
+
|
|
115
|
+
# ── 8. StringIO — truncate ────────────────────────────────────────────────────
|
|
116
|
+
|
|
117
|
+
_tr = StringIO('hello world')
|
|
118
|
+
_tr.seek(5)
|
|
119
|
+
ae(_tr.truncate(), 5)
|
|
120
|
+
ae(_tr.getvalue(), 'hello')
|
|
121
|
+
|
|
122
|
+
_tr2 = StringIO('hello world')
|
|
123
|
+
ae(_tr2.truncate(3), 3)
|
|
124
|
+
ae(_tr2.getvalue(), 'hel')
|
|
125
|
+
|
|
126
|
+
# ── 9. StringIO — closed / context manager ────────────────────────────────────
|
|
127
|
+
|
|
128
|
+
_cm = StringIO('text')
|
|
129
|
+
with _cm as _f:
|
|
130
|
+
ae(_f.read(), 'text')
|
|
131
|
+
ok(_cm.closed)
|
|
132
|
+
|
|
133
|
+
_err_raised = False
|
|
134
|
+
try:
|
|
135
|
+
_cm.read()
|
|
136
|
+
except ValueError:
|
|
137
|
+
_err_raised = True
|
|
138
|
+
ok(_err_raised, 'read on closed StringIO should raise ValueError')
|
|
139
|
+
|
|
140
|
+
# ── 10. StringIO — readable / writable / seekable ────────────────────────────
|
|
141
|
+
|
|
142
|
+
_rws = StringIO()
|
|
143
|
+
ok(_rws.readable())
|
|
144
|
+
ok(_rws.writable())
|
|
145
|
+
ok(_rws.seekable())
|
|
146
|
+
|
|
147
|
+
# ── 11. StringIO — iteration ──────────────────────────────────────────────────
|
|
148
|
+
|
|
149
|
+
_it = StringIO('line1\nline2\nline3\n')
|
|
150
|
+
_lines = list(_it)
|
|
151
|
+
ade(_lines, ['line1\n', 'line2\n', 'line3\n'])
|
|
152
|
+
|
|
153
|
+
# ── 12. BytesIO — construction ────────────────────────────────────────────────
|
|
154
|
+
|
|
155
|
+
_bio = BytesIO()
|
|
156
|
+
ae(len(_bio.getvalue()), 0)
|
|
157
|
+
ae(_bio.tell(), 0)
|
|
158
|
+
ok(not _bio.closed)
|
|
159
|
+
|
|
160
|
+
_bio2 = BytesIO(bytes([72, 101, 108, 108, 111]))
|
|
161
|
+
ae(len(_bio2.getvalue()), 5)
|
|
162
|
+
ae(_bio2.getvalue()[0], 72)
|
|
163
|
+
ae(_bio2.getvalue()[4], 111)
|
|
164
|
+
|
|
165
|
+
# init from bytearray
|
|
166
|
+
_bio3 = BytesIO(bytearray([1, 2, 3]))
|
|
167
|
+
ae(len(_bio3.getvalue()), 3)
|
|
168
|
+
|
|
169
|
+
# ── 13. BytesIO — read ────────────────────────────────────────────────────────
|
|
170
|
+
|
|
171
|
+
_br = BytesIO(bytes([10, 20, 30, 40, 50]))
|
|
172
|
+
ae(_br.read(2)[0], 10)
|
|
173
|
+
ae(_br.read(2)[0], 30)
|
|
174
|
+
ae(len(_br.read()), 1)
|
|
175
|
+
ae(len(_br.read()), 0) # at end
|
|
176
|
+
|
|
177
|
+
_br.seek(0)
|
|
178
|
+
ae(len(_br.read()), 5)
|
|
179
|
+
|
|
180
|
+
# ── 14. BytesIO — readline ────────────────────────────────────────────────────
|
|
181
|
+
|
|
182
|
+
# b'line1\nline2' (0x0a is newline)
|
|
183
|
+
_brl = BytesIO(bytes([108, 49, 10, 108, 50])) # 'l1\nl2'
|
|
184
|
+
_first = _brl.readline()
|
|
185
|
+
ae(len(_first), 3)
|
|
186
|
+
ae(_first[2], 10) # includes the newline byte
|
|
187
|
+
|
|
188
|
+
_second = _brl.readline()
|
|
189
|
+
ae(len(_second), 2)
|
|
190
|
+
|
|
191
|
+
ae(len(_brl.readline()), 0) # at end
|
|
192
|
+
|
|
193
|
+
# ── 15. BytesIO — write ───────────────────────────────────────────────────────
|
|
194
|
+
|
|
195
|
+
_bw = BytesIO()
|
|
196
|
+
ae(_bw.write(bytes([1, 2, 3])), 3)
|
|
197
|
+
ae(_bw.write(bytes([4, 5])), 2)
|
|
198
|
+
_bw.seek(0)
|
|
199
|
+
ae(len(_bw.read()), 5)
|
|
200
|
+
ae(_bw.getvalue()[0], 1)
|
|
201
|
+
ae(_bw.getvalue()[4], 5)
|
|
202
|
+
|
|
203
|
+
# write returns byte count
|
|
204
|
+
_bw2 = BytesIO()
|
|
205
|
+
ae(_bw2.write(bytes()), 0)
|
|
206
|
+
|
|
207
|
+
# overwrite in the middle
|
|
208
|
+
_bo = BytesIO(bytes([1, 2, 3, 4, 5]))
|
|
209
|
+
_bo.seek(1)
|
|
210
|
+
_bo.write(bytes([20, 30]))
|
|
211
|
+
_bo.seek(0)
|
|
212
|
+
_boval = _bo.read()
|
|
213
|
+
ae(_boval[0], 1)
|
|
214
|
+
ae(_boval[1], 20)
|
|
215
|
+
ae(_boval[2], 30)
|
|
216
|
+
ae(_boval[3], 4)
|
|
217
|
+
ae(_boval[4], 5)
|
|
218
|
+
|
|
219
|
+
# write past end extends with zeros
|
|
220
|
+
_bext = BytesIO(bytes([1, 2]))
|
|
221
|
+
_bext.seek(4)
|
|
222
|
+
_bext.write(bytes([99]))
|
|
223
|
+
_bextval = _bext.getvalue()
|
|
224
|
+
ae(len(_bextval), 5)
|
|
225
|
+
ae(_bextval[2], 0)
|
|
226
|
+
ae(_bextval[3], 0)
|
|
227
|
+
ae(_bextval[4], 99)
|
|
228
|
+
|
|
229
|
+
# ── 16. BytesIO — writelines ─────────────────────────────────────────────────
|
|
230
|
+
|
|
231
|
+
_bwl = BytesIO()
|
|
232
|
+
_bwl.writelines([bytes([1, 2]), bytes([3, 4])])
|
|
233
|
+
ae(len(_bwl.getvalue()), 4)
|
|
234
|
+
|
|
235
|
+
# ── 17. BytesIO — seek / tell ─────────────────────────────────────────────────
|
|
236
|
+
|
|
237
|
+
_bsk = BytesIO(bytes([10, 20, 30, 40, 50]))
|
|
238
|
+
ae(_bsk.seek(3), 3)
|
|
239
|
+
ae(_bsk.tell(), 3)
|
|
240
|
+
ae(_bsk.read(1)[0], 40)
|
|
241
|
+
|
|
242
|
+
# whence=1
|
|
243
|
+
_bsk.seek(0)
|
|
244
|
+
_bsk.seek(2, 1)
|
|
245
|
+
ae(_bsk.tell(), 2)
|
|
246
|
+
|
|
247
|
+
# whence=2
|
|
248
|
+
_bsk.seek(-1, 2)
|
|
249
|
+
ae(_bsk.tell(), 4)
|
|
250
|
+
ae(_bsk.read(1)[0], 50)
|
|
251
|
+
|
|
252
|
+
# clamp below 0
|
|
253
|
+
_bsk.seek(-999)
|
|
254
|
+
ae(_bsk.tell(), 0)
|
|
255
|
+
|
|
256
|
+
# ── 18. BytesIO — truncate ────────────────────────────────────────────────────
|
|
257
|
+
|
|
258
|
+
_btr = BytesIO(bytes([1, 2, 3, 4, 5]))
|
|
259
|
+
_btr.seek(3)
|
|
260
|
+
ae(_btr.truncate(), 3)
|
|
261
|
+
ae(len(_btr.getvalue()), 3)
|
|
262
|
+
|
|
263
|
+
_btr2 = BytesIO(bytes([10, 20, 30, 40]))
|
|
264
|
+
ae(_btr2.truncate(2), 2)
|
|
265
|
+
ae(len(_btr2.getvalue()), 2)
|
|
266
|
+
ae(_btr2.getvalue()[1], 20)
|
|
267
|
+
|
|
268
|
+
# ── 19. BytesIO — closed / context manager ───────────────────────────────────
|
|
269
|
+
|
|
270
|
+
_bcm = BytesIO(bytes([7, 8, 9]))
|
|
271
|
+
with _bcm as _bf:
|
|
272
|
+
_bf.seek(0)
|
|
273
|
+
ae(_bf.read()[0], 7)
|
|
274
|
+
ok(_bcm.closed)
|
|
275
|
+
|
|
276
|
+
_berr_raised = False
|
|
277
|
+
try:
|
|
278
|
+
_bcm.read()
|
|
279
|
+
except ValueError:
|
|
280
|
+
_berr_raised = True
|
|
281
|
+
ok(_berr_raised, 'read on closed BytesIO should raise ValueError')
|
|
282
|
+
|
|
283
|
+
# ── 20. BytesIO — readable / writable / seekable ─────────────────────────────
|
|
284
|
+
|
|
285
|
+
_brws = BytesIO()
|
|
286
|
+
ok(_brws.readable())
|
|
287
|
+
ok(_brws.writable())
|
|
288
|
+
ok(_brws.seekable())
|
|
289
|
+
|
|
290
|
+
# ── 21. BytesIO — iteration ───────────────────────────────────────────────────
|
|
291
|
+
|
|
292
|
+
_bit = BytesIO(bytes([65, 66, 10, 67, 68, 10])) # 'AB\nCD\n'
|
|
293
|
+
_bitlines = list(_bit)
|
|
294
|
+
ae(_bitlines.length, 2)
|
|
295
|
+
ae(_bitlines[0][0], 65) # 'A'
|
|
296
|
+
ae(_bitlines[0][2], 10) # '\n'
|
|
297
|
+
|
|
298
|
+
# ── 22. getvalue() does not depend on position ────────────────────────────────
|
|
299
|
+
|
|
300
|
+
_gv = StringIO('hello')
|
|
301
|
+
_gv.seek(3)
|
|
302
|
+
ae(_gv.getvalue(), 'hello') # full buffer regardless of pos
|
|
303
|
+
|
|
304
|
+
_bgv = BytesIO(bytes([1, 2, 3]))
|
|
305
|
+
_bgv.seek(2)
|
|
306
|
+
ae(len(_bgv.getvalue()), 3)
|
|
307
|
+
|
|
308
|
+
# ── 23. round-trip with json.dump / json.load ────────────────────────────────
|
|
309
|
+
|
|
310
|
+
from json import dump, load
|
|
311
|
+
_jsio = StringIO()
|
|
312
|
+
dump({'key': 'value', 'n': 42}, _jsio)
|
|
313
|
+
_jsio.seek(0)
|
|
314
|
+
_result = load(_jsio)
|
|
315
|
+
ae(_result.key, 'value')
|
|
316
|
+
ae(_result.n, 42)
|