@@ -328,6 +328,8 @@ check_bom(int get_char(struct tok_state *),
328328 unget_char (ch , tok );
329329 return 1 ;
330330 }
331+ if (tok -> encoding != NULL )
332+ PyMem_DEL (tok -> encoding );
331333 tok -> encoding = new_string ("utf-8" , 5 ); /* resulting is in utf-8 */
332334 return 1 ;
333335 NON_BOM :
@@ -581,14 +583,14 @@ decode_str(const char *str, struct tok_state *tok)
581583 tok -> enc = NULL ;
582584 tok -> str = str ;
583585 if (!check_bom (buf_getc , buf_ungetc , buf_setreadl , tok ))
584- return NULL ;
586+ return error_ret ( tok ) ;
585587 str = tok -> str ; /* string after BOM if any */
586588 assert (str );
587589#ifdef Py_USING_UNICODE
588590 if (tok -> enc != NULL ) {
589591 utf8 = translate_into_utf8 (str , tok -> enc );
590592 if (utf8 == NULL )
591- return NULL ;
593+ return error_ret ( tok ) ;
592594 str = PyString_AsString (utf8 );
593595 }
594596#endif
@@ -601,15 +603,15 @@ decode_str(const char *str, struct tok_state *tok)
601603 }
602604 tok -> enc = NULL ;
603605 if (!check_coding_spec (str , s - str , tok , buf_setreadl ))
604- return NULL ;
606+ return error_ret ( tok ) ;
605607#ifdef Py_USING_UNICODE
606608 if (tok -> enc != NULL ) {
607609 assert (utf8 == NULL );
608610 utf8 = translate_into_utf8 (str , tok -> enc );
609611 if (utf8 == NULL ) {
610612 PyErr_Format (PyExc_SyntaxError ,
611613 "unknown encoding: %s" , tok -> enc );
612- return NULL ;
614+ return error_ret ( tok ) ;
613615 }
614616 str = PyString_AsString (utf8 );
615617 }
@@ -630,8 +632,11 @@ PyTokenizer_FromString(const char *str)
630632 if (tok == NULL )
631633 return NULL ;
632634 str = (char * )decode_str (str , tok );
633- if (str == NULL )
635+ if (str == NULL ) {
636+ PyTokenizer_Free (tok );
634637 return NULL ;
638+ }
639+
635640 /* XXX: constify members. */
636641 tok -> buf = tok -> cur = tok -> end = tok -> inp = (char * )str ;
637642 return tok ;
@@ -647,7 +652,7 @@ PyTokenizer_FromFile(FILE *fp, char *ps1, char *ps2)
647652 if (tok == NULL )
648653 return NULL ;
649654 if ((tok -> buf = PyMem_NEW (char , BUFSIZ )) == NULL ) {
650- PyMem_DEL (tok );
655+ PyTokenizer_Free (tok );
651656 return NULL ;
652657 }
653658 tok -> cur = tok -> inp = tok -> buf ;
0 commit comments