@@ -56,6 +56,9 @@ tok_new(void)
5656 if (tok == NULL )
5757 return NULL ;
5858 tok -> buf = tok -> cur = tok -> inp = NULL ;
59+ tok -> fp_interactive = 0 ;
60+ tok -> interactive_src_start = NULL ;
61+ tok -> interactive_src_end = NULL ;
5962 tok -> start = NULL ;
6063 tok -> end = NULL ;
6164 tok -> done = E_OK ;
@@ -80,8 +83,6 @@ tok_new(void)
8083 tok -> decoding_readline = NULL ;
8184 tok -> decoding_buffer = NULL ;
8285 tok -> type_comments = 0 ;
83- tok -> stdin_content = NULL ;
84-
8586 tok -> async_hacks = 0 ;
8687 tok -> async_def = 0 ;
8788 tok -> async_def_indent = 0 ;
@@ -323,6 +324,35 @@ check_bom(int get_char(struct tok_state *),
323324 return 1 ;
324325}
325326
327+ static int tok_concatenate_interactive_new_line (struct tok_state * tok , char * line ) {
328+ assert (tok -> fp_interactive );
329+
330+ if (!line ) {
331+ return 0 ;
332+ }
333+
334+ Py_ssize_t current_size = tok -> interactive_src_end - tok -> interactive_src_start ;
335+ Py_ssize_t line_size = strlen (line );
336+ char * new_str = tok -> interactive_src_start ;
337+
338+ new_str = PyMem_Realloc (new_str , current_size + line_size + 1 );
339+ if (!new_str ) {
340+ if (tok -> interactive_src_start ) {
341+ PyMem_Free (tok -> interactive_src_start );
342+ }
343+ tok -> interactive_src_start = NULL ;
344+ tok -> interactive_src_end = NULL ;
345+ tok -> done = E_NOMEM ;
346+ return -1 ;
347+ }
348+ strcpy (new_str + current_size , line );
349+
350+ tok -> interactive_src_start = new_str ;
351+ tok -> interactive_src_end = new_str + current_size + line_size ;
352+ return 0 ;
353+ }
354+
355+
326356/* Read a line of text from TOK into S, using the stream in TOK.
327357 Return NULL on failure, else S.
328358
@@ -552,6 +582,12 @@ decoding_fgets(char *s, int size, struct tok_state *tok)
552582 badchar , tok -> filename , tok -> lineno + 1 );
553583 return error_ret (tok );
554584 }
585+
586+ if (tok -> fp_interactive &&
587+ tok_concatenate_interactive_new_line (tok , line ) == -1 ) {
588+ return NULL ;
589+ }
590+
555591 return line ;
556592}
557593
@@ -807,17 +843,21 @@ PyTokenizer_FromFile(FILE *fp, const char* enc,
807843void
808844PyTokenizer_Free (struct tok_state * tok )
809845{
810- if (tok -> encoding != NULL )
846+ if (tok -> encoding != NULL ) {
811847 PyMem_Free (tok -> encoding );
848+ }
812849 Py_XDECREF (tok -> decoding_readline );
813850 Py_XDECREF (tok -> decoding_buffer );
814851 Py_XDECREF (tok -> filename );
815- if (tok -> fp != NULL && tok -> buf != NULL )
852+ if (tok -> fp != NULL && tok -> buf != NULL ) {
816853 PyMem_Free (tok -> buf );
817- if (tok -> input )
854+ }
855+ if (tok -> input ) {
818856 PyMem_Free (tok -> input );
819- if (tok -> stdin_content )
820- PyMem_Free (tok -> stdin_content );
857+ }
858+ if (tok -> interactive_src_start != NULL ) {
859+ PyMem_Free (tok -> interactive_src_start );
860+ }
821861 PyMem_Free (tok );
822862}
823863
@@ -858,24 +898,6 @@ tok_nextc(struct tok_state *tok)
858898 if (translated == NULL )
859899 return EOF ;
860900 newtok = translated ;
861- if (tok -> stdin_content == NULL ) {
862- tok -> stdin_content = PyMem_Malloc (strlen (translated ) + 1 );
863- if (tok -> stdin_content == NULL ) {
864- tok -> done = E_NOMEM ;
865- return EOF ;
866- }
867- sprintf (tok -> stdin_content , "%s" , translated );
868- }
869- else {
870- char * new_str = PyMem_Malloc (strlen (tok -> stdin_content ) + strlen (translated ) + 1 );
871- if (new_str == NULL ) {
872- tok -> done = E_NOMEM ;
873- return EOF ;
874- }
875- sprintf (new_str , "%s%s" , tok -> stdin_content , translated );
876- PyMem_Free (tok -> stdin_content );
877- tok -> stdin_content = new_str ;
878- }
879901 }
880902 if (tok -> encoding && newtok && * newtok ) {
881903 /* Recode to UTF-8 */
@@ -898,6 +920,10 @@ tok_nextc(struct tok_state *tok)
898920 strcpy (newtok , buf );
899921 Py_DECREF (u );
900922 }
923+ if (tok -> fp_interactive &&
924+ tok_concatenate_interactive_new_line (tok , newtok ) == -1 ) {
925+ return EOF ;
926+ }
901927 if (tok -> nextprompt != NULL )
902928 tok -> prompt = tok -> nextprompt ;
903929 if (newtok == NULL )
@@ -958,7 +984,7 @@ tok_nextc(struct tok_state *tok)
958984 }
959985 if (decoding_fgets (tok -> buf , (int )(tok -> end - tok -> buf ),
960986 tok ) == NULL ) {
961- if (!tok -> decoding_erred )
987+ if (!tok -> decoding_erred && !( tok -> done == E_NOMEM ) )
962988 tok -> done = E_EOF ;
963989 done = 1 ;
964990 }
0 commit comments