comparison Python/errors.c @ 43524:2f30bfb7c738

Minor cleanup of the comment for PyErr_ProgramText() and a tweak to the code to guarantee the claim that it doesn't set an exception.
author Guido van Rossum <guido@python.org>
date Wed, 10 Oct 2007 18:53:36 +0000
parents 876b7ef4775d
children 704e6711e50d
comparison
equal deleted inserted replaced
43523:4c189c7144b7 43524:2f30bfb7c738
798 } 798 }
799 } 799 }
800 PyErr_Restore(exc, v, tb); 800 PyErr_Restore(exc, v, tb);
801 } 801 }
802 802
803 /* com_fetch_program_text will attempt to load the line of text that 803 /* Attempt to load the line of text that the exception refers to. If it
804 the exception refers to. If it fails, it will return NULL but will 804 fails, it will return NULL but will not set an exception.
805 not set an exception.
806 805
807 XXX The functionality of this function is quite similar to the 806 XXX The functionality of this function is quite similar to the
808 functionality in tb_displayline() in traceback.c. 807 functionality in tb_displayline() in traceback.c. */
809 */
810 808
811 PyObject * 809 PyObject *
812 PyErr_ProgramText(const char *filename, int lineno) 810 PyErr_ProgramText(const char *filename, int lineno)
813 { 811 {
814 FILE *fp; 812 FILE *fp;
822 return NULL; 820 return NULL;
823 for (i = 0; i < lineno; i++) { 821 for (i = 0; i < lineno; i++) {
824 char *pLastChar = &linebuf[sizeof(linebuf) - 2]; 822 char *pLastChar = &linebuf[sizeof(linebuf) - 2];
825 do { 823 do {
826 *pLastChar = '\0'; 824 *pLastChar = '\0';
827 if (Py_UniversalNewlineFgets(linebuf, sizeof linebuf, fp, NULL) == NULL) 825 if (Py_UniversalNewlineFgets(linebuf, sizeof linebuf,
826 fp, NULL) == NULL)
828 break; 827 break;
829 /* fgets read *something*; if it didn't get as 828 /* fgets read *something*; if it didn't get as
830 far as pLastChar, it must have found a newline 829 far as pLastChar, it must have found a newline
831 or hit the end of the file; if pLastChar is \n, 830 or hit the end of the file; if pLastChar is \n,
832 it obviously found a newline; else we haven't 831 it obviously found a newline; else we haven't
834 } while (*pLastChar != '\0' && *pLastChar != '\n'); 833 } while (*pLastChar != '\0' && *pLastChar != '\n');
835 } 834 }
836 fclose(fp); 835 fclose(fp);
837 if (i == lineno) { 836 if (i == lineno) {
838 char *p = linebuf; 837 char *p = linebuf;
838 PyObject *res;
839 while (*p == ' ' || *p == '\t' || *p == '\014') 839 while (*p == ' ' || *p == '\t' || *p == '\014')
840 p++; 840 p++;
841 return PyUnicode_FromString(p); 841 res = PyUnicode_FromString(p);
842 if (res == NULL)
843 PyErr_Clear();
844 return res;
842 } 845 }
843 return NULL; 846 return NULL;
844 } 847 }
845 848
846 #ifdef __cplusplus 849 #ifdef __cplusplus