annotate Python/sysmodule.c @ 9964:d44cb87e500d legacy-trunk

_PySys_Init(): Nailed small memory leak. The stringobject created for sys.version was missing a Py_XDECREF().
author Barry Warsaw <barry@python.org>
date Wed, 27 Jan 1999 16:33:19 +0000
parents 030ee50bd8d1
children 66216f32403d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
145
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
1 /***********************************************************
2062
d0d0a69b5ca3 Added 1995 to copyright message.
Guido van Rossum <guido@python.org>
parents: 2052
diff changeset
2 Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
d0d0a69b5ca3 Added 1995 to copyright message.
Guido van Rossum <guido@python.org>
parents: 2052
diff changeset
3 The Netherlands.
145
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
4
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
5 All Rights Reserved
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
6
4503
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4291
diff changeset
7 Permission to use, copy, modify, and distribute this software and its
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4291
diff changeset
8 documentation for any purpose and without fee is hereby granted,
145
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
9 provided that the above copyright notice appear in all copies and that
4503
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4291
diff changeset
10 both that copyright notice and this permission notice appear in
145
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
11 supporting documentation, and that the names of Stichting Mathematisch
4503
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4291
diff changeset
12 Centrum or CWI or Corporation for National Research Initiatives or
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4291
diff changeset
13 CNRI not be used in advertising or publicity pertaining to
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4291
diff changeset
14 distribution of the software without specific, written prior
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4291
diff changeset
15 permission.
145
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
16
4503
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4291
diff changeset
17 While CWI is the initial source for this software, a modified version
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4291
diff changeset
18 is made available by the Corporation for National Research Initiatives
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4291
diff changeset
19 (CNRI) at the Internet address ftp://ftp.python.org.
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4291
diff changeset
20
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4291
diff changeset
21 STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4291
diff changeset
22 REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4291
diff changeset
23 MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4291
diff changeset
24 CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4291
diff changeset
25 DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4291
diff changeset
26 PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4291
diff changeset
27 TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4291
diff changeset
28 PERFORMANCE OF THIS SOFTWARE.
145
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
29
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
30 ******************************************************************/
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
31
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
32 /* System module */
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
33
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
34 /*
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
35 Various bits of information used by the interpreter are collected in
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
36 module 'sys'.
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 83
diff changeset
37 Function member:
2586
ae131b8b4e31 fix comment about exit()
Guido van Rossum <guido@python.org>
parents: 2421
diff changeset
38 - exit(sts): raise SystemExit
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
39 Data members:
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
40 - stdin, stdout, stderr: standard file objects
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 83
diff changeset
41 - modules: the table of modules (dictionary)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
42 - path: module search path (list of strings)
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 83
diff changeset
43 - argv: script arguments (list of strings)
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 83
diff changeset
44 - ps1, ps2: optional primary and secondary prompts (strings)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
45 */
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
46
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
47 #include "Python.h"
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 83
diff changeset
48
710
bc5c77fa8006 Added settrace() and setprofile().
Guido van Rossum <guido@python.org>
parents: 631
diff changeset
49 #include "osdefs.h"
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 83
diff changeset
50
5550
43822e4fc27d Use #ifdef in stead of #if (Jack)
Guido van Rossum <guido@python.org>
parents: 5405
diff changeset
51 #ifdef HAVE_UNISTD_H
5340
db673e98f28d Expand one level of symbolic link in sys.argv[0] before inserting its
Guido van Rossum <guido@python.org>
parents: 5047
diff changeset
52 #include <unistd.h>
db673e98f28d Expand one level of symbolic link in sys.argv[0] before inserting its
Guido van Rossum <guido@python.org>
parents: 5047
diff changeset
53 #endif
db673e98f28d Expand one level of symbolic link in sys.argv[0] before inserting its
Guido van Rossum <guido@python.org>
parents: 5047
diff changeset
54
4291
1427d61f1307 Rationalized MS ifdefs
Guido van Rossum <guido@python.org>
parents: 4256
diff changeset
55 #ifdef MS_COREDLL
3490
1dfc519ea3cb Under NT, define sys.dllhandle and sys.winver (Mark H.).
Guido van Rossum <guido@python.org>
parents: 3326
diff changeset
56 extern void *PyWin_DLLhModule;
6352
b62a7a59414e Get DLL version from a variable.
Guido van Rossum <guido@python.org>
parents: 5955
diff changeset
57 /* A string loaded from the DLL at startup: */
b62a7a59414e Get DLL version from a variable.
Guido van Rossum <guido@python.org>
parents: 5955
diff changeset
58 extern const char *PyWin_DLLVersionString;
3490
1dfc519ea3cb Under NT, define sys.dllhandle and sys.winver (Mark H.).
Guido van Rossum <guido@python.org>
parents: 3326
diff changeset
59 #endif
1dfc519ea3cb Under NT, define sys.dllhandle and sys.winver (Mark H.).
Guido van Rossum <guido@python.org>
parents: 3326
diff changeset
60
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
61 PyObject *
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
62 PySys_GetObject(name)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
63 char *name;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
64 {
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5863
diff changeset
65 PyThreadState *tstate = PyThreadState_Get();
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5863
diff changeset
66 PyObject *sd = tstate->interp->sysdict;
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5863
diff changeset
67 return PyDict_GetItemString(sd, name);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
68 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
69
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
70 FILE *
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
71 PySys_GetFile(name, def)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
72 char *name;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
73 FILE *def;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
74 {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
75 FILE *fp = NULL;
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
76 PyObject *v = PySys_GetObject(name);
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
77 if (v != NULL && PyFile_Check(v))
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
78 fp = PyFile_AsFile(v);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
79 if (fp == NULL)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
80 fp = def;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
81 return fp;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
82 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
83
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
84 int
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
85 PySys_SetObject(name, v)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
86 char *name;
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
87 PyObject *v;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
88 {
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5863
diff changeset
89 PyThreadState *tstate = PyThreadState_Get();
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5863
diff changeset
90 PyObject *sd = tstate->interp->sysdict;
631
f5a2bc39e10e sysset(name, NULL) does nothing if sys.name is undefined
Guido van Rossum <guido@python.org>
parents: 596
diff changeset
91 if (v == NULL) {
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5863
diff changeset
92 if (PyDict_GetItemString(sd, name) == NULL)
631
f5a2bc39e10e sysset(name, NULL) does nothing if sys.name is undefined
Guido van Rossum <guido@python.org>
parents: 596
diff changeset
93 return 0;
f5a2bc39e10e sysset(name, NULL) does nothing if sys.name is undefined
Guido van Rossum <guido@python.org>
parents: 596
diff changeset
94 else
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5863
diff changeset
95 return PyDict_DelItemString(sd, name);
631
f5a2bc39e10e sysset(name, NULL) does nothing if sys.name is undefined
Guido van Rossum <guido@python.org>
parents: 596
diff changeset
96 }
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
97 else
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5863
diff changeset
98 return PyDict_SetItemString(sd, name, v);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
99 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
100
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
101 static PyObject *
5405
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents: 5377
diff changeset
102 sys_exc_info(self, args)
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents: 5377
diff changeset
103 PyObject *self;
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents: 5377
diff changeset
104 PyObject *args;
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents: 5377
diff changeset
105 {
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents: 5377
diff changeset
106 PyThreadState *tstate;
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents: 5377
diff changeset
107 if (!PyArg_Parse(args, ""))
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents: 5377
diff changeset
108 return NULL;
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents: 5377
diff changeset
109 tstate = PyThreadState_Get();
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents: 5377
diff changeset
110 return Py_BuildValue(
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents: 5377
diff changeset
111 "(OOO)",
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents: 5377
diff changeset
112 tstate->exc_type != NULL ? tstate->exc_type : Py_None,
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents: 5377
diff changeset
113 tstate->exc_value != NULL ? tstate->exc_value : Py_None,
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents: 5377
diff changeset
114 tstate->exc_traceback != NULL ?
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents: 5377
diff changeset
115 tstate->exc_traceback : Py_None);
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents: 5377
diff changeset
116 }
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents: 5377
diff changeset
117
8566
6e8b0e754a7f Added doc strings. Maybe the doc string for the module itself is a bit
Guido van Rossum <guido@python.org>
parents: 8314
diff changeset
118 static char exc_info_doc[] =
6e8b0e754a7f Added doc strings. Maybe the doc string for the module itself is a bit
Guido van Rossum <guido@python.org>
parents: 8314
diff changeset
119 "exc_info() -> (type, value, traceback)\n\
6e8b0e754a7f Added doc strings. Maybe the doc string for the module itself is a bit
Guido van Rossum <guido@python.org>
parents: 8314
diff changeset
120 \n\
6e8b0e754a7f Added doc strings. Maybe the doc string for the module itself is a bit
Guido van Rossum <guido@python.org>
parents: 8314
diff changeset
121 Return information about the exception that is currently being handled.\n\
6e8b0e754a7f Added doc strings. Maybe the doc string for the module itself is a bit
Guido van Rossum <guido@python.org>
parents: 8314
diff changeset
122 This should be called from inside an except clause only.";
6e8b0e754a7f Added doc strings. Maybe the doc string for the module itself is a bit
Guido van Rossum <guido@python.org>
parents: 8314
diff changeset
123
5405
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents: 5377
diff changeset
124 static PyObject *
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
125 sys_exit(self, args)
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
126 PyObject *self;
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
127 PyObject *args;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
128 {
549
4efff6cea90f Raise SystemExit instead of calling goaway().
Guido van Rossum <guido@python.org>
parents: 476
diff changeset
129 /* Raise SystemExit so callers may catch it or clean up. */
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
130 PyErr_SetObject(PyExc_SystemExit, args);
549
4efff6cea90f Raise SystemExit instead of calling goaway().
Guido van Rossum <guido@python.org>
parents: 476
diff changeset
131 return NULL;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
132 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
133
8566
6e8b0e754a7f Added doc strings. Maybe the doc string for the module itself is a bit
Guido van Rossum <guido@python.org>
parents: 8314
diff changeset
134 static char exit_doc[] =
6e8b0e754a7f Added doc strings. Maybe the doc string for the module itself is a bit
Guido van Rossum <guido@python.org>
parents: 8314
diff changeset
135 "exit([status])\n\
6e8b0e754a7f Added doc strings. Maybe the doc string for the module itself is a bit
Guido van Rossum <guido@python.org>
parents: 8314
diff changeset
136 \n\
6e8b0e754a7f Added doc strings. Maybe the doc string for the module itself is a bit
Guido van Rossum <guido@python.org>
parents: 8314
diff changeset
137 Exit the interpreter by raising SystemExit(status).\n\
6e8b0e754a7f Added doc strings. Maybe the doc string for the module itself is a bit
Guido van Rossum <guido@python.org>
parents: 8314
diff changeset
138 If the status is omitted or None, it defaults to zero (i.e., success).\n\
6e8b0e754a7f Added doc strings. Maybe the doc string for the module itself is a bit
Guido van Rossum <guido@python.org>
parents: 8314
diff changeset
139 If the status numeric, it will be used as the system exit status.\n\
6e8b0e754a7f Added doc strings. Maybe the doc string for the module itself is a bit
Guido van Rossum <guido@python.org>
parents: 8314
diff changeset
140 If it is another kind of object, it will be printed and the system\n\
6e8b0e754a7f Added doc strings. Maybe the doc string for the module itself is a bit
Guido van Rossum <guido@python.org>
parents: 8314
diff changeset
141 exit status will be one (i.e., failure).";
6e8b0e754a7f Added doc strings. Maybe the doc string for the module itself is a bit
Guido van Rossum <guido@python.org>
parents: 8314
diff changeset
142
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
143 static PyObject *
710
bc5c77fa8006 Added settrace() and setprofile().
Guido van Rossum <guido@python.org>
parents: 631
diff changeset
144 sys_settrace(self, args)
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
145 PyObject *self;
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
146 PyObject *args;
710
bc5c77fa8006 Added settrace() and setprofile().
Guido van Rossum <guido@python.org>
parents: 631
diff changeset
147 {
5405
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents: 5377
diff changeset
148 PyThreadState *tstate = PyThreadState_Get();
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
149 if (args == Py_None)
710
bc5c77fa8006 Added settrace() and setprofile().
Guido van Rossum <guido@python.org>
parents: 631
diff changeset
150 args = NULL;
788
ba4ec13c0f9f Fix reference counts of sys_trace and sys_profile
Guido van Rossum <guido@python.org>
parents: 710
diff changeset
151 else
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
152 Py_XINCREF(args);
5405
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents: 5377
diff changeset
153 Py_XDECREF(tstate->sys_tracefunc);
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents: 5377
diff changeset
154 tstate->sys_tracefunc = args;
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
155 Py_INCREF(Py_None);
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
156 return Py_None;
710
bc5c77fa8006 Added settrace() and setprofile().
Guido van Rossum <guido@python.org>
parents: 631
diff changeset
157 }
bc5c77fa8006 Added settrace() and setprofile().
Guido van Rossum <guido@python.org>
parents: 631
diff changeset
158
8566
6e8b0e754a7f Added doc strings. Maybe the doc string for the module itself is a bit
Guido van Rossum <guido@python.org>
parents: 8314
diff changeset
159 static char settrace_doc[] =
6e8b0e754a7f Added doc strings. Maybe the doc string for the module itself is a bit
Guido van Rossum <guido@python.org>
parents: 8314
diff changeset
160 "settrace(function)\n\
6e8b0e754a7f Added doc strings. Maybe the doc string for the module itself is a bit
Guido van Rossum <guido@python.org>
parents: 8314
diff changeset
161 \n\
6e8b0e754a7f Added doc strings. Maybe the doc string for the module itself is a bit
Guido van Rossum <guido@python.org>
parents: 8314
diff changeset
162 Set the global debug tracing function. It will be called on each\n\
6e8b0e754a7f Added doc strings. Maybe the doc string for the module itself is a bit
Guido van Rossum <guido@python.org>
parents: 8314
diff changeset
163 function call. See the debugger chapter in the library manual.";
6e8b0e754a7f Added doc strings. Maybe the doc string for the module itself is a bit
Guido van Rossum <guido@python.org>
parents: 8314
diff changeset
164
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
165 static PyObject *
710
bc5c77fa8006 Added settrace() and setprofile().
Guido van Rossum <guido@python.org>
parents: 631
diff changeset
166 sys_setprofile(self, args)
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
167 PyObject *self;
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
168 PyObject *args;
710
bc5c77fa8006 Added settrace() and setprofile().
Guido van Rossum <guido@python.org>
parents: 631
diff changeset
169 {
5405
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents: 5377
diff changeset
170 PyThreadState *tstate = PyThreadState_Get();
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
171 if (args == Py_None)
710
bc5c77fa8006 Added settrace() and setprofile().
Guido van Rossum <guido@python.org>
parents: 631
diff changeset
172 args = NULL;
788
ba4ec13c0f9f Fix reference counts of sys_trace and sys_profile
Guido van Rossum <guido@python.org>
parents: 710
diff changeset
173 else
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
174 Py_XINCREF(args);
5405
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents: 5377
diff changeset
175 Py_XDECREF(tstate->sys_profilefunc);
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents: 5377
diff changeset
176 tstate->sys_profilefunc = args;
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
177 Py_INCREF(Py_None);
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
178 return Py_None;
710
bc5c77fa8006 Added settrace() and setprofile().
Guido van Rossum <guido@python.org>
parents: 631
diff changeset
179 }
bc5c77fa8006 Added settrace() and setprofile().
Guido van Rossum <guido@python.org>
parents: 631
diff changeset
180
8566
6e8b0e754a7f Added doc strings. Maybe the doc string for the module itself is a bit
Guido van Rossum <guido@python.org>
parents: 8314
diff changeset
181 static char setprofile_doc[] =
6e8b0e754a7f Added doc strings. Maybe the doc string for the module itself is a bit
Guido van Rossum <guido@python.org>
parents: 8314
diff changeset
182 "setprofile(function)\n\
6e8b0e754a7f Added doc strings. Maybe the doc string for the module itself is a bit
Guido van Rossum <guido@python.org>
parents: 8314
diff changeset
183 \n\
6e8b0e754a7f Added doc strings. Maybe the doc string for the module itself is a bit
Guido van Rossum <guido@python.org>
parents: 8314
diff changeset
184 Set the profiling function. It will be called on each function call\n\
6e8b0e754a7f Added doc strings. Maybe the doc string for the module itself is a bit
Guido van Rossum <guido@python.org>
parents: 8314
diff changeset
185 and return. See the profiler chapter in the library manual.";
6e8b0e754a7f Added doc strings. Maybe the doc string for the module itself is a bit
Guido van Rossum <guido@python.org>
parents: 8314
diff changeset
186
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
187 static PyObject *
2093
e8cafb2b743a sys.check_interval=x -> sys.setcheckinterval(x)
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
188 sys_setcheckinterval(self, args)
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
189 PyObject *self;
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
190 PyObject *args;
2093
e8cafb2b743a sys.check_interval=x -> sys.setcheckinterval(x)
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
191 {
5405
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents: 5377
diff changeset
192 PyThreadState *tstate = PyThreadState_Get();
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5863
diff changeset
193 if (!PyArg_ParseTuple(args, "i", &tstate->interp->checkinterval))
2093
e8cafb2b743a sys.check_interval=x -> sys.setcheckinterval(x)
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
194 return NULL;
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
195 Py_INCREF(Py_None);
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
196 return Py_None;
2093
e8cafb2b743a sys.check_interval=x -> sys.setcheckinterval(x)
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
197 }
e8cafb2b743a sys.check_interval=x -> sys.setcheckinterval(x)
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
198
8566
6e8b0e754a7f Added doc strings. Maybe the doc string for the module itself is a bit
Guido van Rossum <guido@python.org>
parents: 8314
diff changeset
199 static char setcheckinterval_doc[] =
6e8b0e754a7f Added doc strings. Maybe the doc string for the module itself is a bit
Guido van Rossum <guido@python.org>
parents: 8314
diff changeset
200 "setcheckinterval(n)\n\
6e8b0e754a7f Added doc strings. Maybe the doc string for the module itself is a bit
Guido van Rossum <guido@python.org>
parents: 8314
diff changeset
201 \n\
6e8b0e754a7f Added doc strings. Maybe the doc string for the module itself is a bit
Guido van Rossum <guido@python.org>
parents: 8314
diff changeset
202 Tell the Python interpreter to check for asynchronous events every\n\
6e8b0e754a7f Added doc strings. Maybe the doc string for the module itself is a bit
Guido van Rossum <guido@python.org>
parents: 8314
diff changeset
203 n instructions. This also affects how often thread switches occur.";
6e8b0e754a7f Added doc strings. Maybe the doc string for the module itself is a bit
Guido van Rossum <guido@python.org>
parents: 8314
diff changeset
204
1013
a1ce5a949828 Add an optional interface to turn malloc debugging on and off.
Guido van Rossum <guido@python.org>
parents: 901
diff changeset
205 #ifdef USE_MALLOPT
a1ce5a949828 Add an optional interface to turn malloc debugging on and off.
Guido van Rossum <guido@python.org>
parents: 901
diff changeset
206 /* Link with -lmalloc (or -lmpc) on an SGI */
a1ce5a949828 Add an optional interface to turn malloc debugging on and off.
Guido van Rossum <guido@python.org>
parents: 901
diff changeset
207 #include <malloc.h>
a1ce5a949828 Add an optional interface to turn malloc debugging on and off.
Guido van Rossum <guido@python.org>
parents: 901
diff changeset
208
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
209 static PyObject *
1013
a1ce5a949828 Add an optional interface to turn malloc debugging on and off.
Guido van Rossum <guido@python.org>
parents: 901
diff changeset
210 sys_mdebug(self, args)
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
211 PyObject *self;
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
212 PyObject *args;
1013
a1ce5a949828 Add an optional interface to turn malloc debugging on and off.
Guido van Rossum <guido@python.org>
parents: 901
diff changeset
213 {
a1ce5a949828 Add an optional interface to turn malloc debugging on and off.
Guido van Rossum <guido@python.org>
parents: 901
diff changeset
214 int flag;
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
215 if (!PyArg_Parse(args, "i", &flag))
1013
a1ce5a949828 Add an optional interface to turn malloc debugging on and off.
Guido van Rossum <guido@python.org>
parents: 901
diff changeset
216 return NULL;
a1ce5a949828 Add an optional interface to turn malloc debugging on and off.
Guido van Rossum <guido@python.org>
parents: 901
diff changeset
217 mallopt(M_DEBUG, flag);
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
218 Py_INCREF(Py_None);
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
219 return Py_None;
1013
a1ce5a949828 Add an optional interface to turn malloc debugging on and off.
Guido van Rossum <guido@python.org>
parents: 901
diff changeset
220 }
a1ce5a949828 Add an optional interface to turn malloc debugging on and off.
Guido van Rossum <guido@python.org>
parents: 901
diff changeset
221 #endif /* USE_MALLOPT */
a1ce5a949828 Add an optional interface to turn malloc debugging on and off.
Guido van Rossum <guido@python.org>
parents: 901
diff changeset
222
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
223 static PyObject *
3024
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2810
diff changeset
224 sys_getrefcount(self, args)
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
225 PyObject *self;
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
226 PyObject *args;
3024
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2810
diff changeset
227 {
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
228 PyObject *arg;
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
229 if (!PyArg_Parse(args, "O", &arg))
3024
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2810
diff changeset
230 return NULL;
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
231 return PyInt_FromLong((long) arg->ob_refcnt);
3024
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2810
diff changeset
232 }
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2810
diff changeset
233
8566
6e8b0e754a7f Added doc strings. Maybe the doc string for the module itself is a bit
Guido van Rossum <guido@python.org>
parents: 8314
diff changeset
234 static char getrefcount_doc[] =
6e8b0e754a7f Added doc strings. Maybe the doc string for the module itself is a bit
Guido van Rossum <guido@python.org>
parents: 8314
diff changeset
235 "getrefcount(object) -> integer\n\
6e8b0e754a7f Added doc strings. Maybe the doc string for the module itself is a bit
Guido van Rossum <guido@python.org>
parents: 8314
diff changeset
236 \n\
6e8b0e754a7f Added doc strings. Maybe the doc string for the module itself is a bit
Guido van Rossum <guido@python.org>
parents: 8314
diff changeset
237 Return the current reference count for the object. This includes the\n\
6e8b0e754a7f Added doc strings. Maybe the doc string for the module itself is a bit
Guido van Rossum <guido@python.org>
parents: 8314
diff changeset
238 temporary reference in the argument list, so it is at least 2.";
6e8b0e754a7f Added doc strings. Maybe the doc string for the module itself is a bit
Guido van Rossum <guido@python.org>
parents: 8314
diff changeset
239
3024
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2810
diff changeset
240 #ifdef COUNT_ALLOCS
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2810
diff changeset
241 static PyObject *
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2810
diff changeset
242 sys_getcounts(self, args)
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2810
diff changeset
243 PyObject *self, *args;
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2810
diff changeset
244 {
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2810
diff changeset
245 extern PyObject *get_counts Py_PROTO((void));
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2810
diff changeset
246
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2810
diff changeset
247 if (!PyArg_Parse(args, ""))
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2810
diff changeset
248 return NULL;
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2810
diff changeset
249 return get_counts();
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2810
diff changeset
250 }
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2810
diff changeset
251 #endif
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2810
diff changeset
252
3585
4ed09a3c3201 TRACE_REFS -> Py_TRACE_REFS
Guido van Rossum <guido@python.org>
parents: 3490
diff changeset
253 #ifdef Py_TRACE_REFS
3609
0b0c4ed1936a rename printrefs, getobjects to _Py_ prefix
Guido van Rossum <guido@python.org>
parents: 3585
diff changeset
254 /* Defined in objects.c because it uses static globals if that file */
0b0c4ed1936a rename printrefs, getobjects to _Py_ prefix
Guido van Rossum <guido@python.org>
parents: 3585
diff changeset
255 extern PyObject *_Py_GetObjects Py_PROTO((PyObject *, PyObject *));
3024
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2810
diff changeset
256 #endif
3609
0b0c4ed1936a rename printrefs, getobjects to _Py_ prefix
Guido van Rossum <guido@python.org>
parents: 3585
diff changeset
257
5047
7a9cac7cb910 Added optional interface for dynamic execution profile (to be gathered
Guido van Rossum <guido@python.org>
parents: 4904
diff changeset
258 #ifdef DYNAMIC_EXECUTION_PROFILE
7a9cac7cb910 Added optional interface for dynamic execution profile (to be gathered
Guido van Rossum <guido@python.org>
parents: 4904
diff changeset
259 /* Defined in ceval.c because it uses static globals if that file */
7a9cac7cb910 Added optional interface for dynamic execution profile (to be gathered
Guido van Rossum <guido@python.org>
parents: 4904
diff changeset
260 extern PyObject *_Py_GetDXProfile Py_PROTO((PyObject *, PyObject *));
7a9cac7cb910 Added optional interface for dynamic execution profile (to be gathered
Guido van Rossum <guido@python.org>
parents: 4904
diff changeset
261 #endif
7a9cac7cb910 Added optional interface for dynamic execution profile (to be gathered
Guido van Rossum <guido@python.org>
parents: 4904
diff changeset
262
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
263 static PyMethodDef sys_methods[] = {
5047
7a9cac7cb910 Added optional interface for dynamic execution profile (to be gathered
Guido van Rossum <guido@python.org>
parents: 4904
diff changeset
264 /* Might as well keep this in alphabetic order */
8566
6e8b0e754a7f Added doc strings. Maybe the doc string for the module itself is a bit
Guido van Rossum <guido@python.org>
parents: 8314
diff changeset
265 {"exc_info", sys_exc_info, 0, exc_info_doc},
6e8b0e754a7f Added doc strings. Maybe the doc string for the module itself is a bit
Guido van Rossum <guido@python.org>
parents: 8314
diff changeset
266 {"exit", sys_exit, 0, exit_doc},
3024
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2810
diff changeset
267 #ifdef COUNT_ALLOCS
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2810
diff changeset
268 {"getcounts", sys_getcounts, 0},
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2810
diff changeset
269 #endif
5047
7a9cac7cb910 Added optional interface for dynamic execution profile (to be gathered
Guido van Rossum <guido@python.org>
parents: 4904
diff changeset
270 #ifdef DYNAMIC_EXECUTION_PROFILE
7a9cac7cb910 Added optional interface for dynamic execution profile (to be gathered
Guido van Rossum <guido@python.org>
parents: 4904
diff changeset
271 {"getdxp", _Py_GetDXProfile, 1},
7a9cac7cb910 Added optional interface for dynamic execution profile (to be gathered
Guido van Rossum <guido@python.org>
parents: 4904
diff changeset
272 #endif
3585
4ed09a3c3201 TRACE_REFS -> Py_TRACE_REFS
Guido van Rossum <guido@python.org>
parents: 3490
diff changeset
273 #ifdef Py_TRACE_REFS
3609
0b0c4ed1936a rename printrefs, getobjects to _Py_ prefix
Guido van Rossum <guido@python.org>
parents: 3585
diff changeset
274 {"getobjects", _Py_GetObjects, 1},
3024
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2810
diff changeset
275 #endif
8566
6e8b0e754a7f Added doc strings. Maybe the doc string for the module itself is a bit
Guido van Rossum <guido@python.org>
parents: 8314
diff changeset
276 {"getrefcount", sys_getrefcount, 0, getrefcount_doc},
1013
a1ce5a949828 Add an optional interface to turn malloc debugging on and off.
Guido van Rossum <guido@python.org>
parents: 901
diff changeset
277 #ifdef USE_MALLOPT
2421
6eda642f19d0 add explicit 0 flags for methodlist
Guido van Rossum <guido@python.org>
parents: 2093
diff changeset
278 {"mdebug", sys_mdebug, 0},
1013
a1ce5a949828 Add an optional interface to turn malloc debugging on and off.
Guido van Rossum <guido@python.org>
parents: 901
diff changeset
279 #endif
8566
6e8b0e754a7f Added doc strings. Maybe the doc string for the module itself is a bit
Guido van Rossum <guido@python.org>
parents: 8314
diff changeset
280 {"setcheckinterval", sys_setcheckinterval, 1, setcheckinterval_doc},
6e8b0e754a7f Added doc strings. Maybe the doc string for the module itself is a bit
Guido van Rossum <guido@python.org>
parents: 8314
diff changeset
281 {"setprofile", sys_setprofile, 0, setprofile_doc},
6e8b0e754a7f Added doc strings. Maybe the doc string for the module itself is a bit
Guido van Rossum <guido@python.org>
parents: 8314
diff changeset
282 {"settrace", sys_settrace, 0, settrace_doc},
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 83
diff changeset
283 {NULL, NULL} /* sentinel */
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 83
diff changeset
284 };
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 83
diff changeset
285
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
286 static PyObject *
1198
2937897a7d8b * Added Fixcprt.py: script to fix copyright message.
Guido van Rossum <guido@python.org>
parents: 1073
diff changeset
287 list_builtin_module_names()
2937897a7d8b * Added Fixcprt.py: script to fix copyright message.
Guido van Rossum <guido@python.org>
parents: 1073
diff changeset
288 {
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
289 PyObject *list = PyList_New(0);
1198
2937897a7d8b * Added Fixcprt.py: script to fix copyright message.
Guido van Rossum <guido@python.org>
parents: 1073
diff changeset
290 int i;
2937897a7d8b * Added Fixcprt.py: script to fix copyright message.
Guido van Rossum <guido@python.org>
parents: 1073
diff changeset
291 if (list == NULL)
2937897a7d8b * Added Fixcprt.py: script to fix copyright message.
Guido van Rossum <guido@python.org>
parents: 1073
diff changeset
292 return NULL;
6550
f406ec35bfca Get rid of another reference to _PyImport_Inittab (now a static array)
Guido van Rossum <guido@python.org>
parents: 6352
diff changeset
293 for (i = 0; PyImport_Inittab[i].name != NULL; i++) {
5405
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents: 5377
diff changeset
294 PyObject *name = PyString_FromString(
6550
f406ec35bfca Get rid of another reference to _PyImport_Inittab (now a static array)
Guido van Rossum <guido@python.org>
parents: 6352
diff changeset
295 PyImport_Inittab[i].name);
1198
2937897a7d8b * Added Fixcprt.py: script to fix copyright message.
Guido van Rossum <guido@python.org>
parents: 1073
diff changeset
296 if (name == NULL)
2937897a7d8b * Added Fixcprt.py: script to fix copyright message.
Guido van Rossum <guido@python.org>
parents: 1073
diff changeset
297 break;
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
298 PyList_Append(list, name);
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
299 Py_DECREF(name);
1198
2937897a7d8b * Added Fixcprt.py: script to fix copyright message.
Guido van Rossum <guido@python.org>
parents: 1073
diff changeset
300 }
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
301 if (PyList_Sort(list) != 0) {
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
302 Py_DECREF(list);
1848
b724599401ff Merge back to main trunk
Guido van Rossum <guido@python.org>
parents: 1198
diff changeset
303 list = NULL;
b724599401ff Merge back to main trunk
Guido van Rossum <guido@python.org>
parents: 1198
diff changeset
304 }
4904
5dc20a9e0506 Make builtin_module_names a tuple instead of a list.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
305 if (list) {
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
306 PyObject *v = PyList_AsTuple(list);
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
307 Py_DECREF(list);
4904
5dc20a9e0506 Make builtin_module_names a tuple instead of a list.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
308 list = v;
5dc20a9e0506 Make builtin_module_names a tuple instead of a list.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
309 }
1198
2937897a7d8b * Added Fixcprt.py: script to fix copyright message.
Guido van Rossum <guido@python.org>
parents: 1073
diff changeset
310 return list;
2937897a7d8b * Added Fixcprt.py: script to fix copyright message.
Guido van Rossum <guido@python.org>
parents: 1073
diff changeset
311 }
2937897a7d8b * Added Fixcprt.py: script to fix copyright message.
Guido van Rossum <guido@python.org>
parents: 1073
diff changeset
312
8840
44baef8ab5c8 Gack. The module doc string is too long for VC++ 5.0.
Guido van Rossum <guido@python.org>
parents: 8566
diff changeset
313 /* XXX This doc string is too long to be a single string literal in VC++ 5.0.
44baef8ab5c8 Gack. The module doc string is too long for VC++ 5.0.
Guido van Rossum <guido@python.org>
parents: 8566
diff changeset
314 Two literals concatenated works just fine. If you have a K&R compiler
44baef8ab5c8 Gack. The module doc string is too long for VC++ 5.0.
Guido van Rossum <guido@python.org>
parents: 8566
diff changeset
315 or other abomination that however *does* understand longer strings,
44baef8ab5c8 Gack. The module doc string is too long for VC++ 5.0.
Guido van Rossum <guido@python.org>
parents: 8566
diff changeset
316 get rid of the !!! comment in the middle and the quotes that surround it. */
8566
6e8b0e754a7f Added doc strings. Maybe the doc string for the module itself is a bit
Guido van Rossum <guido@python.org>
parents: 8314
diff changeset
317 static char sys_doc[] =
6e8b0e754a7f Added doc strings. Maybe the doc string for the module itself is a bit
Guido van Rossum <guido@python.org>
parents: 8314
diff changeset
318 "This module provides access to some objects used or maintained by the\n\
6e8b0e754a7f Added doc strings. Maybe the doc string for the module itself is a bit
Guido van Rossum <guido@python.org>
parents: 8314
diff changeset
319 interpreter and to functions that interact strongly with the interpreter.\n\
6e8b0e754a7f Added doc strings. Maybe the doc string for the module itself is a bit
Guido van Rossum <guido@python.org>
parents: 8314
diff changeset
320 \n\
6e8b0e754a7f Added doc strings. Maybe the doc string for the module itself is a bit
Guido van Rossum <guido@python.org>
parents: 8314
diff changeset
321 Dynamic objects:\n\
6e8b0e754a7f Added doc strings. Maybe the doc string for the module itself is a bit
Guido van Rossum <guido@python.org>
parents: 8314
diff changeset
322 \n\
6e8b0e754a7f Added doc strings. Maybe the doc string for the module itself is a bit
Guido van Rossum <guido@python.org>
parents: 8314
diff changeset
323 argv -- command line arguments; argv[0] is the script pathname if known\n\
6e8b0e754a7f Added doc strings. Maybe the doc string for the module itself is a bit
Guido van Rossum <guido@python.org>
parents: 8314
diff changeset
324 path -- module search path; path[0] is the script directory, else ''\n\
6e8b0e754a7f Added doc strings. Maybe the doc string for the module itself is a bit
Guido van Rossum <guido@python.org>
parents: 8314
diff changeset
325 modules -- dictionary of loaded modules\n\
6e8b0e754a7f Added doc strings. Maybe the doc string for the module itself is a bit
Guido van Rossum <guido@python.org>
parents: 8314
diff changeset
326 exitfunc -- you may set this to a function to be called when Python exits\n\
6e8b0e754a7f Added doc strings. Maybe the doc string for the module itself is a bit
Guido van Rossum <guido@python.org>
parents: 8314
diff changeset
327 \n\
6e8b0e754a7f Added doc strings. Maybe the doc string for the module itself is a bit
Guido van Rossum <guido@python.org>
parents: 8314
diff changeset
328 stdin -- standard input file object; used by raw_input() and input()\n\
6e8b0e754a7f Added doc strings. Maybe the doc string for the module itself is a bit
Guido van Rossum <guido@python.org>
parents: 8314
diff changeset
329 stdout -- standard output file object; used by the print statement\n\
6e8b0e754a7f Added doc strings. Maybe the doc string for the module itself is a bit
Guido van Rossum <guido@python.org>
parents: 8314
diff changeset
330 stderr -- standard error object; used for error messages\n\
6e8b0e754a7f Added doc strings. Maybe the doc string for the module itself is a bit
Guido van Rossum <guido@python.org>
parents: 8314
diff changeset
331 By assigning another file object (or an object that behaves like a file)\n\
6e8b0e754a7f Added doc strings. Maybe the doc string for the module itself is a bit
Guido van Rossum <guido@python.org>
parents: 8314
diff changeset
332 to one of these, it is possible to redirect all of the interpreter's I/O.\n\
6e8b0e754a7f Added doc strings. Maybe the doc string for the module itself is a bit
Guido van Rossum <guido@python.org>
parents: 8314
diff changeset
333 \n\
6e8b0e754a7f Added doc strings. Maybe the doc string for the module itself is a bit
Guido van Rossum <guido@python.org>
parents: 8314
diff changeset
334 last_type -- type of last uncaught exception\n\
6e8b0e754a7f Added doc strings. Maybe the doc string for the module itself is a bit
Guido van Rossum <guido@python.org>
parents: 8314
diff changeset
335 last_value -- value of last uncaught exception\n\
6e8b0e754a7f Added doc strings. Maybe the doc string for the module itself is a bit
Guido van Rossum <guido@python.org>
parents: 8314
diff changeset
336 last_traceback -- traceback of last uncaught exception\n\
6e8b0e754a7f Added doc strings. Maybe the doc string for the module itself is a bit
Guido van Rossum <guido@python.org>
parents: 8314
diff changeset
337 These three are only available in an interactive session after a\n\
6e8b0e754a7f Added doc strings. Maybe the doc string for the module itself is a bit
Guido van Rossum <guido@python.org>
parents: 8314
diff changeset
338 traceback has been printed.\n\
6e8b0e754a7f Added doc strings. Maybe the doc string for the module itself is a bit
Guido van Rossum <guido@python.org>
parents: 8314
diff changeset
339 \n\
6e8b0e754a7f Added doc strings. Maybe the doc string for the module itself is a bit
Guido van Rossum <guido@python.org>
parents: 8314
diff changeset
340 exc_type -- type of exception currently being handled\n\
6e8b0e754a7f Added doc strings. Maybe the doc string for the module itself is a bit
Guido van Rossum <guido@python.org>
parents: 8314
diff changeset
341 exc_value -- value of exception currently being handled\n\
6e8b0e754a7f Added doc strings. Maybe the doc string for the module itself is a bit
Guido van Rossum <guido@python.org>
parents: 8314
diff changeset
342 exc_traceback -- traceback of exception currently being handled\n\
6e8b0e754a7f Added doc strings. Maybe the doc string for the module itself is a bit
Guido van Rossum <guido@python.org>
parents: 8314
diff changeset
343 The function exc_info() should be used instead of these three,\n\
6e8b0e754a7f Added doc strings. Maybe the doc string for the module itself is a bit
Guido van Rossum <guido@python.org>
parents: 8314
diff changeset
344 because it is thread-safe.\n\
9885
030ee50bd8d1 Jim Ahlstrom patch: the module doc string is too long for 16-bit VC
Guido van Rossum <guido@python.org>
parents: 9746
diff changeset
345 "
030ee50bd8d1 Jim Ahlstrom patch: the module doc string is too long for 16-bit VC
Guido van Rossum <guido@python.org>
parents: 9746
diff changeset
346 #ifndef MS_WIN16
030ee50bd8d1 Jim Ahlstrom patch: the module doc string is too long for 16-bit VC
Guido van Rossum <guido@python.org>
parents: 9746
diff changeset
347 /* Concatenating string here */
030ee50bd8d1 Jim Ahlstrom patch: the module doc string is too long for 16-bit VC
Guido van Rossum <guido@python.org>
parents: 9746
diff changeset
348 "\n\
8566
6e8b0e754a7f Added doc strings. Maybe the doc string for the module itself is a bit
Guido van Rossum <guido@python.org>
parents: 8314
diff changeset
349 Static objects:\n\
6e8b0e754a7f Added doc strings. Maybe the doc string for the module itself is a bit
Guido van Rossum <guido@python.org>
parents: 8314
diff changeset
350 \n\
6e8b0e754a7f Added doc strings. Maybe the doc string for the module itself is a bit
Guido van Rossum <guido@python.org>
parents: 8314
diff changeset
351 maxint -- the largest supported integer (the smallest is -maxint-1)\n\
6e8b0e754a7f Added doc strings. Maybe the doc string for the module itself is a bit
Guido van Rossum <guido@python.org>
parents: 8314
diff changeset
352 builtin_module_names -- tuple of module names built into this intepreter\n\
6e8b0e754a7f Added doc strings. Maybe the doc string for the module itself is a bit
Guido van Rossum <guido@python.org>
parents: 8314
diff changeset
353 version -- the version of this interpreter\n\
6e8b0e754a7f Added doc strings. Maybe the doc string for the module itself is a bit
Guido van Rossum <guido@python.org>
parents: 8314
diff changeset
354 copyright -- copyright notice pertaining to this interpreter\n\
6e8b0e754a7f Added doc strings. Maybe the doc string for the module itself is a bit
Guido van Rossum <guido@python.org>
parents: 8314
diff changeset
355 platform -- platform identifier\n\
6e8b0e754a7f Added doc strings. Maybe the doc string for the module itself is a bit
Guido van Rossum <guido@python.org>
parents: 8314
diff changeset
356 executable -- pathname of this Python interpreter\n\
6e8b0e754a7f Added doc strings. Maybe the doc string for the module itself is a bit
Guido van Rossum <guido@python.org>
parents: 8314
diff changeset
357 prefix -- prefix used to find the Python library\n\
6e8b0e754a7f Added doc strings. Maybe the doc string for the module itself is a bit
Guido van Rossum <guido@python.org>
parents: 8314
diff changeset
358 exec_prefix -- prefix used to find the machine-specific Python library\n\
6e8b0e754a7f Added doc strings. Maybe the doc string for the module itself is a bit
Guido van Rossum <guido@python.org>
parents: 8314
diff changeset
359 dllhandle -- [Windows only] integer handle of the Python DLL\n\
6e8b0e754a7f Added doc strings. Maybe the doc string for the module itself is a bit
Guido van Rossum <guido@python.org>
parents: 8314
diff changeset
360 winver -- [Windows only] version number of the Python DLL\n\
6e8b0e754a7f Added doc strings. Maybe the doc string for the module itself is a bit
Guido van Rossum <guido@python.org>
parents: 8314
diff changeset
361 __stdin__ -- the original stdin; don't use!\n\
6e8b0e754a7f Added doc strings. Maybe the doc string for the module itself is a bit
Guido van Rossum <guido@python.org>
parents: 8314
diff changeset
362 __stdout__ -- the original stdout; don't use!\n\
6e8b0e754a7f Added doc strings. Maybe the doc string for the module itself is a bit
Guido van Rossum <guido@python.org>
parents: 8314
diff changeset
363 __stderr__ -- the original stderr; don't use!\n\
6e8b0e754a7f Added doc strings. Maybe the doc string for the module itself is a bit
Guido van Rossum <guido@python.org>
parents: 8314
diff changeset
364 \n\
6e8b0e754a7f Added doc strings. Maybe the doc string for the module itself is a bit
Guido van Rossum <guido@python.org>
parents: 8314
diff changeset
365 Functions:\n\
6e8b0e754a7f Added doc strings. Maybe the doc string for the module itself is a bit
Guido van Rossum <guido@python.org>
parents: 8314
diff changeset
366 \n\
6e8b0e754a7f Added doc strings. Maybe the doc string for the module itself is a bit
Guido van Rossum <guido@python.org>
parents: 8314
diff changeset
367 exc_info() -- return thread-safe information about the current exception\n\
6e8b0e754a7f Added doc strings. Maybe the doc string for the module itself is a bit
Guido van Rossum <guido@python.org>
parents: 8314
diff changeset
368 exit() -- exit the interpreter by raising SystemExit\n\
6e8b0e754a7f Added doc strings. Maybe the doc string for the module itself is a bit
Guido van Rossum <guido@python.org>
parents: 8314
diff changeset
369 getrefcount() -- return the reference count for an object (plus one :-)\n\
6e8b0e754a7f Added doc strings. Maybe the doc string for the module itself is a bit
Guido van Rossum <guido@python.org>
parents: 8314
diff changeset
370 setcheckinterval() -- control how often the interpreter checks for events\n\
6e8b0e754a7f Added doc strings. Maybe the doc string for the module itself is a bit
Guido van Rossum <guido@python.org>
parents: 8314
diff changeset
371 setprofile() -- set the global profiling function\n\
6e8b0e754a7f Added doc strings. Maybe the doc string for the module itself is a bit
Guido van Rossum <guido@python.org>
parents: 8314
diff changeset
372 settrace() -- set the global debug tracing function\n\
6e8b0e754a7f Added doc strings. Maybe the doc string for the module itself is a bit
Guido van Rossum <guido@python.org>
parents: 8314
diff changeset
373 ";
9885
030ee50bd8d1 Jim Ahlstrom patch: the module doc string is too long for 16-bit VC
Guido van Rossum <guido@python.org>
parents: 9746
diff changeset
374 #endif
8566
6e8b0e754a7f Added doc strings. Maybe the doc string for the module itself is a bit
Guido van Rossum <guido@python.org>
parents: 8314
diff changeset
375
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5863
diff changeset
376 PyObject *
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5863
diff changeset
377 _PySys_Init()
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
378 {
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
379 extern int fclose Py_PROTO((FILE *));
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5863
diff changeset
380 PyObject *m, *v, *sysdict;
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5863
diff changeset
381 PyObject *sysin, *sysout, *syserr;
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5863
diff changeset
382
8566
6e8b0e754a7f Added doc strings. Maybe the doc string for the module itself is a bit
Guido van Rossum <guido@python.org>
parents: 8314
diff changeset
383 m = Py_InitModule3("sys", sys_methods, sys_doc);
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
384 sysdict = PyModule_GetDict(m);
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5863
diff changeset
385
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5863
diff changeset
386 sysin = PyFile_FromFile(stdin, "<stdin>", "r", NULL);
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5863
diff changeset
387 sysout = PyFile_FromFile(stdout, "<stdout>", "w", NULL);
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5863
diff changeset
388 syserr = PyFile_FromFile(stderr, "<stderr>", "w", NULL);
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
389 if (PyErr_Occurred())
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5863
diff changeset
390 return NULL;
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
391 PyDict_SetItemString(sysdict, "stdin", sysin);
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
392 PyDict_SetItemString(sysdict, "stdout", sysout);
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
393 PyDict_SetItemString(sysdict, "stderr", syserr);
7402
c7454ca7d3ac Make backup copies of stdin, stdout, stderr as __stdin__, __stdout__,
Guido van Rossum <guido@python.org>
parents: 6550
diff changeset
394 /* Make backup copies for cleanup */
c7454ca7d3ac Make backup copies of stdin, stdout, stderr as __stdin__, __stdout__,
Guido van Rossum <guido@python.org>
parents: 6550
diff changeset
395 PyDict_SetItemString(sysdict, "__stdin__", sysin);
c7454ca7d3ac Make backup copies of stdin, stdout, stderr as __stdin__, __stdout__,
Guido van Rossum <guido@python.org>
parents: 6550
diff changeset
396 PyDict_SetItemString(sysdict, "__stdout__", sysout);
c7454ca7d3ac Make backup copies of stdin, stdout, stderr as __stdin__, __stdout__,
Guido van Rossum <guido@python.org>
parents: 6550
diff changeset
397 PyDict_SetItemString(sysdict, "__stderr__", syserr);
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5863
diff changeset
398 Py_XDECREF(sysin);
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5863
diff changeset
399 Py_XDECREF(sysout);
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5863
diff changeset
400 Py_XDECREF(syserr);
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
401 PyDict_SetItemString(sysdict, "version",
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
402 v = PyString_FromString(Py_GetVersion()));
9964
d44cb87e500d _PySys_Init(): Nailed small memory leak. The stringobject created for
Barry Warsaw <barry@python.org>
parents: 9885
diff changeset
403 Py_XDECREF(v);
9746
9342c60eaac0 Add sys.hexversion, which is an integer encoding the version in hexadecimal.
Guido van Rossum <guido@python.org>
parents: 9328
diff changeset
404 PyDict_SetItemString(sysdict, "hexversion",
9342c60eaac0 Add sys.hexversion, which is an integer encoding the version in hexadecimal.
Guido van Rossum <guido@python.org>
parents: 9328
diff changeset
405 v = PyInt_FromLong(PY_VERSION_HEX));
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
406 Py_XDECREF(v);
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
407 PyDict_SetItemString(sysdict, "copyright",
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
408 v = PyString_FromString(Py_GetCopyright()));
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
409 Py_XDECREF(v);
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
410 PyDict_SetItemString(sysdict, "platform",
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
411 v = PyString_FromString(Py_GetPlatform()));
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
412 Py_XDECREF(v);
5598
1f206c91daee Set sys.executable to full path of python (from argv[0]).
Guido van Rossum <guido@python.org>
parents: 5550
diff changeset
413 PyDict_SetItemString(sysdict, "executable",
1f206c91daee Set sys.executable to full path of python (from argv[0]).
Guido van Rossum <guido@python.org>
parents: 5550
diff changeset
414 v = PyString_FromString(Py_GetProgramFullPath()));
1f206c91daee Set sys.executable to full path of python (from argv[0]).
Guido van Rossum <guido@python.org>
parents: 5550
diff changeset
415 Py_XDECREF(v);
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
416 PyDict_SetItemString(sysdict, "prefix",
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
417 v = PyString_FromString(Py_GetPrefix()));
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
418 Py_XDECREF(v);
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
419 PyDict_SetItemString(sysdict, "exec_prefix",
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
420 v = PyString_FromString(Py_GetExecPrefix()));
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
421 Py_XDECREF(v);
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
422 PyDict_SetItemString(sysdict, "maxint",
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
423 v = PyInt_FromLong(PyInt_GetMax()));
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
424 Py_XDECREF(v);
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
425 PyDict_SetItemString(sysdict, "builtin_module_names",
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 1848
diff changeset
426 v = list_builtin_module_names());
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
427 Py_XDECREF(v);
4126
c80c515999b9 Use MS_DLL_ID as sys.winver
Guido van Rossum <guido@python.org>
parents: 3877
diff changeset
428 #ifdef MS_COREDLL
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
429 PyDict_SetItemString(sysdict, "dllhandle",
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
430 v = PyInt_FromLong((int)PyWin_DLLhModule));
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
431 Py_XDECREF(v);
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
432 PyDict_SetItemString(sysdict, "winver",
6352
b62a7a59414e Get DLL version from a variable.
Guido van Rossum <guido@python.org>
parents: 5955
diff changeset
433 v = PyString_FromString(PyWin_DLLVersionString));
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
434 Py_XDECREF(v);
3490
1dfc519ea3cb Under NT, define sys.dllhandle and sys.winver (Mark H.).
Guido van Rossum <guido@python.org>
parents: 3326
diff changeset
435 #endif
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
436 if (PyErr_Occurred())
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5863
diff changeset
437 return NULL;
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5863
diff changeset
438 return m;
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 83
diff changeset
439 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 83
diff changeset
440
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
441 static PyObject *
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 83
diff changeset
442 makepathobject(path, delim)
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 83
diff changeset
443 char *path;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 83
diff changeset
444 int delim;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 83
diff changeset
445 {
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 83
diff changeset
446 int i, n;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 83
diff changeset
447 char *p;
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
448 PyObject *v, *w;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
449
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 83
diff changeset
450 n = 1;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 83
diff changeset
451 p = path;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 83
diff changeset
452 while ((p = strchr(p, delim)) != NULL) {
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 83
diff changeset
453 n++;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 83
diff changeset
454 p++;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 83
diff changeset
455 }
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
456 v = PyList_New(n);
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 83
diff changeset
457 if (v == NULL)
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 83
diff changeset
458 return NULL;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 83
diff changeset
459 for (i = 0; ; i++) {
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 83
diff changeset
460 p = strchr(path, delim);
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 83
diff changeset
461 if (p == NULL)
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 83
diff changeset
462 p = strchr(path, '\0'); /* End of string */
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
463 w = PyString_FromStringAndSize(path, (int) (p - path));
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 83
diff changeset
464 if (w == NULL) {
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
465 Py_DECREF(v);
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 83
diff changeset
466 return NULL;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 83
diff changeset
467 }
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
468 PyList_SetItem(v, i, w);
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 83
diff changeset
469 if (*p == '\0')
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 83
diff changeset
470 break;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 83
diff changeset
471 path = p+1;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 83
diff changeset
472 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 83
diff changeset
473 return v;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 83
diff changeset
474 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 83
diff changeset
475
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 83
diff changeset
476 void
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
477 PySys_SetPath(path)
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 83
diff changeset
478 char *path;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 83
diff changeset
479 {
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
480 PyObject *v;
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 83
diff changeset
481 if ((v = makepathobject(path, DELIM)) == NULL)
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
482 Py_FatalError("can't create sys.path");
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
483 if (PySys_SetObject("path", v) != 0)
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
484 Py_FatalError("can't assign sys.path");
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
485 Py_DECREF(v);
83
f6bcf3c1d714 Empty all modules' symbol tables, so most circular references are
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
486 }
f6bcf3c1d714 Empty all modules' symbol tables, so most circular references are
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
487
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
488 static PyObject *
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 83
diff changeset
489 makeargvobject(argc, argv)
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 83
diff changeset
490 int argc;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 83
diff changeset
491 char **argv;
83
f6bcf3c1d714 Empty all modules' symbol tables, so most circular references are
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
492 {
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
493 PyObject *av;
596
e18f1fa398a3 Ensure that sys.argv[0] always exists (maybe as empty string).
Guido van Rossum <guido@python.org>
parents: 549
diff changeset
494 if (argc <= 0 || argv == NULL) {
e18f1fa398a3 Ensure that sys.argv[0] always exists (maybe as empty string).
Guido van Rossum <guido@python.org>
parents: 549
diff changeset
495 /* Ensure at least one (empty) argument is seen */
e18f1fa398a3 Ensure that sys.argv[0] always exists (maybe as empty string).
Guido van Rossum <guido@python.org>
parents: 549
diff changeset
496 static char *empty_argv[1] = {""};
e18f1fa398a3 Ensure that sys.argv[0] always exists (maybe as empty string).
Guido van Rossum <guido@python.org>
parents: 549
diff changeset
497 argv = empty_argv;
e18f1fa398a3 Ensure that sys.argv[0] always exists (maybe as empty string).
Guido van Rossum <guido@python.org>
parents: 549
diff changeset
498 argc = 1;
e18f1fa398a3 Ensure that sys.argv[0] always exists (maybe as empty string).
Guido van Rossum <guido@python.org>
parents: 549
diff changeset
499 }
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
500 av = PyList_New(argc);
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 83
diff changeset
501 if (av != NULL) {
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 83
diff changeset
502 int i;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 83
diff changeset
503 for (i = 0; i < argc; i++) {
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
504 PyObject *v = PyString_FromString(argv[i]);
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 83
diff changeset
505 if (v == NULL) {
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
506 Py_DECREF(av);
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 83
diff changeset
507 av = NULL;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 83
diff changeset
508 break;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 83
diff changeset
509 }
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
510 PyList_SetItem(av, i, v);
83
f6bcf3c1d714 Empty all modules' symbol tables, so most circular references are
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
511 }
f6bcf3c1d714 Empty all modules' symbol tables, so most circular references are
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
512 }
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 83
diff changeset
513 return av;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
514 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
515
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
516 void
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
517 PySys_SetArgv(argc, argv)
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 83
diff changeset
518 int argc;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 83
diff changeset
519 char **argv;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
520 {
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
521 PyObject *av = makeargvobject(argc, argv);
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
522 PyObject *path = PySys_GetObject("path");
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 83
diff changeset
523 if (av == NULL)
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
524 Py_FatalError("no mem for sys.argv");
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
525 if (PySys_SetObject("argv", av) != 0)
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
526 Py_FatalError("can't assign sys.argv");
3877
408e374934e5 Always insert script directory in front of sys.path -- if there's no
Guido van Rossum <guido@python.org>
parents: 3806
diff changeset
527 if (path != NULL) {
5340
db673e98f28d Expand one level of symbolic link in sys.argv[0] before inserting its
Guido van Rossum <guido@python.org>
parents: 5047
diff changeset
528 char *argv0 = argv[0];
3877
408e374934e5 Always insert script directory in front of sys.path -- if there's no
Guido van Rossum <guido@python.org>
parents: 3806
diff changeset
529 char *p = NULL;
4256
e225e70fd657 Changes to setpythonpath():
Guido van Rossum <guido@python.org>
parents: 4126
diff changeset
530 int n = 0;
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
531 PyObject *a;
5340
db673e98f28d Expand one level of symbolic link in sys.argv[0] before inserting its
Guido van Rossum <guido@python.org>
parents: 5047
diff changeset
532 #ifdef HAVE_READLINK
db673e98f28d Expand one level of symbolic link in sys.argv[0] before inserting its
Guido van Rossum <guido@python.org>
parents: 5047
diff changeset
533 char link[MAXPATHLEN+1];
db673e98f28d Expand one level of symbolic link in sys.argv[0] before inserting its
Guido van Rossum <guido@python.org>
parents: 5047
diff changeset
534 char argv0copy[2*MAXPATHLEN+1];
db673e98f28d Expand one level of symbolic link in sys.argv[0] before inserting its
Guido van Rossum <guido@python.org>
parents: 5047
diff changeset
535 int nr = 0;
db673e98f28d Expand one level of symbolic link in sys.argv[0] before inserting its
Guido van Rossum <guido@python.org>
parents: 5047
diff changeset
536 if (argc > 0 && argv0 != NULL)
db673e98f28d Expand one level of symbolic link in sys.argv[0] before inserting its
Guido van Rossum <guido@python.org>
parents: 5047
diff changeset
537 nr = readlink(argv0, link, MAXPATHLEN);
db673e98f28d Expand one level of symbolic link in sys.argv[0] before inserting its
Guido van Rossum <guido@python.org>
parents: 5047
diff changeset
538 if (nr > 0) {
db673e98f28d Expand one level of symbolic link in sys.argv[0] before inserting its
Guido van Rossum <guido@python.org>
parents: 5047
diff changeset
539 /* It's a symlink */
db673e98f28d Expand one level of symbolic link in sys.argv[0] before inserting its
Guido van Rossum <guido@python.org>
parents: 5047
diff changeset
540 link[nr] = '\0';
db673e98f28d Expand one level of symbolic link in sys.argv[0] before inserting its
Guido van Rossum <guido@python.org>
parents: 5047
diff changeset
541 if (link[0] == SEP)
db673e98f28d Expand one level of symbolic link in sys.argv[0] before inserting its
Guido van Rossum <guido@python.org>
parents: 5047
diff changeset
542 argv0 = link; /* Link to absolute path */
db673e98f28d Expand one level of symbolic link in sys.argv[0] before inserting its
Guido van Rossum <guido@python.org>
parents: 5047
diff changeset
543 else if (strchr(link, SEP) == NULL)
db673e98f28d Expand one level of symbolic link in sys.argv[0] before inserting its
Guido van Rossum <guido@python.org>
parents: 5047
diff changeset
544 ; /* Link without path */
db673e98f28d Expand one level of symbolic link in sys.argv[0] before inserting its
Guido van Rossum <guido@python.org>
parents: 5047
diff changeset
545 else {
db673e98f28d Expand one level of symbolic link in sys.argv[0] before inserting its
Guido van Rossum <guido@python.org>
parents: 5047
diff changeset
546 /* Must join(dirname(argv0), link) */
db673e98f28d Expand one level of symbolic link in sys.argv[0] before inserting its
Guido van Rossum <guido@python.org>
parents: 5047
diff changeset
547 char *q = strrchr(argv0, SEP);
db673e98f28d Expand one level of symbolic link in sys.argv[0] before inserting its
Guido van Rossum <guido@python.org>
parents: 5047
diff changeset
548 if (q == NULL)
db673e98f28d Expand one level of symbolic link in sys.argv[0] before inserting its
Guido van Rossum <guido@python.org>
parents: 5047
diff changeset
549 argv0 = link; /* argv0 without path */
db673e98f28d Expand one level of symbolic link in sys.argv[0] before inserting its
Guido van Rossum <guido@python.org>
parents: 5047
diff changeset
550 else {
db673e98f28d Expand one level of symbolic link in sys.argv[0] before inserting its
Guido van Rossum <guido@python.org>
parents: 5047
diff changeset
551 /* Must make a copy */
db673e98f28d Expand one level of symbolic link in sys.argv[0] before inserting its
Guido van Rossum <guido@python.org>
parents: 5047
diff changeset
552 strcpy(argv0copy, argv0);
db673e98f28d Expand one level of symbolic link in sys.argv[0] before inserting its
Guido van Rossum <guido@python.org>
parents: 5047
diff changeset
553 q = strrchr(argv0copy, SEP);
db673e98f28d Expand one level of symbolic link in sys.argv[0] before inserting its
Guido van Rossum <guido@python.org>
parents: 5047
diff changeset
554 strcpy(q+1, link);
db673e98f28d Expand one level of symbolic link in sys.argv[0] before inserting its
Guido van Rossum <guido@python.org>
parents: 5047
diff changeset
555 argv0 = argv0copy;
db673e98f28d Expand one level of symbolic link in sys.argv[0] before inserting its
Guido van Rossum <guido@python.org>
parents: 5047
diff changeset
556 }
db673e98f28d Expand one level of symbolic link in sys.argv[0] before inserting its
Guido van Rossum <guido@python.org>
parents: 5047
diff changeset
557 }
db673e98f28d Expand one level of symbolic link in sys.argv[0] before inserting its
Guido van Rossum <guido@python.org>
parents: 5047
diff changeset
558 }
db673e98f28d Expand one level of symbolic link in sys.argv[0] before inserting its
Guido van Rossum <guido@python.org>
parents: 5047
diff changeset
559 #endif /* HAVE_READLINK */
4256
e225e70fd657 Changes to setpythonpath():
Guido van Rossum <guido@python.org>
parents: 4126
diff changeset
560 #if SEP == '\\' /* Special case for MS filename syntax */
5340
db673e98f28d Expand one level of symbolic link in sys.argv[0] before inserting its
Guido van Rossum <guido@python.org>
parents: 5047
diff changeset
561 if (argc > 0 && argv0 != NULL) {
4256
e225e70fd657 Changes to setpythonpath():
Guido van Rossum <guido@python.org>
parents: 4126
diff changeset
562 char *q;
5340
db673e98f28d Expand one level of symbolic link in sys.argv[0] before inserting its
Guido van Rossum <guido@python.org>
parents: 5047
diff changeset
563 p = strrchr(argv0, SEP);
4256
e225e70fd657 Changes to setpythonpath():
Guido van Rossum <guido@python.org>
parents: 4126
diff changeset
564 /* Test for alternate separator */
5340
db673e98f28d Expand one level of symbolic link in sys.argv[0] before inserting its
Guido van Rossum <guido@python.org>
parents: 5047
diff changeset
565 q = strrchr(p ? p : argv0, '/');
4256
e225e70fd657 Changes to setpythonpath():
Guido van Rossum <guido@python.org>
parents: 4126
diff changeset
566 if (q != NULL)
e225e70fd657 Changes to setpythonpath():
Guido van Rossum <guido@python.org>
parents: 4126
diff changeset
567 p = q;
e225e70fd657 Changes to setpythonpath():
Guido van Rossum <guido@python.org>
parents: 4126
diff changeset
568 if (p != NULL) {
5340
db673e98f28d Expand one level of symbolic link in sys.argv[0] before inserting its
Guido van Rossum <guido@python.org>
parents: 5047
diff changeset
569 n = p + 1 - argv0;
4256
e225e70fd657 Changes to setpythonpath():
Guido van Rossum <guido@python.org>
parents: 4126
diff changeset
570 if (n > 1 && p[-1] != ':')
e225e70fd657 Changes to setpythonpath():
Guido van Rossum <guido@python.org>
parents: 4126
diff changeset
571 n--; /* Drop trailing separator */
e225e70fd657 Changes to setpythonpath():
Guido van Rossum <guido@python.org>
parents: 4126
diff changeset
572 }
e225e70fd657 Changes to setpythonpath():
Guido van Rossum <guido@python.org>
parents: 4126
diff changeset
573 }
e225e70fd657 Changes to setpythonpath():
Guido van Rossum <guido@python.org>
parents: 4126
diff changeset
574 #else /* All other filename syntaxes */
5340
db673e98f28d Expand one level of symbolic link in sys.argv[0] before inserting its
Guido van Rossum <guido@python.org>
parents: 5047
diff changeset
575 if (argc > 0 && argv0 != NULL)
db673e98f28d Expand one level of symbolic link in sys.argv[0] before inserting its
Guido van Rossum <guido@python.org>
parents: 5047
diff changeset
576 p = strrchr(argv0, SEP);
4256
e225e70fd657 Changes to setpythonpath():
Guido van Rossum <guido@python.org>
parents: 4126
diff changeset
577 if (p != NULL) {
5340
db673e98f28d Expand one level of symbolic link in sys.argv[0] before inserting its
Guido van Rossum <guido@python.org>
parents: 5047
diff changeset
578 n = p + 1 - argv0;
4256
e225e70fd657 Changes to setpythonpath():
Guido van Rossum <guido@python.org>
parents: 4126
diff changeset
579 #if SEP == '/' /* Special case for Unix filename syntax */
e225e70fd657 Changes to setpythonpath():
Guido van Rossum <guido@python.org>
parents: 4126
diff changeset
580 if (n > 1)
e225e70fd657 Changes to setpythonpath():
Guido van Rossum <guido@python.org>
parents: 4126
diff changeset
581 n--; /* Drop trailing separator */
e225e70fd657 Changes to setpythonpath():
Guido van Rossum <guido@python.org>
parents: 4126
diff changeset
582 #endif /* Unix */
e225e70fd657 Changes to setpythonpath():
Guido van Rossum <guido@python.org>
parents: 4126
diff changeset
583 }
e225e70fd657 Changes to setpythonpath():
Guido van Rossum <guido@python.org>
parents: 4126
diff changeset
584 #endif /* All others */
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
585 a = PyString_FromStringAndSize(argv0, n);
3877
408e374934e5 Always insert script directory in front of sys.path -- if there's no
Guido van Rossum <guido@python.org>
parents: 3806
diff changeset
586 if (a == NULL)
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
587 Py_FatalError("no mem for sys.path insertion");
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
588 if (PyList_Insert(path, 0, a) < 0)
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
589 Py_FatalError("sys.path.insert(0) failed");
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
590 Py_DECREF(a);
3806
93868c7a15c9 As a side effect of calling PySys_SetArgv (setpythonargv), the
Guido van Rossum <guido@python.org>
parents: 3738
diff changeset
591 }
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
592 Py_DECREF(av);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
593 }
8314
f94d5ae642a9 New APIs to write to sys.stdout or sys.stderr using a printf-like interface.
Guido van Rossum <guido@python.org>
parents: 7402
diff changeset
594
f94d5ae642a9 New APIs to write to sys.stdout or sys.stderr using a printf-like interface.
Guido van Rossum <guido@python.org>
parents: 7402
diff changeset
595
f94d5ae642a9 New APIs to write to sys.stdout or sys.stderr using a printf-like interface.
Guido van Rossum <guido@python.org>
parents: 7402
diff changeset
596 /* APIs to write to sys.stdout or sys.stderr using a printf-like interface.
f94d5ae642a9 New APIs to write to sys.stdout or sys.stderr using a printf-like interface.
Guido van Rossum <guido@python.org>
parents: 7402
diff changeset
597 Adapted from code submitted by Just van Rossum.
f94d5ae642a9 New APIs to write to sys.stdout or sys.stderr using a printf-like interface.
Guido van Rossum <guido@python.org>
parents: 7402
diff changeset
598
f94d5ae642a9 New APIs to write to sys.stdout or sys.stderr using a printf-like interface.
Guido van Rossum <guido@python.org>
parents: 7402
diff changeset
599 PySys_WriteStdout(format, ...)
f94d5ae642a9 New APIs to write to sys.stdout or sys.stderr using a printf-like interface.
Guido van Rossum <guido@python.org>
parents: 7402
diff changeset
600 PySys_WriteStderr(format, ...)
f94d5ae642a9 New APIs to write to sys.stdout or sys.stderr using a printf-like interface.
Guido van Rossum <guido@python.org>
parents: 7402
diff changeset
601
f94d5ae642a9 New APIs to write to sys.stdout or sys.stderr using a printf-like interface.
Guido van Rossum <guido@python.org>
parents: 7402
diff changeset
602 The first function writes to sys.stdout; the second to sys.stderr. When
f94d5ae642a9 New APIs to write to sys.stdout or sys.stderr using a printf-like interface.
Guido van Rossum <guido@python.org>
parents: 7402
diff changeset
603 there is a problem, they write to the real (C level) stdout or stderr;
9328
81574e4646b4 Patches for mywrite() by Marc Lemburg: save/restore the error state
Guido van Rossum <guido@python.org>
parents: 8840
diff changeset
604 no exceptions are raised.
8314
f94d5ae642a9 New APIs to write to sys.stdout or sys.stderr using a printf-like interface.
Guido van Rossum <guido@python.org>
parents: 7402
diff changeset
605
f94d5ae642a9 New APIs to write to sys.stdout or sys.stderr using a printf-like interface.
Guido van Rossum <guido@python.org>
parents: 7402
diff changeset
606 Both take a printf-style format string as their first argument followed
f94d5ae642a9 New APIs to write to sys.stdout or sys.stderr using a printf-like interface.
Guido van Rossum <guido@python.org>
parents: 7402
diff changeset
607 by a variable length argument list determined by the format string.
f94d5ae642a9 New APIs to write to sys.stdout or sys.stderr using a printf-like interface.
Guido van Rossum <guido@python.org>
parents: 7402
diff changeset
608
f94d5ae642a9 New APIs to write to sys.stdout or sys.stderr using a printf-like interface.
Guido van Rossum <guido@python.org>
parents: 7402
diff changeset
609 *** WARNING ***
f94d5ae642a9 New APIs to write to sys.stdout or sys.stderr using a printf-like interface.
Guido van Rossum <guido@python.org>
parents: 7402
diff changeset
610
f94d5ae642a9 New APIs to write to sys.stdout or sys.stderr using a printf-like interface.
Guido van Rossum <guido@python.org>
parents: 7402
diff changeset
611 The format should limit the total size of the formatted output string to
f94d5ae642a9 New APIs to write to sys.stdout or sys.stderr using a printf-like interface.
Guido van Rossum <guido@python.org>
parents: 7402
diff changeset
612 1000 bytes. In particular, this means that no unrestricted "%s" formats
f94d5ae642a9 New APIs to write to sys.stdout or sys.stderr using a printf-like interface.
Guido van Rossum <guido@python.org>
parents: 7402
diff changeset
613 should occur; these should be limited using "%.<N>s where <N> is a
f94d5ae642a9 New APIs to write to sys.stdout or sys.stderr using a printf-like interface.
Guido van Rossum <guido@python.org>
parents: 7402
diff changeset
614 decimal number calculated so that <N> plus the maximum size of other
f94d5ae642a9 New APIs to write to sys.stdout or sys.stderr using a printf-like interface.
Guido van Rossum <guido@python.org>
parents: 7402
diff changeset
615 formatted text does not exceed 1000 bytes. Also watch out for "%f",
f94d5ae642a9 New APIs to write to sys.stdout or sys.stderr using a printf-like interface.
Guido van Rossum <guido@python.org>
parents: 7402
diff changeset
616 which can print hundreds of digits for very large numbers.
f94d5ae642a9 New APIs to write to sys.stdout or sys.stderr using a printf-like interface.
Guido van Rossum <guido@python.org>
parents: 7402
diff changeset
617
f94d5ae642a9 New APIs to write to sys.stdout or sys.stderr using a printf-like interface.
Guido van Rossum <guido@python.org>
parents: 7402
diff changeset
618 */
f94d5ae642a9 New APIs to write to sys.stdout or sys.stderr using a printf-like interface.
Guido van Rossum <guido@python.org>
parents: 7402
diff changeset
619
f94d5ae642a9 New APIs to write to sys.stdout or sys.stderr using a printf-like interface.
Guido van Rossum <guido@python.org>
parents: 7402
diff changeset
620 static void
f94d5ae642a9 New APIs to write to sys.stdout or sys.stderr using a printf-like interface.
Guido van Rossum <guido@python.org>
parents: 7402
diff changeset
621 mywrite(name, fp, format, va)
f94d5ae642a9 New APIs to write to sys.stdout or sys.stderr using a printf-like interface.
Guido van Rossum <guido@python.org>
parents: 7402
diff changeset
622 char *name;
f94d5ae642a9 New APIs to write to sys.stdout or sys.stderr using a printf-like interface.
Guido van Rossum <guido@python.org>
parents: 7402
diff changeset
623 FILE *fp;
f94d5ae642a9 New APIs to write to sys.stdout or sys.stderr using a printf-like interface.
Guido van Rossum <guido@python.org>
parents: 7402
diff changeset
624 const char *format;
f94d5ae642a9 New APIs to write to sys.stdout or sys.stderr using a printf-like interface.
Guido van Rossum <guido@python.org>
parents: 7402
diff changeset
625 va_list va;
f94d5ae642a9 New APIs to write to sys.stdout or sys.stderr using a printf-like interface.
Guido van Rossum <guido@python.org>
parents: 7402
diff changeset
626 {
f94d5ae642a9 New APIs to write to sys.stdout or sys.stderr using a printf-like interface.
Guido van Rossum <guido@python.org>
parents: 7402
diff changeset
627 PyObject *file;
9328
81574e4646b4 Patches for mywrite() by Marc Lemburg: save/restore the error state
Guido van Rossum <guido@python.org>
parents: 8840
diff changeset
628 PyObject *error_type, *error_value, *error_traceback;
8314
f94d5ae642a9 New APIs to write to sys.stdout or sys.stderr using a printf-like interface.
Guido van Rossum <guido@python.org>
parents: 7402
diff changeset
629
9328
81574e4646b4 Patches for mywrite() by Marc Lemburg: save/restore the error state
Guido van Rossum <guido@python.org>
parents: 8840
diff changeset
630 PyErr_Fetch(&error_type, &error_value, &error_traceback);
8314
f94d5ae642a9 New APIs to write to sys.stdout or sys.stderr using a printf-like interface.
Guido van Rossum <guido@python.org>
parents: 7402
diff changeset
631 file = PySys_GetObject(name);
f94d5ae642a9 New APIs to write to sys.stdout or sys.stderr using a printf-like interface.
Guido van Rossum <guido@python.org>
parents: 7402
diff changeset
632 if (file == NULL || PyFile_AsFile(file) == fp)
f94d5ae642a9 New APIs to write to sys.stdout or sys.stderr using a printf-like interface.
Guido van Rossum <guido@python.org>
parents: 7402
diff changeset
633 vfprintf(fp, format, va);
f94d5ae642a9 New APIs to write to sys.stdout or sys.stderr using a printf-like interface.
Guido van Rossum <guido@python.org>
parents: 7402
diff changeset
634 else {
f94d5ae642a9 New APIs to write to sys.stdout or sys.stderr using a printf-like interface.
Guido van Rossum <guido@python.org>
parents: 7402
diff changeset
635 char buffer[1001];
9328
81574e4646b4 Patches for mywrite() by Marc Lemburg: save/restore the error state
Guido van Rossum <guido@python.org>
parents: 8840
diff changeset
636 if (vsprintf(buffer, format, va) >= sizeof(buffer))
81574e4646b4 Patches for mywrite() by Marc Lemburg: save/restore the error state
Guido van Rossum <guido@python.org>
parents: 8840
diff changeset
637 Py_FatalError("PySys_WriteStdout/err: buffer overrun");
8314
f94d5ae642a9 New APIs to write to sys.stdout or sys.stderr using a printf-like interface.
Guido van Rossum <guido@python.org>
parents: 7402
diff changeset
638 if (PyFile_WriteString(buffer, file) != 0) {
f94d5ae642a9 New APIs to write to sys.stdout or sys.stderr using a printf-like interface.
Guido van Rossum <guido@python.org>
parents: 7402
diff changeset
639 PyErr_Clear();
f94d5ae642a9 New APIs to write to sys.stdout or sys.stderr using a printf-like interface.
Guido van Rossum <guido@python.org>
parents: 7402
diff changeset
640 fputs(buffer, fp);
f94d5ae642a9 New APIs to write to sys.stdout or sys.stderr using a printf-like interface.
Guido van Rossum <guido@python.org>
parents: 7402
diff changeset
641 }
f94d5ae642a9 New APIs to write to sys.stdout or sys.stderr using a printf-like interface.
Guido van Rossum <guido@python.org>
parents: 7402
diff changeset
642 }
9328
81574e4646b4 Patches for mywrite() by Marc Lemburg: save/restore the error state
Guido van Rossum <guido@python.org>
parents: 8840
diff changeset
643 PyErr_Restore(error_type, error_value, error_traceback);
8314
f94d5ae642a9 New APIs to write to sys.stdout or sys.stderr using a printf-like interface.
Guido van Rossum <guido@python.org>
parents: 7402
diff changeset
644 }
f94d5ae642a9 New APIs to write to sys.stdout or sys.stderr using a printf-like interface.
Guido van Rossum <guido@python.org>
parents: 7402
diff changeset
645
f94d5ae642a9 New APIs to write to sys.stdout or sys.stderr using a printf-like interface.
Guido van Rossum <guido@python.org>
parents: 7402
diff changeset
646 void
f94d5ae642a9 New APIs to write to sys.stdout or sys.stderr using a printf-like interface.
Guido van Rossum <guido@python.org>
parents: 7402
diff changeset
647 #ifdef HAVE_STDARG_PROTOTYPES
f94d5ae642a9 New APIs to write to sys.stdout or sys.stderr using a printf-like interface.
Guido van Rossum <guido@python.org>
parents: 7402
diff changeset
648 PySys_WriteStdout(const char *format, ...)
f94d5ae642a9 New APIs to write to sys.stdout or sys.stderr using a printf-like interface.
Guido van Rossum <guido@python.org>
parents: 7402
diff changeset
649 #else
f94d5ae642a9 New APIs to write to sys.stdout or sys.stderr using a printf-like interface.
Guido van Rossum <guido@python.org>
parents: 7402
diff changeset
650 PySys_WriteStdout(va_alist)
f94d5ae642a9 New APIs to write to sys.stdout or sys.stderr using a printf-like interface.
Guido van Rossum <guido@python.org>
parents: 7402
diff changeset
651 va_dcl
f94d5ae642a9 New APIs to write to sys.stdout or sys.stderr using a printf-like interface.
Guido van Rossum <guido@python.org>
parents: 7402
diff changeset
652 #endif
f94d5ae642a9 New APIs to write to sys.stdout or sys.stderr using a printf-like interface.
Guido van Rossum <guido@python.org>
parents: 7402
diff changeset
653 {
f94d5ae642a9 New APIs to write to sys.stdout or sys.stderr using a printf-like interface.
Guido van Rossum <guido@python.org>
parents: 7402
diff changeset
654 va_list va;
f94d5ae642a9 New APIs to write to sys.stdout or sys.stderr using a printf-like interface.
Guido van Rossum <guido@python.org>
parents: 7402
diff changeset
655
f94d5ae642a9 New APIs to write to sys.stdout or sys.stderr using a printf-like interface.
Guido van Rossum <guido@python.org>
parents: 7402
diff changeset
656 #ifdef HAVE_STDARG_PROTOTYPES
f94d5ae642a9 New APIs to write to sys.stdout or sys.stderr using a printf-like interface.
Guido van Rossum <guido@python.org>
parents: 7402
diff changeset
657 va_start(va, format);
f94d5ae642a9 New APIs to write to sys.stdout or sys.stderr using a printf-like interface.
Guido van Rossum <guido@python.org>
parents: 7402
diff changeset
658 #else
f94d5ae642a9 New APIs to write to sys.stdout or sys.stderr using a printf-like interface.
Guido van Rossum <guido@python.org>
parents: 7402
diff changeset
659 char *format;
f94d5ae642a9 New APIs to write to sys.stdout or sys.stderr using a printf-like interface.
Guido van Rossum <guido@python.org>
parents: 7402
diff changeset
660 va_start(va);
f94d5ae642a9 New APIs to write to sys.stdout or sys.stderr using a printf-like interface.
Guido van Rossum <guido@python.org>
parents: 7402
diff changeset
661 format = va_arg(va, char *);
f94d5ae642a9 New APIs to write to sys.stdout or sys.stderr using a printf-like interface.
Guido van Rossum <guido@python.org>
parents: 7402
diff changeset
662 #endif
f94d5ae642a9 New APIs to write to sys.stdout or sys.stderr using a printf-like interface.
Guido van Rossum <guido@python.org>
parents: 7402
diff changeset
663 mywrite("stdout", stdout, format, va);
f94d5ae642a9 New APIs to write to sys.stdout or sys.stderr using a printf-like interface.
Guido van Rossum <guido@python.org>
parents: 7402
diff changeset
664 va_end(va);
f94d5ae642a9 New APIs to write to sys.stdout or sys.stderr using a printf-like interface.
Guido van Rossum <guido@python.org>
parents: 7402
diff changeset
665 }
f94d5ae642a9 New APIs to write to sys.stdout or sys.stderr using a printf-like interface.
Guido van Rossum <guido@python.org>
parents: 7402
diff changeset
666
f94d5ae642a9 New APIs to write to sys.stdout or sys.stderr using a printf-like interface.
Guido van Rossum <guido@python.org>
parents: 7402
diff changeset
667 void
f94d5ae642a9 New APIs to write to sys.stdout or sys.stderr using a printf-like interface.
Guido van Rossum <guido@python.org>
parents: 7402
diff changeset
668 #ifdef HAVE_STDARG_PROTOTYPES
f94d5ae642a9 New APIs to write to sys.stdout or sys.stderr using a printf-like interface.
Guido van Rossum <guido@python.org>
parents: 7402
diff changeset
669 PySys_WriteStderr(const char *format, ...)
f94d5ae642a9 New APIs to write to sys.stdout or sys.stderr using a printf-like interface.
Guido van Rossum <guido@python.org>
parents: 7402
diff changeset
670 #else
f94d5ae642a9 New APIs to write to sys.stdout or sys.stderr using a printf-like interface.
Guido van Rossum <guido@python.org>
parents: 7402
diff changeset
671 PySys_WriteStderr(va_alist)
f94d5ae642a9 New APIs to write to sys.stdout or sys.stderr using a printf-like interface.
Guido van Rossum <guido@python.org>
parents: 7402
diff changeset
672 va_dcl
f94d5ae642a9 New APIs to write to sys.stdout or sys.stderr using a printf-like interface.
Guido van Rossum <guido@python.org>
parents: 7402
diff changeset
673 #endif
f94d5ae642a9 New APIs to write to sys.stdout or sys.stderr using a printf-like interface.
Guido van Rossum <guido@python.org>
parents: 7402
diff changeset
674 {
f94d5ae642a9 New APIs to write to sys.stdout or sys.stderr using a printf-like interface.
Guido van Rossum <guido@python.org>
parents: 7402
diff changeset
675 va_list va;
f94d5ae642a9 New APIs to write to sys.stdout or sys.stderr using a printf-like interface.
Guido van Rossum <guido@python.org>
parents: 7402
diff changeset
676
f94d5ae642a9 New APIs to write to sys.stdout or sys.stderr using a printf-like interface.
Guido van Rossum <guido@python.org>
parents: 7402
diff changeset
677 #ifdef HAVE_STDARG_PROTOTYPES
f94d5ae642a9 New APIs to write to sys.stdout or sys.stderr using a printf-like interface.
Guido van Rossum <guido@python.org>
parents: 7402
diff changeset
678 va_start(va, format);
f94d5ae642a9 New APIs to write to sys.stdout or sys.stderr using a printf-like interface.
Guido van Rossum <guido@python.org>
parents: 7402
diff changeset
679 #else
f94d5ae642a9 New APIs to write to sys.stdout or sys.stderr using a printf-like interface.
Guido van Rossum <guido@python.org>
parents: 7402
diff changeset
680 char *format;
f94d5ae642a9 New APIs to write to sys.stdout or sys.stderr using a printf-like interface.
Guido van Rossum <guido@python.org>
parents: 7402
diff changeset
681 va_start(va);
f94d5ae642a9 New APIs to write to sys.stdout or sys.stderr using a printf-like interface.
Guido van Rossum <guido@python.org>
parents: 7402
diff changeset
682 format = va_arg(va, char *);
f94d5ae642a9 New APIs to write to sys.stdout or sys.stderr using a printf-like interface.
Guido van Rossum <guido@python.org>
parents: 7402
diff changeset
683 #endif
f94d5ae642a9 New APIs to write to sys.stdout or sys.stderr using a printf-like interface.
Guido van Rossum <guido@python.org>
parents: 7402
diff changeset
684 mywrite("stderr", stderr, format, va);
f94d5ae642a9 New APIs to write to sys.stdout or sys.stderr using a printf-like interface.
Guido van Rossum <guido@python.org>
parents: 7402
diff changeset
685 va_end(va);
f94d5ae642a9 New APIs to write to sys.stdout or sys.stderr using a printf-like interface.
Guido van Rossum <guido@python.org>
parents: 7402
diff changeset
686 }