comparison Python/pythonrun.c @ 5234:07ed52e200db legacy-trunk

Allow passing a .pyo file. Print correct name in fatal error from PyErr_Print.
author Guido van Rossum <guido@python.org>
date Wed, 02 Apr 1997 05:28:38 +0000
parents f5d707ce4223
children b7871ca930ad
comparison
equal deleted inserted replaced
5233:b749d2dd7582 5234:07ed52e200db
236 m = PyImport_AddModule("__main__"); 236 m = PyImport_AddModule("__main__");
237 if (m == NULL) 237 if (m == NULL)
238 return -1; 238 return -1;
239 d = PyModule_GetDict(m); 239 d = PyModule_GetDict(m);
240 ext = filename + strlen(filename) - 4; 240 ext = filename + strlen(filename) - 4;
241 if (strcmp(ext, ".pyc") == 0 || strcmp(ext, ".pyo") == 0
241 #ifdef macintosh 242 #ifdef macintosh
242 /* On a mac, we also assume a pyc file for types 'PYC ' and 'APPL' */ 243 /* On a mac, we also assume a pyc file for types 'PYC ' and 'APPL' */
243 if ( strcmp(ext, ".pyc") == 0 || getfiletype(filename) == 'PYC ' || 244 || getfiletype(filename) == 'PYC '
244 getfiletype(filename) == 'APPL' ) { 245 || getfiletype(filename) == 'APPL'
245 #else
246 if ( strcmp(ext, ".pyc") == 0 ) {
247 #endif /* macintosh */ 246 #endif /* macintosh */
247 ) {
248 /* Try to run a pyc file. First, re-open in binary */ 248 /* Try to run a pyc file. First, re-open in binary */
249 /* Don't close, done in main: fclose(fp); */ 249 /* Don't close, done in main: fclose(fp); */
250 if( (fp = fopen(filename, "rb")) == NULL ) { 250 if( (fp = fopen(filename, "rb")) == NULL ) {
251 fprintf(stderr, "python: Can't reopen .pyc file\n"); 251 fprintf(stderr, "python: Can't reopen .pyc file\n");
252 return -1; 252 return -1;
253 } 253 }
254 /* Turn on optimization if a .pyo file is given */
255 if (strcmp(ext, ".pyo") == 0)
256 Py_OptimizeFlag = 1;
254 v = run_pyc_file(fp, filename, d, d); 257 v = run_pyc_file(fp, filename, d, d);
255 } else { 258 } else {
256 v = PyRun_File(fp, filename, file_input, d, d); 259 v = PyRun_File(fp, filename, file_input, d, d);
257 } 260 }
258 if (v == NULL) { 261 if (v == NULL) {
289 PyObject *exception, *v, *tb, *f; 292 PyObject *exception, *v, *tb, *f;
290 PyErr_Fetch(&exception, &v, &tb); 293 PyErr_Fetch(&exception, &v, &tb);
291 Py_FlushLine(); 294 Py_FlushLine();
292 fflush(stdout); 295 fflush(stdout);
293 if (exception == NULL) 296 if (exception == NULL)
294 Py_FatalError("print_error called but no exception"); 297 Py_FatalError("PyErr_Print called but no exception");
295 if (exception == PyExc_SystemExit) { 298 if (exception == PyExc_SystemExit) {
296 if (v == NULL || v == Py_None) 299 if (v == NULL || v == Py_None)
297 Py_Exit(0); 300 Py_Exit(0);
298 if (PyInt_Check(v)) 301 if (PyInt_Check(v))
299 Py_Exit((int)PyInt_AsLong(v)); 302 Py_Exit((int)PyInt_AsLong(v));