@@ -38,6 +38,12 @@ static PyObject *
3838test_tokenizer_source (PyObject * Py_UNUSED (module ),
3939 PyObject * Py_UNUSED (args ))
4040{
41+ const char multiple_lines [] = "a\nb\n" ;
42+ const char first_line [] = "alpha\n" ;
43+ const char second_line [] = "\xce\xb2\n" ;
44+ const char expected [] = "alpha\n\xce\xb2\n" ;
45+ const char tail [] = "tail" ;
46+ const char terminated_line [] = "x\n" ;
4147 _PyTok_SourceText source ;
4248 _PyTok_SourceInit (& source );
4349
@@ -46,23 +52,19 @@ test_tokenizer_source(PyObject *Py_UNUSED(module),
4652 }
4753
4854 if (check_system_error (
49- _PyTok_SourceAppendLine (& source , "" , 0 , 0 ) < 0 ,
55+ _PyTok_SourceAppendLine (& source , "" , 0 ) < 0 ,
5056 "accepted empty source line" ) < 0 ||
5157 check_system_error (
52- _PyTok_SourceAppendLine (& source , "a\nb\n" , 4 , 0 ) < 0 ,
58+ _PyTok_SourceAppendLine (
59+ & source , multiple_lines , sizeof (multiple_lines ) - 1 ) < 0 ,
5360 "accepted multiple source lines" ) < 0 ||
54- check_system_error (
55- _PyTok_SourceAppendLine (& source , "a" , 1 , 1 ) < 0 ,
56- "accepted missing implicit newline" ) < 0 ||
5761 check (_PyTok_SourceAppendLine (
58- & source , "alpha\n" , 6 , 0 ) == 0 ,
62+ & source , first_line , sizeof ( first_line ) - 1 ) == 0 ,
5963 "wrong first source offset" ) < 0 ||
6064 check (_PyTok_SourceAppendLine (
61- & source , "\xce\xb2\n" , 3 , 1 ) == 6 ,
62- "wrong second source offset" ) < 0 ||
63- check (!_PyTok_SourceLineIsImplicit (& source , 1 ) &&
64- _PyTok_SourceLineIsImplicit (& source , 2 ),
65- "wrong implicit newline flags" ) < 0 ) {
65+ & source , second_line , sizeof (second_line ) - 1 ) ==
66+ (Py_ssize_t )sizeof (first_line ) - 1 ,
67+ "wrong second source offset" ) < 0 ) {
6668 goto error ;
6769 }
6870
@@ -74,16 +76,17 @@ test_tokenizer_source(PyObject *Py_UNUSED(module),
7476 goto error ;
7577 }
7678
77- if (check (source .len == 9 &&
78- memcmp (source .bytes , "alpha\n\xce\xb2\n" , 10 ) == 0 ,
79+ if (check (source .len == ( Py_ssize_t ) sizeof ( expected ) - 1 &&
80+ memcmp (source .bytes , expected , sizeof ( expected ) ) == 0 ,
7981 "wrong source contents" ) < 0 ) {
8082 goto error ;
8183 }
8284
8385 _PyTok_SourceClear (& source );
84- if (_PyTok_SourceAppendLine (& source , " tail" , 4 , 0 ) < 0 ||
86+ if (_PyTok_SourceAppendLine (& source , tail , sizeof ( tail ) - 1 ) < 0 ||
8587 check_system_error (
86- _PyTok_SourceAppendLine (& source , "x\n" , 2 , 0 ) < 0 ,
88+ _PyTok_SourceAppendLine (
89+ & source , terminated_line , sizeof (terminated_line ) - 1 ) < 0 ,
8790 "appended after unterminated source line" ) < 0 ) {
8891 goto error ;
8992 }
@@ -104,27 +107,35 @@ static PyObject *
104107test_tokenizer_source_discard (PyObject * Py_UNUSED (module ),
105108 PyObject * Py_UNUSED (args ))
106109{
110+ enum { LINE_COUNT = 2 };
111+ const char first_line [] = "x\n" ;
112+ const char second_line [] = "y\n" ;
113+ const char tail [] = "tail" ;
114+ const char final_line [] = "z\n" ;
115+ const _PyTok_Off first_batch_len = LINE_COUNT * (sizeof (first_line ) - 1 );
116+ const _PyTok_Off second_batch_len = LINE_COUNT * (sizeof (second_line ) - 1 );
117+ const _PyTok_Off discarded_len = first_batch_len + second_batch_len ;
107118 _PyTok_SourceText source ;
108119 _PyTok_SourceInit (& source );
109- for (int i = 0 ; i < 260 ; i ++ ) {
110- if (_PyTok_SourceAppendLine (& source , "x\n" , 2 , 1 ) < 0 ) {
120+ for (int i = 0 ; i < LINE_COUNT ; i ++ ) {
121+ if (_PyTok_SourceAppendLine (& source , first_line , sizeof ( first_line ) - 1 ) < 0 ) {
111122 goto error ;
112123 }
113124 }
114125 char * bytes = source .bytes ;
115126 _PyTok_Off capacity = source .cap ;
116127 _PyTok_SourceDiscard (& source );
117- if (check (source .base_offset == 520 && source .len == 0 &&
128+ if (check (source .base_offset == first_batch_len && source .len == 0 &&
118129 source .nlines == 0 && source .bytes == bytes &&
119130 source .cap == capacity && source .bytes [0 ] == '\0' ,
120131 "discard did not preserve source allocation" ) < 0 ) {
121132 goto error ;
122133 }
123- for (int i = 0 ; i < 260 ; i ++ ) {
124- if (check (_PyTok_SourceAppendLine (& source , "y\n" , 2 , 0 ) == 520 + 2 * i ,
125- "wrong source offset after discard" ) < 0 ||
126- check (! _PyTok_SourceLineIsImplicit ( & source , i + 1 ) ,
127- "discard preserved implicit newline flag " ) < 0 ) {
134+ for (int i = 0 ; i < LINE_COUNT ; i ++ ) {
135+ if (check (_PyTok_SourceAppendLine (
136+ & source , second_line , sizeof ( second_line ) - 1 ) ==
137+ first_batch_len + (( Py_ssize_t ) sizeof ( second_line ) - 1 ) * i ,
138+ "wrong source offset after discard " ) < 0 ) {
128139 goto error ;
129140 }
130141 }
@@ -133,18 +144,22 @@ test_tokenizer_source_discard(PyObject *Py_UNUSED(module),
133144 goto error ;
134145 }
135146 _PyTok_SourceDiscard (& source );
136- if (check (_PyTok_SourceAppendLine (& source , "tail" , 4 , 0 ) == 1040 ,
147+ if (check (_PyTok_SourceAppendLine (
148+ & source , tail , sizeof (tail ) - 1 ) == discarded_len ,
137149 "wrong source offset after repeated discard" ) < 0 ) {
138150 goto error ;
139151 }
140152 _PyTok_SourceDiscard (& source );
141- if (check (_PyTok_SourceAppendLine (& source , "z\n" , 2 , 0 ) == 1044 ,
153+ if (check (_PyTok_SourceAppendLine (
154+ & source , final_line , sizeof (final_line ) - 1 ) ==
155+ discarded_len + (Py_ssize_t )sizeof (tail ) - 1 ,
142156 "cannot append after discarding unterminated line" ) < 0 ) {
143157 goto error ;
144158 }
145159 _PyTok_SourceDiscard (& source );
146160 source .base_offset = PY_SSIZE_T_MAX - 1 ;
147- if (check (_PyTok_SourceAppendLine (& source , "z\n" , 2 , 0 ) < 0 &&
161+ if (check (_PyTok_SourceAppendLine (
162+ & source , final_line , sizeof (final_line ) - 1 ) < 0 &&
148163 PyErr_ExceptionMatches (PyExc_MemoryError ),
149164 "accepted overflowing logical source offset" ) < 0 ) {
150165 goto error ;
0 commit comments