2019-12-16 12:01:38 +01:00
|
|
|
from collections import namedtuple
|
|
|
|
|
|
|
|
|
|
import libfoolang
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
print('main.py: Running...')
|
|
|
|
|
|
|
|
|
|
ctx = libfoolang.AnalysisContext('iso-8859-1')
|
|
|
|
|
u = ctx.get_from_buffer('foo.txt', b'')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_from_buffer(buffer, charset):
|
|
|
|
|
ctx.get_from_buffer('foo.txt', buffer, charset)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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).
|
|
|
|
|
|
|
|
|
|
Testcase = namedtuple('Testcase', 'buffer charset')
|
|
|
|
|
|
|
|
|
|
testcases = [
|
|
|
|
|
Testcase(u'example # H\xe9llo', None),
|
|
|
|
|
Testcase(u'example # H\xeallo', ''),
|
|
|
|
|
Testcase(u'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'),
|
|
|
|
|
|
|
|
|
|
# Check that successfully parsing a unit with one encoding (UTF-8) has no
|
|
|
|
|
# influence on the default encoding used later.
|
|
|
|
|
Testcase(b'example # H\xc3\xa9llo', 'utf-8'),
|
|
|
|
|
Testcase(b'example # H\xc3\xa9llo', None),
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for method in (get_from_buffer, reparse):
|
|
|
|
|
print('== {} =='.format(method.__name__))
|
|
|
|
|
print('')
|
|
|
|
|
for tc in testcases:
|
|
|
|
|
try:
|
|
|
|
|
method(tc.buffer, tc.charset)
|
|
|
|
|
except Exception as exc:
|
|
|
|
|
result = '{}: {}'.format(type(exc).__name__, exc)
|
|
|
|
|
else:
|
|
|
|
|
if u.diagnostics:
|
|
|
|
|
result = '\n'.join(['diagnostics:'] +
|
|
|
|
|
[' {}'.format(d)
|
|
|
|
|
for d in u.diagnostics])
|
|
|
|
|
else:
|
2021-09-27 10:46:40 +02:00
|
|
|
result = ascii(u.text)
|
2019-12-16 12:01:38 +01:00
|
|
|
|
|
|
|
|
print(' buffer={}, charset={}: {}'.format(
|
2021-09-27 10:46:40 +02:00
|
|
|
ascii(tc.buffer), ascii(tc.charset), result,
|
2019-12-16 12:01:38 +01:00
|
|
|
))
|
|
|
|
|
|
|
|
|
|
print('main.py: Done.')
|