python中sys模块有什么用_求助,关于sys模块文件在哪个文件中

#ifdef __cplusplus

}

#endif

static PyMethodDef sys_methods[] = {

/* Might as well keep this in alphabetic order */

{"callstats", (PyCFunction)PyEval_GetCallStats, METH_NOARGS,

callstats_doc},

{"_current_frames", sys_current_frames, METH_NOARGS,

current_frames_doc},

{"displayhook", sys_displayhook, METH_O, displayhook_doc},

{"exc_info", sys_exc_info, METH_NOARGS, exc_info_doc},

{"exc_clear", sys_exc_clear, METH_NOARGS, exc_clear_doc},

{"excepthook", sys_excepthook, METH_VARARGS, excepthook_doc},

{"exit", sys_exit, METH_VARARGS, exit_doc},

#ifdef Py_USING_UNICODE

{"getdefaultencoding", (PyCFunction)sys_getdefaultencoding,

METH_NOARGS, getdefaultencoding_doc},

#endif

#ifdef HAVE_DLOPEN

{"getdlopenflags", (PyCFunction)sys_getdlopenflags, METH_NOARGS,

getdlopenflags_doc},

#endif

#ifdef COUNT_ALLOCS

{"getcounts", (PyCFunction)sys_getcounts, METH_NOARGS},

#endif

#ifdef DYNAMIC_EXECUTION_PROFILE

{"getdxp", _Py_GetDXProfile, METH_VARARGS},

#endif

#ifdef Py_USING_UNICODE

{"getfilesystemencoding", (PyCFunction)sys_getfilesystemencoding,

METH_NOARGS, getfilesystemencoding_doc},

#endif

#ifdef Py_TRACE_REFS

{"getobjects", _Py_GetObjects, METH_VARARGS},

#endif

#ifdef Py_REF_DEBUG

{"gettotalrefcount", (PyCFunction)sys_gettotalrefcount, METH_NOARGS},

#endif

{"getrefcount", (PyCFunction)sys_getrefcount, METH_O, getrefcount_doc},

{"getrecursionlimit", (PyCFunction)sys_getrecursionlimit, METH_NOARGS,

getrecursionlimit_doc},

{"_getframe", sys_getframe, METH_VARARGS, getframe_doc},

#ifdef MS_WINDOWS

{"getwindowsversion", (PyCFunction)sys_getwindowsversion, METH_NOARGS,

getwindowsversion_doc},

#endif /* MS_WINDOWS */

#ifdef USE_MALLOPT

{"mdebug", sys_mdebug, METH_VARARGS},

#endif

#ifdef Py_USING_UNICODE

{"setdefaultencoding", sys_setdefaultencoding, METH_VARARGS,

setdefaultencoding_doc},

#endif

{"setcheckinterval", sys_setcheckinterval, METH_VARARGS,

setcheckinterval_doc},

{"getcheckinterval", sys_getcheckinterval, METH_NOARGS,

getcheckinterval_doc},

#ifdef HAVE_DLOPEN

{"setdlopenflags", sys_setdlopenflags, METH_VARARGS,

setdlopenflags_doc},

#endif

{"setprofile", sys_setprofile, METH_O, setprofile_doc},

{"setrecursionlimit", sys_setrecursionlimit, METH_VARARGS,

setrecursionlimit_doc},

#ifdef WITH_TSC

{"settscdump", sys_settscdump, METH_VARARGS, settscdump_doc},

#endif

{"settrace", sys_settrace, METH_O, settrace_doc},

{"call_tracing", sys_call_tracing, METH_VARARGS, call_tracing_doc},

{NULL, NULL} /* sentinel */

};

static PyObject *

list_builtin_module_names(void)

{

PyObject *list = PyList_New(0);

int i;

if (list == NULL)

return NULL;

for (i = 0; PyImport_Inittab[i].name != NULL; i++) {

PyObject *name = PyString_FromString(

PyImport_Inittab[i].name);

if (name == NULL)

break;

PyList_Append(list, name);

Py_DECREF(name);

}

if (PyList_Sort(list) != 0) {

Py_DECREF(list);

list = NULL;

}

if (list) {

PyObject *v = PyList_AsTuple(list);

Py_DECREF(list);

list = v;

}

return list;

}

