|
3 | 3 | import contextlib
|
4 | 4 | import importlib.util
|
5 | 5 | import inspect
|
| 6 | +import io |
6 | 7 | import pydoc
|
7 | 8 | import py_compile
|
8 | 9 | import keyword
|
@@ -841,6 +842,82 @@ def test_synopsis(self):
|
841 | 842 | synopsis = pydoc.synopsis(TESTFN, {})
|
842 | 843 | self.assertEqual(synopsis, 'line 1: h\xe9')
|
843 | 844 |
|
| 845 | + def test_source_synopsis(self): |
| 846 | + def check(source, expected, encoding=None): |
| 847 | + if isinstance(source, str): |
| 848 | + source_file = StringIO(source) |
| 849 | + else: |
| 850 | + source_file = io.TextIOWrapper(io.BytesIO(source), encoding=encoding) |
| 851 | + with source_file: |
| 852 | + result = pydoc.source_synopsis(source_file) |
| 853 | + self.assertEqual(result, expected) |
| 854 | + |
| 855 | + check('"""Single line docstring."""', |
| 856 | + 'Single line docstring.') |
| 857 | + check('"""First line of docstring.\nSecond line.\nThird line."""', |
| 858 | + 'First line of docstring.') |
| 859 | + check('"""First line of docstring.\\nSecond line.\\nThird line."""', |
| 860 | + 'First line of docstring.') |
| 861 | + check('""" Whitespace around docstring. """', |
| 862 | + 'Whitespace around docstring.') |
| 863 | + check('import sys\n"""No docstring"""', |
| 864 | + None) |
| 865 | + check(' \n"""Docstring after empty line."""', |
| 866 | + 'Docstring after empty line.') |
| 867 | + check('# Comment\n"""Docstring after comment."""', |
| 868 | + 'Docstring after comment.') |
| 869 | + check(' # Indented comment\n"""Docstring after comment."""', |
| 870 | + 'Docstring after comment.') |
| 871 | + check('""""""', # Empty docstring |
| 872 | + '') |
| 873 | + check('', # Empty file |
| 874 | + None) |
| 875 | + check('"""Embedded\0null byte"""', |
| 876 | + None) |
| 877 | + check('"""Embedded null byte"""\0', |
| 878 | + None) |
| 879 | + check('"""Café and résumé."""', |
| 880 | + 'Café and résumé.') |
| 881 | + check("'''Triple single quotes'''", |
| 882 | + 'Triple single quotes') |
| 883 | + check('"Single double quotes"', |
| 884 | + 'Single double quotes') |
| 885 | + check("'Single single quotes'", |
| 886 | + 'Single single quotes') |
| 887 | + check('"""split\\\nline"""', |
| 888 | + 'splitline') |
| 889 | + check('"""Unrecognized escape \\sequence"""', |
| 890 | + 'Unrecognized escape \\sequence') |
| 891 | + check('"""Invalid escape seq\\uence"""', |
| 892 | + None) |
| 893 | + check('r"""Raw \\stri\\ng"""', |
| 894 | + 'Raw \\stri\\ng') |
| 895 | + check('b"""Bytes literal"""', |
| 896 | + None) |
| 897 | + check('f"""f-string"""', |
| 898 | + None) |
| 899 | + check('"""Concatenated""" \\\n"string" \'literals\'', |
| 900 | + 'Concatenatedstringliterals') |
| 901 | + check('"""String""" + """expression"""', |
| 902 | + None) |
| 903 | + check('("""In parentheses""")', |
| 904 | + 'In parentheses') |
| 905 | + check('("""Multiple lines """\n"""in parentheses""")', |
| 906 | + 'Multiple lines in parentheses') |
| 907 | + check('()', # tuple |
| 908 | + None) |
| 909 | + check(b'# coding: iso-8859-15\n"""\xa4uro sign"""', |
| 910 | + '€uro sign', encoding='iso-8859-15') |
| 911 | + check(b'"""\xa4"""', # Decoding error |
| 912 | + None, encoding='utf-8') |
| 913 | + |
| 914 | + with tempfile.NamedTemporaryFile(mode='w+', encoding='utf-8') as temp_file: |
| 915 | + temp_file.write('"""Real file test."""\n') |
| 916 | + temp_file.flush() |
| 917 | + temp_file.seek(0) |
| 918 | + result = pydoc.source_synopsis(temp_file) |
| 919 | + self.assertEqual(result, "Real file test.") |
| 920 | + |
844 | 921 | @requires_docstrings
|
845 | 922 | def test_synopsis_sourceless(self):
|
846 | 923 | os = import_helper.import_fresh_module('os')
|
|
0 commit comments