Mercurial > cpython-lru_cache-weakref
comparison Python/bltinmodule.c @ 42552:ac192f89c109
Kill execfile(), use exec() instead
| author | Neal Norwitz <nnorwitz@gmail.com> |
|---|---|
| date | Sun, 12 Aug 2007 00:43:29 +0000 |
| parents | 3772103200ec |
| children | 4d2aa4bfb2d6 |
comparison
equal
deleted
inserted
replaced
| 42551:b6625d2b378a | 42552:ac192f89c109 |
|---|---|
| 635 PyDoc_STRVAR(exec_doc, | 635 PyDoc_STRVAR(exec_doc, |
| 636 "exec(object[, globals[, locals]])\n\ | 636 "exec(object[, globals[, locals]])\n\ |
| 637 \n\ | 637 \n\ |
| 638 Read and execute code from a object, which can be a string, a code\n\ | 638 Read and execute code from a object, which can be a string, a code\n\ |
| 639 object or a file object.\n\ | 639 object or a file object.\n\ |
| 640 The globals and locals are dictionaries, defaulting to the current\n\ | |
| 641 globals and locals. If only globals is given, locals defaults to it."); | |
| 642 | |
| 643 | |
| 644 static PyObject * | |
| 645 builtin_execfile(PyObject *self, PyObject *args) | |
| 646 { | |
| 647 char *filename; | |
| 648 PyObject *globals = Py_None, *locals = Py_None; | |
| 649 PyObject *res; | |
| 650 FILE* fp = NULL; | |
| 651 PyCompilerFlags cf; | |
| 652 int exists; | |
| 653 | |
| 654 if (!PyArg_ParseTuple(args, "s|O!O:execfile", | |
| 655 &filename, | |
| 656 &PyDict_Type, &globals, | |
| 657 &locals)) | |
| 658 return NULL; | |
| 659 if (locals != Py_None && !PyMapping_Check(locals)) { | |
| 660 PyErr_SetString(PyExc_TypeError, "locals must be a mapping"); | |
| 661 return NULL; | |
| 662 } | |
| 663 if (globals == Py_None) { | |
| 664 globals = PyEval_GetGlobals(); | |
| 665 if (locals == Py_None) | |
| 666 locals = PyEval_GetLocals(); | |
| 667 } | |
| 668 else if (locals == Py_None) | |
| 669 locals = globals; | |
| 670 if (PyDict_GetItemString(globals, "__builtins__") == NULL) { | |
| 671 if (PyDict_SetItemString(globals, "__builtins__", | |
| 672 PyEval_GetBuiltins()) != 0) | |
| 673 return NULL; | |
| 674 } | |
| 675 | |
| 676 exists = 0; | |
| 677 /* Test for existence or directory. */ | |
| 678 #if defined(PLAN9) | |
| 679 { | |
| 680 Dir *d; | |
| 681 | |
| 682 if ((d = dirstat(filename))!=nil) { | |
| 683 if(d->mode & DMDIR) | |
| 684 werrstr("is a directory"); | |
| 685 else | |
| 686 exists = 1; | |
| 687 free(d); | |
| 688 } | |
| 689 } | |
| 690 #elif defined(RISCOS) | |
| 691 if (object_exists(filename)) { | |
| 692 if (isdir(filename)) | |
| 693 errno = EISDIR; | |
| 694 else | |
| 695 exists = 1; | |
| 696 } | |
| 697 #else /* standard Posix */ | |
| 698 { | |
| 699 struct stat s; | |
| 700 if (stat(filename, &s) == 0) { | |
| 701 if (S_ISDIR(s.st_mode)) | |
| 702 # if defined(PYOS_OS2) && defined(PYCC_VACPP) | |
| 703 errno = EOS2ERR; | |
| 704 # else | |
| 705 errno = EISDIR; | |
| 706 # endif | |
| 707 else | |
| 708 exists = 1; | |
| 709 } | |
| 710 } | |
| 711 #endif | |
| 712 | |
| 713 if (exists) { | |
| 714 Py_BEGIN_ALLOW_THREADS | |
| 715 fp = fopen(filename, "r" PY_STDIOTEXTMODE); | |
| 716 Py_END_ALLOW_THREADS | |
| 717 | |
| 718 if (fp == NULL) { | |
| 719 exists = 0; | |
| 720 } | |
| 721 } | |
| 722 | |
| 723 if (!exists) { | |
| 724 PyErr_SetFromErrnoWithFilename(PyExc_IOError, filename); | |
| 725 return NULL; | |
| 726 } | |
| 727 cf.cf_flags = 0; | |
| 728 if (PyEval_MergeCompilerFlags(&cf)) | |
| 729 res = PyRun_FileExFlags(fp, filename, Py_file_input, globals, | |
| 730 locals, 1, &cf); | |
| 731 else | |
| 732 res = PyRun_FileEx(fp, filename, Py_file_input, globals, | |
| 733 locals, 1); | |
| 734 return res; | |
| 735 } | |
| 736 | |
| 737 PyDoc_STRVAR(execfile_doc, | |
| 738 "execfile(filename[, globals[, locals]])\n\ | |
| 739 \n\ | |
| 740 Read and execute a Python script from a file.\n\ | |
| 741 The globals and locals are dictionaries, defaulting to the current\n\ | 640 The globals and locals are dictionaries, defaulting to the current\n\ |
| 742 globals and locals. If only globals is given, locals defaults to it."); | 641 globals and locals. If only globals is given, locals defaults to it."); |
| 743 | 642 |
| 744 | 643 |
| 745 static PyObject * | 644 static PyObject * |
| 1735 {"delattr", builtin_delattr, METH_VARARGS, delattr_doc}, | 1634 {"delattr", builtin_delattr, METH_VARARGS, delattr_doc}, |
| 1736 {"dir", builtin_dir, METH_VARARGS, dir_doc}, | 1635 {"dir", builtin_dir, METH_VARARGS, dir_doc}, |
| 1737 {"divmod", builtin_divmod, METH_VARARGS, divmod_doc}, | 1636 {"divmod", builtin_divmod, METH_VARARGS, divmod_doc}, |
| 1738 {"eval", builtin_eval, METH_VARARGS, eval_doc}, | 1637 {"eval", builtin_eval, METH_VARARGS, eval_doc}, |
| 1739 {"exec", builtin_exec, METH_VARARGS, exec_doc}, | 1638 {"exec", builtin_exec, METH_VARARGS, exec_doc}, |
| 1740 {"execfile", builtin_execfile, METH_VARARGS, execfile_doc}, | |
| 1741 {"filter", builtin_filter, METH_VARARGS, filter_doc}, | 1639 {"filter", builtin_filter, METH_VARARGS, filter_doc}, |
| 1742 {"getattr", builtin_getattr, METH_VARARGS, getattr_doc}, | 1640 {"getattr", builtin_getattr, METH_VARARGS, getattr_doc}, |
| 1743 {"globals", (PyCFunction)builtin_globals, METH_NOARGS, globals_doc}, | 1641 {"globals", (PyCFunction)builtin_globals, METH_NOARGS, globals_doc}, |
| 1744 {"hasattr", builtin_hasattr, METH_VARARGS, hasattr_doc}, | 1642 {"hasattr", builtin_hasattr, METH_VARARGS, hasattr_doc}, |
| 1745 {"hash", builtin_hash, METH_O, hash_doc}, | 1643 {"hash", builtin_hash, METH_O, hash_doc}, |