static PyObject *warnoptions = NULL;

void

PySys_ResetWarnOptions(void)

{

if (warnoptions == NULL || !PyList_Check(warnoptions))

return;

PyList_SetSlice(warnoptions, 0, PyList_GET_SIZE(warnoptions), NULL);

}

void

PySys_AddWarnOption(char *s)

{

PyObject *str;

if (warnoptions == NULL || !PyList_Check(warnoptions)) {

Py_XDECREF(warnoptions);

warnoptions = PyList_New(0);

if (warnoptions == NULL)

return;

}

str = PyString_FromString(s);

if (str != NULL) {

PyList_Append(warnoptions, str);

Py_DECREF(str);

}

}

/* XXX This doc string is too long to be a single string literal in VC++ 5.0.

Two literals concatenated works just fine. If you have a K&R compiler

or other abomination that however *does* understand longer strings,

get rid of the !!! comment in the middle and the quotes that surround it. */

PyDoc_VAR(sys_doc) =

PyDoc_STR(

"This module provides access to some objects used or maintained by the\n\

interpreter and to functions that interact strongly with the interpreter.\n\

\n\

Dynamic objects:\n\

\n\

argv -- command line arguments; argv[0] is the script pathname if known\n\

path -- module search path; path[0] is the script directory, else ''\n\

modules -- dictionary of loaded modules\n\

\n\

displayhook -- called to show results in an interactive session\n\

excepthook -- called to handle any uncaught exception other than SystemExit\n\

To customize printing in an interactive session or to install a custom\n\

top-level exception handler, assign other functions to replace these.\n\

\n\

exitfunc -- if sys.exitfunc exists, this routine is called when Python exits\n\

Assigning to sys.exitfunc is deprecated; use the atexit module instead.\n\

\n\

stdin -- standard input file object; used by raw_input() and input()\n\

stdout -- standard output file object; used by the print statement\n\

stderr -- standard error object; used for error messages\n\

By assigning other file objects (or objects that behave like files)\n\

to these, it is possible to redirect all of the interpreter's I/O.\n\

\n\

last_type -- type of last uncaught exception\n\

last_value -- value of last uncaught exception\n\

last_traceback -- traceback of last uncaught exception\n\

These three are only available in an interactive session after a\n\

traceback has been printed.\n\

\n\

exc_type -- type of exception currently being handled\n\

exc_value -- value of exception currently being handled\n\

exc_traceback -- traceback of exception currently being handled\n\

The function exc_info() should be used instead of these three,\n\

because it is thread-safe.\n\

"

)

/* concatenating string here */

PyDoc_STR(

"\n\

Static objects:\n\

\n\

maxint -- the largest supported integer (the smallest is -maxint-1)\n\

maxunicode -- the largest supported character\n\

builtin_module_names -- tuple of module names built into this interpreter\n\

version -- the version of this interpreter as a string\n\

version_info -- version information as a tuple\n\

hexversion -- version information encoded as a single integer\n\

copyright -- copyright notice pertaining to this interpreter\n\

platform -- platform identifier\n\

executable -- pathname of this Python interpreter\n\

prefix -- prefix used to find the Python library\n\

exec_prefix -- prefix used to find the machine-specific Python library\n\

"

)

#ifdef MS_WINDOWS

/* concatenating string here */

PyDoc_STR(

"dllhandle -- [Windows only] integer handle of the Python DLL\n\

winver -- [Windows only] version number of the Python DLL\n\

"

)

#endif /* MS_WINDOWS */

