comparison Python/pythonrun.c @ 14228:20377f15b96a legacy-trunk

Add three new APIs: PyRun_AnyFileEx(), PyRun_SimpleFileEx(), PyRun_FileEx(). These are the same as their non-Ex counterparts but have an extra argument, a flag telling them to close the file when done. Then this is used by Py_Main() and execfile() to close the file after it is parsed but before it is executed. Adding APIs seems strange given the feature freeze but it's the only way I see to close the bug report without incompatible changes. [ Bug #110616 ] source file stays open after parsing is done (PR#209)
author Guido van Rossum <guido@python.org>
date Sun, 27 Aug 2000 19:21:52 +0000
parents 9f9f3280dc68
children 396b4ab3b9c2
comparison
equal deleted inserted replaced
14227:9f9f3280dc68 14228:20377f15b96a
448 /* Parse input from a file and execute it */ 448 /* Parse input from a file and execute it */
449 449
450 int 450 int
451 PyRun_AnyFile(FILE *fp, char *filename) 451 PyRun_AnyFile(FILE *fp, char *filename)
452 { 452 {
453 return PyRun_AnyFileEx(fp, filename, 0);
454 }
455
456 int
457 PyRun_AnyFileEx(FILE *fp, char *filename, int closeit)
458 {
453 if (filename == NULL) 459 if (filename == NULL)
454 filename = "???"; 460 filename = "???";
455 if (Py_FdIsInteractive(fp, filename)) 461 if (Py_FdIsInteractive(fp, filename)) {
456 return PyRun_InteractiveLoop(fp, filename); 462 int err = PyRun_InteractiveLoop(fp, filename);
463 if (closeit)
464 fclose(fp);
465 return err;
466 }
457 else 467 else
458 return PyRun_SimpleFile(fp, filename); 468 return PyRun_SimpleFileEx(fp, filename, closeit);
459 } 469 }
460 470
461 int 471 int
462 PyRun_InteractiveLoop(FILE *fp, char *filename) 472 PyRun_InteractiveLoop(FILE *fp, char *filename)
463 { 473 {
540 } 550 }
541 551
542 int 552 int
543 PyRun_SimpleFile(FILE *fp, char *filename) 553 PyRun_SimpleFile(FILE *fp, char *filename)
544 { 554 {
555 return PyRun_SimpleFileEx(fp, filename, 0);
556 }
557
558 int
559 PyRun_SimpleFileEx(FILE *fp, char *filename, int closeit)
560 {
545 PyObject *m, *d, *v; 561 PyObject *m, *d, *v;
546 char *ext; 562 char *ext;
547 563
548 m = PyImport_AddModule("__main__"); 564 m = PyImport_AddModule("__main__");
549 if (m == NULL) 565 if (m == NULL)
556 || PyMac_getfiletype(filename) == 'PYC ' 572 || PyMac_getfiletype(filename) == 'PYC '
557 || PyMac_getfiletype(filename) == 'APPL' 573 || PyMac_getfiletype(filename) == 'APPL'
558 #endif /* macintosh */ 574 #endif /* macintosh */
559 ) { 575 ) {
560 /* Try to run a pyc file. First, re-open in binary */ 576 /* Try to run a pyc file. First, re-open in binary */
561 /* Don't close, done in main: fclose(fp); */ 577 if (closeit)
578 fclose(fp);
562 if( (fp = fopen(filename, "rb")) == NULL ) { 579 if( (fp = fopen(filename, "rb")) == NULL ) {
563 fprintf(stderr, "python: Can't reopen .pyc file\n"); 580 fprintf(stderr, "python: Can't reopen .pyc file\n");
564 return -1; 581 return -1;
565 } 582 }
566 /* Turn on optimization if a .pyo file is given */ 583 /* Turn on optimization if a .pyo file is given */
567 if (strcmp(ext, ".pyo") == 0) 584 if (strcmp(ext, ".pyo") == 0)
568 Py_OptimizeFlag = 1; 585 Py_OptimizeFlag = 1;
569 v = run_pyc_file(fp, filename, d, d); 586 v = run_pyc_file(fp, filename, d, d);
570 } else { 587 } else {
571 v = PyRun_File(fp, filename, Py_file_input, d, d); 588 v = PyRun_FileEx(fp, filename, Py_file_input, d, d, closeit);
572 } 589 }
573 if (v == NULL) { 590 if (v == NULL) {
574 PyErr_Print(); 591 PyErr_Print();
575 return -1; 592 return -1;
576 } 593 }
843 860
844 PyObject * 861 PyObject *
845 PyRun_File(FILE *fp, char *filename, int start, PyObject *globals, 862 PyRun_File(FILE *fp, char *filename, int start, PyObject *globals,
846 PyObject *locals) 863 PyObject *locals)
847 { 864 {
848 return run_err_node(PyParser_SimpleParseFile(fp, filename, start), 865 PyRun_FileEx(fp, filename, start, globals, locals, 0);
849 filename, globals, locals); 866 }
867
868 PyObject *
869 PyRun_FileEx(FILE *fp, char *filename, int start, PyObject *globals,
870 PyObject *locals, int closeit)
871 {
872 node *n = PyParser_SimpleParseFile(fp, filename, start);
873 if (closeit)
874 fclose(fp);
875 return run_err_node(n, filename, globals, locals);
850 } 876 }
851 877
852 static PyObject * 878 static PyObject *
853 run_err_node(node *n, char *filename, PyObject *globals, PyObject *locals) 879 run_err_node(node *n, char *filename, PyObject *globals, PyObject *locals)
854 { 880 {