annotate Python/sysmodule.c @ 5758:bfe9ba74c19e legacy-trunk

Fix problem discovered by Greg McFarlane: when an imported module replaces its own entry in sys.module, reference count errors ensue; even if there is no reference count problem, it would be preferable for the import to yield the new thing in sys.modules anyway (if only because that's what later imports will yield). This opens the road to an official hack to implement a __getattr__ like feature for modules: stick an instance in sys.modules[__name__].
author Guido van Rossum <guido@python.org>
date Thu, 10 Jul 1997 18:00:45 +0000
parents 1f206c91daee
children 0ac2c747053f
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
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
55 static PyObject *sysdict;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
56
4291
1427d61f1307 Rationalized MS ifdefs
Guido van Rossum <guido@python.org>
parents: 4256
diff changeset
57 #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
58 extern void *PyWin_DLLhModule;
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 {
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
65 return PyDict_GetItemString(sysdict, name);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
66 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
67
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
68 FILE *
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
69 PySys_GetFile(name, def)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
70 char *name;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
71 FILE *def;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
72 {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
73 FILE *fp = NULL;
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
74 PyObject *v = PySys_GetObject(name);
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
75 if (v != NULL && PyFile_Check(v))
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
76 fp = PyFile_AsFile(v);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
77 if (fp == NULL)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
78 fp = def;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
79 return fp;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
80 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
81
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
82 int
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
83 PySys_SetObject(name, v)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
84 char *name;
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
85 PyObject *v;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
86 {
631
f5a2bc39e10e sysset(name, NULL) does nothing if sys.name is undefined
Guido van Rossum <guido@python.org>
parents: 596
diff changeset
87 if (v == NULL) {
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
88 if (PyDict_GetItemString(sysdict, name) == NULL)
631
f5a2bc39e10e sysset(name, NULL) does nothing if sys.name is undefined
Guido van Rossum <guido@python.org>
parents: 596
diff changeset
89 return 0;
f5a2bc39e10e sysset(name, NULL) does nothing if sys.name is undefined
Guido van Rossum <guido@python.org>
parents: 596
diff changeset
90 else
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
91 return PyDict_DelItemString(sysdict, name);
631
f5a2bc39e10e sysset(name, NULL) does nothing if sys.name is undefined
Guido van Rossum <guido@python.org>
parents: 596
diff changeset
92 }
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
93 else
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
94 return PyDict_SetItemString(sysdict, name, v);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
95 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
96
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
97 static PyObject *
5405
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents: 5377
diff changeset
98 sys_exc_info(self, args)
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents: 5377
diff changeset
99 PyObject *self;
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents: 5377
diff changeset
100 PyObject *args;
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents: 5377
diff changeset
101 {
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents: 5377
diff changeset
102 PyThreadState *tstate;
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents: 5377
diff changeset
103 if (!PyArg_Parse(args, ""))
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents: 5377
diff changeset
104 return NULL;
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents: 5377
diff changeset
105 tstate = PyThreadState_Get();
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents: 5377
diff changeset
106 if (tstate == NULL)
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents: 5377
diff changeset
107 Py_FatalError("sys.exc_info(): no thread state");
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents: 5377
diff changeset
108 return Py_BuildValue(
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents: 5377
diff changeset
109 "(OOO)",
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents: 5377
diff changeset
110 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
111 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
112 tstate->exc_traceback != NULL ?
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents: 5377
diff changeset
113 tstate->exc_traceback : Py_None);
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents: 5377
diff changeset
114 }
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents: 5377
diff changeset
115
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents: 5377
diff changeset
116 static PyObject *
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
117 sys_exit(self, args)
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
118 PyObject *self;
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
119 PyObject *args;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
120 {
549
4efff6cea90f Raise SystemExit instead of calling goaway().
Guido van Rossum <guido@python.org>
parents: 476
diff changeset
121 /* Raise SystemExit so callers may catch it or clean up. */
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
122 PyErr_SetObject(PyExc_SystemExit, args);
549
4efff6cea90f Raise SystemExit instead of calling goaway().
Guido van Rossum <guido@python.org>
parents: 476
diff changeset
123 return NULL;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
124 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
125
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
126 static PyObject *
710
bc5c77fa8006 Added settrace() and setprofile().
Guido van Rossum <guido@python.org>
parents: 631
diff changeset
127 sys_settrace(self, args)
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
128 PyObject *self;
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
129 PyObject *args;
710
bc5c77fa8006 Added settrace() and setprofile().
Guido van Rossum <guido@python.org>
parents: 631
diff changeset
130 {
5405
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents: 5377
diff changeset
131 PyThreadState *tstate = PyThreadState_Get();
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
132 if (args == Py_None)
710
bc5c77fa8006 Added settrace() and setprofile().
Guido van Rossum <guido@python.org>
parents: 631
diff changeset
133 args = NULL;
788
ba4ec13c0f9f Fix reference counts of sys_trace and sys_profile
Guido van Rossum <guido@python.org>
parents: 710
diff changeset
134 else
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
135 Py_XINCREF(args);
5405
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents: 5377
diff changeset
136 Py_XDECREF(tstate->sys_tracefunc);
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents: 5377
diff changeset
137 tstate->sys_tracefunc = args;
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
138 Py_INCREF(Py_None);
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
139 return Py_None;
710
bc5c77fa8006 Added settrace() and setprofile().
Guido van Rossum <guido@python.org>
parents: 631
diff changeset
140 }
bc5c77fa8006 Added settrace() and setprofile().
Guido van Rossum <guido@python.org>
parents: 631
diff changeset
141
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
142 static PyObject *
710
bc5c77fa8006 Added settrace() and setprofile().
Guido van Rossum <guido@python.org>
parents: 631
diff changeset
143 sys_setprofile(self, args)
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
144 PyObject *self;
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
145 PyObject *args;
710
bc5c77fa8006 Added settrace() and setprofile().
Guido van Rossum <guido@python.org>
parents: 631
diff changeset
146 {
5405
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents: 5377
diff changeset
147 PyThreadState *tstate = PyThreadState_Get();
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
148 if (args == Py_None)
710
bc5c77fa8006 Added settrace() and setprofile().
Guido van Rossum <guido@python.org>
parents: 631
diff changeset
149 args = NULL;
788
ba4ec13c0f9f Fix reference counts of sys_trace and sys_profile
Guido van Rossum <guido@python.org>
parents: 710
diff changeset
150 else
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
151 Py_XINCREF(args);
5405
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents: 5377
diff changeset
152 Py_XDECREF(tstate->sys_profilefunc);
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents: 5377
diff changeset
153 tstate->sys_profilefunc = args;
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
154 Py_INCREF(Py_None);
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
155 return Py_None;
710
bc5c77fa8006 Added settrace() and setprofile().
Guido van Rossum <guido@python.org>
parents: 631
diff changeset
156 }
bc5c77fa8006 Added settrace() and setprofile().
Guido van Rossum <guido@python.org>
parents: 631
diff changeset
157
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
158 static PyObject *
2093
e8cafb2b743a sys.check_interval=x -> sys.setcheckinterval(x)
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
159 sys_setcheckinterval(self, args)
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
160 PyObject *self;
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
161 PyObject *args;
2093
e8cafb2b743a sys.check_interval=x -> sys.setcheckinterval(x)
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
162 {
5405
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents: 5377
diff changeset
163 PyThreadState *tstate = PyThreadState_Get();
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents: 5377
diff changeset
164 if (!PyArg_ParseTuple(args, "i", &tstate->sys_checkinterval))
2093
e8cafb2b743a sys.check_interval=x -> sys.setcheckinterval(x)
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
165 return NULL;
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
166 Py_INCREF(Py_None);
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
167 return Py_None;
2093
e8cafb2b743a sys.check_interval=x -> sys.setcheckinterval(x)
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
168 }
e8cafb2b743a sys.check_interval=x -> sys.setcheckinterval(x)
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
169
1013
a1ce5a949828 Add an optional interface to turn malloc debugging on and off.
Guido van Rossum <guido@python.org>
parents: 901
diff changeset
170 #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
171 /* 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
172 #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
173
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
174 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
175 sys_mdebug(self, args)
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
176 PyObject *self;
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
177 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
178 {
a1ce5a949828 Add an optional interface to turn malloc debugging on and off.
Guido van Rossum <guido@python.org>
parents: 901
diff changeset
179 int flag;
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
180 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
181 return NULL;
a1ce5a949828 Add an optional interface to turn malloc debugging on and off.
Guido van Rossum <guido@python.org>
parents: 901
diff changeset
182 mallopt(M_DEBUG, flag);
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
183 Py_INCREF(Py_None);
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
184 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
185 }
a1ce5a949828 Add an optional interface to turn malloc debugging on and off.
Guido van Rossum <guido@python.org>
parents: 901
diff changeset
186 #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
187
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
188 static PyObject *
3024
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2810
diff changeset
189 sys_getrefcount(self, args)
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
190 PyObject *self;
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
191 PyObject *args;
3024
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2810
diff changeset
192 {
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
193 PyObject *arg;
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
194 if (!PyArg_Parse(args, "O", &arg))
3024
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2810
diff changeset
195 return NULL;
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
196 return PyInt_FromLong((long) arg->ob_refcnt);
3024
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2810
diff changeset
197 }
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2810
diff changeset
198
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2810
diff changeset
199 #ifdef COUNT_ALLOCS
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2810
diff changeset
200 static PyObject *
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2810
diff changeset
201 sys_getcounts(self, args)
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2810
diff changeset
202 PyObject *self, *args;
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2810
diff changeset
203 {
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2810
diff changeset
204 extern PyObject *get_counts Py_PROTO((void));
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2810
diff changeset
205
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2810
diff changeset
206 if (!PyArg_Parse(args, ""))
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2810
diff changeset
207 return NULL;
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2810
diff changeset
208 return get_counts();
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2810
diff changeset
209 }
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2810
diff changeset
210 #endif
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2810
diff changeset
211
3585
4ed09a3c3201 TRACE_REFS -> Py_TRACE_REFS
Guido van Rossum <guido@python.org>
parents: 3490
diff changeset
212 #ifdef Py_TRACE_REFS
3609
0b0c4ed1936a rename printrefs, getobjects to _Py_ prefix
Guido van Rossum <guido@python.org>
parents: 3585
diff changeset
213 /* 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
214 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
215 #endif
3609
0b0c4ed1936a rename printrefs, getobjects to _Py_ prefix
Guido van Rossum <guido@python.org>
parents: 3585
diff changeset
216
5047
7a9cac7cb910 Added optional interface for dynamic execution profile (to be gathered
Guido van Rossum <guido@python.org>
parents: 4904
diff changeset
217 #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
218 /* 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
219 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
220 #endif
7a9cac7cb910 Added optional interface for dynamic execution profile (to be gathered
Guido van Rossum <guido@python.org>
parents: 4904
diff changeset
221
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
222 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
223 /* Might as well keep this in alphabetic order */
5405
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents: 5377
diff changeset
224 {"exc_info", sys_exc_info, 0},
2421
6eda642f19d0 add explicit 0 flags for methodlist
Guido van Rossum <guido@python.org>
parents: 2093
diff changeset
225 {"exit", sys_exit, 0},
3024
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2810
diff changeset
226 #ifdef COUNT_ALLOCS
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2810
diff changeset
227 {"getcounts", sys_getcounts, 0},
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2810
diff changeset
228 #endif
5047
7a9cac7cb910 Added optional interface for dynamic execution profile (to be gathered
Guido van Rossum <guido@python.org>
parents: 4904
diff changeset
229 #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
230 {"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
231 #endif
3585
4ed09a3c3201 TRACE_REFS -> Py_TRACE_REFS
Guido van Rossum <guido@python.org>
parents: 3490
diff changeset
232 #ifdef Py_TRACE_REFS
3609
0b0c4ed1936a rename printrefs, getobjects to _Py_ prefix
Guido van Rossum <guido@python.org>
parents: 3585
diff changeset
233 {"getobjects", _Py_GetObjects, 1},
3024
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2810
diff changeset
234 #endif
5047
7a9cac7cb910 Added optional interface for dynamic execution profile (to be gathered
Guido van Rossum <guido@python.org>
parents: 4904
diff changeset
235 {"getrefcount", sys_getrefcount, 0},
1013
a1ce5a949828 Add an optional interface to turn malloc debugging on and off.
Guido van Rossum <guido@python.org>
parents: 901
diff changeset
236 #ifdef USE_MALLOPT
2421
6eda642f19d0 add explicit 0 flags for methodlist
Guido van Rossum <guido@python.org>
parents: 2093
diff changeset
237 {"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
238 #endif
5047
7a9cac7cb910 Added optional interface for dynamic execution profile (to be gathered
Guido van Rossum <guido@python.org>
parents: 4904
diff changeset
239 {"setcheckinterval", sys_setcheckinterval, 1},
2421
6eda642f19d0 add explicit 0 flags for methodlist
Guido van Rossum <guido@python.org>
parents: 2093
diff changeset
240 {"setprofile", sys_setprofile, 0},
6eda642f19d0 add explicit 0 flags for methodlist
Guido van Rossum <guido@python.org>
parents: 2093
diff changeset
241 {"settrace", sys_settrace, 0},
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 83
diff changeset
242 {NULL, NULL} /* sentinel */
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 83
diff changeset
243 };
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 83
diff changeset
244
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
245 static PyObject *sysin, *sysout, *syserr;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
246
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
247 static PyObject *
1198
2937897a7d8b * Added Fixcprt.py: script to fix copyright message.
Guido van Rossum <guido@python.org>
parents: 1073
diff changeset
248 list_builtin_module_names()
2937897a7d8b * Added Fixcprt.py: script to fix copyright message.
Guido van Rossum <guido@python.org>
parents: 1073
diff changeset
249 {
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
250 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
251 int i;
2937897a7d8b * Added Fixcprt.py: script to fix copyright message.
Guido van Rossum <guido@python.org>
parents: 1073
diff changeset
252 if (list == NULL)
2937897a7d8b * Added Fixcprt.py: script to fix copyright message.
Guido van Rossum <guido@python.org>
parents: 1073
diff changeset
253 return NULL;
5377
09597a423227 Oops, forgot one: inittab.
Guido van Rossum <guido@python.org>
parents: 5372
diff changeset
254 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
255 PyObject *name = PyString_FromString(
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents: 5377
diff changeset
256 _PyImport_Inittab[i].name);
1198
2937897a7d8b * Added Fixcprt.py: script to fix copyright message.
Guido van Rossum <guido@python.org>
parents: 1073
diff changeset
257 if (name == NULL)
2937897a7d8b * Added Fixcprt.py: script to fix copyright message.
Guido van Rossum <guido@python.org>
parents: 1073
diff changeset
258 break;
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
259 PyList_Append(list, name);
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
260 Py_DECREF(name);
1198
2937897a7d8b * Added Fixcprt.py: script to fix copyright message.
Guido van Rossum <guido@python.org>
parents: 1073
diff changeset
261 }
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
262 if (PyList_Sort(list) != 0) {
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
263 Py_DECREF(list);
1848
b724599401ff Merge back to main trunk
Guido van Rossum <guido@python.org>
parents: 1198
diff changeset
264 list = NULL;
b724599401ff Merge back to main trunk
Guido van Rossum <guido@python.org>
parents: 1198
diff changeset
265 }
4904
5dc20a9e0506 Make builtin_module_names a tuple instead of a list.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
266 if (list) {
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
267 PyObject *v = PyList_AsTuple(list);
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
268 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
269 list = v;
5dc20a9e0506 Make builtin_module_names a tuple instead of a list.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
270 }
1198
2937897a7d8b * Added Fixcprt.py: script to fix copyright message.
Guido van Rossum <guido@python.org>
parents: 1073
diff changeset
271 return list;
2937897a7d8b * Added Fixcprt.py: script to fix copyright message.
Guido van Rossum <guido@python.org>
parents: 1073
diff changeset
272 }
2937897a7d8b * Added Fixcprt.py: script to fix copyright message.
Guido van Rossum <guido@python.org>
parents: 1073
diff changeset
273
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
274 void
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
275 PySys_Init()
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
276 {
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
277 extern long PyInt_GetMax Py_PROTO((void));
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
278 extern char *Py_GetVersion Py_PROTO((void));
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
279 extern char *Py_GetCopyright Py_PROTO((void));
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
280 extern char *Py_GetPlatform Py_PROTO((void));
5598
1f206c91daee Set sys.executable to full path of python (from argv[0]).
Guido van Rossum <guido@python.org>
parents: 5550
diff changeset
281 extern char *Py_GetProgramFullPath Py_PROTO((void));
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
282 extern char *Py_GetPrefix Py_PROTO((void));
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
283 extern char *Py_GetExecPrefix Py_PROTO((void));
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
284 extern int fclose Py_PROTO((FILE *));
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
285 PyObject *m = Py_InitModule("sys", sys_methods);
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
286 PyObject *v;
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
287 sysdict = PyModule_GetDict(m);
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
288 Py_INCREF(sysdict);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
289 /* NB keep an extra ref to the std files to avoid closing them
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
290 when the user deletes them */
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
291 sysin = PyFile_FromFile(stdin, "<stdin>", "r", fclose);
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
292 sysout = PyFile_FromFile(stdout, "<stdout>", "w", fclose);
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
293 syserr = PyFile_FromFile(stderr, "<stderr>", "w", fclose);
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
294 if (PyErr_Occurred())
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
295 Py_FatalError("can't initialize sys.std{in,out,err}");
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
296 PyDict_SetItemString(sysdict, "stdin", sysin);
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
297 PyDict_SetItemString(sysdict, "stdout", sysout);
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
298 PyDict_SetItemString(sysdict, "stderr", syserr);
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
299 PyDict_SetItemString(sysdict, "version",
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
300 v = PyString_FromString(Py_GetVersion()));
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
301 Py_XDECREF(v);
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
302 PyDict_SetItemString(sysdict, "copyright",
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
303 v = PyString_FromString(Py_GetCopyright()));
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
304 Py_XDECREF(v);
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
305 PyDict_SetItemString(sysdict, "platform",
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
306 v = PyString_FromString(Py_GetPlatform()));
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
307 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
308 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
309 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
310 Py_XDECREF(v);
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
311 PyDict_SetItemString(sysdict, "prefix",
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
312 v = PyString_FromString(Py_GetPrefix()));
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
313 Py_XDECREF(v);
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
314 PyDict_SetItemString(sysdict, "exec_prefix",
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
315 v = PyString_FromString(Py_GetExecPrefix()));
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
316 Py_XDECREF(v);
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
317 PyDict_SetItemString(sysdict, "maxint",
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
318 v = PyInt_FromLong(PyInt_GetMax()));
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
319 Py_XDECREF(v);
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
320 PyDict_SetItemString(sysdict, "modules", PyImport_GetModuleDict());
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
321 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
322 v = list_builtin_module_names());
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
323 Py_XDECREF(v);
4126
c80c515999b9 Use MS_DLL_ID as sys.winver
Guido van Rossum <guido@python.org>
parents: 3877
diff changeset
324 #ifdef MS_COREDLL
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
325 PyDict_SetItemString(sysdict, "dllhandle",
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
326 v = PyInt_FromLong((int)PyWin_DLLhModule));
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
327 Py_XDECREF(v);
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
328 PyDict_SetItemString(sysdict, "winver",
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
329 v = PyString_FromString(MS_DLL_ID));
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
330 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
331 #endif
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
332 if (PyErr_Occurred())
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
333 Py_FatalError("can't insert sys.* objects in sys dict");
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 83
diff changeset
334 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 83
diff changeset
335
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
336 static PyObject *
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 83
diff changeset
337 makepathobject(path, delim)
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 83
diff changeset
338 char *path;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 83
diff changeset
339 int delim;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 83
diff changeset
340 {
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 83
diff changeset
341 int i, n;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 83
diff changeset
342 char *p;
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
343 PyObject *v, *w;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
344
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 83
diff changeset
345 n = 1;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 83
diff changeset
346 p = path;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 83
diff changeset
347 while ((p = strchr(p, delim)) != NULL) {
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 83
diff changeset
348 n++;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 83
diff changeset
349 p++;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 83
diff changeset
350 }
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
351 v = PyList_New(n);
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 83
diff changeset
352 if (v == NULL)
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 83
diff changeset
353 return NULL;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 83
diff changeset
354 for (i = 0; ; i++) {
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 83
diff changeset
355 p = strchr(path, delim);
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 83
diff changeset
356 if (p == NULL)
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 83
diff changeset
357 p = strchr(path, '\0'); /* End of string */
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
358 w = PyString_FromStringAndSize(path, (int) (p - path));
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 83
diff changeset
359 if (w == NULL) {
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
360 Py_DECREF(v);
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 83
diff changeset
361 return NULL;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 83
diff changeset
362 }
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
363 PyList_SetItem(v, i, w);
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 83
diff changeset
364 if (*p == '\0')
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 83
diff changeset
365 break;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 83
diff changeset
366 path = p+1;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 83
diff changeset
367 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 83
diff changeset
368 return v;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 83
diff changeset
369 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 83
diff changeset
370
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 83
diff changeset
371 void
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
372 PySys_SetPath(path)
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 83
diff changeset
373 char *path;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 83
diff changeset
374 {
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
375 PyObject *v;
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 83
diff changeset
376 if ((v = makepathobject(path, DELIM)) == NULL)
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
377 Py_FatalError("can't create sys.path");
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
378 if (PySys_SetObject("path", v) != 0)
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
379 Py_FatalError("can't assign sys.path");
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
380 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
381 }
f6bcf3c1d714 Empty all modules' symbol tables, so most circular references are
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
382
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
383 static PyObject *
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 83
diff changeset
384 makeargvobject(argc, argv)
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 83
diff changeset
385 int argc;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 83
diff changeset
386 char **argv;
83
f6bcf3c1d714 Empty all modules' symbol tables, so most circular references are
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
387 {
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
388 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
389 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
390 /* 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
391 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
392 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
393 argc = 1;
e18f1fa398a3 Ensure that sys.argv[0] always exists (maybe as empty string).
Guido van Rossum <guido@python.org>
parents: 549
diff changeset
394 }
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
395 av = PyList_New(argc);
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 83
diff changeset
396 if (av != NULL) {
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 83
diff changeset
397 int i;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 83
diff changeset
398 for (i = 0; i < argc; i++) {
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
399 PyObject *v = PyString_FromString(argv[i]);
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 83
diff changeset
400 if (v == NULL) {
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
401 Py_DECREF(av);
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 83
diff changeset
402 av = NULL;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 83
diff changeset
403 break;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 83
diff changeset
404 }
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
405 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
406 }
f6bcf3c1d714 Empty all modules' symbol tables, so most circular references are
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
407 }
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 83
diff changeset
408 return av;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
409 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
410
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
411 void
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
412 PySys_SetArgv(argc, argv)
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 83
diff changeset
413 int argc;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 83
diff changeset
414 char **argv;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
415 {
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
416 PyObject *av = makeargvobject(argc, argv);
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
417 PyObject *path = PySys_GetObject("path");
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 83
diff changeset
418 if (av == NULL)
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
419 Py_FatalError("no mem for sys.argv");
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
420 if (PySys_SetObject("argv", av) != 0)
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
421 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
422 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
423 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
424 char *p = NULL;
4256
e225e70fd657 Changes to setpythonpath():
Guido van Rossum <guido@python.org>
parents: 4126
diff changeset
425 int n = 0;
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
426 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
427 #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
428 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
429 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
430 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
431 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
432 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
433 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
434 /* 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
435 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
436 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
437 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
438 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
439 ; /* 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
440 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
441 /* 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
442 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
443 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
444 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
445 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
446 /* 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
447 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
448 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
449 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
450 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
451 }
db673e98f28d Expand one level of symbolic link in sys.argv[0] before inserting its
Guido van Rossum <guido@python.org>
parents: 5047
diff changeset
452 }
db673e98f28d Expand one level of symbolic link in sys.argv[0] before inserting its
Guido van Rossum <guido@python.org>
parents: 5047
diff changeset
453 }
db673e98f28d Expand one level of symbolic link in sys.argv[0] before inserting its
Guido van Rossum <guido@python.org>
parents: 5047
diff changeset
454 #endif /* HAVE_READLINK */
4256
e225e70fd657 Changes to setpythonpath():
Guido van Rossum <guido@python.org>
parents: 4126
diff changeset
455 #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
456 if (argc > 0 && argv0 != NULL) {
4256
e225e70fd657 Changes to setpythonpath():
Guido van Rossum <guido@python.org>
parents: 4126
diff changeset
457 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
458 p = strrchr(argv0, SEP);
4256
e225e70fd657 Changes to setpythonpath():
Guido van Rossum <guido@python.org>
parents: 4126
diff changeset
459 /* 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
460 q = strrchr(p ? p : argv0, '/');
4256
e225e70fd657 Changes to setpythonpath():
Guido van Rossum <guido@python.org>
parents: 4126
diff changeset
461 if (q != NULL)
e225e70fd657 Changes to setpythonpath():
Guido van Rossum <guido@python.org>
parents: 4126
diff changeset
462 p = q;
e225e70fd657 Changes to setpythonpath():
Guido van Rossum <guido@python.org>
parents: 4126
diff changeset
463 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
464 n = p + 1 - argv0;
4256
e225e70fd657 Changes to setpythonpath():
Guido van Rossum <guido@python.org>
parents: 4126
diff changeset
465 if (n > 1 && p[-1] != ':')
e225e70fd657 Changes to setpythonpath():
Guido van Rossum <guido@python.org>
parents: 4126
diff changeset
466 n--; /* Drop trailing separator */
e225e70fd657 Changes to setpythonpath():
Guido van Rossum <guido@python.org>
parents: 4126
diff changeset
467 }
e225e70fd657 Changes to setpythonpath():
Guido van Rossum <guido@python.org>
parents: 4126
diff changeset
468 }
e225e70fd657 Changes to setpythonpath():
Guido van Rossum <guido@python.org>
parents: 4126
diff changeset
469 #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
470 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
471 p = strrchr(argv0, SEP);
4256
e225e70fd657 Changes to setpythonpath():
Guido van Rossum <guido@python.org>
parents: 4126
diff changeset
472 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
473 n = p + 1 - argv0;
4256
e225e70fd657 Changes to setpythonpath():
Guido van Rossum <guido@python.org>
parents: 4126
diff changeset
474 #if SEP == '/' /* Special case for Unix filename syntax */
e225e70fd657 Changes to setpythonpath():
Guido van Rossum <guido@python.org>
parents: 4126
diff changeset
475 if (n > 1)
e225e70fd657 Changes to setpythonpath():
Guido van Rossum <guido@python.org>
parents: 4126
diff changeset
476 n--; /* Drop trailing separator */
e225e70fd657 Changes to setpythonpath():
Guido van Rossum <guido@python.org>
parents: 4126
diff changeset
477 #endif /* Unix */
e225e70fd657 Changes to setpythonpath():
Guido van Rossum <guido@python.org>
parents: 4126
diff changeset
478 }
e225e70fd657 Changes to setpythonpath():
Guido van Rossum <guido@python.org>
parents: 4126
diff changeset
479 #endif /* All others */
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
480 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
481 if (a == NULL)
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
482 Py_FatalError("no mem for sys.path insertion");
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
483 if (PyList_Insert(path, 0, a) < 0)
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
484 Py_FatalError("sys.path.insert(0) failed");
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
485 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
486 }
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5340
diff changeset
487 Py_DECREF(av);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
488 }