PyDoc_STR(

"__stdin__ -- the original stdin; don't touch!\n\

__stdout__ -- the original stdout; don't touch!\n\

__stderr__ -- the original stderr; don't touch!\n\

__displayhook__ -- the original displayhook; don't touch!\n\

__excepthook__ -- the original excepthook; don't touch!\n\

\n\

Functions:\n\

\n\

displayhook() -- print an object to the screen, and save it in __builtin__._\n\

excepthook() -- print an exception and its traceback to sys.stderr\n\

exc_info() -- return thread-safe information about the current exception\n\

exc_clear() -- clear the exception state for the current thread\n\

exit() -- exit the interpreter by raising SystemExit\n\

getdlopenflags() -- returns flags to be used for dlopen() calls\n\

getrefcount() -- return the reference count for an object (plus one :-)\n\

getrecursionlimit() -- return the max recursion depth for the interpreter\n\

setcheckinterval() -- control how often the interpreter checks for events\n\

setdlopenflags() -- set the flags to be used for dlopen() calls\n\

setprofile() -- set the global profiling function\n\

setrecursionlimit() -- set the max recursion depth for the interpreter\n\

settrace() -- set the global debug tracing function\n\

"

)

/* end of sys_doc */ ;

static int

_check_and_flush (FILE *stream)

{

int prev_fail = ferror (stream);

return fflush (stream) || prev_fail ? EOF : 0;

}

/* Subversion branch and revision management */

static const char _patchlevel_revision[] = PY_PATCHLEVEL_REVISION;

static const char headurl[] = "$HeadURL$";

static int svn_initialized;

static char patchlevel_revision[50]; /* Just the number */

static char branch[50];

static char shortbranch[50];

static const char *svn_revision;

static void

svnversion_init(void)

{

const char *python, *br_start, *br_end, *br_end2, *svnversion;

Py_ssize_t len;

int istag;

if (svn_initialized)

return;

python = strstr(headurl, "/python/");

if (!python)

Py_FatalError("subversion keywords missing");

br_start = python + 8;

br_end = strchr(br_start, '/');

assert(br_end);

/* Works even for trunk,

as we are in trunk/Python/sysmodule.c */

br_end2 = strchr(br_end+1, '/');

istag = strncmp(br_start, "tags", 4) == 0;

if (strncmp(br_start, "trunk", 5) == 0) {

strcpy(branch, "trunk");

strcpy(shortbranch, "trunk");

}

else if (istag || strncmp(br_start, "branches", 8) == 0) {

len = br_end2 - br_start;

strncpy(branch, br_start, len);

branch[len] = '\0';

len = br_end2 - (br_end + 1);

strncpy(shortbranch, br_end + 1, len);

shortbranch[len] = '\0';

}

else {

Py_FatalError("bad HeadURL");

return;

}

svnversion = _Py_svnversion();

if (strcmp(svnversion, "exported") != 0)

svn_revision = svnversion;

else if (istag) {

len = strlen(_patchlevel_revision);

strncpy(patchlevel_revision, _patchlevel_revision + 11,

len - 13);

patchlevel_revision[len - 13] = '\0';

svn_revision = patchlevel_revision;

}

else

svn_revision = "";

svn_initialized = 1;

}

/* Return svnversion output if available.

Else return Revision of patchlevel.h if on branch.

Else return empty string */

const char*

Py_SubversionRevision()

{

svnversion_init();

return svn_revision;

}

const char*

Py_SubversionShortBranch()

{

svnversion_init();

return shortbranch;

}

PyObject *

_PySys_Init(void)

