Left: | ||
Right: |
LEFT | RIGHT |
---|---|
1 | 1 |
2 /* System module */ | 2 /* System module */ |
3 | 3 |
4 /* | 4 /* |
5 Various bits of information used by the interpreter are collected in | 5 Various bits of information used by the interpreter are collected in |
6 module 'sys'. | 6 module 'sys'. |
7 Function member: | 7 Function member: |
8 - exit(sts): raise SystemExit | 8 - exit(sts): raise SystemExit |
9 Data members: | 9 Data members: |
10 - stdin, stdout, stderr: standard file objects | 10 - stdin, stdout, stderr: standard file objects |
(...skipping 27 matching lines...) Expand all Loading... | |
38 #endif | 38 #endif |
39 | 39 |
40 #ifdef HAVE_LANGINFO_H | 40 #ifdef HAVE_LANGINFO_H |
41 #include <locale.h> | 41 #include <locale.h> |
42 #include <langinfo.h> | 42 #include <langinfo.h> |
43 #endif | 43 #endif |
44 | 44 |
45 PyObject * | 45 PyObject * |
46 PySys_GetObject(const char *name) | 46 PySys_GetObject(const char *name) |
47 { | 47 { |
48 » PyThreadState *tstate = PyThreadState_GET(); | 48 PyThreadState *tstate = PyThreadState_GET(); |
49 » PyObject *sd = tstate->interp->sysdict; | 49 PyObject *sd = tstate->interp->sysdict; |
50 » if (sd == NULL) | 50 if (sd == NULL) |
51 » » return NULL; | 51 return NULL; |
52 » return PyDict_GetItemString(sd, name); | 52 return PyDict_GetItemString(sd, name); |
53 } | 53 } |
54 | 54 |
55 int | 55 int |
56 PySys_SetObject(const char *name, PyObject *v) | 56 PySys_SetObject(const char *name, PyObject *v) |
57 { | 57 { |
58 » PyThreadState *tstate = PyThreadState_GET(); | 58 PyThreadState *tstate = PyThreadState_GET(); |
59 » PyObject *sd = tstate->interp->sysdict; | 59 PyObject *sd = tstate->interp->sysdict; |
60 » if (v == NULL) { | 60 if (v == NULL) { |
61 » » if (PyDict_GetItemString(sd, name) == NULL) | 61 if (PyDict_GetItemString(sd, name) == NULL) |
62 » » » return 0; | 62 return 0; |
63 » » else | 63 else |
64 » » » return PyDict_DelItemString(sd, name); | 64 return PyDict_DelItemString(sd, name); |
65 » } | 65 } |
66 » else | 66 else |
67 » » return PyDict_SetItemString(sd, name, v); | 67 return PyDict_SetItemString(sd, name, v); |
68 } | 68 } |
69 | 69 |
70 static PyObject * | 70 static PyObject * |
71 sys_displayhook(PyObject *self, PyObject *o) | 71 sys_displayhook(PyObject *self, PyObject *o) |
72 { | 72 { |
73 » PyObject *outf; | 73 PyObject *outf; |
74 » PyInterpreterState *interp = PyThreadState_GET()->interp; | 74 PyInterpreterState *interp = PyThreadState_GET()->interp; |
75 » PyObject *modules = interp->modules; | 75 PyObject *modules = interp->modules; |
76 » PyObject *builtins = PyDict_GetItemString(modules, "builtins"); | 76 PyObject *builtins = PyDict_GetItemString(modules, "builtins"); |
77 | 77 |
78 » if (builtins == NULL) { | 78 if (builtins == NULL) { |
79 » » PyErr_SetString(PyExc_RuntimeError, "lost builtins module"); | 79 PyErr_SetString(PyExc_RuntimeError, "lost builtins module"); |
80 » » return NULL; | 80 return NULL; |
81 » } | 81 } |
82 | 82 |
83 » /* Print value except if None */ | 83 /* Print value except if None */ |
84 » /* After printing, also assign to '_' */ | 84 /* After printing, also assign to '_' */ |
85 » /* Before, set '_' to None to avoid recursion */ | 85 /* Before, set '_' to None to avoid recursion */ |
86 » if (o == Py_None) { | 86 if (o == Py_None) { |
87 » » Py_INCREF(Py_None); | 87 Py_INCREF(Py_None); |
88 » » return Py_None; | 88 return Py_None; |
89 » } | 89 } |
90 » if (PyObject_SetAttrString(builtins, "_", Py_None) != 0) | 90 if (PyObject_SetAttrString(builtins, "_", Py_None) != 0) |
91 » » return NULL; | 91 return NULL; |
92 » outf = PySys_GetObject("stdout"); | 92 outf = PySys_GetObject("stdout"); |
93 » if (outf == NULL || outf == Py_None) { | 93 if (outf == NULL || outf == Py_None) { |
94 » » PyErr_SetString(PyExc_RuntimeError, "lost sys.stdout"); | 94 PyErr_SetString(PyExc_RuntimeError, "lost sys.stdout"); |
95 » » return NULL; | 95 return NULL; |
96 » } | 96 } |
97 » if (PyFile_WriteObject(o, outf, 0) != 0) | 97 if (PyFile_WriteObject(o, outf, 0) != 0) |
98 » » return NULL; | 98 return NULL; |
99 » if (PyFile_WriteString("\n", outf) != 0) | 99 if (PyFile_WriteString("\n", outf) != 0) |
100 » » return NULL; | 100 return NULL; |
101 » if (PyObject_SetAttrString(builtins, "_", o) != 0) | 101 if (PyObject_SetAttrString(builtins, "_", o) != 0) |
102 » » return NULL; | 102 return NULL; |
103 » Py_INCREF(Py_None); | 103 Py_INCREF(Py_None); |
104 » return Py_None; | 104 return Py_None; |
105 } | 105 } |
106 | 106 |
107 PyDoc_STRVAR(displayhook_doc, | 107 PyDoc_STRVAR(displayhook_doc, |
108 "displayhook(object) -> None\n" | 108 "displayhook(object) -> None\n" |
109 "\n" | 109 "\n" |
110 "Print an object to sys.stdout and also save it in builtins._\n" | 110 "Print an object to sys.stdout and also save it in builtins._\n" |
111 ); | 111 ); |
112 | 112 |
113 static PyObject * | 113 static PyObject * |
114 sys_excepthook(PyObject* self, PyObject* args) | 114 sys_excepthook(PyObject* self, PyObject* args) |
115 { | 115 { |
116 » PyObject *exc, *value, *tb; | 116 PyObject *exc, *value, *tb; |
117 » if (!PyArg_UnpackTuple(args, "excepthook", 3, 3, &exc, &value, &tb)) | 117 if (!PyArg_UnpackTuple(args, "excepthook", 3, 3, &exc, &value, &tb)) |
118 » » return NULL; | 118 return NULL; |
119 » PyErr_Display(exc, value, tb); | 119 PyErr_Display(exc, value, tb); |
120 » Py_INCREF(Py_None); | 120 Py_INCREF(Py_None); |
121 » return Py_None; | 121 return Py_None; |
122 } | 122 } |
123 | 123 |
124 PyDoc_STRVAR(excepthook_doc, | 124 PyDoc_STRVAR(excepthook_doc, |
125 "excepthook(exctype, value, traceback) -> None\n" | 125 "excepthook(exctype, value, traceback) -> None\n" |
126 "\n" | 126 "\n" |
127 "Handle an exception by displaying it with a traceback on sys.stderr.\n" | 127 "Handle an exception by displaying it with a traceback on sys.stderr.\n" |
128 ); | 128 ); |
129 | 129 |
130 static PyObject * | 130 static PyObject * |
131 sys_exc_info(PyObject *self, PyObject *noargs) | 131 sys_exc_info(PyObject *self, PyObject *noargs) |
132 { | 132 { |
133 » PyThreadState *tstate; | 133 PyThreadState *tstate; |
134 » tstate = PyThreadState_GET(); | 134 tstate = PyThreadState_GET(); |
135 » return Py_BuildValue( | 135 return Py_BuildValue( |
136 » » "(OOO)", | 136 "(OOO)", |
137 » » tstate->exc_type != NULL ? tstate->exc_type : Py_None, | 137 tstate->exc_type != NULL ? tstate->exc_type : Py_None, |
138 » » tstate->exc_value != NULL ? tstate->exc_value : Py_None, | 138 tstate->exc_value != NULL ? tstate->exc_value : Py_None, |
139 » » tstate->exc_traceback != NULL ? | 139 tstate->exc_traceback != NULL ? |
140 » » » tstate->exc_traceback : Py_None); | 140 tstate->exc_traceback : Py_None); |
141 } | 141 } |
142 | 142 |
143 PyDoc_STRVAR(exc_info_doc, | 143 PyDoc_STRVAR(exc_info_doc, |
144 "exc_info() -> (type, value, traceback)\n\ | 144 "exc_info() -> (type, value, traceback)\n\ |
145 \n\ | 145 \n\ |
146 Return information about the most recent exception caught by an except\n\ | 146 Return information about the most recent exception caught by an except\n\ |
147 clause in the current stack frame or in an older stack frame." | 147 clause in the current stack frame or in an older stack frame." |
148 ); | 148 ); |
149 | 149 |
150 static PyObject * | 150 static PyObject * |
151 sys_exit(PyObject *self, PyObject *args) | 151 sys_exit(PyObject *self, PyObject *args) |
152 { | 152 { |
153 » PyObject *exit_code = 0; | 153 PyObject *exit_code = 0; |
154 » if (!PyArg_UnpackTuple(args, "exit", 0, 1, &exit_code)) | 154 if (!PyArg_UnpackTuple(args, "exit", 0, 1, &exit_code)) |
155 » » return NULL; | 155 return NULL; |
156 » /* Raise SystemExit so callers may catch it or clean up. */ | 156 /* Raise SystemExit so callers may catch it or clean up. */ |
157 » PyErr_SetObject(PyExc_SystemExit, exit_code); | 157 PyErr_SetObject(PyExc_SystemExit, exit_code); |
158 » return NULL; | 158 return NULL; |
159 } | 159 } |
160 | 160 |
161 PyDoc_STRVAR(exit_doc, | 161 PyDoc_STRVAR(exit_doc, |
162 "exit([status])\n\ | 162 "exit([status])\n\ |
163 \n\ | 163 \n\ |
164 Exit the interpreter by raising SystemExit(status).\n\ | 164 Exit the interpreter by raising SystemExit(status).\n\ |
165 If the status is omitted or None, it defaults to zero (i.e., success).\n\ | 165 If the status is omitted or None, it defaults to zero (i.e., success).\n\ |
166 If the status is numeric, it will be used as the system exit status.\n\ | 166 If the status is numeric, it will be used as the system exit status.\n\ |
167 If it is another kind of object, it will be printed and the system\n\ | 167 If it is another kind of object, it will be printed and the system\n\ |
168 exit status will be one (i.e., failure)." | 168 exit status will be one (i.e., failure)." |
169 ); | 169 ); |
170 | 170 |
171 | 171 |
172 static PyObject * | 172 static PyObject * |
173 sys_getdefaultencoding(PyObject *self) | 173 sys_getdefaultencoding(PyObject *self) |
174 { | 174 { |
175 » return PyUnicode_FromString(PyUnicode_GetDefaultEncoding()); | 175 return PyUnicode_FromString(PyUnicode_GetDefaultEncoding()); |
176 } | 176 } |
177 | 177 |
178 PyDoc_STRVAR(getdefaultencoding_doc, | 178 PyDoc_STRVAR(getdefaultencoding_doc, |
179 "getdefaultencoding() -> string\n\ | 179 "getdefaultencoding() -> string\n\ |
180 \n\ | 180 \n\ |
181 Return the current default string encoding used by the Unicode \n\ | 181 Return the current default string encoding used by the Unicode \n\ |
182 implementation." | 182 implementation." |
183 ); | 183 ); |
184 | 184 |
185 static PyObject * | 185 static PyObject * |
186 sys_setdefaultencoding(PyObject *self, PyObject *args) | 186 sys_setdefaultencoding(PyObject *self, PyObject *args) |
187 { | 187 { |
188 » char *encoding; | 188 char *encoding; |
189 » if (!PyArg_ParseTuple(args, "s:setdefaultencoding", &encoding)) | 189 if (!PyArg_ParseTuple(args, "s:setdefaultencoding", &encoding)) |
190 » » return NULL; | 190 return NULL; |
191 » if (PyUnicode_SetDefaultEncoding(encoding)) | 191 if (PyUnicode_SetDefaultEncoding(encoding)) |
192 » » return NULL; | 192 return NULL; |
193 » Py_INCREF(Py_None); | 193 Py_INCREF(Py_None); |
194 » return Py_None; | 194 return Py_None; |
195 } | 195 } |
196 | 196 |
197 PyDoc_STRVAR(setdefaultencoding_doc, | 197 PyDoc_STRVAR(setdefaultencoding_doc, |
198 "setdefaultencoding(encoding)\n\ | 198 "setdefaultencoding(encoding)\n\ |
199 \n\ | 199 \n\ |
200 Set the current default string encoding used by the Unicode implementation." | 200 Set the current default string encoding used by the Unicode implementation." |
201 ); | 201 ); |
202 | 202 |
203 static PyObject * | 203 static PyObject * |
204 sys_getfilesystemencoding(PyObject *self) | 204 sys_getfilesystemencoding(PyObject *self) |
205 { | 205 { |
206 » if (Py_FileSystemDefaultEncoding) | 206 if (Py_FileSystemDefaultEncoding) |
207 » » return PyUnicode_FromString(Py_FileSystemDefaultEncoding); | 207 return PyUnicode_FromString(Py_FileSystemDefaultEncoding); |
208 » Py_INCREF(Py_None); | 208 Py_INCREF(Py_None); |
209 » return Py_None; | 209 return Py_None; |
210 } | 210 } |
211 | 211 |
212 PyDoc_STRVAR(getfilesystemencoding_doc, | 212 PyDoc_STRVAR(getfilesystemencoding_doc, |
213 "getfilesystemencoding() -> string\n\ | 213 "getfilesystemencoding() -> string\n\ |
214 \n\ | 214 \n\ |
215 Return the encoding used to convert Unicode filenames in\n\ | 215 Return the encoding used to convert Unicode filenames in\n\ |
216 operating system filenames." | 216 operating system filenames." |
217 ); | 217 ); |
218 | 218 |
219 static PyObject * | 219 static PyObject * |
220 sys_setfilesystemencoding(PyObject *self, PyObject *args) | 220 sys_setfilesystemencoding(PyObject *self, PyObject *args) |
221 { | 221 { |
222 » PyObject *new_encoding; | 222 PyObject *new_encoding; |
223 » if (!PyArg_ParseTuple(args, "U:setfilesystemencoding", &new_encoding)) | 223 if (!PyArg_ParseTuple(args, "U:setfilesystemencoding", &new_encoding)) |
224 » » return NULL; | 224 return NULL; |
225 » if (_Py_SetFileSystemEncoding(new_encoding)) | 225 if (_Py_SetFileSystemEncoding(new_encoding)) |
226 » » return NULL; | 226 return NULL; |
227 » Py_INCREF(Py_None); | 227 Py_INCREF(Py_None); |
228 » return Py_None; | 228 return Py_None; |
229 } | 229 } |
230 | 230 |
231 PyDoc_STRVAR(setfilesystemencoding_doc, | 231 PyDoc_STRVAR(setfilesystemencoding_doc, |
232 "setfilesystemencoding(string) -> None\n\ | 232 "setfilesystemencoding(string) -> None\n\ |
233 \n\ | 233 \n\ |
234 Set the encoding used to convert Unicode filenames in\n\ | 234 Set the encoding used to convert Unicode filenames in\n\ |
235 operating system filenames." | 235 operating system filenames." |
236 ); | 236 ); |
237 | 237 |
238 static PyObject * | 238 static PyObject * |
239 sys_intern(PyObject *self, PyObject *args) | 239 sys_intern(PyObject *self, PyObject *args) |
240 { | 240 { |
241 » PyObject *s; | 241 PyObject *s; |
242 » if (!PyArg_ParseTuple(args, "U:intern", &s)) | 242 if (!PyArg_ParseTuple(args, "U:intern", &s)) |
243 » » return NULL; | 243 return NULL; |
244 » if (PyUnicode_CheckExact(s)) { | 244 if (PyUnicode_CheckExact(s)) { |
245 » » Py_INCREF(s); | 245 Py_INCREF(s); |
246 » » PyUnicode_InternInPlace(&s); | 246 PyUnicode_InternInPlace(&s); |
247 » » return s; | 247 return s; |
248 » } | 248 } |
249 » else { | 249 else { |
250 » » PyErr_Format(PyExc_TypeError, | 250 PyErr_Format(PyExc_TypeError, |
251 » » » » "can't intern %.400s", s->ob_type->tp_name); | 251 "can't intern %.400s", s->ob_type->tp_name); |
252 » » return NULL; | 252 return NULL; |
253 » } | 253 } |
254 } | 254 } |
255 | 255 |
256 PyDoc_STRVAR(intern_doc, | 256 PyDoc_STRVAR(intern_doc, |
257 "intern(string) -> string\n\ | 257 "intern(string) -> string\n\ |
258 \n\ | 258 \n\ |
259 ``Intern'' the given string. This enters the string in the (global)\n\ | 259 ``Intern'' the given string. This enters the string in the (global)\n\ |
260 table of interned strings whose purpose is to speed up dictionary lookups.\n\ | 260 table of interned strings whose purpose is to speed up dictionary lookups.\n\ |
261 Return the string itself or the previously interned string object with the\n\ | 261 Return the string itself or the previously interned string object with the\n\ |
262 same value."); | 262 same value."); |
263 | 263 |
264 | 264 |
265 /* | 265 /* |
266 * Cached interned string objects used for calling the profile and | 266 * Cached interned string objects used for calling the profile and |
267 * trace functions. Initialized by trace_init(). | 267 * trace functions. Initialized by trace_init(). |
268 */ | 268 */ |
269 static PyObject *whatstrings[7] = {NULL, NULL, NULL, NULL, NULL, NULL, NULL}; | 269 static PyObject *whatstrings[7] = {NULL, NULL, NULL, NULL, NULL, NULL, NULL}; |
270 | 270 |
271 static int | 271 static int |
272 trace_init(void) | 272 trace_init(void) |
273 { | 273 { |
274 » static char *whatnames[7] = {"call", "exception", "line", "return", | 274 static char *whatnames[7] = {"call", "exception", "line", "return", |
275 » » » » » "c_call", "c_exception", "c_return"}; | 275 "c_call", "c_exception", "c_return"}; |
276 » PyObject *name; | 276 PyObject *name; |
277 » int i; | 277 int i; |
278 » for (i = 0; i < 7; ++i) { | 278 for (i = 0; i < 7; ++i) { |
279 » » if (whatstrings[i] == NULL) { | 279 if (whatstrings[i] == NULL) { |
280 » » » name = PyUnicode_InternFromString(whatnames[i]); | 280 name = PyUnicode_InternFromString(whatnames[i]); |
281 » » » if (name == NULL) | 281 if (name == NULL) |
282 » » » » return -1; | 282 return -1; |
283 » » » whatstrings[i] = name; | 283 whatstrings[i] = name; |
284 } | 284 } |
285 » } | 285 } |
286 » return 0; | 286 return 0; |
287 } | 287 } |
288 | 288 |
289 | 289 |
290 static PyObject * | 290 static PyObject * |
291 call_trampoline(PyThreadState *tstate, PyObject* callback, | 291 call_trampoline(PyThreadState *tstate, PyObject* callback, |
292 » » PyFrameObject *frame, int what, PyObject *arg) | 292 PyFrameObject *frame, int what, PyObject *arg) |
293 { | 293 { |
294 » PyObject *args = PyTuple_New(3); | 294 PyObject *args = PyTuple_New(3); |
295 » PyObject *whatstr; | 295 PyObject *whatstr; |
296 » PyObject *result; | 296 PyObject *result; |
297 | 297 |
298 » if (args == NULL) | 298 if (args == NULL) |
299 » » return NULL; | 299 return NULL; |
300 » Py_INCREF(frame); | 300 Py_INCREF(frame); |
301 » whatstr = whatstrings[what]; | 301 whatstr = whatstrings[what]; |
302 » Py_INCREF(whatstr); | 302 Py_INCREF(whatstr); |
303 » if (arg == NULL) | 303 if (arg == NULL) |
304 » » arg = Py_None; | 304 arg = Py_None; |
305 » Py_INCREF(arg); | 305 Py_INCREF(arg); |
306 » PyTuple_SET_ITEM(args, 0, (PyObject *)frame); | 306 PyTuple_SET_ITEM(args, 0, (PyObject *)frame); |
307 » PyTuple_SET_ITEM(args, 1, whatstr); | 307 PyTuple_SET_ITEM(args, 1, whatstr); |
308 » PyTuple_SET_ITEM(args, 2, arg); | 308 PyTuple_SET_ITEM(args, 2, arg); |
309 | 309 |
310 » /* call the Python-level function */ | 310 /* call the Python-level function */ |
311 » PyFrame_FastToLocals(frame); | 311 PyFrame_FastToLocals(frame); |
312 » result = PyEval_CallObject(callback, args); | 312 result = PyEval_CallObject(callback, args); |
313 » PyFrame_LocalsToFast(frame, 1); | 313 PyFrame_LocalsToFast(frame, 1); |
314 » if (result == NULL) | 314 if (result == NULL) |
315 » » PyTraceBack_Here(frame); | 315 PyTraceBack_Here(frame); |
316 | 316 |
317 » /* cleanup */ | 317 /* cleanup */ |
318 » Py_DECREF(args); | 318 Py_DECREF(args); |
319 » return result; | 319 return result; |
320 } | 320 } |
321 | 321 |
322 static int | 322 static int |
323 profile_trampoline(PyObject *self, PyFrameObject *frame, | 323 profile_trampoline(PyObject *self, PyFrameObject *frame, |
324 » » int what, PyObject *arg) | 324 int what, PyObject *arg) |
325 { | 325 { |
326 » PyThreadState *tstate = frame->f_tstate; | 326 PyThreadState *tstate = frame->f_tstate; |
327 » PyObject *result; | 327 PyObject *result; |
328 | 328 |
329 » if (arg == NULL) | 329 if (arg == NULL) |
330 » » arg = Py_None; | 330 arg = Py_None; |
331 » result = call_trampoline(tstate, self, frame, what, arg); | 331 result = call_trampoline(tstate, self, frame, what, arg); |
332 » if (result == NULL) { | 332 if (result == NULL) { |
333 » » PyEval_SetProfile(NULL, NULL); | 333 PyEval_SetProfile(NULL, NULL); |
334 » » return -1; | 334 return -1; |
335 » } | 335 } |
336 » Py_DECREF(result); | 336 Py_DECREF(result); |
337 » return 0; | 337 return 0; |
338 } | 338 } |
339 | 339 |
340 static int | 340 static int |
341 trace_trampoline(PyObject *self, PyFrameObject *frame, | 341 trace_trampoline(PyObject *self, PyFrameObject *frame, |
342 » » int what, PyObject *arg) | 342 int what, PyObject *arg) |
343 { | 343 { |
344 » PyThreadState *tstate = frame->f_tstate; | 344 PyThreadState *tstate = frame->f_tstate; |
345 » PyObject *callback; | 345 PyObject *callback; |
346 » PyObject *result; | 346 PyObject *result; |
347 | 347 |
348 » if (what == PyTrace_CALL) | 348 if (what == PyTrace_CALL) |
349 » » callback = self; | 349 callback = self; |
350 » else | 350 else |
351 » » callback = frame->f_trace; | 351 callback = frame->f_trace; |
352 » if (callback == NULL) | 352 if (callback == NULL) |
353 » » return 0; | 353 return 0; |
354 » result = call_trampoline(tstate, callback, frame, what, arg); | 354 result = call_trampoline(tstate, callback, frame, what, arg); |
355 » if (result == NULL) { | 355 if (result == NULL) { |
356 » » PyEval_SetTrace(NULL, NULL); | 356 PyEval_SetTrace(NULL, NULL); |
357 » » Py_XDECREF(frame->f_trace); | 357 Py_XDECREF(frame->f_trace); |
358 » » frame->f_trace = NULL; | 358 frame->f_trace = NULL; |
359 » » return -1; | 359 return -1; |
360 » } | 360 } |
361 » if (result != Py_None) { | 361 if (result != Py_None) { |
362 » » PyObject *temp = frame->f_trace; | 362 PyObject *temp = frame->f_trace; |
363 » » frame->f_trace = NULL; | 363 frame->f_trace = NULL; |
364 » » Py_XDECREF(temp); | 364 Py_XDECREF(temp); |
365 » » frame->f_trace = result; | 365 frame->f_trace = result; |
366 » } | 366 } |
367 » else { | 367 else { |
368 » » Py_DECREF(result); | 368 Py_DECREF(result); |
369 » } | 369 } |
370 » return 0; | 370 return 0; |
371 } | 371 } |
372 | 372 |
373 static PyObject * | 373 static PyObject * |
374 sys_settrace(PyObject *self, PyObject *args) | 374 sys_settrace(PyObject *self, PyObject *args) |
375 { | 375 { |
376 » if (trace_init() == -1) | 376 if (trace_init() == -1) |
377 » » return NULL; | 377 return NULL; |
378 » if (args == Py_None) | 378 if (args == Py_None) |
379 » » PyEval_SetTrace(NULL, NULL); | 379 PyEval_SetTrace(NULL, NULL); |
380 » else | 380 else |
381 » » PyEval_SetTrace(trace_trampoline, args); | 381 PyEval_SetTrace(trace_trampoline, args); |
382 » Py_INCREF(Py_None); | 382 Py_INCREF(Py_None); |
383 » return Py_None; | 383 return Py_None; |
384 } | 384 } |
385 | 385 |
386 PyDoc_STRVAR(settrace_doc, | 386 PyDoc_STRVAR(settrace_doc, |
387 "settrace(function)\n\ | 387 "settrace(function)\n\ |
388 \n\ | 388 \n\ |
389 Set the global debug tracing function. It will be called on each\n\ | 389 Set the global debug tracing function. It will be called on each\n\ |
390 function call. See the debugger chapter in the library manual." | 390 function call. See the debugger chapter in the library manual." |
391 ); | 391 ); |
392 | 392 |
393 static PyObject * | 393 static PyObject * |
394 sys_gettrace(PyObject *self, PyObject *args) | 394 sys_gettrace(PyObject *self, PyObject *args) |
395 { | 395 { |
396 » PyThreadState *tstate = PyThreadState_GET(); | 396 PyThreadState *tstate = PyThreadState_GET(); |
397 » PyObject *temp = tstate->c_traceobj; | 397 PyObject *temp = tstate->c_traceobj; |
398 | 398 |
399 » if (temp == NULL) | 399 if (temp == NULL) |
400 » » temp = Py_None; | 400 temp = Py_None; |
401 » Py_INCREF(temp); | 401 Py_INCREF(temp); |
402 » return temp; | 402 return temp; |
403 } | 403 } |
404 | 404 |
405 PyDoc_STRVAR(gettrace_doc, | 405 PyDoc_STRVAR(gettrace_doc, |
406 "gettrace()\n\ | 406 "gettrace()\n\ |
407 \n\ | 407 \n\ |
408 Return the global debug tracing function set with sys.settrace.\n\ | 408 Return the global debug tracing function set with sys.settrace.\n\ |
409 See the debugger chapter in the library manual." | 409 See the debugger chapter in the library manual." |
410 ); | 410 ); |
411 | 411 |
412 static PyObject * | 412 static PyObject * |
413 sys_setprofile(PyObject *self, PyObject *args) | 413 sys_setprofile(PyObject *self, PyObject *args) |
414 { | 414 { |
415 » if (trace_init() == -1) | 415 if (trace_init() == -1) |
416 » » return NULL; | 416 return NULL; |
417 » if (args == Py_None) | 417 if (args == Py_None) |
418 » » PyEval_SetProfile(NULL, NULL); | 418 PyEval_SetProfile(NULL, NULL); |
419 » else | 419 else |
420 » » PyEval_SetProfile(profile_trampoline, args); | 420 PyEval_SetProfile(profile_trampoline, args); |
421 » Py_INCREF(Py_None); | 421 Py_INCREF(Py_None); |
422 » return Py_None; | 422 return Py_None; |
423 } | 423 } |
424 | 424 |
425 PyDoc_STRVAR(setprofile_doc, | 425 PyDoc_STRVAR(setprofile_doc, |
426 "setprofile(function)\n\ | 426 "setprofile(function)\n\ |
427 \n\ | 427 \n\ |
428 Set the profiling function. It will be called on each function call\n\ | 428 Set the profiling function. It will be called on each function call\n\ |
429 and return. See the profiler chapter in the library manual." | 429 and return. See the profiler chapter in the library manual." |
430 ); | 430 ); |
431 | 431 |
432 static PyObject * | 432 static PyObject * |
433 sys_getprofile(PyObject *self, PyObject *args) | 433 sys_getprofile(PyObject *self, PyObject *args) |
434 { | 434 { |
435 » PyThreadState *tstate = PyThreadState_GET(); | 435 PyThreadState *tstate = PyThreadState_GET(); |
436 » PyObject *temp = tstate->c_profileobj; | 436 PyObject *temp = tstate->c_profileobj; |
437 | 437 |
438 » if (temp == NULL) | 438 if (temp == NULL) |
439 » » temp = Py_None; | 439 temp = Py_None; |
440 » Py_INCREF(temp); | 440 Py_INCREF(temp); |
441 » return temp; | 441 return temp; |
442 } | 442 } |
443 | 443 |
444 PyDoc_STRVAR(getprofile_doc, | 444 PyDoc_STRVAR(getprofile_doc, |
445 "getprofile()\n\ | 445 "getprofile()\n\ |
446 \n\ | 446 \n\ |
447 Return the profiling function set with sys.setprofile.\n\ | 447 Return the profiling function set with sys.setprofile.\n\ |
448 See the profiler chapter in the library manual." | 448 See the profiler chapter in the library manual." |
449 ); | 449 ); |
450 | 450 |
451 static int _check_interval = 100; | 451 static int _check_interval = 100; |
452 | 452 |
453 static PyObject * | 453 static PyObject * |
454 sys_setcheckinterval(PyObject *self, PyObject *args) | 454 sys_setcheckinterval(PyObject *self, PyObject *args) |
455 { | 455 { |
456 » if (PyErr_WarnEx(PyExc_DeprecationWarning, | 456 if (PyErr_WarnEx(PyExc_DeprecationWarning, |
457 » » » "sys.getcheckinterval() and sys.setcheckinterval() " | 457 "sys.getcheckinterval() and sys.setcheckinterval() " |
458 » » » "are deprecated. Use sys.setswitchinterval() " | 458 "are deprecated. Use sys.setswitchinterval() " |
459 » » » "instead.", 1) < 0) | 459 "instead.", 1) < 0) |
460 » » return NULL; | 460 return NULL; |
461 » if (!PyArg_ParseTuple(args, "i:setcheckinterval", &_check_interval)) | 461 if (!PyArg_ParseTuple(args, "i:setcheckinterval", &_check_interval)) |
462 » » return NULL; | 462 return NULL; |
463 » Py_INCREF(Py_None); | 463 Py_INCREF(Py_None); |
464 » return Py_None; | 464 return Py_None; |
465 } | 465 } |
466 | 466 |
467 PyDoc_STRVAR(setcheckinterval_doc, | 467 PyDoc_STRVAR(setcheckinterval_doc, |
468 "setcheckinterval(n)\n\ | 468 "setcheckinterval(n)\n\ |
469 \n\ | 469 \n\ |
470 Tell the Python interpreter to check for asynchronous events every\n\ | 470 Tell the Python interpreter to check for asynchronous events every\n\ |
471 n instructions. This also affects how often thread switches occur." | 471 n instructions. This also affects how often thread switches occur." |
472 ); | 472 ); |
473 | 473 |
474 static PyObject * | 474 static PyObject * |
475 sys_getcheckinterval(PyObject *self, PyObject *args) | 475 sys_getcheckinterval(PyObject *self, PyObject *args) |
476 { | 476 { |
477 » if (PyErr_WarnEx(PyExc_DeprecationWarning, | 477 if (PyErr_WarnEx(PyExc_DeprecationWarning, |
478 » » » "sys.getcheckinterval() and sys.setcheckinterval() " | 478 "sys.getcheckinterval() and sys.setcheckinterval() " |
479 » » » "are deprecated. Use sys.getswitchinterval() " | 479 "are deprecated. Use sys.getswitchinterval() " |
480 » » » "instead.", 1) < 0) | 480 "instead.", 1) < 0) |
481 » » return NULL; | 481 return NULL; |
482 » return PyLong_FromLong(_check_interval); | 482 return PyLong_FromLong(_check_interval); |
483 } | 483 } |
484 | 484 |
485 PyDoc_STRVAR(getcheckinterval_doc, | 485 PyDoc_STRVAR(getcheckinterval_doc, |
486 "getcheckinterval() -> current check interval; see setcheckinterval()." | 486 "getcheckinterval() -> current check interval; see setcheckinterval()." |
487 ); | 487 ); |
488 | 488 |
489 #ifdef WITH_THREAD | 489 #ifdef WITH_THREAD |
490 static PyObject * | 490 static PyObject * |
491 sys_setswitchinterval(PyObject *self, PyObject *args) | 491 sys_setswitchinterval(PyObject *self, PyObject *args) |
492 { | 492 { |
493 » double d; | 493 double d; |
494 » if (!PyArg_ParseTuple(args, "d:setswitchinterval", &d)) | 494 if (!PyArg_ParseTuple(args, "d:setswitchinterval", &d)) |
495 » » return NULL; | 495 return NULL; |
496 » if (d <= 0.0) { | 496 if (d <= 0.0) { |
497 » » PyErr_SetString(PyExc_ValueError, | 497 PyErr_SetString(PyExc_ValueError, |
498 » » » » "switch interval must be strictly positive"); | 498 "switch interval must be strictly positive"); |
499 » » return NULL; | 499 return NULL; |
500 » } | 500 } |
501 » _PyEval_SetSwitchInterval((unsigned long) (1e6 * d)); | 501 _PyEval_SetSwitchInterval((unsigned long) (1e6 * d)); |
502 » Py_INCREF(Py_None); | 502 Py_INCREF(Py_None); |
503 » return Py_None; | 503 return Py_None; |
504 } | 504 } |
505 | 505 |
506 PyDoc_STRVAR(setswitchinterval_doc, | 506 PyDoc_STRVAR(setswitchinterval_doc, |
507 "setswitchinterval(n)\n\ | 507 "setswitchinterval(n)\n\ |
508 \n\ | 508 \n\ |
509 Set the ideal thread switching delay inside the Python interpreter\n\ | 509 Set the ideal thread switching delay inside the Python interpreter\n\ |
510 The actual frequency of switching threads can be lower if the\n\ | 510 The actual frequency of switching threads can be lower if the\n\ |
511 interpreter executes long sequences of uninterruptible code\n\ | 511 interpreter executes long sequences of uninterruptible code\n\ |
512 (this is implementation-specific and workload-dependent).\n\ | 512 (this is implementation-specific and workload-dependent).\n\ |
513 \n\ | 513 \n\ |
514 The parameter must represent the desired switching delay in seconds\n\ | 514 The parameter must represent the desired switching delay in seconds\n\ |
515 A typical value is 0.005 (5 milliseconds)." | 515 A typical value is 0.005 (5 milliseconds)." |
516 ); | 516 ); |
517 | 517 |
518 static PyObject * | 518 static PyObject * |
519 sys_getswitchinterval(PyObject *self, PyObject *args) | 519 sys_getswitchinterval(PyObject *self, PyObject *args) |
520 { | 520 { |
521 » return PyFloat_FromDouble(1e-6 * _PyEval_GetSwitchInterval()); | 521 return PyFloat_FromDouble(1e-6 * _PyEval_GetSwitchInterval()); |
522 } | 522 } |
523 | 523 |
524 PyDoc_STRVAR(getswitchinterval_doc, | 524 PyDoc_STRVAR(getswitchinterval_doc, |
525 "getswitchinterval() -> current thread switch interval; see setswitchinterval(). " | 525 "getswitchinterval() -> current thread switch interval; see setswitchinterval(). " |
526 ); | 526 ); |
527 | 527 |
528 #endif /* WITH_THREAD */ | 528 #endif /* WITH_THREAD */ |
529 | 529 |
530 #ifdef WITH_TSC | 530 #ifdef WITH_TSC |
531 static PyObject * | 531 static PyObject * |
532 sys_settscdump(PyObject *self, PyObject *args) | 532 sys_settscdump(PyObject *self, PyObject *args) |
533 { | 533 { |
534 » int bool; | 534 int bool; |
535 » PyThreadState *tstate = PyThreadState_Get(); | 535 PyThreadState *tstate = PyThreadState_Get(); |
536 | 536 |
537 » if (!PyArg_ParseTuple(args, "i:settscdump", &bool)) | 537 if (!PyArg_ParseTuple(args, "i:settscdump", &bool)) |
538 » » return NULL; | 538 return NULL; |
539 » if (bool) | 539 if (bool) |
540 » » tstate->interp->tscdump = 1; | 540 tstate->interp->tscdump = 1; |
541 » else | 541 else |
542 » » tstate->interp->tscdump = 0; | 542 tstate->interp->tscdump = 0; |
543 » Py_INCREF(Py_None); | 543 Py_INCREF(Py_None); |
544 » return Py_None; | 544 return Py_None; |
545 | 545 |
546 } | 546 } |
547 | 547 |
548 PyDoc_STRVAR(settscdump_doc, | 548 PyDoc_STRVAR(settscdump_doc, |
549 "settscdump(bool)\n\ | 549 "settscdump(bool)\n\ |
550 \n\ | 550 \n\ |
551 If true, tell the Python interpreter to dump VM measurements to\n\ | 551 If true, tell the Python interpreter to dump VM measurements to\n\ |
552 stderr. If false, turn off dump. The measurements are based on the\n\ | 552 stderr. If false, turn off dump. The measurements are based on the\n\ |
553 processor's time-stamp counter." | 553 processor's time-stamp counter." |
554 ); | 554 ); |
555 #endif /* TSC */ | 555 #endif /* TSC */ |
556 | 556 |
557 static PyObject * | 557 static PyObject * |
558 sys_setrecursionlimit(PyObject *self, PyObject *args) | 558 sys_setrecursionlimit(PyObject *self, PyObject *args) |
559 { | 559 { |
560 » int new_limit; | 560 int new_limit; |
561 » if (!PyArg_ParseTuple(args, "i:setrecursionlimit", &new_limit)) | 561 if (!PyArg_ParseTuple(args, "i:setrecursionlimit", &new_limit)) |
562 » » return NULL; | 562 return NULL; |
563 » if (new_limit <= 0) { | 563 if (new_limit <= 0) { |
564 » » PyErr_SetString(PyExc_ValueError, | 564 PyErr_SetString(PyExc_ValueError, |
565 » » » » "recursion limit must be positive"); | 565 "recursion limit must be positive"); |
566 » » return NULL; | 566 return NULL; |
567 » } | 567 } |
568 » Py_SetRecursionLimit(new_limit); | 568 Py_SetRecursionLimit(new_limit); |
569 » Py_INCREF(Py_None); | 569 Py_INCREF(Py_None); |
570 » return Py_None; | 570 return Py_None; |
571 } | 571 } |
572 | 572 |
573 static PyTypeObject _Hash_InfoType; | 573 static PyTypeObject Hash_InfoType; |
574 | 574 |
575 PyDoc_STRVAR(_hash_info_doc, | 575 PyDoc_STRVAR(hash_info_doc, |
576 "_hash_info\n\ | 576 "hash_info\n\ |
577 \n\ | 577 \n\ |
578 A private struct sequence providing parameters used for computing\n\ | 578 A struct sequence providing parameters used for computing\n\ |
GvR
2010/03/21 17:04:48
Why private? I'm sure 3rd party number implementat
dickinsm
2010/03/23 11:56:56
Okay, will make it public. That gives me greater
| |
579 numeric hashes. The attributes are read only."); | 579 numeric hashes. The attributes are read only."); |
580 | 580 |
581 static PyStructSequence_Field _hash_info_fields[] = { | 581 static PyStructSequence_Field hash_info_fields[] = { |
582 » {"bits", "hash width"}, | 582 {"width", "width of the type used for hashing, in bits"}, |
583 » {"modulus", "prime number giving the modulus on which the hash " | 583 {"modulus", "prime number giving the modulus on which the hash " |
584 » "function is based"}, | 584 "function is based"}, |
585 » {"inf", "value to be used for hash of a positive or unsigned infinity"}, | 585 {"inf", "value to be used for hash of a positive infinity"}, |
586 » {"ninf", "value to be used for hash of a negative infinity"}, | 586 {"nan", "value to be used for hash of a nan"}, |
587 » {"nan", "value to be used for hash of a nan"}, | 587 {"imag", "multiplier used for the imaginary part of a complex number"}, |
588 » {NULL, NULL} | 588 {NULL, NULL} |
589 }; | 589 }; |
590 | 590 |
591 static PyStructSequence_Desc _hash_info_desc = { | 591 static PyStructSequence_Desc hash_info_desc = { |
592 » "sys._hash_info", | 592 "sys.hash_info", |
593 » _hash_info_doc, | 593 hash_info_doc, |
594 » _hash_info_fields, | 594 hash_info_fields, |
595 » 5, | 595 5, |
596 }; | 596 }; |
597 | 597 |
598 PyObject * | 598 PyObject * |
599 get_hash_info(void) | 599 get_hash_info(void) |
600 { | 600 { |
601 » PyObject *hash_info; | 601 PyObject *hash_info; |
602 » int field = 0; | 602 int field = 0; |
603 » hash_info = PyStructSequence_New(&_Hash_InfoType); | 603 hash_info = PyStructSequence_New(&Hash_InfoType); |
604 » if (hash_info == NULL) | 604 if (hash_info == NULL) |
605 » » return NULL; | 605 return NULL; |
606 » PyStructSequence_SET_ITEM(hash_info, field++, | 606 PyStructSequence_SET_ITEM(hash_info, field++, |
607 » » » » PyLong_FromLong(_PyHASH_BITS)); | 607 PyLong_FromLong(8*sizeof(long))); |
608 » PyStructSequence_SET_ITEM(hash_info, field++, | 608 PyStructSequence_SET_ITEM(hash_info, field++, |
609 » » » » PyLong_FromLong(_PyHASH_MASK)); | 609 PyLong_FromLong(_PyHASH_MODULUS)); |
610 » PyStructSequence_SET_ITEM(hash_info, field++, | 610 PyStructSequence_SET_ITEM(hash_info, field++, |
611 » » » » PyLong_FromLong(_PyHASH_INF)); | 611 PyLong_FromLong(_PyHASH_INF)); |
612 » PyStructSequence_SET_ITEM(hash_info, field++, | 612 PyStructSequence_SET_ITEM(hash_info, field++, |
613 » » » » PyLong_FromLong(_PyHASH_NINF)); | 613 PyLong_FromLong(_PyHASH_NAN)); |
614 » PyStructSequence_SET_ITEM(hash_info, field++, | 614 PyStructSequence_SET_ITEM(hash_info, field++, |
615 » » » » PyLong_FromLong(_PyHASH_NAN)); | 615 PyLong_FromLong(_PyHASH_IMAG)); |
616 » if (PyErr_Occurred()) { | 616 if (PyErr_Occurred()) { |
617 » » Py_CLEAR(hash_info); | 617 Py_CLEAR(hash_info); |
618 » » return NULL; | 618 return NULL; |
619 » } | 619 } |
620 » return hash_info; | 620 return hash_info; |
621 } | 621 } |
622 | 622 |
623 | 623 |
624 PyDoc_STRVAR(setrecursionlimit_doc, | 624 PyDoc_STRVAR(setrecursionlimit_doc, |
625 "setrecursionlimit(n)\n\ | 625 "setrecursionlimit(n)\n\ |
626 \n\ | 626 \n\ |
627 Set the maximum depth of the Python interpreter stack to n. This\n\ | 627 Set the maximum depth of the Python interpreter stack to n. This\n\ |
628 limit prevents infinite recursion from causing an overflow of the C\n\ | 628 limit prevents infinite recursion from causing an overflow of the C\n\ |
629 stack and crashing Python. The highest possible limit is platform-\n\ | 629 stack and crashing Python. The highest possible limit is platform-\n\ |
630 dependent." | 630 dependent." |
631 ); | 631 ); |
632 | 632 |
633 static PyObject * | 633 static PyObject * |
634 sys_getrecursionlimit(PyObject *self) | 634 sys_getrecursionlimit(PyObject *self) |
635 { | 635 { |
636 » return PyLong_FromLong(Py_GetRecursionLimit()); | 636 return PyLong_FromLong(Py_GetRecursionLimit()); |
637 } | 637 } |
638 | 638 |
639 PyDoc_STRVAR(getrecursionlimit_doc, | 639 PyDoc_STRVAR(getrecursionlimit_doc, |
640 "getrecursionlimit()\n\ | 640 "getrecursionlimit()\n\ |
641 \n\ | 641 \n\ |
642 Return the current value of the recursion limit, the maximum depth\n\ | 642 Return the current value of the recursion limit, the maximum depth\n\ |
643 of the Python interpreter stack. This limit prevents infinite\n\ | 643 of the Python interpreter stack. This limit prevents infinite\n\ |
644 recursion from causing an overflow of the C stack and crashing Python." | 644 recursion from causing an overflow of the C stack and crashing Python." |
645 ); | 645 ); |
646 | 646 |
647 #ifdef MS_WINDOWS | 647 #ifdef MS_WINDOWS |
648 PyDoc_STRVAR(getwindowsversion_doc, | 648 PyDoc_STRVAR(getwindowsversion_doc, |
649 "getwindowsversion()\n\ | 649 "getwindowsversion()\n\ |
650 \n\ | 650 \n\ |
651 Return information about the running version of Windows as a named tuple.\n\ | 651 Return information about the running version of Windows as a named tuple.\n\ |
652 The members are named: major, minor, build, platform, service_pack,\n\ | 652 The members are named: major, minor, build, platform, service_pack,\n\ |
653 service_pack_major, service_pack_minor, suite_mask, and product_type. For\n\ | 653 service_pack_major, service_pack_minor, suite_mask, and product_type. For\n\ |
654 backward compatibiliy, only the first 5 items are available by indexing.\n\ | 654 backward compatibiliy, only the first 5 items are available by indexing.\n\ |
655 All elements are numbers, except service_pack which is a string. Platform\n\ | 655 All elements are numbers, except service_pack which is a string. Platform\n\ |
656 may be 0 for win32s, 1 for Windows 9x/ME, 2 for Windows NT/2000/XP/Vista/7,\n\ | 656 may be 0 for win32s, 1 for Windows 9x/ME, 2 for Windows NT/2000/XP/Vista/7,\n\ |
657 3 for Windows CE. Product_type may be 1 for a workstation, 2 for a domain\n\ | 657 3 for Windows CE. Product_type may be 1 for a workstation, 2 for a domain\n\ |
658 controller, 3 for a server." | 658 controller, 3 for a server." |
659 ); | 659 ); |
660 | 660 |
661 static PyTypeObject WindowsVersionType = {0, 0, 0, 0, 0, 0}; | 661 static PyTypeObject WindowsVersionType = {0, 0, 0, 0, 0, 0}; |
662 | 662 |
663 static PyStructSequence_Field windows_version_fields[] = { | 663 static PyStructSequence_Field windows_version_fields[] = { |
664 » {"major", "Major version number"}, | 664 {"major", "Major version number"}, |
665 » {"minor", "Minor version number"}, | 665 {"minor", "Minor version number"}, |
666 » {"build", "Build number"}, | 666 {"build", "Build number"}, |
667 » {"platform", "Operating system platform"}, | 667 {"platform", "Operating system platform"}, |
668 » {"service_pack", "Latest Service Pack installed on the system"}, | 668 {"service_pack", "Latest Service Pack installed on the system"}, |
669 » {"service_pack_major", "Service Pack major version number"}, | 669 {"service_pack_major", "Service Pack major version number"}, |
670 » {"service_pack_minor", "Service Pack minor version number"}, | 670 {"service_pack_minor", "Service Pack minor version number"}, |
671 » {"suite_mask", "Bit mask identifying available product suites"}, | 671 {"suite_mask", "Bit mask identifying available product suites"}, |
672 » {"product_type", "System product type"}, | 672 {"product_type", "System product type"}, |
673 » {0} | 673 {0} |
674 }; | 674 }; |
675 | 675 |
676 static PyStructSequence_Desc windows_version_desc = { | 676 static PyStructSequence_Desc windows_version_desc = { |
677 » "sys.getwindowsversion", /* name */ | 677 "sys.getwindowsversion", /* name */ |
678 » getwindowsversion_doc, /* doc */ | 678 getwindowsversion_doc, /* doc */ |
679 » windows_version_fields, /* fields */ | 679 windows_version_fields, /* fields */ |
680 » 5 /* For backward compatibility, | 680 5 /* For backward compatibility, |
681 only the first 5 items are accessible | 681 only the first 5 items are accessible |
682 via indexing, the rest are name only */ | 682 via indexing, the rest are name only */ |
683 }; | 683 }; |
684 | 684 |
685 static PyObject * | 685 static PyObject * |
686 sys_getwindowsversion(PyObject *self) | 686 sys_getwindowsversion(PyObject *self) |
687 { | 687 { |
688 » PyObject *version; | 688 PyObject *version; |
689 » int pos = 0; | 689 int pos = 0; |
690 » OSVERSIONINFOEX ver; | 690 OSVERSIONINFOEX ver; |
691 » ver.dwOSVersionInfoSize = sizeof(ver); | 691 ver.dwOSVersionInfoSize = sizeof(ver); |
692 » if (!GetVersionEx((OSVERSIONINFO*) &ver)) | 692 if (!GetVersionEx((OSVERSIONINFO*) &ver)) |
693 » » return PyErr_SetFromWindowsErr(0); | 693 return PyErr_SetFromWindowsErr(0); |
694 | 694 |
695 » version = PyStructSequence_New(&WindowsVersionType); | 695 version = PyStructSequence_New(&WindowsVersionType); |
696 » if (version == NULL) | 696 if (version == NULL) |
697 » » return NULL; | 697 return NULL; |
698 | 698 |
699 » PyStructSequence_SET_ITEM(version, pos++, PyLong_FromLong(ver.dwMajorVer sion)); | 699 PyStructSequence_SET_ITEM(version, pos++, PyLong_FromLong(ver.dwMajorVersion )); |
700 » PyStructSequence_SET_ITEM(version, pos++, PyLong_FromLong(ver.dwMinorVer sion)); | 700 PyStructSequence_SET_ITEM(version, pos++, PyLong_FromLong(ver.dwMinorVersion )); |
701 » PyStructSequence_SET_ITEM(version, pos++, PyLong_FromLong(ver.dwBuildNum ber)); | 701 PyStructSequence_SET_ITEM(version, pos++, PyLong_FromLong(ver.dwBuildNumber) ); |
702 » PyStructSequence_SET_ITEM(version, pos++, PyLong_FromLong(ver.dwPlatform Id)); | 702 PyStructSequence_SET_ITEM(version, pos++, PyLong_FromLong(ver.dwPlatformId)) ; |
703 » PyStructSequence_SET_ITEM(version, pos++, PyUnicode_FromString(ver.szCSD Version)); | 703 PyStructSequence_SET_ITEM(version, pos++, PyUnicode_FromString(ver.szCSDVers ion)); |
704 » PyStructSequence_SET_ITEM(version, pos++, PyLong_FromLong(ver.wServicePa ckMajor)); | 704 PyStructSequence_SET_ITEM(version, pos++, PyLong_FromLong(ver.wServicePackMa jor)); |
705 » PyStructSequence_SET_ITEM(version, pos++, PyLong_FromLong(ver.wServicePa ckMinor)); | 705 PyStructSequence_SET_ITEM(version, pos++, PyLong_FromLong(ver.wServicePackMi nor)); |
706 » PyStructSequence_SET_ITEM(version, pos++, PyLong_FromLong(ver.wSuiteMask )); | 706 PyStructSequence_SET_ITEM(version, pos++, PyLong_FromLong(ver.wSuiteMask)); |
707 » PyStructSequence_SET_ITEM(version, pos++, PyLong_FromLong(ver.wProductTy pe)); | 707 PyStructSequence_SET_ITEM(version, pos++, PyLong_FromLong(ver.wProductType)) ; |
708 | 708 |
709 » return version; | 709 return version; |
710 } | 710 } |
711 | 711 |
712 #endif /* MS_WINDOWS */ | 712 #endif /* MS_WINDOWS */ |
713 | 713 |
714 #ifdef HAVE_DLOPEN | 714 #ifdef HAVE_DLOPEN |
715 static PyObject * | 715 static PyObject * |
716 sys_setdlopenflags(PyObject *self, PyObject *args) | 716 sys_setdlopenflags(PyObject *self, PyObject *args) |
717 { | 717 { |
718 » int new_val; | 718 int new_val; |
719 PyThreadState *tstate = PyThreadState_GET(); | 719 PyThreadState *tstate = PyThreadState_GET(); |
720 » if (!PyArg_ParseTuple(args, "i:setdlopenflags", &new_val)) | 720 if (!PyArg_ParseTuple(args, "i:setdlopenflags", &new_val)) |
721 » » return NULL; | 721 return NULL; |
722 if (!tstate) | 722 if (!tstate) |
723 » » return NULL; | 723 return NULL; |
724 tstate->interp->dlopenflags = new_val; | 724 tstate->interp->dlopenflags = new_val; |
725 » Py_INCREF(Py_None); | 725 Py_INCREF(Py_None); |
726 » return Py_None; | 726 return Py_None; |
727 } | 727 } |
728 | 728 |
729 PyDoc_STRVAR(setdlopenflags_doc, | 729 PyDoc_STRVAR(setdlopenflags_doc, |
730 "setdlopenflags(n) -> None\n\ | 730 "setdlopenflags(n) -> None\n\ |
731 \n\ | 731 \n\ |
732 Set the flags used by the interpreter for dlopen calls, such as when the\n\ | 732 Set the flags used by the interpreter for dlopen calls, such as when the\n\ |
733 interpreter loads extension modules. Among other things, this will enable\n\ | 733 interpreter loads extension modules. Among other things, this will enable\n\ |
734 a lazy resolving of symbols when importing a module, if called as\n\ | 734 a lazy resolving of symbols when importing a module, if called as\n\ |
735 sys.setdlopenflags(0). To share symbols across extension modules, call as\n\ | 735 sys.setdlopenflags(0). To share symbols across extension modules, call as\n\ |
736 sys.setdlopenflags(ctypes.RTLD_GLOBAL). Symbolic names for the flag modules\n\ | 736 sys.setdlopenflags(ctypes.RTLD_GLOBAL). Symbolic names for the flag modules\n\ |
737 can be either found in the ctypes module, or in the DLFCN module. If DLFCN\n\ | 737 can be either found in the ctypes module, or in the DLFCN module. If DLFCN\n\ |
738 is not available, it can be generated from /usr/include/dlfcn.h using the\n\ | 738 is not available, it can be generated from /usr/include/dlfcn.h using the\n\ |
739 h2py script."); | 739 h2py script."); |
740 | 740 |
741 static PyObject * | 741 static PyObject * |
742 sys_getdlopenflags(PyObject *self, PyObject *args) | 742 sys_getdlopenflags(PyObject *self, PyObject *args) |
743 { | 743 { |
744 PyThreadState *tstate = PyThreadState_GET(); | 744 PyThreadState *tstate = PyThreadState_GET(); |
745 if (!tstate) | 745 if (!tstate) |
746 » » return NULL; | 746 return NULL; |
747 return PyLong_FromLong(tstate->interp->dlopenflags); | 747 return PyLong_FromLong(tstate->interp->dlopenflags); |
748 } | 748 } |
749 | 749 |
750 PyDoc_STRVAR(getdlopenflags_doc, | 750 PyDoc_STRVAR(getdlopenflags_doc, |
751 "getdlopenflags() -> int\n\ | 751 "getdlopenflags() -> int\n\ |
752 \n\ | 752 \n\ |
753 Return the current value of the flags that are used for dlopen calls.\n\ | 753 Return the current value of the flags that are used for dlopen calls.\n\ |
754 The flag constants are defined in the ctypes and DLFCN modules."); | 754 The flag constants are defined in the ctypes and DLFCN modules."); |
755 | 755 |
756 #endif» /* HAVE_DLOPEN */ | 756 #endif /* HAVE_DLOPEN */ |
757 | 757 |
758 #ifdef USE_MALLOPT | 758 #ifdef USE_MALLOPT |
759 /* Link with -lmalloc (or -lmpc) on an SGI */ | 759 /* Link with -lmalloc (or -lmpc) on an SGI */ |
760 #include <malloc.h> | 760 #include <malloc.h> |
761 | 761 |
762 static PyObject * | 762 static PyObject * |
763 sys_mdebug(PyObject *self, PyObject *args) | 763 sys_mdebug(PyObject *self, PyObject *args) |
764 { | 764 { |
765 » int flag; | 765 int flag; |
766 » if (!PyArg_ParseTuple(args, "i:mdebug", &flag)) | 766 if (!PyArg_ParseTuple(args, "i:mdebug", &flag)) |
767 » » return NULL; | 767 return NULL; |
768 » mallopt(M_DEBUG, flag); | 768 mallopt(M_DEBUG, flag); |
769 » Py_INCREF(Py_None); | 769 Py_INCREF(Py_None); |
770 » return Py_None; | 770 return Py_None; |
771 } | 771 } |
772 #endif /* USE_MALLOPT */ | 772 #endif /* USE_MALLOPT */ |
773 | 773 |
774 static PyObject * | 774 static PyObject * |
775 sys_getsizeof(PyObject *self, PyObject *args, PyObject *kwds) | 775 sys_getsizeof(PyObject *self, PyObject *args, PyObject *kwds) |
776 { | 776 { |
777 » PyObject *res = NULL; | 777 PyObject *res = NULL; |
778 » static PyObject *str__sizeof__ = NULL, *gc_head_size = NULL; | 778 static PyObject *str__sizeof__ = NULL, *gc_head_size = NULL; |
779 » static char *kwlist[] = {"object", "default", 0}; | 779 static char *kwlist[] = {"object", "default", 0}; |
780 » PyObject *o, *dflt = NULL; | 780 PyObject *o, *dflt = NULL; |
781 » PyObject *method; | 781 PyObject *method; |
782 | 782 |
783 » if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O:getsizeof", | 783 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O:getsizeof", |
784 » » » » » kwlist, &o, &dflt)) | 784 kwlist, &o, &dflt)) |
785 » » return NULL; | 785 return NULL; |
786 | 786 |
787 /* Initialize static variable for GC head size */ | 787 /* Initialize static variable for GC head size */ |
788 » if (gc_head_size == NULL) { | 788 if (gc_head_size == NULL) { |
789 » » gc_head_size = PyLong_FromSsize_t(sizeof(PyGC_Head)); | 789 gc_head_size = PyLong_FromSsize_t(sizeof(PyGC_Head)); |
790 » » if (gc_head_size == NULL) | 790 if (gc_head_size == NULL) |
791 » » » return NULL; | 791 return NULL; |
792 » } | 792 } |
793 »······· | 793 |
794 » /* Make sure the type is initialized. float gets initialized late */ | 794 /* Make sure the type is initialized. float gets initialized late */ |
795 » if (PyType_Ready(Py_TYPE(o)) < 0) | 795 if (PyType_Ready(Py_TYPE(o)) < 0) |
796 » » return NULL; | 796 return NULL; |
797 | 797 |
798 » method = _PyObject_LookupSpecial(o, "__sizeof__", | 798 method = _PyObject_LookupSpecial(o, "__sizeof__", |
799 » » » » » &str__sizeof__); | 799 &str__sizeof__); |
800 » if (method == NULL) { | 800 if (method == NULL) { |
801 » » if (!PyErr_Occurred()) | 801 if (!PyErr_Occurred()) |
802 » » » PyErr_Format(PyExc_TypeError, | 802 PyErr_Format(PyExc_TypeError, |
803 » » » » "Type %.100s doesn't define __sizeof__", | 803 "Type %.100s doesn't define __sizeof__", |
804 » » » » Py_TYPE(o)->tp_name); | 804 Py_TYPE(o)->tp_name); |
805 » } | 805 } |
806 » else { | 806 else { |
807 » » res = PyObject_CallFunctionObjArgs(method, NULL); | 807 res = PyObject_CallFunctionObjArgs(method, NULL); |
808 » » Py_DECREF(method); | 808 Py_DECREF(method); |
809 » } | 809 } |
810 »······· | 810 |
811 » /* Has a default value been given */ | 811 /* Has a default value been given */ |
812 » if ((res == NULL) && (dflt != NULL) && | 812 if ((res == NULL) && (dflt != NULL) && |
813 » PyErr_ExceptionMatches(PyExc_TypeError)) | 813 PyErr_ExceptionMatches(PyExc_TypeError)) |
814 » { | 814 { |
815 » » PyErr_Clear(); | 815 PyErr_Clear(); |
816 » » Py_INCREF(dflt); | 816 Py_INCREF(dflt); |
817 » » return dflt; | 817 return dflt; |
818 » } | 818 } |
819 » else if (res == NULL) | 819 else if (res == NULL) |
820 » » return res; | 820 return res; |
821 | 821 |
822 » /* add gc_head size */ | 822 /* add gc_head size */ |
823 » if (PyObject_IS_GC(o)) { | 823 if (PyObject_IS_GC(o)) { |
824 » » PyObject *tmp = res; | 824 PyObject *tmp = res; |
825 » » res = PyNumber_Add(tmp, gc_head_size); | 825 res = PyNumber_Add(tmp, gc_head_size); |
826 » » Py_DECREF(tmp); | 826 Py_DECREF(tmp); |
827 » } | 827 } |
828 » return res; | 828 return res; |
829 } | 829 } |
830 | 830 |
831 PyDoc_STRVAR(getsizeof_doc, | 831 PyDoc_STRVAR(getsizeof_doc, |
832 "getsizeof(object, default) -> int\n\ | 832 "getsizeof(object, default) -> int\n\ |
833 \n\ | 833 \n\ |
834 Return the size of object in bytes."); | 834 Return the size of object in bytes."); |
835 | 835 |
836 static PyObject * | 836 static PyObject * |
837 sys_getrefcount(PyObject *self, PyObject *arg) | 837 sys_getrefcount(PyObject *self, PyObject *arg) |
838 { | 838 { |
839 » return PyLong_FromSsize_t(arg->ob_refcnt); | 839 return PyLong_FromSsize_t(arg->ob_refcnt); |
840 } | 840 } |
841 | 841 |
842 #ifdef Py_REF_DEBUG | 842 #ifdef Py_REF_DEBUG |
843 static PyObject * | 843 static PyObject * |
844 sys_gettotalrefcount(PyObject *self) | 844 sys_gettotalrefcount(PyObject *self) |
845 { | 845 { |
846 » return PyLong_FromSsize_t(_Py_GetRefTotal()); | 846 return PyLong_FromSsize_t(_Py_GetRefTotal()); |
847 } | 847 } |
848 #endif /* Py_REF_DEBUG */ | 848 #endif /* Py_REF_DEBUG */ |
849 | 849 |
850 PyDoc_STRVAR(getrefcount_doc, | 850 PyDoc_STRVAR(getrefcount_doc, |
851 "getrefcount(object) -> integer\n\ | 851 "getrefcount(object) -> integer\n\ |
852 \n\ | 852 \n\ |
853 Return the reference count of object. The count returned is generally\n\ | 853 Return the reference count of object. The count returned is generally\n\ |
854 one higher than you might expect, because it includes the (temporary)\n\ | 854 one higher than you might expect, because it includes the (temporary)\n\ |
855 reference as an argument to getrefcount()." | 855 reference as an argument to getrefcount()." |
856 ); | 856 ); |
857 | 857 |
858 #ifdef COUNT_ALLOCS | 858 #ifdef COUNT_ALLOCS |
859 static PyObject * | 859 static PyObject * |
860 sys_getcounts(PyObject *self) | 860 sys_getcounts(PyObject *self) |
861 { | 861 { |
862 » extern PyObject *get_counts(void); | 862 extern PyObject *get_counts(void); |
863 | 863 |
864 » return get_counts(); | 864 return get_counts(); |
865 } | 865 } |
866 #endif | 866 #endif |
867 | 867 |
868 PyDoc_STRVAR(getframe_doc, | 868 PyDoc_STRVAR(getframe_doc, |
869 "_getframe([depth]) -> frameobject\n\ | 869 "_getframe([depth]) -> frameobject\n\ |
870 \n\ | 870 \n\ |
871 Return a frame object from the call stack. If optional integer depth is\n\ | 871 Return a frame object from the call stack. If optional integer depth is\n\ |
872 given, return the frame object that many calls below the top of the stack.\n\ | 872 given, return the frame object that many calls below the top of the stack.\n\ |
873 If that is deeper than the call stack, ValueError is raised. The default\n\ | 873 If that is deeper than the call stack, ValueError is raised. The default\n\ |
874 for depth is zero, returning the frame at the top of the call stack.\n\ | 874 for depth is zero, returning the frame at the top of the call stack.\n\ |
875 \n\ | 875 \n\ |
876 This function should be used for internal and specialized\n\ | 876 This function should be used for internal and specialized\n\ |
877 purposes only." | 877 purposes only." |
878 ); | 878 ); |
879 | 879 |
880 static PyObject * | 880 static PyObject * |
881 sys_getframe(PyObject *self, PyObject *args) | 881 sys_getframe(PyObject *self, PyObject *args) |
882 { | 882 { |
883 » PyFrameObject *f = PyThreadState_GET()->frame; | 883 PyFrameObject *f = PyThreadState_GET()->frame; |
884 » int depth = -1; | 884 int depth = -1; |
885 | 885 |
886 » if (!PyArg_ParseTuple(args, "|i:_getframe", &depth)) | 886 if (!PyArg_ParseTuple(args, "|i:_getframe", &depth)) |
887 » » return NULL; | 887 return NULL; |
888 | 888 |
889 » while (depth > 0 && f != NULL) { | 889 while (depth > 0 && f != NULL) { |
890 » » f = f->f_back; | 890 f = f->f_back; |
891 » » --depth; | 891 --depth; |
892 » } | 892 } |
893 » if (f == NULL) { | 893 if (f == NULL) { |
894 » » PyErr_SetString(PyExc_ValueError, | 894 PyErr_SetString(PyExc_ValueError, |
895 » » » » "call stack is not deep enough"); | 895 "call stack is not deep enough"); |
896 » » return NULL; | 896 return NULL; |
897 » } | 897 } |
898 » Py_INCREF(f); | 898 Py_INCREF(f); |
899 » return (PyObject*)f; | 899 return (PyObject*)f; |
900 } | 900 } |
901 | 901 |
902 PyDoc_STRVAR(current_frames_doc, | 902 PyDoc_STRVAR(current_frames_doc, |
903 "_current_frames() -> dictionary\n\ | 903 "_current_frames() -> dictionary\n\ |
904 \n\ | 904 \n\ |
905 Return a dictionary mapping each current thread T's thread id to T's\n\ | 905 Return a dictionary mapping each current thread T's thread id to T's\n\ |
906 current stack frame.\n\ | 906 current stack frame.\n\ |
907 \n\ | 907 \n\ |
908 This function should be used for specialized purposes only." | 908 This function should be used for specialized purposes only." |
909 ); | 909 ); |
910 | 910 |
911 static PyObject * | 911 static PyObject * |
912 sys_current_frames(PyObject *self, PyObject *noargs) | 912 sys_current_frames(PyObject *self, PyObject *noargs) |
913 { | 913 { |
914 » return _PyThread_CurrentFrames(); | 914 return _PyThread_CurrentFrames(); |
915 } | 915 } |
916 | 916 |
917 PyDoc_STRVAR(call_tracing_doc, | 917 PyDoc_STRVAR(call_tracing_doc, |
918 "call_tracing(func, args) -> object\n\ | 918 "call_tracing(func, args) -> object\n\ |
919 \n\ | 919 \n\ |
920 Call func(*args), while tracing is enabled. The tracing state is\n\ | 920 Call func(*args), while tracing is enabled. The tracing state is\n\ |
921 saved, and restored afterwards. This is intended to be called from\n\ | 921 saved, and restored afterwards. This is intended to be called from\n\ |
922 a debugger from a checkpoint, to recursively debug some other code." | 922 a debugger from a checkpoint, to recursively debug some other code." |
923 ); | 923 ); |
924 | 924 |
925 static PyObject * | 925 static PyObject * |
926 sys_call_tracing(PyObject *self, PyObject *args) | 926 sys_call_tracing(PyObject *self, PyObject *args) |
927 { | 927 { |
928 » PyObject *func, *funcargs; | 928 PyObject *func, *funcargs; |
929 » if (!PyArg_ParseTuple(args, "OO!:call_tracing", &func, &PyTuple_Type, &f uncargs)) | 929 if (!PyArg_ParseTuple(args, "OO!:call_tracing", &func, &PyTuple_Type, &funca rgs)) |
930 » » return NULL; | 930 return NULL; |
931 » return _PyEval_CallTracing(func, funcargs); | 931 return _PyEval_CallTracing(func, funcargs); |
932 } | 932 } |
933 | 933 |
934 PyDoc_STRVAR(callstats_doc, | 934 PyDoc_STRVAR(callstats_doc, |
935 "callstats() -> tuple of integers\n\ | 935 "callstats() -> tuple of integers\n\ |
936 \n\ | 936 \n\ |
937 Return a tuple of function call statistics, if CALL_PROFILE was defined\n\ | 937 Return a tuple of function call statistics, if CALL_PROFILE was defined\n\ |
938 when Python was built. Otherwise, return None.\n\ | 938 when Python was built. Otherwise, return None.\n\ |
939 \n\ | 939 \n\ |
940 When enabled, this function returns detailed, implementation-specific\n\ | 940 When enabled, this function returns detailed, implementation-specific\n\ |
941 details about the number of function calls executed. The return value is\n\ | 941 details about the number of function calls executed. The return value is\n\ |
(...skipping 26 matching lines...) Expand all Loading... | |
968 extern PyObject *_Py_GetDXProfile(PyObject *, PyObject *); | 968 extern PyObject *_Py_GetDXProfile(PyObject *, PyObject *); |
969 #endif | 969 #endif |
970 | 970 |
971 #ifdef __cplusplus | 971 #ifdef __cplusplus |
972 } | 972 } |
973 #endif | 973 #endif |
974 | 974 |
975 static PyObject * | 975 static PyObject * |
976 sys_clear_type_cache(PyObject* self, PyObject* args) | 976 sys_clear_type_cache(PyObject* self, PyObject* args) |
977 { | 977 { |
978 » PyType_ClearCache(); | 978 PyType_ClearCache(); |
979 » Py_RETURN_NONE; | 979 Py_RETURN_NONE; |
980 } | 980 } |
981 | 981 |
982 PyDoc_STRVAR(sys_clear_type_cache__doc__, | 982 PyDoc_STRVAR(sys_clear_type_cache__doc__, |
983 "_clear_type_cache() -> None\n\ | 983 "_clear_type_cache() -> None\n\ |
984 Clear the internal type lookup cache."); | 984 Clear the internal type lookup cache."); |
985 | 985 |
986 | 986 |
987 static PyMethodDef sys_methods[] = { | 987 static PyMethodDef sys_methods[] = { |
988 » /* Might as well keep this in alphabetic order */ | 988 /* Might as well keep this in alphabetic order */ |
989 » {"callstats", (PyCFunction)PyEval_GetCallStats, METH_NOARGS, | 989 {"callstats", (PyCFunction)PyEval_GetCallStats, METH_NOARGS, |
990 » callstats_doc}, | 990 callstats_doc}, |
991 » {"_clear_type_cache",» sys_clear_type_cache,» METH_NOARGS, | 991 {"_clear_type_cache", sys_clear_type_cache, METH_NOARGS, |
992 » sys_clear_type_cache__doc__}, | 992 sys_clear_type_cache__doc__}, |
993 » {"_current_frames", sys_current_frames, METH_NOARGS, | 993 {"_current_frames", sys_current_frames, METH_NOARGS, |
994 » current_frames_doc}, | 994 current_frames_doc}, |
995 » {"displayhook",»sys_displayhook, METH_O, displayhook_doc}, | 995 {"displayhook", sys_displayhook, METH_O, displayhook_doc}, |
996 » {"exc_info",» sys_exc_info, METH_NOARGS, exc_info_doc}, | 996 {"exc_info", sys_exc_info, METH_NOARGS, exc_info_doc}, |
997 » {"excepthook",» sys_excepthook, METH_VARARGS, excepthook_doc}, | 997 {"excepthook", sys_excepthook, METH_VARARGS, excepthook_doc}, |
998 » {"exit",» sys_exit, METH_VARARGS, exit_doc}, | 998 {"exit", sys_exit, METH_VARARGS, exit_doc}, |
999 » {"getdefaultencoding", (PyCFunction)sys_getdefaultencoding, | 999 {"getdefaultencoding", (PyCFunction)sys_getdefaultencoding, |
1000 » METH_NOARGS, getdefaultencoding_doc}, | 1000 METH_NOARGS, getdefaultencoding_doc}, |
1001 #ifdef HAVE_DLOPEN | 1001 #ifdef HAVE_DLOPEN |
1002 » {"getdlopenflags", (PyCFunction)sys_getdlopenflags, METH_NOARGS, | 1002 {"getdlopenflags", (PyCFunction)sys_getdlopenflags, METH_NOARGS, |
1003 » getdlopenflags_doc}, | 1003 getdlopenflags_doc}, |
1004 #endif | 1004 #endif |
1005 #ifdef COUNT_ALLOCS | 1005 #ifdef COUNT_ALLOCS |
1006 » {"getcounts",» (PyCFunction)sys_getcounts, METH_NOARGS}, | 1006 {"getcounts", (PyCFunction)sys_getcounts, METH_NOARGS}, |
1007 #endif | 1007 #endif |
1008 #ifdef DYNAMIC_EXECUTION_PROFILE | 1008 #ifdef DYNAMIC_EXECUTION_PROFILE |
1009 » {"getdxp",» _Py_GetDXProfile, METH_VARARGS}, | 1009 {"getdxp", _Py_GetDXProfile, METH_VARARGS}, |
1010 #endif | 1010 #endif |
1011 » {"getfilesystemencoding", (PyCFunction)sys_getfilesystemencoding, | 1011 {"getfilesystemencoding", (PyCFunction)sys_getfilesystemencoding, |
1012 » METH_NOARGS, getfilesystemencoding_doc}, | 1012 METH_NOARGS, getfilesystemencoding_doc}, |
1013 #ifdef Py_TRACE_REFS | 1013 #ifdef Py_TRACE_REFS |
1014 » {"getobjects",» _Py_GetObjects, METH_VARARGS}, | 1014 {"getobjects", _Py_GetObjects, METH_VARARGS}, |
1015 #endif | 1015 #endif |
1016 #ifdef Py_REF_DEBUG | 1016 #ifdef Py_REF_DEBUG |
1017 » {"gettotalrefcount", (PyCFunction)sys_gettotalrefcount, METH_NOARGS}, | 1017 {"gettotalrefcount", (PyCFunction)sys_gettotalrefcount, METH_NOARGS}, |
1018 #endif | 1018 #endif |
1019 » {"getrefcount",»(PyCFunction)sys_getrefcount, METH_O, getrefcount_doc}, | 1019 {"getrefcount", (PyCFunction)sys_getrefcount, METH_O, getrefcount_doc}, |
1020 » {"getrecursionlimit", (PyCFunction)sys_getrecursionlimit, METH_NOARGS, | 1020 {"getrecursionlimit", (PyCFunction)sys_getrecursionlimit, METH_NOARGS, |
1021 » getrecursionlimit_doc}, | 1021 getrecursionlimit_doc}, |
1022 » {"getsizeof", (PyCFunction)sys_getsizeof, | 1022 {"getsizeof", (PyCFunction)sys_getsizeof, |
1023 » METH_VARARGS | METH_KEYWORDS, getsizeof_doc}, | 1023 METH_VARARGS | METH_KEYWORDS, getsizeof_doc}, |
1024 » {"_getframe", sys_getframe, METH_VARARGS, getframe_doc}, | 1024 {"_getframe", sys_getframe, METH_VARARGS, getframe_doc}, |
1025 #ifdef MS_WINDOWS | 1025 #ifdef MS_WINDOWS |
1026 » {"getwindowsversion", (PyCFunction)sys_getwindowsversion, METH_NOARGS, | 1026 {"getwindowsversion", (PyCFunction)sys_getwindowsversion, METH_NOARGS, |
1027 » getwindowsversion_doc}, | 1027 getwindowsversion_doc}, |
1028 #endif /* MS_WINDOWS */ | 1028 #endif /* MS_WINDOWS */ |
1029 » {"intern",» sys_intern, METH_VARARGS, intern_doc}, | 1029 {"intern", sys_intern, METH_VARARGS, intern_doc}, |
1030 #ifdef USE_MALLOPT | 1030 #ifdef USE_MALLOPT |
1031 » {"mdebug",» sys_mdebug, METH_VARARGS}, | 1031 {"mdebug", sys_mdebug, METH_VARARGS}, |
1032 #endif | 1032 #endif |
1033 » {"setdefaultencoding", sys_setdefaultencoding, METH_VARARGS, | 1033 {"setdefaultencoding", sys_setdefaultencoding, METH_VARARGS, |
1034 » setdefaultencoding_doc}, | 1034 setdefaultencoding_doc}, |
1035 » {"setfilesystemencoding", sys_setfilesystemencoding, METH_VARARGS, | 1035 {"setfilesystemencoding", sys_setfilesystemencoding, METH_VARARGS, |
1036 » setfilesystemencoding_doc}, | 1036 setfilesystemencoding_doc}, |
1037 » {"setcheckinterval",» sys_setcheckinterval, METH_VARARGS, | 1037 {"setcheckinterval", sys_setcheckinterval, METH_VARARGS, |
1038 » setcheckinterval_doc}, | 1038 setcheckinterval_doc}, |
1039 » {"getcheckinterval",» sys_getcheckinterval, METH_NOARGS, | 1039 {"getcheckinterval", sys_getcheckinterval, METH_NOARGS, |
1040 » getcheckinterval_doc}, | 1040 getcheckinterval_doc}, |
1041 #ifdef WITH_THREAD | 1041 #ifdef WITH_THREAD |
1042 » {"setswitchinterval",» sys_setswitchinterval, METH_VARARGS, | 1042 {"setswitchinterval", sys_setswitchinterval, METH_VARARGS, |
1043 » setswitchinterval_doc}, | 1043 setswitchinterval_doc}, |
1044 » {"getswitchinterval",» sys_getswitchinterval, METH_NOARGS, | 1044 {"getswitchinterval", sys_getswitchinterval, METH_NOARGS, |
1045 » getswitchinterval_doc}, | 1045 getswitchinterval_doc}, |
1046 #endif | 1046 #endif |
1047 #ifdef HAVE_DLOPEN | 1047 #ifdef HAVE_DLOPEN |
1048 » {"setdlopenflags", sys_setdlopenflags, METH_VARARGS, | 1048 {"setdlopenflags", sys_setdlopenflags, METH_VARARGS, |
1049 » setdlopenflags_doc}, | 1049 setdlopenflags_doc}, |
1050 #endif | 1050 #endif |
1051 » {"setprofile",» sys_setprofile, METH_O, setprofile_doc}, | 1051 {"setprofile", sys_setprofile, METH_O, setprofile_doc}, |
1052 » {"getprofile",» sys_getprofile, METH_NOARGS, getprofile_doc}, | 1052 {"getprofile", sys_getprofile, METH_NOARGS, getprofile_doc}, |
1053 » {"setrecursionlimit", sys_setrecursionlimit, METH_VARARGS, | 1053 {"setrecursionlimit", sys_setrecursionlimit, METH_VARARGS, |
1054 » setrecursionlimit_doc}, | 1054 setrecursionlimit_doc}, |
1055 #ifdef WITH_TSC | 1055 #ifdef WITH_TSC |
1056 » {"settscdump", sys_settscdump, METH_VARARGS, settscdump_doc}, | 1056 {"settscdump", sys_settscdump, METH_VARARGS, settscdump_doc}, |
1057 #endif | 1057 #endif |
1058 » {"settrace",» sys_settrace, METH_O, settrace_doc}, | 1058 {"settrace", sys_settrace, METH_O, settrace_doc}, |
1059 » {"gettrace",» sys_gettrace, METH_NOARGS, gettrace_doc}, | 1059 {"gettrace", sys_gettrace, METH_NOARGS, gettrace_doc}, |
1060 » {"call_tracing", sys_call_tracing, METH_VARARGS, call_tracing_doc}, | 1060 {"call_tracing", sys_call_tracing, METH_VARARGS, call_tracing_doc}, |
1061 » {NULL,» » NULL}» » /* sentinel */ | 1061 {NULL, NULL} /* sentinel */ |
1062 }; | 1062 }; |
1063 | 1063 |
1064 static PyObject * | 1064 static PyObject * |
1065 list_builtin_module_names(void) | 1065 list_builtin_module_names(void) |
1066 { | 1066 { |
1067 » PyObject *list = PyList_New(0); | 1067 PyObject *list = PyList_New(0); |
1068 » int i; | 1068 int i; |
1069 » if (list == NULL) | 1069 if (list == NULL) |
1070 » » return NULL; | 1070 return NULL; |
1071 » for (i = 0; PyImport_Inittab[i].name != NULL; i++) { | 1071 for (i = 0; PyImport_Inittab[i].name != NULL; i++) { |
1072 » » PyObject *name = PyUnicode_FromString( | 1072 PyObject *name = PyUnicode_FromString( |
1073 » » » PyImport_Inittab[i].name); | 1073 PyImport_Inittab[i].name); |
1074 » » if (name == NULL) | 1074 if (name == NULL) |
1075 » » » break; | 1075 break; |
1076 » » PyList_Append(list, name); | 1076 PyList_Append(list, name); |
1077 » » Py_DECREF(name); | 1077 Py_DECREF(name); |
1078 » } | 1078 } |
1079 » if (PyList_Sort(list) != 0) { | 1079 if (PyList_Sort(list) != 0) { |
1080 » » Py_DECREF(list); | 1080 Py_DECREF(list); |
1081 » » list = NULL; | 1081 list = NULL; |
1082 » } | 1082 } |
1083 » if (list) { | 1083 if (list) { |
1084 » » PyObject *v = PyList_AsTuple(list); | 1084 PyObject *v = PyList_AsTuple(list); |
1085 » » Py_DECREF(list); | 1085 Py_DECREF(list); |
1086 » » list = v; | 1086 list = v; |
1087 » } | 1087 } |
1088 » return list; | 1088 return list; |
1089 } | 1089 } |
1090 | 1090 |
1091 static PyObject *warnoptions = NULL; | 1091 static PyObject *warnoptions = NULL; |
1092 | 1092 |
1093 void | 1093 void |
1094 PySys_ResetWarnOptions(void) | 1094 PySys_ResetWarnOptions(void) |
1095 { | 1095 { |
1096 » if (warnoptions == NULL || !PyList_Check(warnoptions)) | 1096 if (warnoptions == NULL || !PyList_Check(warnoptions)) |
1097 » » return; | 1097 return; |
1098 » PyList_SetSlice(warnoptions, 0, PyList_GET_SIZE(warnoptions), NULL); | 1098 PyList_SetSlice(warnoptions, 0, PyList_GET_SIZE(warnoptions), NULL); |
1099 } | |
1100 | |
1101 void | |
1102 PySys_AddWarnOptionUnicode(PyObject *unicode) | |
1103 { | |
1104 if (warnoptions == NULL || !PyList_Check(warnoptions)) { | |
1105 Py_XDECREF(warnoptions); | |
1106 warnoptions = PyList_New(0); | |
1107 if (warnoptions == NULL) | |
1108 return; | |
1109 } | |
1110 PyList_Append(warnoptions, unicode); | |
1099 } | 1111 } |
1100 | 1112 |
1101 void | 1113 void |
1102 PySys_AddWarnOption(const wchar_t *s) | 1114 PySys_AddWarnOption(const wchar_t *s) |
1103 { | 1115 { |
1104 » PyObject *str; | 1116 PyObject *unicode; |
1105 | 1117 unicode = PyUnicode_FromWideChar(s, -1); |
1106 » if (warnoptions == NULL || !PyList_Check(warnoptions)) { | 1118 if (unicode == NULL) |
1107 » » Py_XDECREF(warnoptions); | 1119 return; |
1108 » » warnoptions = PyList_New(0); | 1120 PySys_AddWarnOptionUnicode(unicode); |
1109 » » if (warnoptions == NULL) | 1121 Py_DECREF(unicode); |
1110 » » » return; | |
1111 » } | |
1112 » str = PyUnicode_FromWideChar(s, -1); | |
1113 » if (str != NULL) { | |
1114 » » PyList_Append(warnoptions, str); | |
1115 » » Py_DECREF(str); | |
1116 » } | |
1117 } | 1122 } |
1118 | 1123 |
1119 int | 1124 int |
1120 PySys_HasWarnOptions(void) | 1125 PySys_HasWarnOptions(void) |
1121 { | 1126 { |
1122 return (warnoptions != NULL && (PyList_Size(warnoptions) > 0)) ? 1 : 0; | 1127 return (warnoptions != NULL && (PyList_Size(warnoptions) > 0)) ? 1 : 0; |
1123 } | 1128 } |
1124 | 1129 |
1125 /* XXX This doc string is too long to be a single string literal in VC++ 5.0. | 1130 /* XXX This doc string is too long to be a single string literal in VC++ 5.0. |
1126 Two literals concatenated works just fine. If you have a K&R compiler | 1131 Two literals concatenated works just fine. If you have a K&R compiler |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1218 static const char headurl[] = "$HeadURL: svn+ssh://pythondev@svn.python.org/pyth on/branches/py3k/Python/sysmodule.c $"; | 1223 static const char headurl[] = "$HeadURL: svn+ssh://pythondev@svn.python.org/pyth on/branches/py3k/Python/sysmodule.c $"; |
1219 static int svn_initialized; | 1224 static int svn_initialized; |
1220 static char patchlevel_revision[50]; /* Just the number */ | 1225 static char patchlevel_revision[50]; /* Just the number */ |
1221 static char branch[50]; | 1226 static char branch[50]; |
1222 static char shortbranch[50]; | 1227 static char shortbranch[50]; |
1223 static const char *svn_revision; | 1228 static const char *svn_revision; |
1224 | 1229 |
1225 static void | 1230 static void |
1226 svnversion_init(void) | 1231 svnversion_init(void) |
1227 { | 1232 { |
1228 » const char *python, *br_start, *br_end, *br_end2, *svnversion; | 1233 const char *python, *br_start, *br_end, *br_end2, *svnversion; |
1229 » Py_ssize_t len; | 1234 Py_ssize_t len; |
1230 » int istag = 0; | 1235 int istag = 0; |
1231 | 1236 |
1232 » if (svn_initialized) | 1237 if (svn_initialized) |
1233 » » return; | 1238 return; |
1234 | 1239 |
1235 » python = strstr(headurl, "/python/"); | 1240 python = strstr(headurl, "/python/"); |
1236 » if (!python) { | 1241 if (!python) { |
1237 » » strcpy(branch, "unknown branch"); | 1242 strcpy(branch, "unknown branch"); |
1238 » » strcpy(shortbranch, "unknown"); | 1243 strcpy(shortbranch, "unknown"); |
1239 » } | 1244 } |
1240 » else { | 1245 else { |
1241 » » br_start = python + 8; | 1246 br_start = python + 8; |
1242 » » br_end = strchr(br_start, '/'); | 1247 br_end = strchr(br_start, '/'); |
1243 » » assert(br_end); | 1248 assert(br_end); |
1244 | 1249 |
1245 » » /* Works even for trunk, | 1250 /* Works even for trunk, |
1246 » » as we are in trunk/Python/sysmodule.c */ | 1251 as we are in trunk/Python/sysmodule.c */ |
1247 » » br_end2 = strchr(br_end+1, '/'); | 1252 br_end2 = strchr(br_end+1, '/'); |
1248 | 1253 |
1249 » » istag = strncmp(br_start, "tags", 4) == 0; | 1254 istag = strncmp(br_start, "tags", 4) == 0; |
1250 » » if (strncmp(br_start, "trunk", 5) == 0) { | 1255 if (strncmp(br_start, "trunk", 5) == 0) { |
1251 » » » strcpy(branch, "trunk"); | 1256 strcpy(branch, "trunk"); |
1252 » » » strcpy(shortbranch, "trunk"); | 1257 strcpy(shortbranch, "trunk"); |
1253 » » } | 1258 } |
1254 » » else if (istag || strncmp(br_start, "branches", 8) == 0) { | 1259 else if (istag || strncmp(br_start, "branches", 8) == 0) { |
1255 » » » len = br_end2 - br_start; | 1260 len = br_end2 - br_start; |
1256 » » » strncpy(branch, br_start, len); | 1261 strncpy(branch, br_start, len); |
1257 » » » branch[len] = '\0'; | 1262 branch[len] = '\0'; |
1258 | 1263 |
1259 » » » len = br_end2 - (br_end + 1); | 1264 len = br_end2 - (br_end + 1); |
1260 » » » strncpy(shortbranch, br_end + 1, len); | 1265 strncpy(shortbranch, br_end + 1, len); |
1261 » » » shortbranch[len] = '\0'; | 1266 shortbranch[len] = '\0'; |
1262 » » } | 1267 } |
1263 » » else { | 1268 else { |
1264 » » » Py_FatalError("bad HeadURL"); | 1269 Py_FatalError("bad HeadURL"); |
1265 » » » return; | 1270 return; |
1266 » » } | 1271 } |
1267 » } | 1272 } |
1268 | 1273 |
1269 | 1274 |
1270 » svnversion = _Py_svnversion(); | 1275 svnversion = _Py_svnversion(); |
1271 » if (strcmp(svnversion, "Unversioned directory") != 0 && strcmp(svnversio n, "exported") != 0) | 1276 if (strcmp(svnversion, "Unversioned directory") != 0 && strcmp(svnversion, " exported") != 0) |
1272 » » svn_revision = svnversion; | 1277 svn_revision = svnversion; |
1273 » else if (istag) { | 1278 else if (istag) { |
1274 » » len = strlen(_patchlevel_revision); | 1279 len = strlen(_patchlevel_revision); |
1275 » » assert(len >= 13); | 1280 assert(len >= 13); |
1276 » » assert(len < (sizeof(patchlevel_revision) + 13)); | 1281 assert(len < (sizeof(patchlevel_revision) + 13)); |
1277 » » strncpy(patchlevel_revision, _patchlevel_revision + 11, | 1282 strncpy(patchlevel_revision, _patchlevel_revision + 11, |
1278 » » » len - 13); | 1283 len - 13); |
1279 » » patchlevel_revision[len - 13] = '\0'; | 1284 patchlevel_revision[len - 13] = '\0'; |
1280 » » svn_revision = patchlevel_revision; | 1285 svn_revision = patchlevel_revision; |
1281 » } | 1286 } |
1282 » else | 1287 else |
1283 » » svn_revision = ""; | 1288 svn_revision = ""; |
1284 | 1289 |
1285 » svn_initialized = 1; | 1290 svn_initialized = 1; |
1286 } | 1291 } |
1287 | 1292 |
1288 /* Return svnversion output if available. | 1293 /* Return svnversion output if available. |
1289 Else return Revision of patchlevel.h if on branch. | 1294 Else return Revision of patchlevel.h if on branch. |
1290 Else return empty string */ | 1295 Else return empty string */ |
1291 const char* | 1296 const char* |
1292 Py_SubversionRevision() | 1297 Py_SubversionRevision() |
1293 { | 1298 { |
1294 » svnversion_init(); | 1299 svnversion_init(); |
1295 » return svn_revision; | 1300 return svn_revision; |
1296 } | 1301 } |
1297 | 1302 |
1298 const char* | 1303 const char* |
1299 Py_SubversionShortBranch() | 1304 Py_SubversionShortBranch() |
1300 { | 1305 { |
1301 » svnversion_init(); | 1306 svnversion_init(); |
1302 » return shortbranch; | 1307 return shortbranch; |
1303 } | 1308 } |
1304 | 1309 |
1305 | 1310 |
1306 PyDoc_STRVAR(flags__doc__, | 1311 PyDoc_STRVAR(flags__doc__, |
1307 "sys.flags\n\ | 1312 "sys.flags\n\ |
1308 \n\ | 1313 \n\ |
1309 Flags provided through command line arguments or environment vars."); | 1314 Flags provided through command line arguments or environment vars."); |
1310 | 1315 |
1311 static PyTypeObject FlagsType; | 1316 static PyTypeObject FlagsType; |
1312 | 1317 |
1313 static PyStructSequence_Field flags_fields[] = { | 1318 static PyStructSequence_Field flags_fields[] = { |
1314 » {"debug",» » "-d"}, | 1319 {"debug", "-d"}, |
1315 » {"division_warning",» "-Q"}, | 1320 {"division_warning", "-Q"}, |
1316 » {"inspect",» » "-i"}, | 1321 {"inspect", "-i"}, |
1317 » {"interactive",»» "-i"}, | 1322 {"interactive", "-i"}, |
1318 » {"optimize",» » "-O or -OO"}, | 1323 {"optimize", "-O or -OO"}, |
1319 » {"dont_write_bytecode",»"-B"}, | 1324 {"dont_write_bytecode", "-B"}, |
1320 » {"no_user_site",» "-s"}, | 1325 {"no_user_site", "-s"}, |
1321 » {"no_site",» » "-S"}, | 1326 {"no_site", "-S"}, |
1322 » {"ignore_environment",» "-E"}, | 1327 {"ignore_environment", "-E"}, |
1323 » {"verbose",» » "-v"}, | 1328 {"verbose", "-v"}, |
1324 #ifdef RISCOS | 1329 #ifdef RISCOS |
1325 » {"riscos_wimp",»» "???"}, | 1330 {"riscos_wimp", "???"}, |
1326 #endif | 1331 #endif |
1327 » /* {"unbuffered",» » "-u"}, */ | 1332 /* {"unbuffered", "-u"}, */ |
1328 » /* {"skip_first",» » "-x"}, */ | 1333 /* {"skip_first", "-x"}, */ |
1329 » {"bytes_warning", "-b"}, | 1334 {"bytes_warning", "-b"}, |
1330 » {0} | 1335 {0} |
1331 }; | 1336 }; |
1332 | 1337 |
1333 static PyStructSequence_Desc flags_desc = { | 1338 static PyStructSequence_Desc flags_desc = { |
1334 » "sys.flags",» /* name */ | 1339 "sys.flags", /* name */ |
1335 » flags__doc__,» /* doc */ | 1340 flags__doc__, /* doc */ |
1336 » flags_fields,» /* fields */ | 1341 flags_fields, /* fields */ |
1337 #ifdef RISCOS | 1342 #ifdef RISCOS |
1338 » 12 | 1343 12 |
1339 #else | 1344 #else |
1340 » 11 | 1345 11 |
1341 #endif | 1346 #endif |
1342 }; | 1347 }; |
1343 | 1348 |
1344 static PyObject* | 1349 static PyObject* |
1345 make_flags(void) | 1350 make_flags(void) |
1346 { | 1351 { |
1347 » int pos = 0; | 1352 int pos = 0; |
1348 » PyObject *seq; | 1353 PyObject *seq; |
1349 | 1354 |
1350 » seq = PyStructSequence_New(&FlagsType); | 1355 seq = PyStructSequence_New(&FlagsType); |
1351 » if (seq == NULL) | 1356 if (seq == NULL) |
1352 » » return NULL; | 1357 return NULL; |
1353 | 1358 |
1354 #define SetFlag(flag) \ | 1359 #define SetFlag(flag) \ |
1355 » PyStructSequence_SET_ITEM(seq, pos++, PyLong_FromLong(flag)) | 1360 PyStructSequence_SET_ITEM(seq, pos++, PyLong_FromLong(flag)) |
1356 | 1361 |
1357 » SetFlag(Py_DebugFlag); | 1362 SetFlag(Py_DebugFlag); |
1358 » SetFlag(Py_DivisionWarningFlag); | 1363 SetFlag(Py_DivisionWarningFlag); |
1359 » SetFlag(Py_InspectFlag); | 1364 SetFlag(Py_InspectFlag); |
1360 » SetFlag(Py_InteractiveFlag); | 1365 SetFlag(Py_InteractiveFlag); |
1361 » SetFlag(Py_OptimizeFlag); | 1366 SetFlag(Py_OptimizeFlag); |
1362 » SetFlag(Py_DontWriteBytecodeFlag); | 1367 SetFlag(Py_DontWriteBytecodeFlag); |
1363 » SetFlag(Py_NoUserSiteDirectory); | 1368 SetFlag(Py_NoUserSiteDirectory); |
1364 » SetFlag(Py_NoSiteFlag); | 1369 SetFlag(Py_NoSiteFlag); |
1365 » SetFlag(Py_IgnoreEnvironmentFlag); | 1370 SetFlag(Py_IgnoreEnvironmentFlag); |
1366 » SetFlag(Py_VerboseFlag); | 1371 SetFlag(Py_VerboseFlag); |
1367 #ifdef RISCOS | 1372 #ifdef RISCOS |
1368 » SetFlag(Py_RISCOSWimpFlag); | 1373 SetFlag(Py_RISCOSWimpFlag); |
1369 #endif | 1374 #endif |
1370 » /* SetFlag(saw_unbuffered_flag); */ | 1375 /* SetFlag(saw_unbuffered_flag); */ |
1371 » /* SetFlag(skipfirstline); */ | 1376 /* SetFlag(skipfirstline); */ |
1372 SetFlag(Py_BytesWarningFlag); | 1377 SetFlag(Py_BytesWarningFlag); |
1373 #undef SetFlag | 1378 #undef SetFlag |
1374 | 1379 |
1375 » if (PyErr_Occurred()) { | 1380 if (PyErr_Occurred()) { |
1376 » » return NULL; | 1381 return NULL; |
1377 » } | 1382 } |
1378 » return seq; | 1383 return seq; |
1379 } | 1384 } |
1380 | 1385 |
1381 PyDoc_STRVAR(version_info__doc__, | 1386 PyDoc_STRVAR(version_info__doc__, |
1382 "sys.version_info\n\ | 1387 "sys.version_info\n\ |
1383 \n\ | 1388 \n\ |
1384 Version information as a named tuple."); | 1389 Version information as a named tuple."); |
1385 | 1390 |
1386 static PyTypeObject VersionInfoType; | 1391 static PyTypeObject VersionInfoType; |
1387 | 1392 |
1388 static PyStructSequence_Field version_info_fields[] = { | 1393 static PyStructSequence_Field version_info_fields[] = { |
1389 » {"major", "Major release number"}, | 1394 {"major", "Major release number"}, |
1390 » {"minor", "Minor release number"}, | 1395 {"minor", "Minor release number"}, |
1391 » {"micro", "Patch release number"}, | 1396 {"micro", "Patch release number"}, |
1392 » {"releaselevel", "'alpha', 'beta', 'candidate', or 'release'"}, | 1397 {"releaselevel", "'alpha', 'beta', 'candidate', or 'release'"}, |
1393 » {"serial", "Serial release number"}, | 1398 {"serial", "Serial release number"}, |
1394 » {0} | 1399 {0} |
1395 }; | 1400 }; |
1396 | 1401 |
1397 static PyStructSequence_Desc version_info_desc = { | 1402 static PyStructSequence_Desc version_info_desc = { |
1398 » "sys.version_info", /* name */ | 1403 "sys.version_info", /* name */ |
1399 » version_info__doc__, /* doc */ | 1404 version_info__doc__, /* doc */ |
1400 » version_info_fields, /* fields */ | 1405 version_info_fields, /* fields */ |
1401 » 5 | 1406 5 |
1402 }; | 1407 }; |
1403 | 1408 |
1404 static PyObject * | 1409 static PyObject * |
1405 make_version_info(void) | 1410 make_version_info(void) |
1406 { | 1411 { |
1407 » PyObject *version_info; | 1412 PyObject *version_info; |
1408 » char *s; | 1413 char *s; |
1409 » int pos = 0; | 1414 int pos = 0; |
1410 | 1415 |
1411 » version_info = PyStructSequence_New(&VersionInfoType); | 1416 version_info = PyStructSequence_New(&VersionInfoType); |
1412 » if (version_info == NULL) { | 1417 if (version_info == NULL) { |
1413 » » return NULL; | 1418 return NULL; |
1414 » } | 1419 } |
1415 | 1420 |
1416 » /* | 1421 /* |
1417 » * These release level checks are mutually exclusive and cover | 1422 * These release level checks are mutually exclusive and cover |
1418 » * the field, so don't get too fancy with the pre-processor! | 1423 * the field, so don't get too fancy with the pre-processor! |
1419 » */ | 1424 */ |
1420 #if PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_ALPHA | 1425 #if PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_ALPHA |
1421 » s = "alpha"; | 1426 s = "alpha"; |
1422 #elif PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_BETA | 1427 #elif PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_BETA |
1423 » s = "beta"; | 1428 s = "beta"; |
1424 #elif PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_GAMMA | 1429 #elif PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_GAMMA |
1425 » s = "candidate"; | 1430 s = "candidate"; |
1426 #elif PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_FINAL | 1431 #elif PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_FINAL |
1427 » s = "final"; | 1432 s = "final"; |
1428 #endif | 1433 #endif |
1429 | 1434 |
1430 #define SetIntItem(flag) \ | 1435 #define SetIntItem(flag) \ |
1431 » PyStructSequence_SET_ITEM(version_info, pos++, PyLong_FromLong(flag)) | 1436 PyStructSequence_SET_ITEM(version_info, pos++, PyLong_FromLong(flag)) |
1432 #define SetStrItem(flag) \ | 1437 #define SetStrItem(flag) \ |
1433 » PyStructSequence_SET_ITEM(version_info, pos++, PyUnicode_FromString(flag )) | 1438 PyStructSequence_SET_ITEM(version_info, pos++, PyUnicode_FromString(flag)) |
1434 | 1439 |
1435 » SetIntItem(PY_MAJOR_VERSION); | 1440 SetIntItem(PY_MAJOR_VERSION); |
1436 » SetIntItem(PY_MINOR_VERSION); | 1441 SetIntItem(PY_MINOR_VERSION); |
1437 » SetIntItem(PY_MICRO_VERSION); | 1442 SetIntItem(PY_MICRO_VERSION); |
1438 » SetStrItem(s); | 1443 SetStrItem(s); |
1439 » SetIntItem(PY_RELEASE_SERIAL); | 1444 SetIntItem(PY_RELEASE_SERIAL); |
1440 #undef SetIntItem | 1445 #undef SetIntItem |
1441 #undef SetStrItem | 1446 #undef SetStrItem |
1442 | 1447 |
1443 » if (PyErr_Occurred()) { | 1448 if (PyErr_Occurred()) { |
1444 » » Py_CLEAR(version_info); | 1449 Py_CLEAR(version_info); |
1445 » » return NULL; | 1450 return NULL; |
1446 » } | 1451 } |
1447 » return version_info; | 1452 return version_info; |
1448 } | 1453 } |
1449 | 1454 |
1450 static struct PyModuleDef sysmodule = { | 1455 static struct PyModuleDef sysmodule = { |
1451 » PyModuleDef_HEAD_INIT, | 1456 PyModuleDef_HEAD_INIT, |
1452 » "sys", | 1457 "sys", |
1453 » sys_doc, | 1458 sys_doc, |
1454 » -1, /* multiple "initialization" just copies the module dict. */ | 1459 -1, /* multiple "initialization" just copies the module dict. */ |
1455 » sys_methods, | 1460 sys_methods, |
1456 » NULL, | 1461 NULL, |
1457 » NULL, | 1462 NULL, |
1458 » NULL, | 1463 NULL, |
1459 » NULL | 1464 NULL |
1460 }; | 1465 }; |
1461 | 1466 |
1462 PyObject * | 1467 PyObject * |
1463 _PySys_Init(void) | 1468 _PySys_Init(void) |
1464 { | 1469 { |
1465 » PyObject *m, *v, *sysdict; | 1470 PyObject *m, *v, *sysdict; |
1466 » char *s; | 1471 char *s; |
1467 | 1472 |
1468 » m = PyModule_Create(&sysmodule); | 1473 m = PyModule_Create(&sysmodule); |
1469 » if (m == NULL) | 1474 if (m == NULL) |
1470 » » return NULL; | 1475 return NULL; |
1471 » sysdict = PyModule_GetDict(m); | 1476 sysdict = PyModule_GetDict(m); |
1472 #define SET_SYS_FROM_STRING(key, value)»» » \ | 1477 #define SET_SYS_FROM_STRING(key, value) \ |
1473 » v = value;» » » » » \ | 1478 v = value; \ |
1474 » if (v != NULL)» » » » » \ | 1479 if (v != NULL) \ |
1475 » » PyDict_SetItemString(sysdict, key, v);» \ | 1480 PyDict_SetItemString(sysdict, key, v); \ |
1476 » Py_XDECREF(v) | 1481 Py_XDECREF(v) |
1477 | 1482 |
1478 » /* Check that stdin is not a directory | 1483 /* Check that stdin is not a directory |
1479 » Using shell redirection, you can redirect stdin to a directory, | 1484 Using shell redirection, you can redirect stdin to a directory, |
1480 » crashing the Python interpreter. Catch this common mistake here | 1485 crashing the Python interpreter. Catch this common mistake here |
1481 » and output a useful error message. Note that under MS Windows, | 1486 and output a useful error message. Note that under MS Windows, |
1482 » the shell already prevents that. */ | 1487 the shell already prevents that. */ |
1483 #if !defined(MS_WINDOWS) | 1488 #if !defined(MS_WINDOWS) |
1484 » { | 1489 { |
1485 » » struct stat sb; | 1490 struct stat sb; |
1486 » » if (fstat(fileno(stdin), &sb) == 0 && | 1491 if (fstat(fileno(stdin), &sb) == 0 && |
1487 » » S_ISDIR(sb.st_mode)) { | 1492 S_ISDIR(sb.st_mode)) { |
1488 » » » /* There's nothing more we can do. */ | 1493 /* There's nothing more we can do. */ |
1489 » » » /* Py_FatalError() will core dump, so just exit. */ | 1494 /* Py_FatalError() will core dump, so just exit. */ |
1490 » » » PySys_WriteStderr("Python error: <stdin> is a directory, cannot continue\n"); | 1495 PySys_WriteStderr("Python error: <stdin> is a directory, cannot cont inue\n"); |
1491 » » » exit(EXIT_FAILURE); | 1496 exit(EXIT_FAILURE); |
1492 » » } | 1497 } |
1493 » } | 1498 } |
1494 #endif | 1499 #endif |
1495 | 1500 |
1496 /* stdin/stdout/stderr are now set by pythonrun.c */ | 1501 /* stdin/stdout/stderr are now set by pythonrun.c */ |
1497 | 1502 |
1498 » PyDict_SetItemString(sysdict, "__displayhook__", | 1503 PyDict_SetItemString(sysdict, "__displayhook__", |
1499 » » » PyDict_GetItemString(sysdict, "displayhook")); | 1504 PyDict_GetItemString(sysdict, "displayhook")); |
1500 » PyDict_SetItemString(sysdict, "__excepthook__", | 1505 PyDict_SetItemString(sysdict, "__excepthook__", |
1501 » » » PyDict_GetItemString(sysdict, "excepthook")); | 1506 PyDict_GetItemString(sysdict, "excepthook")); |
1502 » SET_SYS_FROM_STRING("version", | 1507 SET_SYS_FROM_STRING("version", |
1503 » » » PyUnicode_FromString(Py_GetVersion())); | 1508 PyUnicode_FromString(Py_GetVersion())); |
1504 » SET_SYS_FROM_STRING("hexversion", | 1509 SET_SYS_FROM_STRING("hexversion", |
1505 » » » PyLong_FromLong(PY_VERSION_HEX)); | 1510 PyLong_FromLong(PY_VERSION_HEX)); |
1506 » svnversion_init(); | 1511 svnversion_init(); |
1507 » SET_SYS_FROM_STRING("subversion", | 1512 SET_SYS_FROM_STRING("subversion", |
1508 » » » Py_BuildValue("(UUU)", "CPython", branch, | 1513 Py_BuildValue("(UUU)", "CPython", branch, |
1509 » » » » » svn_revision)); | 1514 svn_revision)); |
1510 » SET_SYS_FROM_STRING("dont_write_bytecode", | 1515 SET_SYS_FROM_STRING("dont_write_bytecode", |
1511 » » » PyBool_FromLong(Py_DontWriteBytecodeFlag)); | 1516 PyBool_FromLong(Py_DontWriteBytecodeFlag)); |
1512 » SET_SYS_FROM_STRING("api_version", | 1517 SET_SYS_FROM_STRING("api_version", |
1513 » » » PyLong_FromLong(PYTHON_API_VERSION)); | 1518 PyLong_FromLong(PYTHON_API_VERSION)); |
1514 » SET_SYS_FROM_STRING("copyright", | 1519 SET_SYS_FROM_STRING("copyright", |
1515 » » » PyUnicode_FromString(Py_GetCopyright())); | 1520 PyUnicode_FromString(Py_GetCopyright())); |
1516 » SET_SYS_FROM_STRING("platform", | 1521 SET_SYS_FROM_STRING("platform", |
1517 » » » PyUnicode_FromString(Py_GetPlatform())); | 1522 PyUnicode_FromString(Py_GetPlatform())); |
1518 » SET_SYS_FROM_STRING("executable", | 1523 SET_SYS_FROM_STRING("executable", |
1519 » » » PyUnicode_FromWideChar( | 1524 PyUnicode_FromWideChar( |
1520 » » » » Py_GetProgramFullPath(), -1)); | 1525 Py_GetProgramFullPath(), -1)); |
1521 » SET_SYS_FROM_STRING("prefix", | 1526 SET_SYS_FROM_STRING("prefix", |
1522 » » » PyUnicode_FromWideChar(Py_GetPrefix(), -1)); | 1527 PyUnicode_FromWideChar(Py_GetPrefix(), -1)); |
1523 » SET_SYS_FROM_STRING("exec_prefix", | 1528 SET_SYS_FROM_STRING("exec_prefix", |
1524 » » » PyUnicode_FromWideChar(Py_GetExecPrefix(), -1)); | 1529 PyUnicode_FromWideChar(Py_GetExecPrefix(), -1)); |
1525 » SET_SYS_FROM_STRING("maxsize", | 1530 SET_SYS_FROM_STRING("maxsize", |
1526 » » » PyLong_FromSsize_t(PY_SSIZE_T_MAX)); | 1531 PyLong_FromSsize_t(PY_SSIZE_T_MAX)); |
1527 » SET_SYS_FROM_STRING("float_info", | 1532 SET_SYS_FROM_STRING("float_info", |
1528 » » » PyFloat_GetInfo()); | 1533 PyFloat_GetInfo()); |
1529 » SET_SYS_FROM_STRING("int_info", | 1534 SET_SYS_FROM_STRING("int_info", |
1530 » » » PyLong_GetInfo()); | 1535 PyLong_GetInfo()); |
1531 » /* initialize _hash_info */ | 1536 /* initialize hash_info */ |
1532 » if (_Hash_InfoType.tp_name == 0) | 1537 if (Hash_InfoType.tp_name == 0) |
1533 » » PyStructSequence_InitType(&_Hash_InfoType, &_hash_info_desc); | 1538 PyStructSequence_InitType(&Hash_InfoType, &hash_info_desc); |
1534 » SET_SYS_FROM_STRING("_hash_info", | 1539 SET_SYS_FROM_STRING("hash_info", |
1535 » » » get_hash_info()); | 1540 get_hash_info()); |
1536 » SET_SYS_FROM_STRING("maxunicode", | 1541 SET_SYS_FROM_STRING("maxunicode", |
1537 » » » PyLong_FromLong(PyUnicode_GetMax())); | 1542 PyLong_FromLong(PyUnicode_GetMax())); |
1538 » SET_SYS_FROM_STRING("builtin_module_names", | 1543 SET_SYS_FROM_STRING("builtin_module_names", |
1539 » » » list_builtin_module_names()); | 1544 list_builtin_module_names()); |
1540 » { | 1545 { |
1541 » » /* Assumes that longs are at least 2 bytes long. | 1546 /* Assumes that longs are at least 2 bytes long. |
1542 » » Should be safe! */ | 1547 Should be safe! */ |
1543 » » unsigned long number = 1; | 1548 unsigned long number = 1; |
1544 » » char *value; | 1549 char *value; |
1545 | 1550 |
1546 » » s = (char *) &number; | 1551 s = (char *) &number; |
1547 » » if (s[0] == 0) | 1552 if (s[0] == 0) |
1548 » » » value = "big"; | 1553 value = "big"; |
1549 » » else | 1554 else |
1550 » » » value = "little"; | 1555 value = "little"; |
1551 » » SET_SYS_FROM_STRING("byteorder", | 1556 SET_SYS_FROM_STRING("byteorder", |
1552 » » » » PyUnicode_FromString(value)); | 1557 PyUnicode_FromString(value)); |
1553 » } | 1558 } |
1554 #ifdef MS_COREDLL | 1559 #ifdef MS_COREDLL |
1555 » SET_SYS_FROM_STRING("dllhandle", | 1560 SET_SYS_FROM_STRING("dllhandle", |
1556 » » » PyLong_FromVoidPtr(PyWin_DLLhModule)); | 1561 PyLong_FromVoidPtr(PyWin_DLLhModule)); |
1557 » SET_SYS_FROM_STRING("winver", | 1562 SET_SYS_FROM_STRING("winver", |
1558 » » » PyUnicode_FromString(PyWin_DLLVersionString)); | 1563 PyUnicode_FromString(PyWin_DLLVersionString)); |
1559 #endif | 1564 #endif |
1560 » if (warnoptions == NULL) { | 1565 if (warnoptions == NULL) { |
1561 » » warnoptions = PyList_New(0); | 1566 warnoptions = PyList_New(0); |
1562 » } | 1567 } |
1563 » else { | 1568 else { |
1564 » » Py_INCREF(warnoptions); | 1569 Py_INCREF(warnoptions); |
1565 » } | 1570 } |
1566 » if (warnoptions != NULL) { | 1571 if (warnoptions != NULL) { |
1567 » » PyDict_SetItemString(sysdict, "warnoptions", warnoptions); | 1572 PyDict_SetItemString(sysdict, "warnoptions", warnoptions); |
1568 » } | 1573 } |
1569 | 1574 |
1570 » /* version_info */ | 1575 /* version_info */ |
1571 » if (VersionInfoType.tp_name == 0) | 1576 if (VersionInfoType.tp_name == 0) |
1572 » » PyStructSequence_InitType(&VersionInfoType, &version_info_desc); | 1577 PyStructSequence_InitType(&VersionInfoType, &version_info_desc); |
1573 » SET_SYS_FROM_STRING("version_info", make_version_info()); | 1578 SET_SYS_FROM_STRING("version_info", make_version_info()); |
1574 » /* prevent user from creating new instances */ | 1579 /* prevent user from creating new instances */ |
1575 » VersionInfoType.tp_init = NULL; | 1580 VersionInfoType.tp_init = NULL; |
1576 » VersionInfoType.tp_new = NULL; | 1581 VersionInfoType.tp_new = NULL; |
1577 | 1582 |
1578 » /* flags */ | 1583 /* flags */ |
1579 » if (FlagsType.tp_name == 0) | 1584 if (FlagsType.tp_name == 0) |
1580 » » PyStructSequence_InitType(&FlagsType, &flags_desc); | 1585 PyStructSequence_InitType(&FlagsType, &flags_desc); |
1581 » SET_SYS_FROM_STRING("flags", make_flags()); | 1586 SET_SYS_FROM_STRING("flags", make_flags()); |
1582 » /* prevent user from creating new instances */ | 1587 /* prevent user from creating new instances */ |
1583 » FlagsType.tp_init = NULL; | 1588 FlagsType.tp_init = NULL; |
1584 » FlagsType.tp_new = NULL; | 1589 FlagsType.tp_new = NULL; |
1585 | 1590 |
1586 | 1591 |
1587 #if defined(MS_WINDOWS) | 1592 #if defined(MS_WINDOWS) |
1588 » /* getwindowsversion */ | 1593 /* getwindowsversion */ |
1589 » if (WindowsVersionType.tp_name == 0) | 1594 if (WindowsVersionType.tp_name == 0) |
1590 » » PyStructSequence_InitType(&WindowsVersionType, &windows_version_ desc); | 1595 PyStructSequence_InitType(&WindowsVersionType, &windows_version_desc); |
1591 » /* prevent user from creating new instances */ | 1596 /* prevent user from creating new instances */ |
1592 » WindowsVersionType.tp_init = NULL; | 1597 WindowsVersionType.tp_init = NULL; |
1593 » WindowsVersionType.tp_new = NULL; | 1598 WindowsVersionType.tp_new = NULL; |
1594 #endif | 1599 #endif |
1595 | 1600 |
1596 » /* float repr style: 0.03 (short) vs 0.029999999999999999 (legacy) */ | 1601 /* float repr style: 0.03 (short) vs 0.029999999999999999 (legacy) */ |
1597 #ifndef PY_NO_SHORT_FLOAT_REPR | 1602 #ifndef PY_NO_SHORT_FLOAT_REPR |
1598 » SET_SYS_FROM_STRING("float_repr_style", | 1603 SET_SYS_FROM_STRING("float_repr_style", |
1599 » » » PyUnicode_FromString("short")); | 1604 PyUnicode_FromString("short")); |
1600 #else | 1605 #else |
1601 » SET_SYS_FROM_STRING("float_repr_style", | 1606 SET_SYS_FROM_STRING("float_repr_style", |
1602 » » » PyUnicode_FromString("legacy")); | 1607 PyUnicode_FromString("legacy")); |
1603 #endif | 1608 #endif |
1604 | 1609 |
1605 #undef SET_SYS_FROM_STRING | 1610 #undef SET_SYS_FROM_STRING |
1606 » if (PyErr_Occurred()) | 1611 if (PyErr_Occurred()) |
1607 » » return NULL; | 1612 return NULL; |
1608 » return m; | 1613 return m; |
1609 } | 1614 } |
1610 | 1615 |
1611 static PyObject * | 1616 static PyObject * |
1612 makepathobject(const wchar_t *path, wchar_t delim) | 1617 makepathobject(const wchar_t *path, wchar_t delim) |
1613 { | 1618 { |
1614 » int i, n; | 1619 int i, n; |
1615 » const wchar_t *p; | 1620 const wchar_t *p; |
1616 » PyObject *v, *w; | 1621 PyObject *v, *w; |
1617 | 1622 |
1618 » n = 1; | 1623 n = 1; |
1619 » p = path; | 1624 p = path; |
1620 » while ((p = wcschr(p, delim)) != NULL) { | 1625 while ((p = wcschr(p, delim)) != NULL) { |
1621 » » n++; | 1626 n++; |
1622 » » p++; | 1627 p++; |
1623 » } | 1628 } |
1624 » v = PyList_New(n); | 1629 v = PyList_New(n); |
1625 » if (v == NULL) | 1630 if (v == NULL) |
1626 » » return NULL; | 1631 return NULL; |
1627 » for (i = 0; ; i++) { | 1632 for (i = 0; ; i++) { |
1628 » » p = wcschr(path, delim); | 1633 p = wcschr(path, delim); |
1629 » » if (p == NULL) | 1634 if (p == NULL) |
1630 » » » p = path + wcslen(path); /* End of string */ | 1635 p = path + wcslen(path); /* End of string */ |
1631 » » w = PyUnicode_FromWideChar(path, (Py_ssize_t)(p - path)); | 1636 w = PyUnicode_FromWideChar(path, (Py_ssize_t)(p - path)); |
1632 » » if (w == NULL) { | 1637 if (w == NULL) { |
1633 » » » Py_DECREF(v); | 1638 Py_DECREF(v); |
1634 » » » return NULL; | 1639 return NULL; |
1635 » » } | 1640 } |
1636 » » PyList_SetItem(v, i, w); | 1641 PyList_SetItem(v, i, w); |
1637 » » if (*p == '\0') | 1642 if (*p == '\0') |
1638 » » » break; | 1643 break; |
1639 » » path = p+1; | 1644 path = p+1; |
1640 » } | 1645 } |
1641 » return v; | 1646 return v; |
1642 } | 1647 } |
1643 | 1648 |
1644 void | 1649 void |
1645 PySys_SetPath(const wchar_t *path) | 1650 PySys_SetPath(const wchar_t *path) |
1646 { | 1651 { |
1647 » PyObject *v; | 1652 PyObject *v; |
1648 » if ((v = makepathobject(path, DELIM)) == NULL) | 1653 if ((v = makepathobject(path, DELIM)) == NULL) |
1649 » » Py_FatalError("can't create sys.path"); | 1654 Py_FatalError("can't create sys.path"); |
1650 » if (PySys_SetObject("path", v) != 0) | 1655 if (PySys_SetObject("path", v) != 0) |
1651 » » Py_FatalError("can't assign sys.path"); | 1656 Py_FatalError("can't assign sys.path"); |
1652 » Py_DECREF(v); | 1657 Py_DECREF(v); |
1653 } | 1658 } |
1654 | 1659 |
1655 static PyObject * | 1660 static PyObject * |
1656 makeargvobject(int argc, wchar_t **argv) | 1661 makeargvobject(int argc, wchar_t **argv) |
1657 { | 1662 { |
1658 » PyObject *av; | 1663 PyObject *av; |
1659 » if (argc <= 0 || argv == NULL) { | 1664 if (argc <= 0 || argv == NULL) { |
1660 » » /* Ensure at least one (empty) argument is seen */ | 1665 /* Ensure at least one (empty) argument is seen */ |
1661 » » static wchar_t *empty_argv[1] = {L""}; | 1666 static wchar_t *empty_argv[1] = {L""}; |
1662 » » argv = empty_argv; | 1667 argv = empty_argv; |
1663 » » argc = 1; | 1668 argc = 1; |
1664 » } | 1669 } |
1665 » av = PyList_New(argc); | 1670 av = PyList_New(argc); |
1666 » if (av != NULL) { | 1671 if (av != NULL) { |
1667 » » int i; | 1672 int i; |
1668 » » for (i = 0; i < argc; i++) { | 1673 for (i = 0; i < argc; i++) { |
1669 #ifdef __VMS | 1674 #ifdef __VMS |
1670 » » » PyObject *v; | 1675 PyObject *v; |
1671 | 1676 |
1672 » » » /* argv[0] is the script pathname if known */ | 1677 /* argv[0] is the script pathname if known */ |
1673 » » » if (i == 0) { | 1678 if (i == 0) { |
1674 » » » » char* fn = decc$translate_vms(argv[0]); | 1679 char* fn = decc$translate_vms(argv[0]); |
1675 » » » » if ((fn == (char *)0) || fn == (char *)-1) | 1680 if ((fn == (char *)0) || fn == (char *)-1) |
1676 » » » » » v = PyUnicode_FromString(argv[0]); | 1681 v = PyUnicode_FromString(argv[0]); |
1677 » » » » else | 1682 else |
1678 » » » » » v = PyUnicode_FromString( | 1683 v = PyUnicode_FromString( |
1679 » » » » » » decc$translate_vms(argv[0])); | 1684 decc$translate_vms(argv[0])); |
1680 » » » } else | 1685 } else |
1681 » » » » v = PyUnicode_FromString(argv[i]); | 1686 v = PyUnicode_FromString(argv[i]); |
1682 #else | 1687 #else |
1683 » » » PyObject *v = PyUnicode_FromWideChar(argv[i], -1); | 1688 PyObject *v = PyUnicode_FromWideChar(argv[i], -1); |
1684 #endif | 1689 #endif |
1685 » » » if (v == NULL) { | 1690 if (v == NULL) { |
1686 » » » » Py_DECREF(av); | 1691 Py_DECREF(av); |
1687 » » » » av = NULL; | 1692 av = NULL; |
1688 » » » » break; | 1693 break; |
1689 » » » } | 1694 } |
1690 » » » PyList_SetItem(av, i, v); | 1695 PyList_SetItem(av, i, v); |
1691 » » } | 1696 } |
1692 » } | 1697 } |
1693 » return av; | 1698 return av; |
1694 } | 1699 } |
1695 | 1700 |
1696 #ifdef HAVE_REALPATH | 1701 #ifdef HAVE_REALPATH |
1697 static wchar_t* | 1702 static wchar_t* |
1698 _wrealpath(const wchar_t *path, wchar_t *resolved_path) | 1703 _wrealpath(const wchar_t *path, wchar_t *resolved_path) |
1699 { | 1704 { |
1700 » char cpath[PATH_MAX]; | 1705 char cpath[PATH_MAX]; |
1701 » char cresolved_path[PATH_MAX]; | 1706 char cresolved_path[PATH_MAX]; |
1702 » char *res; | 1707 char *res; |
1703 » size_t r; | 1708 size_t r; |
1704 » r = wcstombs(cpath, path, PATH_MAX); | 1709 r = wcstombs(cpath, path, PATH_MAX); |
1705 » if (r == (size_t)-1 || r >= PATH_MAX) { | 1710 if (r == (size_t)-1 || r >= PATH_MAX) { |
1706 » » errno = EINVAL; | 1711 errno = EINVAL; |
1707 » » return NULL; | 1712 return NULL; |
1708 » } | 1713 } |
1709 » res = realpath(cpath, cresolved_path); | 1714 res = realpath(cpath, cresolved_path); |
1710 » if (res == NULL) | 1715 if (res == NULL) |
1711 » » return NULL; | 1716 return NULL; |
1712 » r = mbstowcs(resolved_path, cresolved_path, PATH_MAX); | 1717 r = mbstowcs(resolved_path, cresolved_path, PATH_MAX); |
1713 » if (r == (size_t)-1 || r >= PATH_MAX) { | 1718 if (r == (size_t)-1 || r >= PATH_MAX) { |
1714 » » errno = EINVAL; | 1719 errno = EINVAL; |
1715 » » return NULL; | 1720 return NULL; |
1716 » } | 1721 } |
1717 » return resolved_path; | 1722 return resolved_path; |
1718 } | 1723 } |
1719 #endif | 1724 #endif |
1725 | |
1726 void | |
1727 PySys_SetArgvEx(int argc, wchar_t **argv, int updatepath) | |
1728 { | |
1729 #if defined(HAVE_REALPATH) | |
1730 wchar_t fullpath[MAXPATHLEN]; | |
1731 #elif defined(MS_WINDOWS) && !defined(MS_WINCE) | |
1732 wchar_t fullpath[MAX_PATH]; | |
1733 #endif | |
1734 PyObject *av = makeargvobject(argc, argv); | |
1735 PyObject *path = PySys_GetObject("path"); | |
1736 if (av == NULL) | |
1737 Py_FatalError("no mem for sys.argv"); | |
1738 if (PySys_SetObject("argv", av) != 0) | |
1739 Py_FatalError("can't assign sys.argv"); | |
1740 if (updatepath && path != NULL) { | |
1741 wchar_t *argv0 = argv[0]; | |
1742 wchar_t *p = NULL; | |
1743 Py_ssize_t n = 0; | |
1744 PyObject *a; | |
1745 extern int _Py_wreadlink(const wchar_t *, wchar_t *, size_t); | |
1746 #ifdef HAVE_READLINK | |
1747 wchar_t link[MAXPATHLEN+1]; | |
1748 wchar_t argv0copy[2*MAXPATHLEN+1]; | |
1749 int nr = 0; | |
1750 if (argc > 0 && argv0 != NULL && wcscmp(argv0, L"-c") != 0) | |
1751 nr = _Py_wreadlink(argv0, link, MAXPATHLEN); | |
1752 if (nr > 0) { | |
1753 /* It's a symlink */ | |
1754 link[nr] = '\0'; | |
1755 if (link[0] == SEP) | |
1756 argv0 = link; /* Link to absolute path */ | |
1757 else if (wcschr(link, SEP) == NULL) | |
1758 ; /* Link without path */ | |
1759 else { | |
1760 /* Must join(dirname(argv0), link) */ | |
1761 wchar_t *q = wcsrchr(argv0, SEP); | |
1762 if (q == NULL) | |
1763 argv0 = link; /* argv0 without path */ | |
1764 else { | |
1765 /* Must make a copy */ | |
1766 wcscpy(argv0copy, argv0); | |
1767 q = wcsrchr(argv0copy, SEP); | |
1768 wcscpy(q+1, link); | |
1769 argv0 = argv0copy; | |
1770 } | |
1771 } | |
1772 } | |
1773 #endif /* HAVE_READLINK */ | |
1774 #if SEP == '\\' /* Special case for MS filename syntax */ | |
1775 if (argc > 0 && argv0 != NULL && wcscmp(argv0, L"-c") != 0) { | |
1776 wchar_t *q; | |
1777 #if defined(MS_WINDOWS) && !defined(MS_WINCE) | |
1778 /* This code here replaces the first element in argv with the full | |
1779 path that it represents. Under CE, there are no relative paths so | |
1780 the argument must be the full path anyway. */ | |
1781 wchar_t *ptemp; | |
1782 if (GetFullPathNameW(argv0, | |
1783 sizeof(fullpath)/sizeof(fullpath[0]), | |
1784 fullpath, | |
1785 &ptemp)) { | |
1786 argv0 = fullpath; | |
1787 } | |
1788 #endif | |
1789 p = wcsrchr(argv0, SEP); | |
1790 /* Test for alternate separator */ | |
1791 q = wcsrchr(p ? p : argv0, '/'); | |
1792 if (q != NULL) | |
1793 p = q; | |
1794 if (p != NULL) { | |
1795 n = p + 1 - argv0; | |
1796 if (n > 1 && p[-1] != ':') | |
1797 n--; /* Drop trailing separator */ | |
1798 } | |
1799 } | |
1800 #else /* All other filename syntaxes */ | |
1801 if (argc > 0 && argv0 != NULL && wcscmp(argv0, L"-c") != 0) { | |
1802 #if defined(HAVE_REALPATH) | |
1803 if (_wrealpath(argv0, fullpath)) { | |
1804 argv0 = fullpath; | |
1805 } | |
1806 #endif | |
1807 p = wcsrchr(argv0, SEP); | |
1808 } | |
1809 if (p != NULL) { | |
1810 n = p + 1 - argv0; | |
1811 #if SEP == '/' /* Special case for Unix filename syntax */ | |
1812 if (n > 1) | |
1813 n--; /* Drop trailing separator */ | |
1814 #endif /* Unix */ | |
1815 } | |
1816 #endif /* All others */ | |
1817 a = PyUnicode_FromWideChar(argv0, n); | |
1818 if (a == NULL) | |
1819 Py_FatalError("no mem for sys.path insertion"); | |
1820 if (PyList_Insert(path, 0, a) < 0) | |
1821 Py_FatalError("sys.path.insert(0) failed"); | |
1822 Py_DECREF(a); | |
1823 } | |
1824 Py_DECREF(av); | |
1825 } | |
1720 | 1826 |
1721 void | 1827 void |
1722 PySys_SetArgv(int argc, wchar_t **argv) | 1828 PySys_SetArgv(int argc, wchar_t **argv) |
1723 { | 1829 { |
1724 #if defined(HAVE_REALPATH) | 1830 PySys_SetArgvEx(argc, argv, 1); |
1725 » wchar_t fullpath[MAXPATHLEN]; | 1831 } |
1726 #elif defined(MS_WINDOWS) && !defined(MS_WINCE) | 1832 |
1727 » wchar_t fullpath[MAX_PATH]; | 1833 /* Reimplementation of PyFile_WriteString() no calling indirectly |
1728 #endif | 1834 PyErr_CheckSignals(): avoid the call to PyObject_Str(). */ |
1729 » PyObject *av = makeargvobject(argc, argv); | 1835 |
1730 » PyObject *path = PySys_GetObject("path"); | 1836 static int |
1731 » if (av == NULL) | 1837 sys_pyfile_write(const char *text, PyObject *file) |
1732 » » Py_FatalError("no mem for sys.argv"); | 1838 { |
1733 » if (PySys_SetObject("argv", av) != 0) | 1839 PyObject *unicode = NULL, *writer = NULL, *args = NULL, *result = NULL; |
1734 » » Py_FatalError("can't assign sys.argv"); | 1840 int err; |
1735 » if (path != NULL) { | 1841 |
1736 » » wchar_t *argv0 = argv[0]; | 1842 unicode = PyUnicode_FromString(text); |
1737 » » wchar_t *p = NULL; | 1843 if (unicode == NULL) |
1738 » » Py_ssize_t n = 0; | 1844 goto error; |
1739 » » PyObject *a; | 1845 |
1740 » » extern int _Py_wreadlink(const wchar_t *, wchar_t *, size_t); | 1846 writer = PyObject_GetAttrString(file, "write"); |
1741 #ifdef HAVE_READLINK | 1847 if (writer == NULL) |
1742 » » wchar_t link[MAXPATHLEN+1]; | 1848 goto error; |
1743 » » wchar_t argv0copy[2*MAXPATHLEN+1]; | 1849 |
1744 » » int nr = 0; | 1850 args = PyTuple_Pack(1, unicode); |
1745 » » if (argc > 0 && argv0 != NULL && wcscmp(argv0, L"-c") != 0) | 1851 if (args == NULL) |
1746 » » » nr = _Py_wreadlink(argv0, link, MAXPATHLEN); | 1852 goto error; |
1747 » » if (nr > 0) { | 1853 |
1748 » » » /* It's a symlink */ | 1854 result = PyEval_CallObject(writer, args); |
1749 » » » link[nr] = '\0'; | 1855 if (result == NULL) { |
1750 » » » if (link[0] == SEP) | 1856 goto error; |
1751 » » » » argv0 = link; /* Link to absolute path */ | 1857 } else { |
1752 » » » else if (wcschr(link, SEP) == NULL) | 1858 err = 0; |
1753 » » » » ; /* Link without path */ | 1859 goto finally; |
1754 » » » else { | 1860 } |
1755 » » » » /* Must join(dirname(argv0), link) */ | 1861 |
1756 » » » » wchar_t *q = wcsrchr(argv0, SEP); | 1862 error: |
1757 » » » » if (q == NULL) | 1863 err = -1; |
1758 » » » » » argv0 = link; /* argv0 without path */ | 1864 finally: |
1759 » » » » else { | 1865 Py_XDECREF(unicode); |
1760 » » » » » /* Must make a copy */ | 1866 Py_XDECREF(writer); |
1761 » » » » » wcscpy(argv0copy, argv0); | 1867 Py_XDECREF(args); |
1762 » » » » » q = wcsrchr(argv0copy, SEP); | 1868 Py_XDECREF(result); |
1763 » » » » » wcscpy(q+1, link); | 1869 return err; |
1764 » » » » » argv0 = argv0copy; | |
1765 » » » » } | |
1766 » » » } | |
1767 » » } | |
1768 #endif /* HAVE_READLINK */ | |
1769 #if SEP == '\\' /* Special case for MS filename syntax */ | |
1770 » » if (argc > 0 && argv0 != NULL && wcscmp(argv0, L"-c") != 0) { | |
1771 » » » wchar_t *q; | |
1772 #if defined(MS_WINDOWS) && !defined(MS_WINCE) | |
1773 » » » /* This code here replaces the first element in argv wit h the full | |
1774 » » » path that it represents. Under CE, there are no relative paths so | |
1775 » » » the argument must be the full path anyway. */ | |
1776 » » » wchar_t *ptemp; | |
1777 » » » if (GetFullPathNameW(argv0, | |
1778 » » » » » sizeof(fullpath)/sizeof(fullpath[0]), | |
1779 » » » » » fullpath, | |
1780 » » » » » &ptemp)) { | |
1781 » » » » argv0 = fullpath; | |
1782 » » » } | |
1783 #endif | |
1784 » » » p = wcsrchr(argv0, SEP); | |
1785 » » » /* Test for alternate separator */ | |
1786 » » » q = wcsrchr(p ? p : argv0, '/'); | |
1787 » » » if (q != NULL) | |
1788 » » » » p = q; | |
1789 » » » if (p != NULL) { | |
1790 » » » » n = p + 1 - argv0; | |
1791 » » » » if (n > 1 && p[-1] != ':') | |
1792 » » » » » n--; /* Drop trailing separator */ | |
1793 » » » } | |
1794 » » } | |
1795 #else /* All other filename syntaxes */ | |
1796 » » if (argc > 0 && argv0 != NULL && wcscmp(argv0, L"-c") != 0) { | |
1797 #if defined(HAVE_REALPATH) | |
1798 » » » if (_wrealpath(argv0, fullpath)) { | |
1799 » » » » argv0 = fullpath; | |
1800 » » » } | |
1801 #endif | |
1802 » » » p = wcsrchr(argv0, SEP); | |
1803 » » } | |
1804 » » if (p != NULL) { | |
1805 » » » n = p + 1 - argv0; | |
1806 #if SEP == '/' /* Special case for Unix filename syntax */ | |
1807 » » » if (n > 1) | |
1808 » » » » n--; /* Drop trailing separator */ | |
1809 #endif /* Unix */ | |
1810 » » } | |
1811 #endif /* All others */ | |
1812 » » a = PyUnicode_FromWideChar(argv0, n); | |
1813 » » if (a == NULL) | |
1814 » » » Py_FatalError("no mem for sys.path insertion"); | |
1815 » » if (PyList_Insert(path, 0, a) < 0) | |
1816 » » » Py_FatalError("sys.path.insert(0) failed"); | |
1817 » » Py_DECREF(a); | |
1818 » } | |
1819 » Py_DECREF(av); | |
1820 } | 1870 } |
1821 | 1871 |
1822 | 1872 |
1823 /* APIs to write to sys.stdout or sys.stderr using a printf-like interface. | 1873 /* APIs to write to sys.stdout or sys.stderr using a printf-like interface. |
1824 Adapted from code submitted by Just van Rossum. | 1874 Adapted from code submitted by Just van Rossum. |
1825 | 1875 |
1826 PySys_WriteStdout(format, ...) | 1876 PySys_WriteStdout(format, ...) |
1827 PySys_WriteStderr(format, ...) | 1877 PySys_WriteStderr(format, ...) |
1828 | 1878 |
1829 The first function writes to sys.stdout; the second to sys.stderr. When | 1879 The first function writes to sys.stdout; the second to sys.stderr. When |
1830 there is a problem, they write to the real (C level) stdout or stderr; | 1880 there is a problem, they write to the real (C level) stdout or stderr; |
1831 no exceptions are raised. | 1881 no exceptions are raised. |
1882 | |
1883 PyErr_CheckSignals() is not called to avoid the execution of the Python | |
1884 signal handlers: they may raise a new exception whereas mywrite() ignores | |
1885 all exceptions. | |
1832 | 1886 |
1833 Both take a printf-style format string as their first argument followed | 1887 Both take a printf-style format string as their first argument followed |
1834 by a variable length argument list determined by the format string. | 1888 by a variable length argument list determined by the format string. |
1835 | 1889 |
1836 *** WARNING *** | 1890 *** WARNING *** |
1837 | 1891 |
1838 The format should limit the total size of the formatted output string to | 1892 The format should limit the total size of the formatted output string to |
1839 1000 bytes. In particular, this means that no unrestricted "%s" formats | 1893 1000 bytes. In particular, this means that no unrestricted "%s" formats |
1840 should occur; these should be limited using "%.<N>s where <N> is a | 1894 should occur; these should be limited using "%.<N>s where <N> is a |
1841 decimal number calculated so that <N> plus the maximum size of other | 1895 decimal number calculated so that <N> plus the maximum size of other |
1842 formatted text does not exceed 1000 bytes. Also watch out for "%f", | 1896 formatted text does not exceed 1000 bytes. Also watch out for "%f", |
1843 which can print hundreds of digits for very large numbers. | 1897 which can print hundreds of digits for very large numbers. |
1844 | 1898 |
1845 */ | 1899 */ |
1846 | 1900 |
1847 static void | 1901 static void |
1848 mywrite(char *name, FILE *fp, const char *format, va_list va) | 1902 mywrite(char *name, FILE *fp, const char *format, va_list va) |
1849 { | 1903 { |
1850 » PyObject *file; | 1904 PyObject *file; |
1851 » PyObject *error_type, *error_value, *error_traceback; | 1905 PyObject *error_type, *error_value, *error_traceback; |
1852 » char buffer[1001]; | 1906 char buffer[1001]; |
1853 » int written; | 1907 int written; |
1854 | 1908 |
1855 » PyErr_Fetch(&error_type, &error_value, &error_traceback); | 1909 PyErr_Fetch(&error_type, &error_value, &error_traceback); |
1856 » file = PySys_GetObject(name); | 1910 file = PySys_GetObject(name); |
1857 » written = PyOS_vsnprintf(buffer, sizeof(buffer), format, va); | 1911 written = PyOS_vsnprintf(buffer, sizeof(buffer), format, va); |
1858 » if (PyFile_WriteString(buffer, file) != 0) { | 1912 if (sys_pyfile_write(buffer, file) != 0) { |
1859 » » PyErr_Clear(); | 1913 PyErr_Clear(); |
1860 » » fputs(buffer, fp); | 1914 fputs(buffer, fp); |
1861 » } | 1915 } |
1862 » if (written < 0 || (size_t)written >= sizeof(buffer)) { | 1916 if (written < 0 || (size_t)written >= sizeof(buffer)) { |
1863 » » const char *truncated = "... truncated"; | 1917 const char *truncated = "... truncated"; |
1864 » » if (PyFile_WriteString(truncated, file) != 0) { | 1918 if (sys_pyfile_write(truncated, file) != 0) { |
1865 » » » PyErr_Clear(); | 1919 PyErr_Clear(); |
1866 » » » fputs(truncated, fp); | 1920 fputs(truncated, fp); |
1867 » » } | 1921 } |
1868 » } | 1922 } |
1869 » PyErr_Restore(error_type, error_value, error_traceback); | 1923 PyErr_Restore(error_type, error_value, error_traceback); |
1870 } | 1924 } |
1871 | 1925 |
1872 void | 1926 void |
1873 PySys_WriteStdout(const char *format, ...) | 1927 PySys_WriteStdout(const char *format, ...) |
1874 { | 1928 { |
1875 » va_list va; | 1929 va_list va; |
1876 | 1930 |
1877 » va_start(va, format); | 1931 va_start(va, format); |
1878 » mywrite("stdout", stdout, format, va); | 1932 mywrite("stdout", stdout, format, va); |
1879 » va_end(va); | 1933 va_end(va); |
1880 } | 1934 } |
1881 | 1935 |
1882 void | 1936 void |
1883 PySys_WriteStderr(const char *format, ...) | 1937 PySys_WriteStderr(const char *format, ...) |
1884 { | 1938 { |
1885 » va_list va; | 1939 va_list va; |
1886 | 1940 |
1887 » va_start(va, format); | 1941 va_start(va, format); |
1888 » mywrite("stderr", stderr, format, va); | 1942 mywrite("stderr", stderr, format, va); |
1889 » va_end(va); | 1943 va_end(va); |
1890 } | 1944 } |
LEFT | RIGHT |