comparison Python/sysmodule.c @ 61466:cab14be0ada1

Recorded merge of revisions 81029 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r81029 | antoine.pitrou | 2010-05-09 16:46:46 +0200 (dim., 09 mai 2010) | 3 lines Untabify C files. Will watch buildbots. ........
author Antoine Pitrou <solipsis@pitrou.net>
date Sun, 09 May 2010 15:52:27 +0000
parents 86fadaebcf66
children b7f71f60437b
comparison
equal deleted inserted replaced
61462:19aac84ac1da 61466:cab14be0ada1
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"
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"
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\
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\
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\
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\
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\
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\
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\
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\
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\
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\
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\
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\
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\
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 );
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\
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 PyDoc_STRVAR(setrecursionlimit_doc, 573 PyDoc_STRVAR(setrecursionlimit_doc,
574 "setrecursionlimit(n)\n\ 574 "setrecursionlimit(n)\n\
575 \n\ 575 \n\
580 ); 580 );
581 581
582 static PyObject * 582 static PyObject *
583 sys_getrecursionlimit(PyObject *self) 583 sys_getrecursionlimit(PyObject *self)
584 { 584 {
585 return PyLong_FromLong(Py_GetRecursionLimit()); 585 return PyLong_FromLong(Py_GetRecursionLimit());
586 } 586 }
587 587
588 PyDoc_STRVAR(getrecursionlimit_doc, 588 PyDoc_STRVAR(getrecursionlimit_doc,
589 "getrecursionlimit()\n\ 589 "getrecursionlimit()\n\
590 \n\ 590 \n\
608 ); 608 );
609 609
610 static PyTypeObject WindowsVersionType = {0, 0, 0, 0, 0, 0}; 610 static PyTypeObject WindowsVersionType = {0, 0, 0, 0, 0, 0};
611 611
612 static PyStructSequence_Field windows_version_fields[] = { 612 static PyStructSequence_Field windows_version_fields[] = {
613 {"major", "Major version number"}, 613 {"major", "Major version number"},
614 {"minor", "Minor version number"}, 614 {"minor", "Minor version number"},
615 {"build", "Build number"}, 615 {"build", "Build number"},
616 {"platform", "Operating system platform"}, 616 {"platform", "Operating system platform"},
617 {"service_pack", "Latest Service Pack installed on the system"}, 617 {"service_pack", "Latest Service Pack installed on the system"},
618 {"service_pack_major", "Service Pack major version number"}, 618 {"service_pack_major", "Service Pack major version number"},
619 {"service_pack_minor", "Service Pack minor version number"}, 619 {"service_pack_minor", "Service Pack minor version number"},
620 {"suite_mask", "Bit mask identifying available product suites"}, 620 {"suite_mask", "Bit mask identifying available product suites"},
621 {"product_type", "System product type"}, 621 {"product_type", "System product type"},
622 {0} 622 {0}
623 }; 623 };
624 624
625 static PyStructSequence_Desc windows_version_desc = { 625 static PyStructSequence_Desc windows_version_desc = {
626 "sys.getwindowsversion", /* name */ 626 "sys.getwindowsversion", /* name */
627 getwindowsversion_doc, /* doc */ 627 getwindowsversion_doc, /* doc */
628 windows_version_fields, /* fields */ 628 windows_version_fields, /* fields */
629 5 /* For backward compatibility, 629 5 /* For backward compatibility,
630 only the first 5 items are accessible 630 only the first 5 items are accessible
631 via indexing, the rest are name only */ 631 via indexing, the rest are name only */
632 }; 632 };
633 633
634 static PyObject * 634 static PyObject *
635 sys_getwindowsversion(PyObject *self) 635 sys_getwindowsversion(PyObject *self)
636 { 636 {
637 PyObject *version; 637 PyObject *version;
638 int pos = 0; 638 int pos = 0;
639 OSVERSIONINFOEX ver; 639 OSVERSIONINFOEX ver;
640 ver.dwOSVersionInfoSize = sizeof(ver); 640 ver.dwOSVersionInfoSize = sizeof(ver);
641 if (!GetVersionEx((OSVERSIONINFO*) &ver)) 641 if (!GetVersionEx((OSVERSIONINFO*) &ver))
642 return PyErr_SetFromWindowsErr(0); 642 return PyErr_SetFromWindowsErr(0);
643 643
644 version = PyStructSequence_New(&WindowsVersionType); 644 version = PyStructSequence_New(&WindowsVersionType);
645 if (version == NULL) 645 if (version == NULL)
646 return NULL; 646 return NULL;
647 647
648 PyStructSequence_SET_ITEM(version, pos++, PyLong_FromLong(ver.dwMajorVersion)); 648 PyStructSequence_SET_ITEM(version, pos++, PyLong_FromLong(ver.dwMajorVersion));
649 PyStructSequence_SET_ITEM(version, pos++, PyLong_FromLong(ver.dwMinorVersion)); 649 PyStructSequence_SET_ITEM(version, pos++, PyLong_FromLong(ver.dwMinorVersion));
650 PyStructSequence_SET_ITEM(version, pos++, PyLong_FromLong(ver.dwBuildNumber)); 650 PyStructSequence_SET_ITEM(version, pos++, PyLong_FromLong(ver.dwBuildNumber));
651 PyStructSequence_SET_ITEM(version, pos++, PyLong_FromLong(ver.dwPlatformId)); 651 PyStructSequence_SET_ITEM(version, pos++, PyLong_FromLong(ver.dwPlatformId));
652 PyStructSequence_SET_ITEM(version, pos++, PyUnicode_FromString(ver.szCSDVersion)); 652 PyStructSequence_SET_ITEM(version, pos++, PyUnicode_FromString(ver.szCSDVersion));
653 PyStructSequence_SET_ITEM(version, pos++, PyLong_FromLong(ver.wServicePackMajor)); 653 PyStructSequence_SET_ITEM(version, pos++, PyLong_FromLong(ver.wServicePackMajor));
654 PyStructSequence_SET_ITEM(version, pos++, PyLong_FromLong(ver.wServicePackMinor)); 654 PyStructSequence_SET_ITEM(version, pos++, PyLong_FromLong(ver.wServicePackMinor));
655 PyStructSequence_SET_ITEM(version, pos++, PyLong_FromLong(ver.wSuiteMask)); 655 PyStructSequence_SET_ITEM(version, pos++, PyLong_FromLong(ver.wSuiteMask));
656 PyStructSequence_SET_ITEM(version, pos++, PyLong_FromLong(ver.wProductType)); 656 PyStructSequence_SET_ITEM(version, pos++, PyLong_FromLong(ver.wProductType));
657 657
658 return version; 658 return version;
659 } 659 }
660 660
661 #endif /* MS_WINDOWS */ 661 #endif /* MS_WINDOWS */
662 662
663 #ifdef HAVE_DLOPEN 663 #ifdef HAVE_DLOPEN
664 static PyObject * 664 static PyObject *
665 sys_setdlopenflags(PyObject *self, PyObject *args) 665 sys_setdlopenflags(PyObject *self, PyObject *args)
666 { 666 {
667 int new_val; 667 int new_val;
668 PyThreadState *tstate = PyThreadState_GET(); 668 PyThreadState *tstate = PyThreadState_GET();
669 if (!PyArg_ParseTuple(args, "i:setdlopenflags", &new_val)) 669 if (!PyArg_ParseTuple(args, "i:setdlopenflags", &new_val))
670 return NULL; 670 return NULL;
671 if (!tstate) 671 if (!tstate)
672 return NULL; 672 return NULL;
673 tstate->interp->dlopenflags = new_val; 673 tstate->interp->dlopenflags = new_val;
674 Py_INCREF(Py_None); 674 Py_INCREF(Py_None);
675 return Py_None; 675 return Py_None;
676 } 676 }
677 677
678 PyDoc_STRVAR(setdlopenflags_doc, 678 PyDoc_STRVAR(setdlopenflags_doc,
679 "setdlopenflags(n) -> None\n\ 679 "setdlopenflags(n) -> None\n\
680 \n\ 680 \n\
688 h2py script."); 688 h2py script.");
689 689
690 static PyObject * 690 static PyObject *
691 sys_getdlopenflags(PyObject *self, PyObject *args) 691 sys_getdlopenflags(PyObject *self, PyObject *args)
692 { 692 {
693 PyThreadState *tstate = PyThreadState_GET(); 693 PyThreadState *tstate = PyThreadState_GET();
694 if (!tstate) 694 if (!tstate)
695 return NULL; 695 return NULL;
696 return PyLong_FromLong(tstate->interp->dlopenflags); 696 return PyLong_FromLong(tstate->interp->dlopenflags);
697 } 697 }
698 698
699 PyDoc_STRVAR(getdlopenflags_doc, 699 PyDoc_STRVAR(getdlopenflags_doc,
700 "getdlopenflags() -> int\n\ 700 "getdlopenflags() -> int\n\
701 \n\ 701 \n\
702 Return the current value of the flags that are used for dlopen calls.\n\ 702 Return the current value of the flags that are used for dlopen calls.\n\
703 The flag constants are defined in the ctypes and DLFCN modules."); 703 The flag constants are defined in the ctypes and DLFCN modules.");
704 704
705 #endif /* HAVE_DLOPEN */ 705 #endif /* HAVE_DLOPEN */
706 706
707 #ifdef USE_MALLOPT 707 #ifdef USE_MALLOPT
708 /* Link with -lmalloc (or -lmpc) on an SGI */ 708 /* Link with -lmalloc (or -lmpc) on an SGI */
709 #include <malloc.h> 709 #include <malloc.h>
710 710
711 static PyObject * 711 static PyObject *
712 sys_mdebug(PyObject *self, PyObject *args) 712 sys_mdebug(PyObject *self, PyObject *args)
713 { 713 {
714 int flag; 714 int flag;
715 if (!PyArg_ParseTuple(args, "i:mdebug", &flag)) 715 if (!PyArg_ParseTuple(args, "i:mdebug", &flag))
716 return NULL; 716 return NULL;
717 mallopt(M_DEBUG, flag); 717 mallopt(M_DEBUG, flag);
718 Py_INCREF(Py_None); 718 Py_INCREF(Py_None);
719 return Py_None; 719 return Py_None;
720 } 720 }
721 #endif /* USE_MALLOPT */ 721 #endif /* USE_MALLOPT */
722 722
723 static PyObject * 723 static PyObject *
724 sys_getsizeof(PyObject *self, PyObject *args, PyObject *kwds) 724 sys_getsizeof(PyObject *self, PyObject *args, PyObject *kwds)
725 { 725 {
726 PyObject *res = NULL; 726 PyObject *res = NULL;
727 static PyObject *str__sizeof__ = NULL, *gc_head_size = NULL; 727 static PyObject *str__sizeof__ = NULL, *gc_head_size = NULL;
728 static char *kwlist[] = {"object", "default", 0}; 728 static char *kwlist[] = {"object", "default", 0};
729 PyObject *o, *dflt = NULL; 729 PyObject *o, *dflt = NULL;
730 PyObject *method; 730 PyObject *method;
731 731
732 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O:getsizeof", 732 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O:getsizeof",
733 kwlist, &o, &dflt)) 733 kwlist, &o, &dflt))
734 return NULL; 734 return NULL;
735 735
736 /* Initialize static variable for GC head size */ 736 /* Initialize static variable for GC head size */
737 if (gc_head_size == NULL) { 737 if (gc_head_size == NULL) {
738 gc_head_size = PyLong_FromSsize_t(sizeof(PyGC_Head)); 738 gc_head_size = PyLong_FromSsize_t(sizeof(PyGC_Head));
739 if (gc_head_size == NULL) 739 if (gc_head_size == NULL)
740 return NULL; 740 return NULL;
741 } 741 }
742 742
743 /* Make sure the type is initialized. float gets initialized late */ 743 /* Make sure the type is initialized. float gets initialized late */
744 if (PyType_Ready(Py_TYPE(o)) < 0) 744 if (PyType_Ready(Py_TYPE(o)) < 0)
745 return NULL; 745 return NULL;
746 746
747 method = _PyObject_LookupSpecial(o, "__sizeof__", 747 method = _PyObject_LookupSpecial(o, "__sizeof__",
748 &str__sizeof__); 748 &str__sizeof__);
749 if (method == NULL) { 749 if (method == NULL) {
750 if (!PyErr_Occurred()) 750 if (!PyErr_Occurred())
751 PyErr_Format(PyExc_TypeError, 751 PyErr_Format(PyExc_TypeError,
752 "Type %.100s doesn't define __sizeof__", 752 "Type %.100s doesn't define __sizeof__",
753 Py_TYPE(o)->tp_name); 753 Py_TYPE(o)->tp_name);
754 } 754 }
755 else { 755 else {
756 res = PyObject_CallFunctionObjArgs(method, NULL); 756 res = PyObject_CallFunctionObjArgs(method, NULL);
757 Py_DECREF(method); 757 Py_DECREF(method);
758 } 758 }
759 759
760 /* Has a default value been given */ 760 /* Has a default value been given */
761 if ((res == NULL) && (dflt != NULL) && 761 if ((res == NULL) && (dflt != NULL) &&
762 PyErr_ExceptionMatches(PyExc_TypeError)) 762 PyErr_ExceptionMatches(PyExc_TypeError))
763 { 763 {
764 PyErr_Clear(); 764 PyErr_Clear();
765 Py_INCREF(dflt); 765 Py_INCREF(dflt);
766 return dflt; 766 return dflt;
767 } 767 }
768 else if (res == NULL) 768 else if (res == NULL)
769 return res; 769 return res;
770 770
771 /* add gc_head size */ 771 /* add gc_head size */
772 if (PyObject_IS_GC(o)) { 772 if (PyObject_IS_GC(o)) {
773 PyObject *tmp = res; 773 PyObject *tmp = res;
774 res = PyNumber_Add(tmp, gc_head_size); 774 res = PyNumber_Add(tmp, gc_head_size);
775 Py_DECREF(tmp); 775 Py_DECREF(tmp);
776 } 776 }
777 return res; 777 return res;
778 } 778 }
779 779
780 PyDoc_STRVAR(getsizeof_doc, 780 PyDoc_STRVAR(getsizeof_doc,
781 "getsizeof(object, default) -> int\n\ 781 "getsizeof(object, default) -> int\n\
782 \n\ 782 \n\
783 Return the size of object in bytes."); 783 Return the size of object in bytes.");
784 784
785 static PyObject * 785 static PyObject *
786 sys_getrefcount(PyObject *self, PyObject *arg) 786 sys_getrefcount(PyObject *self, PyObject *arg)
787 { 787 {
788 return PyLong_FromSsize_t(arg->ob_refcnt); 788 return PyLong_FromSsize_t(arg->ob_refcnt);
789 } 789 }
790 790
791 #ifdef Py_REF_DEBUG 791 #ifdef Py_REF_DEBUG
792 static PyObject * 792 static PyObject *
793 sys_gettotalrefcount(PyObject *self) 793 sys_gettotalrefcount(PyObject *self)
794 { 794 {
795 return PyLong_FromSsize_t(_Py_GetRefTotal()); 795 return PyLong_FromSsize_t(_Py_GetRefTotal());
796 } 796 }
797 #endif /* Py_REF_DEBUG */ 797 #endif /* Py_REF_DEBUG */
798 798
799 PyDoc_STRVAR(getrefcount_doc, 799 PyDoc_STRVAR(getrefcount_doc,
800 "getrefcount(object) -> integer\n\ 800 "getrefcount(object) -> integer\n\
806 806
807 #ifdef COUNT_ALLOCS 807 #ifdef COUNT_ALLOCS
808 static PyObject * 808 static PyObject *
809 sys_getcounts(PyObject *self) 809 sys_getcounts(PyObject *self)
810 { 810 {
811 extern PyObject *get_counts(void); 811 extern PyObject *get_counts(void);
812 812
813 return get_counts(); 813 return get_counts();
814 } 814 }
815 #endif 815 #endif
816 816
817 PyDoc_STRVAR(getframe_doc, 817 PyDoc_STRVAR(getframe_doc,
818 "_getframe([depth]) -> frameobject\n\ 818 "_getframe([depth]) -> frameobject\n\
827 ); 827 );
828 828
829 static PyObject * 829 static PyObject *
830 sys_getframe(PyObject *self, PyObject *args) 830 sys_getframe(PyObject *self, PyObject *args)
831 { 831 {
832 PyFrameObject *f = PyThreadState_GET()->frame; 832 PyFrameObject *f = PyThreadState_GET()->frame;
833 int depth = -1; 833 int depth = -1;
834 834
835 if (!PyArg_ParseTuple(args, "|i:_getframe", &depth)) 835 if (!PyArg_ParseTuple(args, "|i:_getframe", &depth))
836 return NULL; 836 return NULL;
837 837
838 while (depth > 0 && f != NULL) { 838 while (depth > 0 && f != NULL) {
839 f = f->f_back; 839 f = f->f_back;
840 --depth; 840 --depth;
841 } 841 }
842 if (f == NULL) { 842 if (f == NULL) {
843 PyErr_SetString(PyExc_ValueError, 843 PyErr_SetString(PyExc_ValueError,
844 "call stack is not deep enough"); 844 "call stack is not deep enough");
845 return NULL; 845 return NULL;
846 } 846 }
847 Py_INCREF(f); 847 Py_INCREF(f);
848 return (PyObject*)f; 848 return (PyObject*)f;
849 } 849 }
850 850
851 PyDoc_STRVAR(current_frames_doc, 851 PyDoc_STRVAR(current_frames_doc,
852 "_current_frames() -> dictionary\n\ 852 "_current_frames() -> dictionary\n\
853 \n\ 853 \n\
858 ); 858 );
859 859
860 static PyObject * 860 static PyObject *
861 sys_current_frames(PyObject *self, PyObject *noargs) 861 sys_current_frames(PyObject *self, PyObject *noargs)
862 { 862 {
863 return _PyThread_CurrentFrames(); 863 return _PyThread_CurrentFrames();
864 } 864 }
865 865
866 PyDoc_STRVAR(call_tracing_doc, 866 PyDoc_STRVAR(call_tracing_doc,
867 "call_tracing(func, args) -> object\n\ 867 "call_tracing(func, args) -> object\n\
868 \n\ 868 \n\
872 ); 872 );
873 873
874 static PyObject * 874 static PyObject *
875 sys_call_tracing(PyObject *self, PyObject *args) 875 sys_call_tracing(PyObject *self, PyObject *args)
876 { 876 {
877 PyObject *func, *funcargs; 877 PyObject *func, *funcargs;
878 if (!PyArg_ParseTuple(args, "OO!:call_tracing", &func, &PyTuple_Type, &funcargs)) 878 if (!PyArg_ParseTuple(args, "OO!:call_tracing", &func, &PyTuple_Type, &funcargs))
879 return NULL; 879 return NULL;
880 return _PyEval_CallTracing(func, funcargs); 880 return _PyEval_CallTracing(func, funcargs);
881 } 881 }
882 882
883 PyDoc_STRVAR(callstats_doc, 883 PyDoc_STRVAR(callstats_doc,
884 "callstats() -> tuple of integers\n\ 884 "callstats() -> tuple of integers\n\
885 \n\ 885 \n\
922 #endif 922 #endif
923 923
924 static PyObject * 924 static PyObject *
925 sys_clear_type_cache(PyObject* self, PyObject* args) 925 sys_clear_type_cache(PyObject* self, PyObject* args)
926 { 926 {
927 PyType_ClearCache(); 927 PyType_ClearCache();
928 Py_RETURN_NONE; 928 Py_RETURN_NONE;
929 } 929 }
930 930
931 PyDoc_STRVAR(sys_clear_type_cache__doc__, 931 PyDoc_STRVAR(sys_clear_type_cache__doc__,
932 "_clear_type_cache() -> None\n\ 932 "_clear_type_cache() -> None\n\
933 Clear the internal type lookup cache."); 933 Clear the internal type lookup cache.");
934 934
935 935
936 static PyMethodDef sys_methods[] = { 936 static PyMethodDef sys_methods[] = {
937 /* Might as well keep this in alphabetic order */ 937 /* Might as well keep this in alphabetic order */
938 {"callstats", (PyCFunction)PyEval_GetCallStats, METH_NOARGS, 938 {"callstats", (PyCFunction)PyEval_GetCallStats, METH_NOARGS,
939 callstats_doc}, 939 callstats_doc},
940 {"_clear_type_cache", sys_clear_type_cache, METH_NOARGS, 940 {"_clear_type_cache", sys_clear_type_cache, METH_NOARGS,
941 sys_clear_type_cache__doc__}, 941 sys_clear_type_cache__doc__},
942 {"_current_frames", sys_current_frames, METH_NOARGS, 942 {"_current_frames", sys_current_frames, METH_NOARGS,
943 current_frames_doc}, 943 current_frames_doc},
944 {"displayhook", sys_displayhook, METH_O, displayhook_doc}, 944 {"displayhook", sys_displayhook, METH_O, displayhook_doc},
945 {"exc_info", sys_exc_info, METH_NOARGS, exc_info_doc}, 945 {"exc_info", sys_exc_info, METH_NOARGS, exc_info_doc},
946 {"excepthook", sys_excepthook, METH_VARARGS, excepthook_doc}, 946 {"excepthook", sys_excepthook, METH_VARARGS, excepthook_doc},
947 {"exit", sys_exit, METH_VARARGS, exit_doc}, 947 {"exit", sys_exit, METH_VARARGS, exit_doc},
948 {"getdefaultencoding", (PyCFunction)sys_getdefaultencoding, 948 {"getdefaultencoding", (PyCFunction)sys_getdefaultencoding,
949 METH_NOARGS, getdefaultencoding_doc}, 949 METH_NOARGS, getdefaultencoding_doc},
950 #ifdef HAVE_DLOPEN 950 #ifdef HAVE_DLOPEN
951 {"getdlopenflags", (PyCFunction)sys_getdlopenflags, METH_NOARGS, 951 {"getdlopenflags", (PyCFunction)sys_getdlopenflags, METH_NOARGS,
952 getdlopenflags_doc}, 952 getdlopenflags_doc},
953 #endif 953 #endif
954 #ifdef COUNT_ALLOCS 954 #ifdef COUNT_ALLOCS
955 {"getcounts", (PyCFunction)sys_getcounts, METH_NOARGS}, 955 {"getcounts", (PyCFunction)sys_getcounts, METH_NOARGS},
956 #endif 956 #endif
957 #ifdef DYNAMIC_EXECUTION_PROFILE 957 #ifdef DYNAMIC_EXECUTION_PROFILE
958 {"getdxp", _Py_GetDXProfile, METH_VARARGS}, 958 {"getdxp", _Py_GetDXProfile, METH_VARARGS},
959 #endif 959 #endif
960 {"getfilesystemencoding", (PyCFunction)sys_getfilesystemencoding, 960 {"getfilesystemencoding", (PyCFunction)sys_getfilesystemencoding,
961 METH_NOARGS, getfilesystemencoding_doc}, 961 METH_NOARGS, getfilesystemencoding_doc},
962 #ifdef Py_TRACE_REFS 962 #ifdef Py_TRACE_REFS
963 {"getobjects", _Py_GetObjects, METH_VARARGS}, 963 {"getobjects", _Py_GetObjects, METH_VARARGS},
964 #endif 964 #endif
965 #ifdef Py_REF_DEBUG 965 #ifdef Py_REF_DEBUG
966 {"gettotalrefcount", (PyCFunction)sys_gettotalrefcount, METH_NOARGS}, 966 {"gettotalrefcount", (PyCFunction)sys_gettotalrefcount, METH_NOARGS},
967 #endif 967 #endif
968 {"getrefcount", (PyCFunction)sys_getrefcount, METH_O, getrefcount_doc}, 968 {"getrefcount", (PyCFunction)sys_getrefcount, METH_O, getrefcount_doc},
969 {"getrecursionlimit", (PyCFunction)sys_getrecursionlimit, METH_NOARGS, 969 {"getrecursionlimit", (PyCFunction)sys_getrecursionlimit, METH_NOARGS,
970 getrecursionlimit_doc}, 970 getrecursionlimit_doc},
971 {"getsizeof", (PyCFunction)sys_getsizeof, 971 {"getsizeof", (PyCFunction)sys_getsizeof,
972 METH_VARARGS | METH_KEYWORDS, getsizeof_doc}, 972 METH_VARARGS | METH_KEYWORDS, getsizeof_doc},
973 {"_getframe", sys_getframe, METH_VARARGS, getframe_doc}, 973 {"_getframe", sys_getframe, METH_VARARGS, getframe_doc},
974 #ifdef MS_WINDOWS 974 #ifdef MS_WINDOWS
975 {"getwindowsversion", (PyCFunction)sys_getwindowsversion, METH_NOARGS, 975 {"getwindowsversion", (PyCFunction)sys_getwindowsversion, METH_NOARGS,
976 getwindowsversion_doc}, 976 getwindowsversion_doc},
977 #endif /* MS_WINDOWS */ 977 #endif /* MS_WINDOWS */
978 {"intern", sys_intern, METH_VARARGS, intern_doc}, 978 {"intern", sys_intern, METH_VARARGS, intern_doc},
979 #ifdef USE_MALLOPT 979 #ifdef USE_MALLOPT
980 {"mdebug", sys_mdebug, METH_VARARGS}, 980 {"mdebug", sys_mdebug, METH_VARARGS},
981 #endif 981 #endif
982 {"setdefaultencoding", sys_setdefaultencoding, METH_VARARGS, 982 {"setdefaultencoding", sys_setdefaultencoding, METH_VARARGS,
983 setdefaultencoding_doc}, 983 setdefaultencoding_doc},
984 {"setfilesystemencoding", sys_setfilesystemencoding, METH_VARARGS, 984 {"setfilesystemencoding", sys_setfilesystemencoding, METH_VARARGS,
985 setfilesystemencoding_doc}, 985 setfilesystemencoding_doc},
986 {"setcheckinterval", sys_setcheckinterval, METH_VARARGS, 986 {"setcheckinterval", sys_setcheckinterval, METH_VARARGS,
987 setcheckinterval_doc}, 987 setcheckinterval_doc},
988 {"getcheckinterval", sys_getcheckinterval, METH_NOARGS, 988 {"getcheckinterval", sys_getcheckinterval, METH_NOARGS,
989 getcheckinterval_doc}, 989 getcheckinterval_doc},
990 #ifdef WITH_THREAD 990 #ifdef WITH_THREAD
991 {"setswitchinterval", sys_setswitchinterval, METH_VARARGS, 991 {"setswitchinterval", sys_setswitchinterval, METH_VARARGS,
992 setswitchinterval_doc}, 992 setswitchinterval_doc},
993 {"getswitchinterval", sys_getswitchinterval, METH_NOARGS, 993 {"getswitchinterval", sys_getswitchinterval, METH_NOARGS,
994 getswitchinterval_doc}, 994 getswitchinterval_doc},
995 #endif 995 #endif
996 #ifdef HAVE_DLOPEN 996 #ifdef HAVE_DLOPEN
997 {"setdlopenflags", sys_setdlopenflags, METH_VARARGS, 997 {"setdlopenflags", sys_setdlopenflags, METH_VARARGS,
998 setdlopenflags_doc}, 998 setdlopenflags_doc},
999 #endif 999 #endif
1000 {"setprofile", sys_setprofile, METH_O, setprofile_doc}, 1000 {"setprofile", sys_setprofile, METH_O, setprofile_doc},
1001 {"getprofile", sys_getprofile, METH_NOARGS, getprofile_doc}, 1001 {"getprofile", sys_getprofile, METH_NOARGS, getprofile_doc},
1002 {"setrecursionlimit", sys_setrecursionlimit, METH_VARARGS, 1002 {"setrecursionlimit", sys_setrecursionlimit, METH_VARARGS,
1003 setrecursionlimit_doc}, 1003 setrecursionlimit_doc},
1004 #ifdef WITH_TSC 1004 #ifdef WITH_TSC
1005 {"settscdump", sys_settscdump, METH_VARARGS, settscdump_doc}, 1005 {"settscdump", sys_settscdump, METH_VARARGS, settscdump_doc},
1006 #endif 1006 #endif
1007 {"settrace", sys_settrace, METH_O, settrace_doc}, 1007 {"settrace", sys_settrace, METH_O, settrace_doc},
1008 {"gettrace", sys_gettrace, METH_NOARGS, gettrace_doc}, 1008 {"gettrace", sys_gettrace, METH_NOARGS, gettrace_doc},
1009 {"call_tracing", sys_call_tracing, METH_VARARGS, call_tracing_doc}, 1009 {"call_tracing", sys_call_tracing, METH_VARARGS, call_tracing_doc},
1010 {NULL, NULL} /* sentinel */ 1010 {NULL, NULL} /* sentinel */
1011 }; 1011 };
1012 1012
1013 static PyObject * 1013 static PyObject *
1014 list_builtin_module_names(void) 1014 list_builtin_module_names(void)
1015 { 1015 {
1016 PyObject *list = PyList_New(0); 1016 PyObject *list = PyList_New(0);
1017 int i; 1017 int i;
1018 if (list == NULL) 1018 if (list == NULL)
1019 return NULL; 1019 return NULL;
1020 for (i = 0; PyImport_Inittab[i].name != NULL; i++) { 1020 for (i = 0; PyImport_Inittab[i].name != NULL; i++) {
1021 PyObject *name = PyUnicode_FromString( 1021 PyObject *name = PyUnicode_FromString(
1022 PyImport_Inittab[i].name); 1022 PyImport_Inittab[i].name);
1023 if (name == NULL) 1023 if (name == NULL)
1024 break; 1024 break;
1025 PyList_Append(list, name); 1025 PyList_Append(list, name);
1026 Py_DECREF(name); 1026 Py_DECREF(name);
1027 } 1027 }
1028 if (PyList_Sort(list) != 0) { 1028 if (PyList_Sort(list) != 0) {
1029 Py_DECREF(list); 1029 Py_DECREF(list);
1030 list = NULL; 1030 list = NULL;
1031 } 1031 }
1032 if (list) { 1032 if (list) {
1033 PyObject *v = PyList_AsTuple(list); 1033 PyObject *v = PyList_AsTuple(list);
1034 Py_DECREF(list); 1034 Py_DECREF(list);
1035 list = v; 1035 list = v;
1036 } 1036 }
1037 return list; 1037 return list;
1038 } 1038 }
1039 1039
1040 static PyObject *warnoptions = NULL; 1040 static PyObject *warnoptions = NULL;
1041 1041
1042 void 1042 void
1043 PySys_ResetWarnOptions(void) 1043 PySys_ResetWarnOptions(void)
1044 { 1044 {
1045 if (warnoptions == NULL || !PyList_Check(warnoptions)) 1045 if (warnoptions == NULL || !PyList_Check(warnoptions))
1046 return; 1046 return;
1047 PyList_SetSlice(warnoptions, 0, PyList_GET_SIZE(warnoptions), NULL); 1047 PyList_SetSlice(warnoptions, 0, PyList_GET_SIZE(warnoptions), NULL);
1048 } 1048 }
1049 1049
1050 void 1050 void
1051 PySys_AddWarnOption(const wchar_t *s) 1051 PySys_AddWarnOption(const wchar_t *s)
1052 { 1052 {
1053 PyObject *str; 1053 PyObject *str;
1054 1054
1055 if (warnoptions == NULL || !PyList_Check(warnoptions)) { 1055 if (warnoptions == NULL || !PyList_Check(warnoptions)) {
1056 Py_XDECREF(warnoptions); 1056 Py_XDECREF(warnoptions);
1057 warnoptions = PyList_New(0); 1057 warnoptions = PyList_New(0);
1058 if (warnoptions == NULL) 1058 if (warnoptions == NULL)
1059 return; 1059 return;
1060 } 1060 }
1061 str = PyUnicode_FromWideChar(s, -1); 1061 str = PyUnicode_FromWideChar(s, -1);
1062 if (str != NULL) { 1062 if (str != NULL) {
1063 PyList_Append(warnoptions, str); 1063 PyList_Append(warnoptions, str);
1064 Py_DECREF(str); 1064 Py_DECREF(str);
1065 } 1065 }
1066 } 1066 }
1067 1067
1068 int 1068 int
1069 PySys_HasWarnOptions(void) 1069 PySys_HasWarnOptions(void)
1070 { 1070 {
1172 static const char *svn_revision; 1172 static const char *svn_revision;
1173 1173
1174 static void 1174 static void
1175 svnversion_init(void) 1175 svnversion_init(void)
1176 { 1176 {
1177 const char *python, *br_start, *br_end, *br_end2, *svnversion; 1177 const char *python, *br_start, *br_end, *br_end2, *svnversion;
1178 Py_ssize_t len; 1178 Py_ssize_t len;
1179 int istag = 0; 1179 int istag = 0;
1180 1180
1181 if (svn_initialized) 1181 if (svn_initialized)
1182 return; 1182 return;
1183 1183
1184 python = strstr(headurl, "/python/"); 1184 python = strstr(headurl, "/python/");
1185 if (!python) { 1185 if (!python) {
1186 strcpy(branch, "unknown branch"); 1186 strcpy(branch, "unknown branch");
1187 strcpy(shortbranch, "unknown"); 1187 strcpy(shortbranch, "unknown");
1188 } 1188 }
1189 else { 1189 else {
1190 br_start = python + 8; 1190 br_start = python + 8;
1191 br_end = strchr(br_start, '/'); 1191 br_end = strchr(br_start, '/');
1192 assert(br_end); 1192 assert(br_end);
1193 1193
1194 /* Works even for trunk, 1194 /* Works even for trunk,
1195 as we are in trunk/Python/sysmodule.c */ 1195 as we are in trunk/Python/sysmodule.c */
1196 br_end2 = strchr(br_end+1, '/'); 1196 br_end2 = strchr(br_end+1, '/');
1197 1197
1198 istag = strncmp(br_start, "tags", 4) == 0; 1198 istag = strncmp(br_start, "tags", 4) == 0;
1199 if (strncmp(br_start, "trunk", 5) == 0) { 1199 if (strncmp(br_start, "trunk", 5) == 0) {
1200 strcpy(branch, "trunk"); 1200 strcpy(branch, "trunk");
1201 strcpy(shortbranch, "trunk"); 1201 strcpy(shortbranch, "trunk");
1202 } 1202 }
1203 else if (istag || strncmp(br_start, "branches", 8) == 0) { 1203 else if (istag || strncmp(br_start, "branches", 8) == 0) {
1204 len = br_end2 - br_start; 1204 len = br_end2 - br_start;
1205 strncpy(branch, br_start, len); 1205 strncpy(branch, br_start, len);
1206 branch[len] = '\0'; 1206 branch[len] = '\0';
1207 1207
1208 len = br_end2 - (br_end + 1); 1208 len = br_end2 - (br_end + 1);
1209 strncpy(shortbranch, br_end + 1, len); 1209 strncpy(shortbranch, br_end + 1, len);
1210 shortbranch[len] = '\0'; 1210 shortbranch[len] = '\0';
1211 } 1211 }
1212 else { 1212 else {
1213 Py_FatalError("bad HeadURL"); 1213 Py_FatalError("bad HeadURL");
1214 return; 1214 return;
1215 } 1215 }
1216 } 1216 }
1217 1217
1218 1218
1219 svnversion = _Py_svnversion(); 1219 svnversion = _Py_svnversion();
1220 if (strcmp(svnversion, "Unversioned directory") != 0 && strcmp(svnversion, "exported") != 0) 1220 if (strcmp(svnversion, "Unversioned directory") != 0 && strcmp(svnversion, "exported") != 0)
1221 svn_revision = svnversion; 1221 svn_revision = svnversion;
1222 else if (istag) { 1222 else if (istag) {
1223 len = strlen(_patchlevel_revision); 1223 len = strlen(_patchlevel_revision);
1224 assert(len >= 13); 1224 assert(len >= 13);
1225 assert(len < (sizeof(patchlevel_revision) + 13)); 1225 assert(len < (sizeof(patchlevel_revision) + 13));
1226 strncpy(patchlevel_revision, _patchlevel_revision + 11, 1226 strncpy(patchlevel_revision, _patchlevel_revision + 11,
1227 len - 13); 1227 len - 13);
1228 patchlevel_revision[len - 13] = '\0'; 1228 patchlevel_revision[len - 13] = '\0';
1229 svn_revision = patchlevel_revision; 1229 svn_revision = patchlevel_revision;
1230 } 1230 }
1231 else 1231 else
1232 svn_revision = ""; 1232 svn_revision = "";
1233 1233
1234 svn_initialized = 1; 1234 svn_initialized = 1;
1235 } 1235 }
1236 1236
1237 /* Return svnversion output if available. 1237 /* Return svnversion output if available.
1238 Else return Revision of patchlevel.h if on branch. 1238 Else return Revision of patchlevel.h if on branch.
1239 Else return empty string */ 1239 Else return empty string */
1240 const char* 1240 const char*
1241 Py_SubversionRevision() 1241 Py_SubversionRevision()
1242 { 1242 {
1243 svnversion_init(); 1243 svnversion_init();
1244 return svn_revision; 1244 return svn_revision;
1245 } 1245 }
1246 1246
1247 const char* 1247 const char*
1248 Py_SubversionShortBranch() 1248 Py_SubversionShortBranch()
1249 { 1249 {
1250 svnversion_init(); 1250 svnversion_init();
1251 return shortbranch; 1251 return shortbranch;
1252 } 1252 }
1253 1253
1254 1254
1255 PyDoc_STRVAR(flags__doc__, 1255 PyDoc_STRVAR(flags__doc__,
1256 "sys.flags\n\ 1256 "sys.flags\n\
1258 Flags provided through command line arguments or environment vars."); 1258 Flags provided through command line arguments or environment vars.");
1259 1259
1260 static PyTypeObject FlagsType; 1260 static PyTypeObject FlagsType;
1261 1261
1262 static PyStructSequence_Field flags_fields[] = { 1262 static PyStructSequence_Field flags_fields[] = {
1263 {"debug", "-d"}, 1263 {"debug", "-d"},
1264 {"division_warning", "-Q"}, 1264 {"division_warning", "-Q"},
1265 {"inspect", "-i"}, 1265 {"inspect", "-i"},
1266 {"interactive", "-i"}, 1266 {"interactive", "-i"},
1267 {"optimize", "-O or -OO"}, 1267 {"optimize", "-O or -OO"},
1268 {"dont_write_bytecode", "-B"}, 1268 {"dont_write_bytecode", "-B"},
1269 {"no_user_site", "-s"}, 1269 {"no_user_site", "-s"},
1270 {"no_site", "-S"}, 1270 {"no_site", "-S"},
1271 {"ignore_environment", "-E"}, 1271 {"ignore_environment", "-E"},
1272 {"verbose", "-v"}, 1272 {"verbose", "-v"},
1273 #ifdef RISCOS 1273 #ifdef RISCOS
1274 {"riscos_wimp", "???"}, 1274 {"riscos_wimp", "???"},
1275 #endif 1275 #endif
1276 /* {"unbuffered", "-u"}, */ 1276 /* {"unbuffered", "-u"}, */
1277 /* {"skip_first", "-x"}, */ 1277 /* {"skip_first", "-x"}, */
1278 {"bytes_warning", "-b"}, 1278 {"bytes_warning", "-b"},
1279 {0} 1279 {0}
1280 }; 1280 };
1281 1281
1282 static PyStructSequence_Desc flags_desc = { 1282 static PyStructSequence_Desc flags_desc = {
1283 "sys.flags", /* name */ 1283 "sys.flags", /* name */
1284 flags__doc__, /* doc */ 1284 flags__doc__, /* doc */
1285 flags_fields, /* fields */ 1285 flags_fields, /* fields */
1286 #ifdef RISCOS 1286 #ifdef RISCOS
1287 12 1287 12
1288 #else 1288 #else
1289 11 1289 11
1290 #endif 1290 #endif
1291 }; 1291 };
1292 1292
1293 static PyObject* 1293 static PyObject*
1294 make_flags(void) 1294 make_flags(void)
1295 { 1295 {
1296 int pos = 0; 1296 int pos = 0;
1297 PyObject *seq; 1297 PyObject *seq;
1298 1298
1299 seq = PyStructSequence_New(&FlagsType); 1299 seq = PyStructSequence_New(&FlagsType);
1300 if (seq == NULL) 1300 if (seq == NULL)
1301 return NULL; 1301 return NULL;
1302 1302
1303 #define SetFlag(flag) \ 1303 #define SetFlag(flag) \
1304 PyStructSequence_SET_ITEM(seq, pos++, PyLong_FromLong(flag)) 1304 PyStructSequence_SET_ITEM(seq, pos++, PyLong_FromLong(flag))
1305 1305
1306 SetFlag(Py_DebugFlag); 1306 SetFlag(Py_DebugFlag);
1307 SetFlag(Py_DivisionWarningFlag); 1307 SetFlag(Py_DivisionWarningFlag);
1308 SetFlag(Py_InspectFlag); 1308 SetFlag(Py_InspectFlag);
1309 SetFlag(Py_InteractiveFlag); 1309 SetFlag(Py_InteractiveFlag);
1310 SetFlag(Py_OptimizeFlag); 1310 SetFlag(Py_OptimizeFlag);
1311 SetFlag(Py_DontWriteBytecodeFlag); 1311 SetFlag(Py_DontWriteBytecodeFlag);
1312 SetFlag(Py_NoUserSiteDirectory); 1312 SetFlag(Py_NoUserSiteDirectory);
1313 SetFlag(Py_NoSiteFlag); 1313 SetFlag(Py_NoSiteFlag);
1314 SetFlag(Py_IgnoreEnvironmentFlag); 1314 SetFlag(Py_IgnoreEnvironmentFlag);
1315 SetFlag(Py_VerboseFlag); 1315 SetFlag(Py_VerboseFlag);
1316 #ifdef RISCOS 1316 #ifdef RISCOS
1317 SetFlag(Py_RISCOSWimpFlag); 1317 SetFlag(Py_RISCOSWimpFlag);
1318 #endif 1318 #endif
1319 /* SetFlag(saw_unbuffered_flag); */ 1319 /* SetFlag(saw_unbuffered_flag); */
1320 /* SetFlag(skipfirstline); */ 1320 /* SetFlag(skipfirstline); */
1321 SetFlag(Py_BytesWarningFlag); 1321 SetFlag(Py_BytesWarningFlag);
1322 #undef SetFlag 1322 #undef SetFlag
1323 1323
1324 if (PyErr_Occurred()) { 1324 if (PyErr_Occurred()) {
1325 return NULL; 1325 return NULL;
1326 } 1326 }
1327 return seq; 1327 return seq;
1328 } 1328 }
1329 1329
1330 PyDoc_STRVAR(version_info__doc__, 1330 PyDoc_STRVAR(version_info__doc__,
1331 "sys.version_info\n\ 1331 "sys.version_info\n\
1332 \n\ 1332 \n\
1333 Version information as a named tuple."); 1333 Version information as a named tuple.");
1334 1334
1335 static PyTypeObject VersionInfoType; 1335 static PyTypeObject VersionInfoType;
1336 1336
1337 static PyStructSequence_Field version_info_fields[] = { 1337 static PyStructSequence_Field version_info_fields[] = {
1338 {"major", "Major release number"}, 1338 {"major", "Major release number"},
1339 {"minor", "Minor release number"}, 1339 {"minor", "Minor release number"},
1340 {"micro", "Patch release number"}, 1340 {"micro", "Patch release number"},
1341 {"releaselevel", "'alpha', 'beta', 'candidate', or 'release'"}, 1341 {"releaselevel", "'alpha', 'beta', 'candidate', or 'release'"},
1342 {"serial", "Serial release number"}, 1342 {"serial", "Serial release number"},
1343 {0} 1343 {0}
1344 }; 1344 };
1345 1345
1346 static PyStructSequence_Desc version_info_desc = { 1346 static PyStructSequence_Desc version_info_desc = {
1347 "sys.version_info", /* name */ 1347 "sys.version_info", /* name */
1348 version_info__doc__, /* doc */ 1348 version_info__doc__, /* doc */
1349 version_info_fields, /* fields */ 1349 version_info_fields, /* fields */
1350 5 1350 5
1351 }; 1351 };
1352 1352
1353 static PyObject * 1353 static PyObject *
1354 make_version_info(void) 1354 make_version_info(void)
1355 { 1355 {
1356 PyObject *version_info; 1356 PyObject *version_info;
1357 char *s; 1357 char *s;
1358 int pos = 0; 1358 int pos = 0;
1359 1359
1360 version_info = PyStructSequence_New(&VersionInfoType); 1360 version_info = PyStructSequence_New(&VersionInfoType);
1361 if (version_info == NULL) { 1361 if (version_info == NULL) {
1362 return NULL; 1362 return NULL;
1363 } 1363 }
1364 1364
1365 /* 1365 /*
1366 * These release level checks are mutually exclusive and cover 1366 * These release level checks are mutually exclusive and cover
1367 * the field, so don't get too fancy with the pre-processor! 1367 * the field, so don't get too fancy with the pre-processor!
1368 */ 1368 */
1369 #if PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_ALPHA 1369 #if PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_ALPHA
1370 s = "alpha"; 1370 s = "alpha";
1371 #elif PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_BETA 1371 #elif PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_BETA
1372 s = "beta"; 1372 s = "beta";
1373 #elif PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_GAMMA 1373 #elif PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_GAMMA
1374 s = "candidate"; 1374 s = "candidate";
1375 #elif PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_FINAL 1375 #elif PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_FINAL
1376 s = "final"; 1376 s = "final";
1377 #endif 1377 #endif
1378 1378
1379 #define SetIntItem(flag) \ 1379 #define SetIntItem(flag) \
1380 PyStructSequence_SET_ITEM(version_info, pos++, PyLong_FromLong(flag)) 1380 PyStructSequence_SET_ITEM(version_info, pos++, PyLong_FromLong(flag))
1381 #define SetStrItem(flag) \ 1381 #define SetStrItem(flag) \
1382 PyStructSequence_SET_ITEM(version_info, pos++, PyUnicode_FromString(flag)) 1382 PyStructSequence_SET_ITEM(version_info, pos++, PyUnicode_FromString(flag))
1383 1383
1384 SetIntItem(PY_MAJOR_VERSION); 1384 SetIntItem(PY_MAJOR_VERSION);
1385 SetIntItem(PY_MINOR_VERSION); 1385 SetIntItem(PY_MINOR_VERSION);
1386 SetIntItem(PY_MICRO_VERSION); 1386 SetIntItem(PY_MICRO_VERSION);
1387 SetStrItem(s); 1387 SetStrItem(s);
1388 SetIntItem(PY_RELEASE_SERIAL); 1388 SetIntItem(PY_RELEASE_SERIAL);
1389 #undef SetIntItem 1389 #undef SetIntItem
1390 #undef SetStrItem 1390 #undef SetStrItem
1391 1391
1392 if (PyErr_Occurred()) { 1392 if (PyErr_Occurred()) {
1393 Py_CLEAR(version_info); 1393 Py_CLEAR(version_info);
1394 return NULL; 1394 return NULL;
1395 } 1395 }
1396 return version_info; 1396 return version_info;
1397 } 1397 }
1398 1398
1399 static struct PyModuleDef sysmodule = { 1399 static struct PyModuleDef sysmodule = {
1400 PyModuleDef_HEAD_INIT, 1400 PyModuleDef_HEAD_INIT,
1401 "sys", 1401 "sys",
1402 sys_doc, 1402 sys_doc,
1403 -1, /* multiple "initialization" just copies the module dict. */ 1403 -1, /* multiple "initialization" just copies the module dict. */
1404 sys_methods, 1404 sys_methods,
1405 NULL, 1405 NULL,
1406 NULL, 1406 NULL,
1407 NULL, 1407 NULL,
1408 NULL 1408 NULL
1409 }; 1409 };
1410 1410
1411 PyObject * 1411 PyObject *
1412 _PySys_Init(void) 1412 _PySys_Init(void)
1413 { 1413 {
1414 PyObject *m, *v, *sysdict; 1414 PyObject *m, *v, *sysdict;
1415 char *s; 1415 char *s;
1416 1416
1417 m = PyModule_Create(&sysmodule); 1417 m = PyModule_Create(&sysmodule);
1418 if (m == NULL) 1418 if (m == NULL)
1419 return NULL; 1419 return NULL;
1420 sysdict = PyModule_GetDict(m); 1420 sysdict = PyModule_GetDict(m);
1421 #define SET_SYS_FROM_STRING(key, value) \ 1421 #define SET_SYS_FROM_STRING(key, value) \
1422 v = value; \ 1422 v = value; \
1423 if (v != NULL) \ 1423 if (v != NULL) \
1424 PyDict_SetItemString(sysdict, key, v); \ 1424 PyDict_SetItemString(sysdict, key, v); \
1425 Py_XDECREF(v) 1425 Py_XDECREF(v)
1426 1426
1427 /* Check that stdin is not a directory 1427 /* Check that stdin is not a directory
1428 Using shell redirection, you can redirect stdin to a directory, 1428 Using shell redirection, you can redirect stdin to a directory,
1429 crashing the Python interpreter. Catch this common mistake here 1429 crashing the Python interpreter. Catch this common mistake here
1430 and output a useful error message. Note that under MS Windows, 1430 and output a useful error message. Note that under MS Windows,
1431 the shell already prevents that. */ 1431 the shell already prevents that. */
1432 #if !defined(MS_WINDOWS) 1432 #if !defined(MS_WINDOWS)
1433 { 1433 {
1434 struct stat sb; 1434 struct stat sb;
1435 if (fstat(fileno(stdin), &sb) == 0 && 1435 if (fstat(fileno(stdin), &sb) == 0 &&
1436 S_ISDIR(sb.st_mode)) { 1436 S_ISDIR(sb.st_mode)) {
1437 /* There's nothing more we can do. */ 1437 /* There's nothing more we can do. */
1438 /* Py_FatalError() will core dump, so just exit. */ 1438 /* Py_FatalError() will core dump, so just exit. */
1439 PySys_WriteStderr("Python error: <stdin> is a directory, cannot continue\n"); 1439 PySys_WriteStderr("Python error: <stdin> is a directory, cannot continue\n");
1440 exit(EXIT_FAILURE); 1440 exit(EXIT_FAILURE);
1441 } 1441 }
1442 } 1442 }
1443 #endif 1443 #endif
1444 1444
1445 /* stdin/stdout/stderr are now set by pythonrun.c */ 1445 /* stdin/stdout/stderr are now set by pythonrun.c */
1446 1446
1447 PyDict_SetItemString(sysdict, "__displayhook__", 1447 PyDict_SetItemString(sysdict, "__displayhook__",
1448 PyDict_GetItemString(sysdict, "displayhook")); 1448 PyDict_GetItemString(sysdict, "displayhook"));
1449 PyDict_SetItemString(sysdict, "__excepthook__", 1449 PyDict_SetItemString(sysdict, "__excepthook__",
1450 PyDict_GetItemString(sysdict, "excepthook")); 1450 PyDict_GetItemString(sysdict, "excepthook"));
1451 SET_SYS_FROM_STRING("version", 1451 SET_SYS_FROM_STRING("version",
1452 PyUnicode_FromString(Py_GetVersion())); 1452 PyUnicode_FromString(Py_GetVersion()));
1453 SET_SYS_FROM_STRING("hexversion", 1453 SET_SYS_FROM_STRING("hexversion",
1454 PyLong_FromLong(PY_VERSION_HEX)); 1454 PyLong_FromLong(PY_VERSION_HEX));
1455 svnversion_init(); 1455 svnversion_init();
1456 SET_SYS_FROM_STRING("subversion", 1456 SET_SYS_FROM_STRING("subversion",
1457 Py_BuildValue("(UUU)", "CPython", branch, 1457 Py_BuildValue("(UUU)", "CPython", branch,
1458 svn_revision)); 1458 svn_revision));
1459 SET_SYS_FROM_STRING("dont_write_bytecode", 1459 SET_SYS_FROM_STRING("dont_write_bytecode",
1460 PyBool_FromLong(Py_DontWriteBytecodeFlag)); 1460 PyBool_FromLong(Py_DontWriteBytecodeFlag));
1461 SET_SYS_FROM_STRING("api_version", 1461 SET_SYS_FROM_STRING("api_version",
1462 PyLong_FromLong(PYTHON_API_VERSION)); 1462 PyLong_FromLong(PYTHON_API_VERSION));
1463 SET_SYS_FROM_STRING("copyright", 1463 SET_SYS_FROM_STRING("copyright",
1464 PyUnicode_FromString(Py_GetCopyright())); 1464 PyUnicode_FromString(Py_GetCopyright()));
1465 SET_SYS_FROM_STRING("platform", 1465 SET_SYS_FROM_STRING("platform",
1466 PyUnicode_FromString(Py_GetPlatform())); 1466 PyUnicode_FromString(Py_GetPlatform()));
1467 SET_SYS_FROM_STRING("executable", 1467 SET_SYS_FROM_STRING("executable",
1468 PyUnicode_FromWideChar( 1468 PyUnicode_FromWideChar(
1469 Py_GetProgramFullPath(), -1)); 1469 Py_GetProgramFullPath(), -1));
1470 SET_SYS_FROM_STRING("prefix", 1470 SET_SYS_FROM_STRING("prefix",
1471 PyUnicode_FromWideChar(Py_GetPrefix(), -1)); 1471 PyUnicode_FromWideChar(Py_GetPrefix(), -1));
1472 SET_SYS_FROM_STRING("exec_prefix", 1472 SET_SYS_FROM_STRING("exec_prefix",
1473 PyUnicode_FromWideChar(Py_GetExecPrefix(), -1)); 1473 PyUnicode_FromWideChar(Py_GetExecPrefix(), -1));
1474 SET_SYS_FROM_STRING("maxsize", 1474 SET_SYS_FROM_STRING("maxsize",
1475 PyLong_FromSsize_t(PY_SSIZE_T_MAX)); 1475 PyLong_FromSsize_t(PY_SSIZE_T_MAX));
1476 SET_SYS_FROM_STRING("float_info", 1476 SET_SYS_FROM_STRING("float_info",
1477 PyFloat_GetInfo()); 1477 PyFloat_GetInfo());
1478 SET_SYS_FROM_STRING("int_info", 1478 SET_SYS_FROM_STRING("int_info",
1479 PyLong_GetInfo()); 1479 PyLong_GetInfo());
1480 SET_SYS_FROM_STRING("maxunicode", 1480 SET_SYS_FROM_STRING("maxunicode",
1481 PyLong_FromLong(PyUnicode_GetMax())); 1481 PyLong_FromLong(PyUnicode_GetMax()));
1482 SET_SYS_FROM_STRING("builtin_module_names", 1482 SET_SYS_FROM_STRING("builtin_module_names",
1483 list_builtin_module_names()); 1483 list_builtin_module_names());
1484 { 1484 {
1485 /* Assumes that longs are at least 2 bytes long. 1485 /* Assumes that longs are at least 2 bytes long.
1486 Should be safe! */ 1486 Should be safe! */
1487 unsigned long number = 1; 1487 unsigned long number = 1;
1488 char *value; 1488 char *value;
1489 1489
1490 s = (char *) &number; 1490 s = (char *) &number;
1491 if (s[0] == 0) 1491 if (s[0] == 0)
1492 value = "big"; 1492 value = "big";
1493 else 1493 else
1494 value = "little"; 1494 value = "little";
1495 SET_SYS_FROM_STRING("byteorder", 1495 SET_SYS_FROM_STRING("byteorder",
1496 PyUnicode_FromString(value)); 1496 PyUnicode_FromString(value));
1497 } 1497 }
1498 #ifdef MS_COREDLL 1498 #ifdef MS_COREDLL
1499 SET_SYS_FROM_STRING("dllhandle", 1499 SET_SYS_FROM_STRING("dllhandle",
1500 PyLong_FromVoidPtr(PyWin_DLLhModule)); 1500 PyLong_FromVoidPtr(PyWin_DLLhModule));
1501 SET_SYS_FROM_STRING("winver", 1501 SET_SYS_FROM_STRING("winver",
1502 PyUnicode_FromString(PyWin_DLLVersionString)); 1502 PyUnicode_FromString(PyWin_DLLVersionString));
1503 #endif 1503 #endif
1504 if (warnoptions == NULL) { 1504 if (warnoptions == NULL) {
1505 warnoptions = PyList_New(0); 1505 warnoptions = PyList_New(0);
1506 } 1506 }
1507 else { 1507 else {
1508 Py_INCREF(warnoptions); 1508 Py_INCREF(warnoptions);
1509 } 1509 }
1510 if (warnoptions != NULL) { 1510 if (warnoptions != NULL) {
1511 PyDict_SetItemString(sysdict, "warnoptions", warnoptions); 1511 PyDict_SetItemString(sysdict, "warnoptions", warnoptions);
1512 } 1512 }
1513 1513
1514 /* version_info */ 1514 /* version_info */
1515 if (VersionInfoType.tp_name == 0) 1515 if (VersionInfoType.tp_name == 0)
1516 PyStructSequence_InitType(&VersionInfoType, &version_info_desc); 1516 PyStructSequence_InitType(&VersionInfoType, &version_info_desc);
1517 SET_SYS_FROM_STRING("version_info", make_version_info()); 1517 SET_SYS_FROM_STRING("version_info", make_version_info());
1518 /* prevent user from creating new instances */ 1518 /* prevent user from creating new instances */
1519 VersionInfoType.tp_init = NULL; 1519 VersionInfoType.tp_init = NULL;
1520 VersionInfoType.tp_new = NULL; 1520 VersionInfoType.tp_new = NULL;
1521 1521
1522 /* flags */ 1522 /* flags */
1523 if (FlagsType.tp_name == 0) 1523 if (FlagsType.tp_name == 0)
1524 PyStructSequence_InitType(&FlagsType, &flags_desc); 1524 PyStructSequence_InitType(&FlagsType, &flags_desc);
1525 SET_SYS_FROM_STRING("flags", make_flags()); 1525 SET_SYS_FROM_STRING("flags", make_flags());
1526 /* prevent user from creating new instances */ 1526 /* prevent user from creating new instances */
1527 FlagsType.tp_init = NULL; 1527 FlagsType.tp_init = NULL;
1528 FlagsType.tp_new = NULL; 1528 FlagsType.tp_new = NULL;
1529 1529
1530 1530
1531 #if defined(MS_WINDOWS) 1531 #if defined(MS_WINDOWS)
1532 /* getwindowsversion */ 1532 /* getwindowsversion */
1533 if (WindowsVersionType.tp_name == 0) 1533 if (WindowsVersionType.tp_name == 0)
1534 PyStructSequence_InitType(&WindowsVersionType, &windows_version_desc); 1534 PyStructSequence_InitType(&WindowsVersionType, &windows_version_desc);
1535 /* prevent user from creating new instances */ 1535 /* prevent user from creating new instances */
1536 WindowsVersionType.tp_init = NULL; 1536 WindowsVersionType.tp_init = NULL;
1537 WindowsVersionType.tp_new = NULL; 1537 WindowsVersionType.tp_new = NULL;
1538 #endif 1538 #endif
1539 1539
1540 /* float repr style: 0.03 (short) vs 0.029999999999999999 (legacy) */ 1540 /* float repr style: 0.03 (short) vs 0.029999999999999999 (legacy) */
1541 #ifndef PY_NO_SHORT_FLOAT_REPR 1541 #ifndef PY_NO_SHORT_FLOAT_REPR
1542 SET_SYS_FROM_STRING("float_repr_style", 1542 SET_SYS_FROM_STRING("float_repr_style",
1543 PyUnicode_FromString("short")); 1543 PyUnicode_FromString("short"));
1544 #else 1544 #else
1545 SET_SYS_FROM_STRING("float_repr_style", 1545 SET_SYS_FROM_STRING("float_repr_style",
1546 PyUnicode_FromString("legacy")); 1546 PyUnicode_FromString("legacy"));
1547 #endif 1547 #endif
1548 1548
1549 #undef SET_SYS_FROM_STRING 1549 #undef SET_SYS_FROM_STRING
1550 if (PyErr_Occurred()) 1550 if (PyErr_Occurred())
1551 return NULL; 1551 return NULL;
1552 return m; 1552 return m;
1553 } 1553 }
1554 1554
1555 static PyObject * 1555 static PyObject *
1556 makepathobject(const wchar_t *path, wchar_t delim) 1556 makepathobject(const wchar_t *path, wchar_t delim)
1557 { 1557 {
1558 int i, n; 1558 int i, n;
1559 const wchar_t *p; 1559 const wchar_t *p;
1560 PyObject *v, *w; 1560 PyObject *v, *w;
1561 1561
1562 n = 1; 1562 n = 1;
1563 p = path; 1563 p = path;
1564 while ((p = wcschr(p, delim)) != NULL) { 1564 while ((p = wcschr(p, delim)) != NULL) {
1565 n++; 1565 n++;
1566 p++; 1566 p++;
1567 } 1567 }
1568 v = PyList_New(n); 1568 v = PyList_New(n);
1569 if (v == NULL) 1569 if (v == NULL)
1570 return NULL; 1570 return NULL;
1571 for (i = 0; ; i++) { 1571 for (i = 0; ; i++) {
1572 p = wcschr(path, delim); 1572 p = wcschr(path, delim);
1573 if (p == NULL) 1573 if (p == NULL)
1574 p = path + wcslen(path); /* End of string */ 1574 p = path + wcslen(path); /* End of string */
1575 w = PyUnicode_FromWideChar(path, (Py_ssize_t)(p - path)); 1575 w = PyUnicode_FromWideChar(path, (Py_ssize_t)(p - path));
1576 if (w == NULL) { 1576 if (w == NULL) {
1577 Py_DECREF(v); 1577 Py_DECREF(v);
1578 return NULL; 1578 return NULL;
1579 } 1579 }
1580 PyList_SetItem(v, i, w); 1580 PyList_SetItem(v, i, w);
1581 if (*p == '\0') 1581 if (*p == '\0')
1582 break; 1582 break;
1583 path = p+1; 1583 path = p+1;
1584 } 1584 }
1585 return v; 1585 return v;
1586 } 1586 }
1587 1587
1588 void 1588 void
1589 PySys_SetPath(const wchar_t *path) 1589 PySys_SetPath(const wchar_t *path)
1590 { 1590 {
1591 PyObject *v; 1591 PyObject *v;
1592 if ((v = makepathobject(path, DELIM)) == NULL) 1592 if ((v = makepathobject(path, DELIM)) == NULL)
1593 Py_FatalError("can't create sys.path"); 1593 Py_FatalError("can't create sys.path");
1594 if (PySys_SetObject("path", v) != 0) 1594 if (PySys_SetObject("path", v) != 0)
1595 Py_FatalError("can't assign sys.path"); 1595 Py_FatalError("can't assign sys.path");
1596 Py_DECREF(v); 1596 Py_DECREF(v);
1597 } 1597 }
1598 1598
1599 static PyObject * 1599 static PyObject *
1600 makeargvobject(int argc, wchar_t **argv) 1600 makeargvobject(int argc, wchar_t **argv)
1601 { 1601 {
1602 PyObject *av; 1602 PyObject *av;
1603 if (argc <= 0 || argv == NULL) { 1603 if (argc <= 0 || argv == NULL) {
1604 /* Ensure at least one (empty) argument is seen */ 1604 /* Ensure at least one (empty) argument is seen */
1605 static wchar_t *empty_argv[1] = {L""}; 1605 static wchar_t *empty_argv[1] = {L""};
1606 argv = empty_argv; 1606 argv = empty_argv;
1607 argc = 1; 1607 argc = 1;
1608 } 1608 }
1609 av = PyList_New(argc); 1609 av = PyList_New(argc);
1610 if (av != NULL) { 1610 if (av != NULL) {
1611 int i; 1611 int i;
1612 for (i = 0; i < argc; i++) { 1612 for (i = 0; i < argc; i++) {
1613 #ifdef __VMS 1613 #ifdef __VMS
1614 PyObject *v; 1614 PyObject *v;
1615 1615
1616 /* argv[0] is the script pathname if known */ 1616 /* argv[0] is the script pathname if known */
1617 if (i == 0) { 1617 if (i == 0) {
1618 char* fn = decc$translate_vms(argv[0]); 1618 char* fn = decc$translate_vms(argv[0]);
1619 if ((fn == (char *)0) || fn == (char *)-1) 1619 if ((fn == (char *)0) || fn == (char *)-1)
1620 v = PyUnicode_FromString(argv[0]); 1620 v = PyUnicode_FromString(argv[0]);
1621 else 1621 else
1622 v = PyUnicode_FromString( 1622 v = PyUnicode_FromString(
1623 decc$translate_vms(argv[0])); 1623 decc$translate_vms(argv[0]));
1624 } else 1624 } else
1625 v = PyUnicode_FromString(argv[i]); 1625 v = PyUnicode_FromString(argv[i]);
1626 #else 1626 #else
1627 PyObject *v = PyUnicode_FromWideChar(argv[i], -1); 1627 PyObject *v = PyUnicode_FromWideChar(argv[i], -1);
1628 #endif 1628 #endif
1629 if (v == NULL) { 1629 if (v == NULL) {
1630 Py_DECREF(av); 1630 Py_DECREF(av);
1631 av = NULL; 1631 av = NULL;
1632 break; 1632 break;
1633 } 1633 }
1634 PyList_SetItem(av, i, v); 1634 PyList_SetItem(av, i, v);
1635 } 1635 }
1636 } 1636 }
1637 return av; 1637 return av;
1638 } 1638 }
1639 1639
1640 #ifdef HAVE_REALPATH 1640 #ifdef HAVE_REALPATH
1641 static wchar_t* 1641 static wchar_t*
1642 _wrealpath(const wchar_t *path, wchar_t *resolved_path) 1642 _wrealpath(const wchar_t *path, wchar_t *resolved_path)
1643 { 1643 {
1644 char cpath[PATH_MAX]; 1644 char cpath[PATH_MAX];
1645 char cresolved_path[PATH_MAX]; 1645 char cresolved_path[PATH_MAX];
1646 char *res; 1646 char *res;
1647 size_t r; 1647 size_t r;
1648 r = wcstombs(cpath, path, PATH_MAX); 1648 r = wcstombs(cpath, path, PATH_MAX);
1649 if (r == (size_t)-1 || r >= PATH_MAX) { 1649 if (r == (size_t)-1 || r >= PATH_MAX) {
1650 errno = EINVAL; 1650 errno = EINVAL;
1651 return NULL; 1651 return NULL;
1652 } 1652 }
1653 res = realpath(cpath, cresolved_path); 1653 res = realpath(cpath, cresolved_path);
1654 if (res == NULL) 1654 if (res == NULL)
1655 return NULL; 1655 return NULL;
1656 r = mbstowcs(resolved_path, cresolved_path, PATH_MAX); 1656 r = mbstowcs(resolved_path, cresolved_path, PATH_MAX);
1657 if (r == (size_t)-1 || r >= PATH_MAX) { 1657 if (r == (size_t)-1 || r >= PATH_MAX) {
1658 errno = EINVAL; 1658 errno = EINVAL;
1659 return NULL; 1659 return NULL;
1660 } 1660 }
1661 return resolved_path; 1661 return resolved_path;
1662 } 1662 }
1663 #endif 1663 #endif
1664 1664
1665 void 1665 void
1666 PySys_SetArgv(int argc, wchar_t **argv) 1666 PySys_SetArgv(int argc, wchar_t **argv)
1667 { 1667 {
1668 #if defined(HAVE_REALPATH) 1668 #if defined(HAVE_REALPATH)
1669 wchar_t fullpath[MAXPATHLEN]; 1669 wchar_t fullpath[MAXPATHLEN];
1670 #elif defined(MS_WINDOWS) && !defined(MS_WINCE) 1670 #elif defined(MS_WINDOWS) && !defined(MS_WINCE)
1671 wchar_t fullpath[MAX_PATH]; 1671 wchar_t fullpath[MAX_PATH];
1672 #endif 1672 #endif
1673 PyObject *av = makeargvobject(argc, argv); 1673 PyObject *av = makeargvobject(argc, argv);
1674 PyObject *path = PySys_GetObject("path"); 1674 PyObject *path = PySys_GetObject("path");
1675 if (av == NULL) 1675 if (av == NULL)
1676 Py_FatalError("no mem for sys.argv"); 1676 Py_FatalError("no mem for sys.argv");
1677 if (PySys_SetObject("argv", av) != 0) 1677 if (PySys_SetObject("argv", av) != 0)
1678 Py_FatalError("can't assign sys.argv"); 1678 Py_FatalError("can't assign sys.argv");
1679 if (path != NULL) { 1679 if (path != NULL) {
1680 wchar_t *argv0 = argv[0]; 1680 wchar_t *argv0 = argv[0];
1681 wchar_t *p = NULL; 1681 wchar_t *p = NULL;
1682 Py_ssize_t n = 0; 1682 Py_ssize_t n = 0;
1683 PyObject *a; 1683 PyObject *a;
1684 extern int _Py_wreadlink(const wchar_t *, wchar_t *, size_t); 1684 extern int _Py_wreadlink(const wchar_t *, wchar_t *, size_t);
1685 #ifdef HAVE_READLINK 1685 #ifdef HAVE_READLINK
1686 wchar_t link[MAXPATHLEN+1]; 1686 wchar_t link[MAXPATHLEN+1];
1687 wchar_t argv0copy[2*MAXPATHLEN+1]; 1687 wchar_t argv0copy[2*MAXPATHLEN+1];
1688 int nr = 0; 1688 int nr = 0;
1689 if (argc > 0 && argv0 != NULL && wcscmp(argv0, L"-c") != 0) 1689 if (argc > 0 && argv0 != NULL && wcscmp(argv0, L"-c") != 0)
1690 nr = _Py_wreadlink(argv0, link, MAXPATHLEN); 1690 nr = _Py_wreadlink(argv0, link, MAXPATHLEN);
1691 if (nr > 0) { 1691 if (nr > 0) {
1692 /* It's a symlink */ 1692 /* It's a symlink */
1693 link[nr] = '\0'; 1693 link[nr] = '\0';
1694 if (link[0] == SEP) 1694 if (link[0] == SEP)
1695 argv0 = link; /* Link to absolute path */ 1695 argv0 = link; /* Link to absolute path */
1696 else if (wcschr(link, SEP) == NULL) 1696 else if (wcschr(link, SEP) == NULL)
1697 ; /* Link without path */ 1697 ; /* Link without path */
1698 else { 1698 else {
1699 /* Must join(dirname(argv0), link) */ 1699 /* Must join(dirname(argv0), link) */
1700 wchar_t *q = wcsrchr(argv0, SEP); 1700 wchar_t *q = wcsrchr(argv0, SEP);
1701 if (q == NULL) 1701 if (q == NULL)
1702 argv0 = link; /* argv0 without path */ 1702 argv0 = link; /* argv0 without path */
1703 else { 1703 else {
1704 /* Must make a copy */ 1704 /* Must make a copy */
1705 wcscpy(argv0copy, argv0); 1705 wcscpy(argv0copy, argv0);
1706 q = wcsrchr(argv0copy, SEP); 1706 q = wcsrchr(argv0copy, SEP);
1707 wcscpy(q+1, link); 1707 wcscpy(q+1, link);
1708 argv0 = argv0copy; 1708 argv0 = argv0copy;
1709 } 1709 }
1710 } 1710 }
1711 } 1711 }
1712 #endif /* HAVE_READLINK */ 1712 #endif /* HAVE_READLINK */
1713 #if SEP == '\\' /* Special case for MS filename syntax */ 1713 #if SEP == '\\' /* Special case for MS filename syntax */
1714 if (argc > 0 && argv0 != NULL && wcscmp(argv0, L"-c") != 0) { 1714 if (argc > 0 && argv0 != NULL && wcscmp(argv0, L"-c") != 0) {
1715 wchar_t *q; 1715 wchar_t *q;
1716 #if defined(MS_WINDOWS) && !defined(MS_WINCE) 1716 #if defined(MS_WINDOWS) && !defined(MS_WINCE)
1717 /* This code here replaces the first element in argv with the full 1717 /* This code here replaces the first element in argv with the full
1718 path that it represents. Under CE, there are no relative paths so 1718 path that it represents. Under CE, there are no relative paths so
1719 the argument must be the full path anyway. */ 1719 the argument must be the full path anyway. */
1720 wchar_t *ptemp; 1720 wchar_t *ptemp;
1721 if (GetFullPathNameW(argv0, 1721 if (GetFullPathNameW(argv0,
1722 sizeof(fullpath)/sizeof(fullpath[0]), 1722 sizeof(fullpath)/sizeof(fullpath[0]),
1723 fullpath, 1723 fullpath,
1724 &ptemp)) { 1724 &ptemp)) {
1725 argv0 = fullpath; 1725 argv0 = fullpath;
1726 } 1726 }
1727 #endif 1727 #endif
1728 p = wcsrchr(argv0, SEP); 1728 p = wcsrchr(argv0, SEP);
1729 /* Test for alternate separator */ 1729 /* Test for alternate separator */
1730 q = wcsrchr(p ? p : argv0, '/'); 1730 q = wcsrchr(p ? p : argv0, '/');
1731 if (q != NULL) 1731 if (q != NULL)
1732 p = q; 1732 p = q;
1733 if (p != NULL) { 1733 if (p != NULL) {
1734 n = p + 1 - argv0; 1734 n = p + 1 - argv0;
1735 if (n > 1 && p[-1] != ':') 1735 if (n > 1 && p[-1] != ':')
1736 n--; /* Drop trailing separator */ 1736 n--; /* Drop trailing separator */
1737 } 1737 }
1738 } 1738 }
1739 #else /* All other filename syntaxes */ 1739 #else /* All other filename syntaxes */
1740 if (argc > 0 && argv0 != NULL && wcscmp(argv0, L"-c") != 0) { 1740 if (argc > 0 && argv0 != NULL && wcscmp(argv0, L"-c") != 0) {
1741 #if defined(HAVE_REALPATH) 1741 #if defined(HAVE_REALPATH)
1742 if (_wrealpath(argv0, fullpath)) { 1742 if (_wrealpath(argv0, fullpath)) {
1743 argv0 = fullpath; 1743 argv0 = fullpath;
1744 } 1744 }
1745 #endif 1745 #endif
1746 p = wcsrchr(argv0, SEP); 1746 p = wcsrchr(argv0, SEP);
1747 } 1747 }
1748 if (p != NULL) { 1748 if (p != NULL) {
1749 n = p + 1 - argv0; 1749 n = p + 1 - argv0;
1750 #if SEP == '/' /* Special case for Unix filename syntax */ 1750 #if SEP == '/' /* Special case for Unix filename syntax */
1751 if (n > 1) 1751 if (n > 1)
1752 n--; /* Drop trailing separator */ 1752 n--; /* Drop trailing separator */
1753 #endif /* Unix */ 1753 #endif /* Unix */
1754 } 1754 }
1755 #endif /* All others */ 1755 #endif /* All others */
1756 a = PyUnicode_FromWideChar(argv0, n); 1756 a = PyUnicode_FromWideChar(argv0, n);
1757 if (a == NULL) 1757 if (a == NULL)
1758 Py_FatalError("no mem for sys.path insertion"); 1758 Py_FatalError("no mem for sys.path insertion");
1759 if (PyList_Insert(path, 0, a) < 0) 1759 if (PyList_Insert(path, 0, a) < 0)
1760 Py_FatalError("sys.path.insert(0) failed"); 1760 Py_FatalError("sys.path.insert(0) failed");
1761 Py_DECREF(a); 1761 Py_DECREF(a);
1762 } 1762 }
1763 Py_DECREF(av); 1763 Py_DECREF(av);
1764 } 1764 }
1765 1765
1766 /* Reimplementation of PyFile_WriteString() no calling indirectly 1766 /* Reimplementation of PyFile_WriteString() no calling indirectly
1767 PyErr_CheckSignals(): avoid the call to PyObject_Str(). */ 1767 PyErr_CheckSignals(): avoid the call to PyObject_Str(). */
1768 1768
1769 static int 1769 static int
1770 sys_pyfile_write(const char *text, PyObject *file) 1770 sys_pyfile_write(const char *text, PyObject *file)
1771 { 1771 {
1772 PyObject *unicode = NULL, *writer = NULL, *args = NULL, *result = NULL; 1772 PyObject *unicode = NULL, *writer = NULL, *args = NULL, *result = NULL;
1773 int err; 1773 int err;
1774 1774
1775 unicode = PyUnicode_FromString(text); 1775 unicode = PyUnicode_FromString(text);
1776 if (unicode == NULL) 1776 if (unicode == NULL)
1777 goto error; 1777 goto error;
1778 1778
1779 writer = PyObject_GetAttrString(file, "write"); 1779 writer = PyObject_GetAttrString(file, "write");
1780 if (writer == NULL) 1780 if (writer == NULL)
1781 goto error; 1781 goto error;
1782 1782
1783 args = PyTuple_Pack(1, unicode); 1783 args = PyTuple_Pack(1, unicode);
1784 if (args == NULL) 1784 if (args == NULL)
1785 goto error; 1785 goto error;
1786 1786
1787 result = PyEval_CallObject(writer, args); 1787 result = PyEval_CallObject(writer, args);
1788 if (result == NULL) { 1788 if (result == NULL) {
1789 goto error; 1789 goto error;
1790 } else { 1790 } else {
1791 err = 0; 1791 err = 0;
1792 goto finally; 1792 goto finally;
1793 } 1793 }
1794 1794
1795 error: 1795 error:
1796 err = -1; 1796 err = -1;
1797 finally: 1797 finally:
1798 Py_XDECREF(unicode); 1798 Py_XDECREF(unicode);
1799 Py_XDECREF(writer); 1799 Py_XDECREF(writer);
1800 Py_XDECREF(args); 1800 Py_XDECREF(args);
1801 Py_XDECREF(result); 1801 Py_XDECREF(result);
1802 return err; 1802 return err;
1803 } 1803 }
1804 1804
1805 1805
1806 /* APIs to write to sys.stdout or sys.stderr using a printf-like interface. 1806 /* APIs to write to sys.stdout or sys.stderr using a printf-like interface.
1807 Adapted from code submitted by Just van Rossum. 1807 Adapted from code submitted by Just van Rossum.
1832 */ 1832 */
1833 1833
1834 static void 1834 static void
1835 mywrite(char *name, FILE *fp, const char *format, va_list va) 1835 mywrite(char *name, FILE *fp, const char *format, va_list va)
1836 { 1836 {
1837 PyObject *file; 1837 PyObject *file;
1838 PyObject *error_type, *error_value, *error_traceback; 1838 PyObject *error_type, *error_value, *error_traceback;
1839 char buffer[1001]; 1839 char buffer[1001];
1840 int written; 1840 int written;
1841 1841
1842 PyErr_Fetch(&error_type, &error_value, &error_traceback); 1842 PyErr_Fetch(&error_type, &error_value, &error_traceback);
1843 file = PySys_GetObject(name); 1843 file = PySys_GetObject(name);
1844 written = PyOS_vsnprintf(buffer, sizeof(buffer), format, va); 1844 written = PyOS_vsnprintf(buffer, sizeof(buffer), format, va);
1845 if (sys_pyfile_write(buffer, file) != 0) { 1845 if (sys_pyfile_write(buffer, file) != 0) {
1846 PyErr_Clear(); 1846 PyErr_Clear();
1847 fputs(buffer, fp); 1847 fputs(buffer, fp);
1848 } 1848 }
1849 if (written < 0 || (size_t)written >= sizeof(buffer)) { 1849 if (written < 0 || (size_t)written >= sizeof(buffer)) {
1850 const char *truncated = "... truncated"; 1850 const char *truncated = "... truncated";
1851 if (sys_pyfile_write(truncated, file) != 0) { 1851 if (sys_pyfile_write(truncated, file) != 0) {
1852 PyErr_Clear(); 1852 PyErr_Clear();
1853 fputs(truncated, fp); 1853 fputs(truncated, fp);
1854 } 1854 }
1855 } 1855 }
1856 PyErr_Restore(error_type, error_value, error_traceback); 1856 PyErr_Restore(error_type, error_value, error_traceback);
1857 } 1857 }
1858 1858
1859 void 1859 void
1860 PySys_WriteStdout(const char *format, ...) 1860 PySys_WriteStdout(const char *format, ...)
1861 { 1861 {
1862 va_list va; 1862 va_list va;
1863 1863
1864 va_start(va, format); 1864 va_start(va, format);
1865 mywrite("stdout", stdout, format, va); 1865 mywrite("stdout", stdout, format, va);
1866 va_end(va); 1866 va_end(va);
1867 } 1867 }
1868 1868
1869 void 1869 void
1870 PySys_WriteStderr(const char *format, ...) 1870 PySys_WriteStderr(const char *format, ...)
1871 { 1871 {
1872 va_list va; 1872 va_list va;
1873 1873
1874 va_start(va, format); 1874 va_start(va, format);
1875 mywrite("stderr", stderr, format, va); 1875 mywrite("stderr", stderr, format, va);
1876 va_end(va); 1876 va_end(va);
1877 } 1877 }