{

PyObject *m, *v, *sysdict;

PyObject *sysin, *sysout, *syserr;

char *s;

#ifdef MS_WINDOWS

char buf[128];

#endif

m = Py_InitModule3("sys", sys_methods, sys_doc);

if (m == NULL)

return NULL;

sysdict = PyModule_GetDict(m);

{

/* XXX: does this work on Win/Win64? (see posix_fstat) */

struct stat sb;

if (fstat(fileno(stdin), &sb) == 0 &&

S_ISDIR(sb.st_mode)) {

/* There's nothing more we can do. */

/* Py_FatalError() will core dump, so just exit. */

PySys_WriteStderr("Python error: is a directory, cannot continue\n");

exit(EXIT_FAILURE);

}

}

/* Closing the standard FILE* if sys.std* goes aways causes problems

* for embedded Python usages. Closing them when somebody explicitly

* invokes .close() might be possible, but the FAQ promises they get

* never closed. However, we still need to get write errors when

* writing fails (e.g. because stdout is redirected), so we flush the

* streams and check for errors before the file objects are deleted.

* On OS X, fflush()ing stdin causes an error, so we exempt stdin

* from that procedure.

*/

sysin = PyFile_FromFile(stdin, "", "r", NULL);

sysout = PyFile_FromFile(stdout, "", "w", _check_and_flush);

syserr = PyFile_FromFile(stderr, "", "w", _check_and_flush);

if (PyErr_Occurred())

return NULL;

#ifdef MS_WINDOWS

if(isatty(_fileno(stdin)) && PyFile_Check(sysin)) {

sprintf(buf, "cp%d", GetConsoleCP());

if (!PyFile_SetEncoding(sysin, buf))

return NULL;

}

if(isatty(_fileno(stdout)) && PyFile_Check(sysout)) {

sprintf(buf, "cp%d", GetConsoleOutputCP());

if (!PyFile_SetEncoding(sysout, buf))

return NULL;

}

if(isatty(_fileno(stderr)) && PyFile_Check(syserr)) {

sprintf(buf, "cp%d", GetConsoleOutputCP());

if (!PyFile_SetEncoding(syserr, buf))

return NULL;

}

#endif

PyDict_SetItemString(sysdict, "stdin", sysin);

PyDict_SetItemString(sysdict, "stdout", sysout);

PyDict_SetItemString(sysdict, "stderr", syserr);

/* Make backup copies for cleanup */

PyDict_SetItemString(sysdict, "__stdin__", sysin);

PyDict_SetItemString(sysdict, "__stdout__", sysout);

PyDict_SetItemString(sysdict, "__stderr__", syserr);

PyDict_SetItemString(sysdict, "__displayhook__",

PyDict_GetItemString(sysdict, "displayhook"));

PyDict_SetItemString(sysdict, "__excepthook__",

PyDict_GetItemString(sysdict, "excepthook"));

Py_XDECREF(sysin);

Py_XDECREF(sysout);

Py_XDECREF(syserr);

PyDict_SetItemString(sysdict, "version",

v = PyString_FromString(Py_GetVersion()));

Py_XDECREF(v);

PyDict_SetItemString(sysdict, "hexversion",

v = PyInt_FromLong(PY_VERSION_HEX));

Py_XDECREF(v);

svnversion_init();

v = Py_BuildValue("(ssz)", "CPython", branch, svn_revision);

PyDict_SetItemString(sysdict, "subversion", v);

Py_XDECREF(v);

/*

* These release level checks are mutually exclusive and cover

* the field, so don't get too fancy with the pre-processor!

*/

#if PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_ALPHA

s = "alpha";

#elif PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_BETA

s = "beta";

#elif PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_GAMMA

s = "candidate";

#elif PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_FINAL

s = "final";

#endif

#define SET_SYS_FROM_STRING(key, value) \

v = value; \

if (v != NULL) \

PyDict_SetItemString(sysdict, key, v); \

Py_XDECREF(v)

SET_SYS_FROM_STRING("version_info",

Py_BuildValue("iiisi", PY_MAJOR_VERSION,

PY_MINOR_VERSION,

PY_MICRO_VERSION, s,

PY_RELEASE_SERIAL));

SET_SYS_FROM_STRING("api_version",

PyInt_FromLong(PYTHON_API_VERSION));

SET_SYS_FROM_STRING("copyright",

PyString_FromString(Py_GetCopyright()));

SET_SYS_FROM_STRING("platform",

PyString_FromString(Py_GetPlatform()));

SET_SYS_FROM_STRING("executable",

PyString_FromString(Py_GetProgramFullPath()));

SET_SYS_FROM_STRING("prefix",

PyString_FromString(Py_GetPrefix()));

SET_SYS_FROM_STRING("exec_prefix",

PyString_FromString(Py_GetExecPrefix()));

SET_SYS_FROM_STRING("maxint",

PyInt_FromLong(PyInt_GetMax()));

#ifdef Py_USING_UNICODE

SET_SYS_FROM_STRING("maxunicode",

PyInt_FromLong(PyUnicode_GetMax()));

#endif

SET_SYS_FROM_STRING("builtin_module_names",

list_builtin_module_names());

{

/* Assumes that longs are at least 2 bytes long.

Should be safe! */

unsigned long number = 1;

char *value;

s = (char *) &number;

if (s[0] == 0)

value = "big";

else

value = "little";

SET_SYS_FROM_STRING("byteorder",

PyString_FromString(value));

}

#ifdef MS_COREDLL

SET_SYS_FROM_STRING("dllhandle",

PyLong_FromVoidPtr(PyWin_DLLhModule));

SET_SYS_FROM_STRING("winver",

PyString_FromString(PyWin_DLLVersionString));

#endif

#undef SET_SYS_FROM_STRING

if (warnoptions == NULL) {

warnoptions = PyList_New(0);

}

else {

Py_INCREF(warnoptions);

}

if (warnoptions != NULL) {

PyDict_SetItemString(sysdict, "warnoptions", warnoptions);

}

if (PyErr_Occurred())

return NULL;

return m;

}

