2019-12-16 12:01:38 +01:00
|
|
|
from collections import namedtuple
|
|
|
|
|
|
|
|
|
|
import libfoolang
|
|
|
|
|
|
|
|
|
|
|
2025-03-20 12:57:48 +00:00
|
|
|
print("main.py: Running...")
|
2019-12-16 12:01:38 +01:00
|
|
|
|
2025-03-20 12:57:48 +00:00
|
|
|
ctx = libfoolang.AnalysisContext("iso-8859-1")
|
|
|
|
|
u = ctx.get_from_buffer("foo.txt", b"")
|
2019-12-16 12:01:38 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_from_buffer(buffer, charset):
|
2025-03-20 12:57:48 +00:00
|
|
|
ctx.get_from_buffer("foo.txt", buffer, charset)
|
2019-12-16 12:01:38 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def reparse(buffer, charset):
|
|
|
|
|
u.reparse(buffer, charset)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Check that get_from_buffer/reparse correctly process buffer/charset
|
|
|
|
|
# arguments:
|
|
|
|
|
#
|
|
|
|
|
# * either buffer is a bytes string, then the charset argument (if provided) or
|
|
|
|
|
# the context-wide charset (iso-8859-1, see above) is used to decode it.
|
|
|
|
|
#
|
|
|
|
|
# * either buffer is a Unicode string, then the charset argument must be null
|
|
|
|
|
# (None or empty string).
|
|
|
|
|
|
2025-03-20 12:57:48 +00:00
|
|
|
Testcase = namedtuple("Testcase", "buffer charset")
|
2019-12-16 12:01:38 +01:00
|
|
|
|
|
|
|
|
testcases = [
|
2025-03-20 12:57:48 +00:00
|
|
|
Testcase("example # H\xe9llo", None),
|
|
|
|
|
Testcase("example # H\xeallo", ""),
|
|
|
|
|
Testcase("example # H\xebllo", "utf-8"),
|
|
|
|
|
Testcase(b"example # H\xe9llo", None),
|
|
|
|
|
Testcase(b"example # H\xeallo", ""),
|
|
|
|
|
Testcase(b"example # H\xebllo", "iso-8859-1"),
|
|
|
|
|
Testcase(b"example # H\xecllo", "utf-8"),
|
|
|
|
|
Testcase(b"example # H\xecllo", "unknown-charset"),
|
2019-12-16 12:01:38 +01:00
|
|
|
# Check that successfully parsing a unit with one encoding (UTF-8) has no
|
|
|
|
|
# influence on the default encoding used later.
|
2025-03-20 12:57:48 +00:00
|
|
|
Testcase(b"example # H\xc3\xa9llo", "utf-8"),
|
|
|
|
|
Testcase(b"example # H\xc3\xa9llo", None),
|
2019-12-16 12:01:38 +01:00
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for method in (get_from_buffer, reparse):
|
2025-03-20 12:57:48 +00:00
|
|
|
print("== {} ==".format(method.__name__))
|
|
|
|
|
print("")
|
2019-12-16 12:01:38 +01:00
|
|
|
for tc in testcases:
|
|
|
|
|
try:
|
|
|
|
|
method(tc.buffer, tc.charset)
|
|
|
|
|
except Exception as exc:
|
2025-03-20 12:57:48 +00:00
|
|
|
result = "{}: {}".format(type(exc).__name__, exc)
|
2019-12-16 12:01:38 +01:00
|
|
|
else:
|
|
|
|
|
if u.diagnostics:
|
2025-03-20 12:57:48 +00:00
|
|
|
result = "\n".join(
|
|
|
|
|
["diagnostics:"]
|
|
|
|
|
+ [" {}".format(d) for d in u.diagnostics]
|
|
|
|
|
)
|
2019-12-16 12:01:38 +01:00
|
|
|
else:
|
2021-09-27 10:46:40 +02:00
|
|
|
result = ascii(u.text)
|
2019-12-16 12:01:38 +01:00
|
|
|
|
2025-03-20 12:57:48 +00:00
|
|
|
print(
|
|
|
|
|
" buffer={}, charset={}: {}".format(
|
|
|
|
|
ascii(tc.buffer),
|
|
|
|
|
ascii(tc.charset),
|
|
|
|
|
result,
|
|
|
|
|
)
|
|
|
|
|
)
|
2019-12-16 12:01:38 +01:00
|
|
|
|
2025-03-20 12:57:48 +00:00
|
|
|
print("main.py: Done.")
|