static PyObject *

makepathobject(char *path, int delim)

{

int i, n;

char *p;

PyObject *v, *w;

n = 1;

p = path;

while ((p = strchr(p, delim)) != NULL) {

n++;

p++;

}

v = PyList_New(n);

if (v == NULL)

return NULL;

for (i = 0; ; i++) {

p = strchr(path, delim);

if (p == NULL)

p = strchr(path, '\0'); /* End of string */

w = PyString_FromStringAndSize(path, (Py_ssize_t) (p - path));

if (w == NULL) {

Py_DECREF(v);

return NULL;

}

PyList_SetItem(v, i, w);

if (*p == '\0')

break;

path = p+1;

}

return v;

}

void

PySys_SetPath(char *path)

{

PyObject *v;

if ((v = makepathobject(path, DELIM)) == NULL)

Py_FatalError("can't create sys.path");

if (PySys_SetObject("path", v) != 0)

Py_FatalError("can't assign sys.path");

Py_DECREF(v);

}

static PyObject *

makeargvobject(int argc, char **argv)

{

PyObject *av;

if (argc <= 0 || argv == NULL) {

/* Ensure at least one (empty) argument is seen */

static char *empty_argv[1] = {""};

argv = empty_argv;

argc = 1;

}

av = PyList_New(argc);

if (av != NULL) {

int i;

for (i = 0; i < argc; i++) {

#ifdef __VMS

PyObject *v;

/* argv[0] is the script pathname if known */

if (i == 0) {

char* fn = decc$translate_vms(argv[0]);

if ((fn == (char *)0) || fn == (char *)-1)

v = PyString_FromString(argv[0]);

else

v = PyString_FromString(

decc$translate_vms(argv[0]));

} else

v = PyString_FromString(argv[i]);

#else

PyObject *v = PyString_FromString(argv[i]);

#endif

if (v == NULL) {

Py_DECREF(av);

av = NULL;

break;

}

PyList_SetItem(av, i, v);

}

}

return av;

}

void

PySys_SetArgv(int argc, char **argv)

{

#if defined(HAVE_REALPATH)

char fullpath[MAXPATHLEN];

#elif defined(MS_WINDOWS)

char fullpath[MAX_PATH];

#endif

PyObject *av = makeargvobject(argc, argv);

PyObject *path = PySys_GetObject("path");

if (av == NULL)

Py_FatalError("no mem for sys.argv");

if (PySys_SetObject("argv", av) != 0)

Py_FatalError("can't assign sys.argv");

if (path != NULL) {

char *argv0 = argv[0];

char *p = NULL;

Py_ssize_t n = 0;

PyObject *a;

#ifdef HAVE_READLINK

char link[MAXPATHLEN+1];

char argv0copy[2*MAXPATHLEN+1];

int nr = 0;

if (argc > 0 && argv0 != NULL && strcmp(argv0, "-c") != 0)

nr = readlink(argv0, link, MAXPATHLEN);

if (nr > 0) {

/* It's a symlink */

link[nr] = '\0';

if (link[0] == SEP)

argv0 = link; /* Link to absolute path */

else if (strchr(link, SEP) == NULL)

; /* Link without path */

else {

/* Must join(dirname(argv0), link) */

char *q = strrchr(argv0, SEP);

if (q == NULL)

argv0 = link; /* argv0 without path */

else {

/* Must make a copy */

strcpy(argv0copy, argv0);

q = strrchr(argv0copy, SEP);

strcpy(q+1, link);

argv0 = argv0copy;

}

}

}

#endif /* HAVE_READLINK */

#if SEP == '\\' /* Special case for MS filename syntax */

if (argc > 0 && argv0 != NULL && strcmp(argv0, "-c") != 0) {

char *q;

#ifdef MS_WINDOWS

char *ptemp;

if (GetFullPathName(argv0,

sizeof(fullpath),

fullpath,

&ptemp)) {

argv0 = fullpath;

}

#endif

p = strrchr(argv0, SEP);

/* Test for alternate separator */

q = strrchr(p ? p : argv0, '/');

if (q != NULL)

p = q;

if (p != NULL) {

n = p + 1 - argv0;

if (n > 1 && p[-1] != ':')

n--; /* Drop trailing separator */

}

}

#else /* All other filename syntaxes */

if (argc > 0 && argv0 != NULL && strcmp(argv0, "-c") != 0) {

#if defined(HAVE_REALPATH)

if (realpath(argv0, fullpath)) {

argv0 = fullpath;

}

#endif

p = strrchr(argv0, SEP);

}

if (p != NULL) {

#ifndef RISCOS

n = p + 1 - argv0;

#else /* don't include trailing separator */

n = p - argv0;

#endif /* RISCOS */

#if SEP == '/' /* Special case for Unix filename syntax */

if (n > 1)

n--; /* Drop trailing separator */

#endif /* Unix */

}

#endif /* All others */

a = PyString_FromStringAndSize(argv0, n);

if (a == NULL)

Py_FatalError("no mem for sys.path insertion");

if (PyList_Insert(path, 0, a) < 0)

Py_FatalError("sys.path.insert(0) failed");

Py_DECREF(a);

}

Py_DECREF(av);

}

/* APIs to write to sys.stdout or sys.stderr using a printf-like interface.

Adapted from code submitted by Just van Rossum.

PySys_WriteStdout(format, ...)

PySys_WriteStderr(format, ...)

The first function writes to sys.stdout; the second to sys.stderr. When

there is a problem, they write to the real (C level) stdout or stderr;

no exceptions are raised.

Both take a printf-style format string as their first argument followed

by a variable length argument list determined by the format string.

*** WARNING ***

The format should limit the total size of the formatted output string to

1000 bytes. In particular, this means that no unrestricted "%s" formats

should occur; these should be limited using "%.s where is a

decimal number calculated so that plus the maximum size of other

formatted text does not exceed 1000 bytes. Also watch out for "%f",

which can print hundreds of digits for very large numbers.

*/

static void

mywrite(char *name, FILE *fp, const char *format, va_list va)

{

PyObject *file;

PyObject *error_type, *error_value, *error_traceback;

PyErr_Fetch(&error_type, &error_value, &error_traceback);

file = PySys_GetObject(name);

if (file == NULL || PyFile_AsFile(file) == fp)

vfprintf(fp, format, va);

else {

char buffer[1001];

const int written = PyOS_vsnprintf(buffer, sizeof(buffer),

format, va);

if (PyFile_WriteString(buffer, file) != 0) {

PyErr_Clear();

fputs(buffer, fp);

}

if (written < 0 || (size_t)written >= sizeof(buffer)) {

const char *truncated = "... truncated";

if (PyFile_WriteString(truncated, file) != 0) {

PyErr_Clear();

fputs(truncated, fp);

}

}

}

PyErr_Restore(error_type, error_value, error_traceback);

}

void

PySys_WriteStdout(const char *format, ...)

{

va_list va;

va_start(va, format);

mywrite("stdout", stdout, format, va);

va_end(va);

}

void

PySys_WriteStderr(const char *format, ...)

{

va_list va;

va_start(va, format);

mywrite("stderr", stderr, format, va);

va_end(va);

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值