| 1 | n/a | |
|---|
| 2 | n/a | /* System module */ |
|---|
| 3 | n/a | |
|---|
| 4 | n/a | /* |
|---|
| 5 | n/a | Various bits of information used by the interpreter are collected in |
|---|
| 6 | n/a | module 'sys'. |
|---|
| 7 | n/a | Function member: |
|---|
| 8 | n/a | - exit(sts): raise SystemExit |
|---|
| 9 | n/a | Data members: |
|---|
| 10 | n/a | - stdin, stdout, stderr: standard file objects |
|---|
| 11 | n/a | - modules: the table of modules (dictionary) |
|---|
| 12 | n/a | - path: module search path (list of strings) |
|---|
| 13 | n/a | - argv: script arguments (list of strings) |
|---|
| 14 | n/a | - ps1, ps2: optional primary and secondary prompts (strings) |
|---|
| 15 | n/a | */ |
|---|
| 16 | n/a | |
|---|
| 17 | n/a | #include "Python.h" |
|---|
| 18 | n/a | #include "code.h" |
|---|
| 19 | n/a | #include "frameobject.h" |
|---|
| 20 | n/a | #include "pythread.h" |
|---|
| 21 | n/a | |
|---|
| 22 | n/a | #include "osdefs.h" |
|---|
| 23 | n/a | #include <locale.h> |
|---|
| 24 | n/a | |
|---|
| 25 | n/a | #ifdef MS_WINDOWS |
|---|
| 26 | n/a | #define WIN32_LEAN_AND_MEAN |
|---|
| 27 | n/a | #include <windows.h> |
|---|
| 28 | n/a | #endif /* MS_WINDOWS */ |
|---|
| 29 | n/a | |
|---|
| 30 | n/a | #ifdef MS_COREDLL |
|---|
| 31 | n/a | extern void *PyWin_DLLhModule; |
|---|
| 32 | n/a | /* A string loaded from the DLL at startup: */ |
|---|
| 33 | n/a | extern const char *PyWin_DLLVersionString; |
|---|
| 34 | n/a | #endif |
|---|
| 35 | n/a | |
|---|
| 36 | n/a | #ifdef HAVE_LANGINFO_H |
|---|
| 37 | n/a | #include <langinfo.h> |
|---|
| 38 | n/a | #endif |
|---|
| 39 | n/a | |
|---|
| 40 | n/a | _Py_IDENTIFIER(_); |
|---|
| 41 | n/a | _Py_IDENTIFIER(__sizeof__); |
|---|
| 42 | n/a | _Py_IDENTIFIER(buffer); |
|---|
| 43 | n/a | _Py_IDENTIFIER(builtins); |
|---|
| 44 | n/a | _Py_IDENTIFIER(encoding); |
|---|
| 45 | n/a | _Py_IDENTIFIER(path); |
|---|
| 46 | n/a | _Py_IDENTIFIER(stdout); |
|---|
| 47 | n/a | _Py_IDENTIFIER(stderr); |
|---|
| 48 | n/a | _Py_IDENTIFIER(write); |
|---|
| 49 | n/a | |
|---|
| 50 | n/a | PyObject * |
|---|
| 51 | n/a | _PySys_GetObjectId(_Py_Identifier *key) |
|---|
| 52 | n/a | { |
|---|
| 53 | n/a | PyThreadState *tstate = PyThreadState_GET(); |
|---|
| 54 | n/a | PyObject *sd = tstate->interp->sysdict; |
|---|
| 55 | n/a | if (sd == NULL) |
|---|
| 56 | n/a | return NULL; |
|---|
| 57 | n/a | return _PyDict_GetItemId(sd, key); |
|---|
| 58 | n/a | } |
|---|
| 59 | n/a | |
|---|
| 60 | n/a | PyObject * |
|---|
| 61 | n/a | PySys_GetObject(const char *name) |
|---|
| 62 | n/a | { |
|---|
| 63 | n/a | PyThreadState *tstate = PyThreadState_GET(); |
|---|
| 64 | n/a | PyObject *sd = tstate->interp->sysdict; |
|---|
| 65 | n/a | if (sd == NULL) |
|---|
| 66 | n/a | return NULL; |
|---|
| 67 | n/a | return PyDict_GetItemString(sd, name); |
|---|
| 68 | n/a | } |
|---|
| 69 | n/a | |
|---|
| 70 | n/a | int |
|---|
| 71 | n/a | _PySys_SetObjectId(_Py_Identifier *key, PyObject *v) |
|---|
| 72 | n/a | { |
|---|
| 73 | n/a | PyThreadState *tstate = PyThreadState_GET(); |
|---|
| 74 | n/a | PyObject *sd = tstate->interp->sysdict; |
|---|
| 75 | n/a | if (v == NULL) { |
|---|
| 76 | n/a | if (_PyDict_GetItemId(sd, key) == NULL) |
|---|
| 77 | n/a | return 0; |
|---|
| 78 | n/a | else |
|---|
| 79 | n/a | return _PyDict_DelItemId(sd, key); |
|---|
| 80 | n/a | } |
|---|
| 81 | n/a | else |
|---|
| 82 | n/a | return _PyDict_SetItemId(sd, key, v); |
|---|
| 83 | n/a | } |
|---|
| 84 | n/a | |
|---|
| 85 | n/a | int |
|---|
| 86 | n/a | PySys_SetObject(const char *name, PyObject *v) |
|---|
| 87 | n/a | { |
|---|
| 88 | n/a | PyThreadState *tstate = PyThreadState_GET(); |
|---|
| 89 | n/a | PyObject *sd = tstate->interp->sysdict; |
|---|
| 90 | n/a | if (v == NULL) { |
|---|
| 91 | n/a | if (PyDict_GetItemString(sd, name) == NULL) |
|---|
| 92 | n/a | return 0; |
|---|
| 93 | n/a | else |
|---|
| 94 | n/a | return PyDict_DelItemString(sd, name); |
|---|
| 95 | n/a | } |
|---|
| 96 | n/a | else |
|---|
| 97 | n/a | return PyDict_SetItemString(sd, name, v); |
|---|
| 98 | n/a | } |
|---|
| 99 | n/a | |
|---|
| 100 | n/a | /* Write repr(o) to sys.stdout using sys.stdout.encoding and 'backslashreplace' |
|---|
| 101 | n/a | error handler. If sys.stdout has a buffer attribute, use |
|---|
| 102 | n/a | sys.stdout.buffer.write(encoded), otherwise redecode the string and use |
|---|
| 103 | n/a | sys.stdout.write(redecoded). |
|---|
| 104 | n/a | |
|---|
| 105 | n/a | Helper function for sys_displayhook(). */ |
|---|
| 106 | n/a | static int |
|---|
| 107 | n/a | sys_displayhook_unencodable(PyObject *outf, PyObject *o) |
|---|
| 108 | n/a | { |
|---|
| 109 | n/a | PyObject *stdout_encoding = NULL; |
|---|
| 110 | n/a | PyObject *encoded, *escaped_str, *repr_str, *buffer, *result; |
|---|
| 111 | n/a | const char *stdout_encoding_str; |
|---|
| 112 | n/a | int ret; |
|---|
| 113 | n/a | |
|---|
| 114 | n/a | stdout_encoding = _PyObject_GetAttrId(outf, &PyId_encoding); |
|---|
| 115 | n/a | if (stdout_encoding == NULL) |
|---|
| 116 | n/a | goto error; |
|---|
| 117 | n/a | stdout_encoding_str = PyUnicode_AsUTF8(stdout_encoding); |
|---|
| 118 | n/a | if (stdout_encoding_str == NULL) |
|---|
| 119 | n/a | goto error; |
|---|
| 120 | n/a | |
|---|
| 121 | n/a | repr_str = PyObject_Repr(o); |
|---|
| 122 | n/a | if (repr_str == NULL) |
|---|
| 123 | n/a | goto error; |
|---|
| 124 | n/a | encoded = PyUnicode_AsEncodedString(repr_str, |
|---|
| 125 | n/a | stdout_encoding_str, |
|---|
| 126 | n/a | "backslashreplace"); |
|---|
| 127 | n/a | Py_DECREF(repr_str); |
|---|
| 128 | n/a | if (encoded == NULL) |
|---|
| 129 | n/a | goto error; |
|---|
| 130 | n/a | |
|---|
| 131 | n/a | buffer = _PyObject_GetAttrId(outf, &PyId_buffer); |
|---|
| 132 | n/a | if (buffer) { |
|---|
| 133 | n/a | result = _PyObject_CallMethodIdObjArgs(buffer, &PyId_write, encoded, NULL); |
|---|
| 134 | n/a | Py_DECREF(buffer); |
|---|
| 135 | n/a | Py_DECREF(encoded); |
|---|
| 136 | n/a | if (result == NULL) |
|---|
| 137 | n/a | goto error; |
|---|
| 138 | n/a | Py_DECREF(result); |
|---|
| 139 | n/a | } |
|---|
| 140 | n/a | else { |
|---|
| 141 | n/a | PyErr_Clear(); |
|---|
| 142 | n/a | escaped_str = PyUnicode_FromEncodedObject(encoded, |
|---|
| 143 | n/a | stdout_encoding_str, |
|---|
| 144 | n/a | "strict"); |
|---|
| 145 | n/a | Py_DECREF(encoded); |
|---|
| 146 | n/a | if (PyFile_WriteObject(escaped_str, outf, Py_PRINT_RAW) != 0) { |
|---|
| 147 | n/a | Py_DECREF(escaped_str); |
|---|
| 148 | n/a | goto error; |
|---|
| 149 | n/a | } |
|---|
| 150 | n/a | Py_DECREF(escaped_str); |
|---|
| 151 | n/a | } |
|---|
| 152 | n/a | ret = 0; |
|---|
| 153 | n/a | goto finally; |
|---|
| 154 | n/a | |
|---|
| 155 | n/a | error: |
|---|
| 156 | n/a | ret = -1; |
|---|
| 157 | n/a | finally: |
|---|
| 158 | n/a | Py_XDECREF(stdout_encoding); |
|---|
| 159 | n/a | return ret; |
|---|
| 160 | n/a | } |
|---|
| 161 | n/a | |
|---|
| 162 | n/a | static PyObject * |
|---|
| 163 | n/a | sys_displayhook(PyObject *self, PyObject *o) |
|---|
| 164 | n/a | { |
|---|
| 165 | n/a | PyObject *outf; |
|---|
| 166 | n/a | PyInterpreterState *interp = PyThreadState_GET()->interp; |
|---|
| 167 | n/a | PyObject *modules = interp->modules; |
|---|
| 168 | n/a | PyObject *builtins; |
|---|
| 169 | n/a | static PyObject *newline = NULL; |
|---|
| 170 | n/a | int err; |
|---|
| 171 | n/a | |
|---|
| 172 | n/a | builtins = _PyDict_GetItemId(modules, &PyId_builtins); |
|---|
| 173 | n/a | if (builtins == NULL) { |
|---|
| 174 | n/a | PyErr_SetString(PyExc_RuntimeError, "lost builtins module"); |
|---|
| 175 | n/a | return NULL; |
|---|
| 176 | n/a | } |
|---|
| 177 | n/a | |
|---|
| 178 | n/a | /* Print value except if None */ |
|---|
| 179 | n/a | /* After printing, also assign to '_' */ |
|---|
| 180 | n/a | /* Before, set '_' to None to avoid recursion */ |
|---|
| 181 | n/a | if (o == Py_None) { |
|---|
| 182 | n/a | Py_RETURN_NONE; |
|---|
| 183 | n/a | } |
|---|
| 184 | n/a | if (_PyObject_SetAttrId(builtins, &PyId__, Py_None) != 0) |
|---|
| 185 | n/a | return NULL; |
|---|
| 186 | n/a | outf = _PySys_GetObjectId(&PyId_stdout); |
|---|
| 187 | n/a | if (outf == NULL || outf == Py_None) { |
|---|
| 188 | n/a | PyErr_SetString(PyExc_RuntimeError, "lost sys.stdout"); |
|---|
| 189 | n/a | return NULL; |
|---|
| 190 | n/a | } |
|---|
| 191 | n/a | if (PyFile_WriteObject(o, outf, 0) != 0) { |
|---|
| 192 | n/a | if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError)) { |
|---|
| 193 | n/a | /* repr(o) is not encodable to sys.stdout.encoding with |
|---|
| 194 | n/a | * sys.stdout.errors error handler (which is probably 'strict') */ |
|---|
| 195 | n/a | PyErr_Clear(); |
|---|
| 196 | n/a | err = sys_displayhook_unencodable(outf, o); |
|---|
| 197 | n/a | if (err) |
|---|
| 198 | n/a | return NULL; |
|---|
| 199 | n/a | } |
|---|
| 200 | n/a | else { |
|---|
| 201 | n/a | return NULL; |
|---|
| 202 | n/a | } |
|---|
| 203 | n/a | } |
|---|
| 204 | n/a | if (newline == NULL) { |
|---|
| 205 | n/a | newline = PyUnicode_FromString("\n"); |
|---|
| 206 | n/a | if (newline == NULL) |
|---|
| 207 | n/a | return NULL; |
|---|
| 208 | n/a | } |
|---|
| 209 | n/a | if (PyFile_WriteObject(newline, outf, Py_PRINT_RAW) != 0) |
|---|
| 210 | n/a | return NULL; |
|---|
| 211 | n/a | if (_PyObject_SetAttrId(builtins, &PyId__, o) != 0) |
|---|
| 212 | n/a | return NULL; |
|---|
| 213 | n/a | Py_RETURN_NONE; |
|---|
| 214 | n/a | } |
|---|
| 215 | n/a | |
|---|
| 216 | n/a | PyDoc_STRVAR(displayhook_doc, |
|---|
| 217 | n/a | "displayhook(object) -> None\n" |
|---|
| 218 | n/a | "\n" |
|---|
| 219 | n/a | "Print an object to sys.stdout and also save it in builtins._\n" |
|---|
| 220 | n/a | ); |
|---|
| 221 | n/a | |
|---|
| 222 | n/a | static PyObject * |
|---|
| 223 | n/a | sys_excepthook(PyObject* self, PyObject* args) |
|---|
| 224 | n/a | { |
|---|
| 225 | n/a | PyObject *exc, *value, *tb; |
|---|
| 226 | n/a | if (!PyArg_UnpackTuple(args, "excepthook", 3, 3, &exc, &value, &tb)) |
|---|
| 227 | n/a | return NULL; |
|---|
| 228 | n/a | PyErr_Display(exc, value, tb); |
|---|
| 229 | n/a | Py_RETURN_NONE; |
|---|
| 230 | n/a | } |
|---|
| 231 | n/a | |
|---|
| 232 | n/a | PyDoc_STRVAR(excepthook_doc, |
|---|
| 233 | n/a | "excepthook(exctype, value, traceback) -> None\n" |
|---|
| 234 | n/a | "\n" |
|---|
| 235 | n/a | "Handle an exception by displaying it with a traceback on sys.stderr.\n" |
|---|
| 236 | n/a | ); |
|---|
| 237 | n/a | |
|---|
| 238 | n/a | static PyObject * |
|---|
| 239 | n/a | sys_exc_info(PyObject *self, PyObject *noargs) |
|---|
| 240 | n/a | { |
|---|
| 241 | n/a | PyThreadState *tstate; |
|---|
| 242 | n/a | tstate = PyThreadState_GET(); |
|---|
| 243 | n/a | return Py_BuildValue( |
|---|
| 244 | n/a | "(OOO)", |
|---|
| 245 | n/a | tstate->exc_type != NULL ? tstate->exc_type : Py_None, |
|---|
| 246 | n/a | tstate->exc_value != NULL ? tstate->exc_value : Py_None, |
|---|
| 247 | n/a | tstate->exc_traceback != NULL ? |
|---|
| 248 | n/a | tstate->exc_traceback : Py_None); |
|---|
| 249 | n/a | } |
|---|
| 250 | n/a | |
|---|
| 251 | n/a | PyDoc_STRVAR(exc_info_doc, |
|---|
| 252 | n/a | "exc_info() -> (type, value, traceback)\n\ |
|---|
| 253 | n/a | \n\ |
|---|
| 254 | n/a | Return information about the most recent exception caught by an except\n\ |
|---|
| 255 | n/a | clause in the current stack frame or in an older stack frame." |
|---|
| 256 | n/a | ); |
|---|
| 257 | n/a | |
|---|
| 258 | n/a | static PyObject * |
|---|
| 259 | n/a | sys_exit(PyObject *self, PyObject *args) |
|---|
| 260 | n/a | { |
|---|
| 261 | n/a | PyObject *exit_code = 0; |
|---|
| 262 | n/a | if (!PyArg_UnpackTuple(args, "exit", 0, 1, &exit_code)) |
|---|
| 263 | n/a | return NULL; |
|---|
| 264 | n/a | /* Raise SystemExit so callers may catch it or clean up. */ |
|---|
| 265 | n/a | PyErr_SetObject(PyExc_SystemExit, exit_code); |
|---|
| 266 | n/a | return NULL; |
|---|
| 267 | n/a | } |
|---|
| 268 | n/a | |
|---|
| 269 | n/a | PyDoc_STRVAR(exit_doc, |
|---|
| 270 | n/a | "exit([status])\n\ |
|---|
| 271 | n/a | \n\ |
|---|
| 272 | n/a | Exit the interpreter by raising SystemExit(status).\n\ |
|---|
| 273 | n/a | If the status is omitted or None, it defaults to zero (i.e., success).\n\ |
|---|
| 274 | n/a | If the status is an integer, it will be used as the system exit status.\n\ |
|---|
| 275 | n/a | If it is another kind of object, it will be printed and the system\n\ |
|---|
| 276 | n/a | exit status will be one (i.e., failure)." |
|---|
| 277 | n/a | ); |
|---|
| 278 | n/a | |
|---|
| 279 | n/a | |
|---|
| 280 | n/a | static PyObject * |
|---|
| 281 | n/a | sys_getdefaultencoding(PyObject *self) |
|---|
| 282 | n/a | { |
|---|
| 283 | n/a | return PyUnicode_FromString(PyUnicode_GetDefaultEncoding()); |
|---|
| 284 | n/a | } |
|---|
| 285 | n/a | |
|---|
| 286 | n/a | PyDoc_STRVAR(getdefaultencoding_doc, |
|---|
| 287 | n/a | "getdefaultencoding() -> string\n\ |
|---|
| 288 | n/a | \n\ |
|---|
| 289 | n/a | Return the current default string encoding used by the Unicode \n\ |
|---|
| 290 | n/a | implementation." |
|---|
| 291 | n/a | ); |
|---|
| 292 | n/a | |
|---|
| 293 | n/a | static PyObject * |
|---|
| 294 | n/a | sys_getfilesystemencoding(PyObject *self) |
|---|
| 295 | n/a | { |
|---|
| 296 | n/a | if (Py_FileSystemDefaultEncoding) |
|---|
| 297 | n/a | return PyUnicode_FromString(Py_FileSystemDefaultEncoding); |
|---|
| 298 | n/a | PyErr_SetString(PyExc_RuntimeError, |
|---|
| 299 | n/a | "filesystem encoding is not initialized"); |
|---|
| 300 | n/a | return NULL; |
|---|
| 301 | n/a | } |
|---|
| 302 | n/a | |
|---|
| 303 | n/a | PyDoc_STRVAR(getfilesystemencoding_doc, |
|---|
| 304 | n/a | "getfilesystemencoding() -> string\n\ |
|---|
| 305 | n/a | \n\ |
|---|
| 306 | n/a | Return the encoding used to convert Unicode filenames in\n\ |
|---|
| 307 | n/a | operating system filenames." |
|---|
| 308 | n/a | ); |
|---|
| 309 | n/a | |
|---|
| 310 | n/a | static PyObject * |
|---|
| 311 | n/a | sys_getfilesystemencodeerrors(PyObject *self) |
|---|
| 312 | n/a | { |
|---|
| 313 | n/a | if (Py_FileSystemDefaultEncodeErrors) |
|---|
| 314 | n/a | return PyUnicode_FromString(Py_FileSystemDefaultEncodeErrors); |
|---|
| 315 | n/a | PyErr_SetString(PyExc_RuntimeError, |
|---|
| 316 | n/a | "filesystem encoding is not initialized"); |
|---|
| 317 | n/a | return NULL; |
|---|
| 318 | n/a | } |
|---|
| 319 | n/a | |
|---|
| 320 | n/a | PyDoc_STRVAR(getfilesystemencodeerrors_doc, |
|---|
| 321 | n/a | "getfilesystemencodeerrors() -> string\n\ |
|---|
| 322 | n/a | \n\ |
|---|
| 323 | n/a | Return the error mode used to convert Unicode filenames in\n\ |
|---|
| 324 | n/a | operating system filenames." |
|---|
| 325 | n/a | ); |
|---|
| 326 | n/a | |
|---|
| 327 | n/a | static PyObject * |
|---|
| 328 | n/a | sys_intern(PyObject *self, PyObject *args) |
|---|
| 329 | n/a | { |
|---|
| 330 | n/a | PyObject *s; |
|---|
| 331 | n/a | if (!PyArg_ParseTuple(args, "U:intern", &s)) |
|---|
| 332 | n/a | return NULL; |
|---|
| 333 | n/a | if (PyUnicode_CheckExact(s)) { |
|---|
| 334 | n/a | Py_INCREF(s); |
|---|
| 335 | n/a | PyUnicode_InternInPlace(&s); |
|---|
| 336 | n/a | return s; |
|---|
| 337 | n/a | } |
|---|
| 338 | n/a | else { |
|---|
| 339 | n/a | PyErr_Format(PyExc_TypeError, |
|---|
| 340 | n/a | "can't intern %.400s", s->ob_type->tp_name); |
|---|
| 341 | n/a | return NULL; |
|---|
| 342 | n/a | } |
|---|
| 343 | n/a | } |
|---|
| 344 | n/a | |
|---|
| 345 | n/a | PyDoc_STRVAR(intern_doc, |
|---|
| 346 | n/a | "intern(string) -> string\n\ |
|---|
| 347 | n/a | \n\ |
|---|
| 348 | n/a | ``Intern'' the given string. This enters the string in the (global)\n\ |
|---|
| 349 | n/a | table of interned strings whose purpose is to speed up dictionary lookups.\n\ |
|---|
| 350 | n/a | Return the string itself or the previously interned string object with the\n\ |
|---|
| 351 | n/a | same value."); |
|---|
| 352 | n/a | |
|---|
| 353 | n/a | |
|---|
| 354 | n/a | /* |
|---|
| 355 | n/a | * Cached interned string objects used for calling the profile and |
|---|
| 356 | n/a | * trace functions. Initialized by trace_init(). |
|---|
| 357 | n/a | */ |
|---|
| 358 | n/a | static PyObject *whatstrings[7] = {NULL, NULL, NULL, NULL, NULL, NULL, NULL}; |
|---|
| 359 | n/a | |
|---|
| 360 | n/a | static int |
|---|
| 361 | n/a | trace_init(void) |
|---|
| 362 | n/a | { |
|---|
| 363 | n/a | static const char * const whatnames[7] = { |
|---|
| 364 | n/a | "call", "exception", "line", "return", |
|---|
| 365 | n/a | "c_call", "c_exception", "c_return" |
|---|
| 366 | n/a | }; |
|---|
| 367 | n/a | PyObject *name; |
|---|
| 368 | n/a | int i; |
|---|
| 369 | n/a | for (i = 0; i < 7; ++i) { |
|---|
| 370 | n/a | if (whatstrings[i] == NULL) { |
|---|
| 371 | n/a | name = PyUnicode_InternFromString(whatnames[i]); |
|---|
| 372 | n/a | if (name == NULL) |
|---|
| 373 | n/a | return -1; |
|---|
| 374 | n/a | whatstrings[i] = name; |
|---|
| 375 | n/a | } |
|---|
| 376 | n/a | } |
|---|
| 377 | n/a | return 0; |
|---|
| 378 | n/a | } |
|---|
| 379 | n/a | |
|---|
| 380 | n/a | |
|---|
| 381 | n/a | static PyObject * |
|---|
| 382 | n/a | call_trampoline(PyObject* callback, |
|---|
| 383 | n/a | PyFrameObject *frame, int what, PyObject *arg) |
|---|
| 384 | n/a | { |
|---|
| 385 | n/a | PyObject *result; |
|---|
| 386 | n/a | PyObject *stack[3]; |
|---|
| 387 | n/a | |
|---|
| 388 | n/a | if (PyFrame_FastToLocalsWithError(frame) < 0) { |
|---|
| 389 | n/a | return NULL; |
|---|
| 390 | n/a | } |
|---|
| 391 | n/a | |
|---|
| 392 | n/a | stack[0] = (PyObject *)frame; |
|---|
| 393 | n/a | stack[1] = whatstrings[what]; |
|---|
| 394 | n/a | stack[2] = (arg != NULL) ? arg : Py_None; |
|---|
| 395 | n/a | |
|---|
| 396 | n/a | /* call the Python-level function */ |
|---|
| 397 | n/a | result = _PyObject_FastCall(callback, stack, 3); |
|---|
| 398 | n/a | |
|---|
| 399 | n/a | PyFrame_LocalsToFast(frame, 1); |
|---|
| 400 | n/a | if (result == NULL) { |
|---|
| 401 | n/a | PyTraceBack_Here(frame); |
|---|
| 402 | n/a | } |
|---|
| 403 | n/a | |
|---|
| 404 | n/a | return result; |
|---|
| 405 | n/a | } |
|---|
| 406 | n/a | |
|---|
| 407 | n/a | static int |
|---|
| 408 | n/a | profile_trampoline(PyObject *self, PyFrameObject *frame, |
|---|
| 409 | n/a | int what, PyObject *arg) |
|---|
| 410 | n/a | { |
|---|
| 411 | n/a | PyObject *result; |
|---|
| 412 | n/a | |
|---|
| 413 | n/a | if (arg == NULL) |
|---|
| 414 | n/a | arg = Py_None; |
|---|
| 415 | n/a | result = call_trampoline(self, frame, what, arg); |
|---|
| 416 | n/a | if (result == NULL) { |
|---|
| 417 | n/a | PyEval_SetProfile(NULL, NULL); |
|---|
| 418 | n/a | return -1; |
|---|
| 419 | n/a | } |
|---|
| 420 | n/a | Py_DECREF(result); |
|---|
| 421 | n/a | return 0; |
|---|
| 422 | n/a | } |
|---|
| 423 | n/a | |
|---|
| 424 | n/a | static int |
|---|
| 425 | n/a | trace_trampoline(PyObject *self, PyFrameObject *frame, |
|---|
| 426 | n/a | int what, PyObject *arg) |
|---|
| 427 | n/a | { |
|---|
| 428 | n/a | PyObject *callback; |
|---|
| 429 | n/a | PyObject *result; |
|---|
| 430 | n/a | |
|---|
| 431 | n/a | if (what == PyTrace_CALL) |
|---|
| 432 | n/a | callback = self; |
|---|
| 433 | n/a | else |
|---|
| 434 | n/a | callback = frame->f_trace; |
|---|
| 435 | n/a | if (callback == NULL) |
|---|
| 436 | n/a | return 0; |
|---|
| 437 | n/a | result = call_trampoline(callback, frame, what, arg); |
|---|
| 438 | n/a | if (result == NULL) { |
|---|
| 439 | n/a | PyEval_SetTrace(NULL, NULL); |
|---|
| 440 | n/a | Py_CLEAR(frame->f_trace); |
|---|
| 441 | n/a | return -1; |
|---|
| 442 | n/a | } |
|---|
| 443 | n/a | if (result != Py_None) { |
|---|
| 444 | n/a | Py_XSETREF(frame->f_trace, result); |
|---|
| 445 | n/a | } |
|---|
| 446 | n/a | else { |
|---|
| 447 | n/a | Py_DECREF(result); |
|---|
| 448 | n/a | } |
|---|
| 449 | n/a | return 0; |
|---|
| 450 | n/a | } |
|---|
| 451 | n/a | |
|---|
| 452 | n/a | static PyObject * |
|---|
| 453 | n/a | sys_settrace(PyObject *self, PyObject *args) |
|---|
| 454 | n/a | { |
|---|
| 455 | n/a | if (trace_init() == -1) |
|---|
| 456 | n/a | return NULL; |
|---|
| 457 | n/a | if (args == Py_None) |
|---|
| 458 | n/a | PyEval_SetTrace(NULL, NULL); |
|---|
| 459 | n/a | else |
|---|
| 460 | n/a | PyEval_SetTrace(trace_trampoline, args); |
|---|
| 461 | n/a | Py_RETURN_NONE; |
|---|
| 462 | n/a | } |
|---|
| 463 | n/a | |
|---|
| 464 | n/a | PyDoc_STRVAR(settrace_doc, |
|---|
| 465 | n/a | "settrace(function)\n\ |
|---|
| 466 | n/a | \n\ |
|---|
| 467 | n/a | Set the global debug tracing function. It will be called on each\n\ |
|---|
| 468 | n/a | function call. See the debugger chapter in the library manual." |
|---|
| 469 | n/a | ); |
|---|
| 470 | n/a | |
|---|
| 471 | n/a | static PyObject * |
|---|
| 472 | n/a | sys_gettrace(PyObject *self, PyObject *args) |
|---|
| 473 | n/a | { |
|---|
| 474 | n/a | PyThreadState *tstate = PyThreadState_GET(); |
|---|
| 475 | n/a | PyObject *temp = tstate->c_traceobj; |
|---|
| 476 | n/a | |
|---|
| 477 | n/a | if (temp == NULL) |
|---|
| 478 | n/a | temp = Py_None; |
|---|
| 479 | n/a | Py_INCREF(temp); |
|---|
| 480 | n/a | return temp; |
|---|
| 481 | n/a | } |
|---|
| 482 | n/a | |
|---|
| 483 | n/a | PyDoc_STRVAR(gettrace_doc, |
|---|
| 484 | n/a | "gettrace()\n\ |
|---|
| 485 | n/a | \n\ |
|---|
| 486 | n/a | Return the global debug tracing function set with sys.settrace.\n\ |
|---|
| 487 | n/a | See the debugger chapter in the library manual." |
|---|
| 488 | n/a | ); |
|---|
| 489 | n/a | |
|---|
| 490 | n/a | static PyObject * |
|---|
| 491 | n/a | sys_setprofile(PyObject *self, PyObject *args) |
|---|
| 492 | n/a | { |
|---|
| 493 | n/a | if (trace_init() == -1) |
|---|
| 494 | n/a | return NULL; |
|---|
| 495 | n/a | if (args == Py_None) |
|---|
| 496 | n/a | PyEval_SetProfile(NULL, NULL); |
|---|
| 497 | n/a | else |
|---|
| 498 | n/a | PyEval_SetProfile(profile_trampoline, args); |
|---|
| 499 | n/a | Py_RETURN_NONE; |
|---|
| 500 | n/a | } |
|---|
| 501 | n/a | |
|---|
| 502 | n/a | PyDoc_STRVAR(setprofile_doc, |
|---|
| 503 | n/a | "setprofile(function)\n\ |
|---|
| 504 | n/a | \n\ |
|---|
| 505 | n/a | Set the profiling function. It will be called on each function call\n\ |
|---|
| 506 | n/a | and return. See the profiler chapter in the library manual." |
|---|
| 507 | n/a | ); |
|---|
| 508 | n/a | |
|---|
| 509 | n/a | static PyObject * |
|---|
| 510 | n/a | sys_getprofile(PyObject *self, PyObject *args) |
|---|
| 511 | n/a | { |
|---|
| 512 | n/a | PyThreadState *tstate = PyThreadState_GET(); |
|---|
| 513 | n/a | PyObject *temp = tstate->c_profileobj; |
|---|
| 514 | n/a | |
|---|
| 515 | n/a | if (temp == NULL) |
|---|
| 516 | n/a | temp = Py_None; |
|---|
| 517 | n/a | Py_INCREF(temp); |
|---|
| 518 | n/a | return temp; |
|---|
| 519 | n/a | } |
|---|
| 520 | n/a | |
|---|
| 521 | n/a | PyDoc_STRVAR(getprofile_doc, |
|---|
| 522 | n/a | "getprofile()\n\ |
|---|
| 523 | n/a | \n\ |
|---|
| 524 | n/a | Return the profiling function set with sys.setprofile.\n\ |
|---|
| 525 | n/a | See the profiler chapter in the library manual." |
|---|
| 526 | n/a | ); |
|---|
| 527 | n/a | |
|---|
| 528 | n/a | static int _check_interval = 100; |
|---|
| 529 | n/a | |
|---|
| 530 | n/a | static PyObject * |
|---|
| 531 | n/a | sys_setcheckinterval(PyObject *self, PyObject *args) |
|---|
| 532 | n/a | { |
|---|
| 533 | n/a | if (PyErr_WarnEx(PyExc_DeprecationWarning, |
|---|
| 534 | n/a | "sys.getcheckinterval() and sys.setcheckinterval() " |
|---|
| 535 | n/a | "are deprecated. Use sys.setswitchinterval() " |
|---|
| 536 | n/a | "instead.", 1) < 0) |
|---|
| 537 | n/a | return NULL; |
|---|
| 538 | n/a | if (!PyArg_ParseTuple(args, "i:setcheckinterval", &_check_interval)) |
|---|
| 539 | n/a | return NULL; |
|---|
| 540 | n/a | Py_RETURN_NONE; |
|---|
| 541 | n/a | } |
|---|
| 542 | n/a | |
|---|
| 543 | n/a | PyDoc_STRVAR(setcheckinterval_doc, |
|---|
| 544 | n/a | "setcheckinterval(n)\n\ |
|---|
| 545 | n/a | \n\ |
|---|
| 546 | n/a | Tell the Python interpreter to check for asynchronous events every\n\ |
|---|
| 547 | n/a | n instructions. This also affects how often thread switches occur." |
|---|
| 548 | n/a | ); |
|---|
| 549 | n/a | |
|---|
| 550 | n/a | static PyObject * |
|---|
| 551 | n/a | sys_getcheckinterval(PyObject *self, PyObject *args) |
|---|
| 552 | n/a | { |
|---|
| 553 | n/a | if (PyErr_WarnEx(PyExc_DeprecationWarning, |
|---|
| 554 | n/a | "sys.getcheckinterval() and sys.setcheckinterval() " |
|---|
| 555 | n/a | "are deprecated. Use sys.getswitchinterval() " |
|---|
| 556 | n/a | "instead.", 1) < 0) |
|---|
| 557 | n/a | return NULL; |
|---|
| 558 | n/a | return PyLong_FromLong(_check_interval); |
|---|
| 559 | n/a | } |
|---|
| 560 | n/a | |
|---|
| 561 | n/a | PyDoc_STRVAR(getcheckinterval_doc, |
|---|
| 562 | n/a | "getcheckinterval() -> current check interval; see setcheckinterval()." |
|---|
| 563 | n/a | ); |
|---|
| 564 | n/a | |
|---|
| 565 | n/a | #ifdef WITH_THREAD |
|---|
| 566 | n/a | static PyObject * |
|---|
| 567 | n/a | sys_setswitchinterval(PyObject *self, PyObject *args) |
|---|
| 568 | n/a | { |
|---|
| 569 | n/a | double d; |
|---|
| 570 | n/a | if (!PyArg_ParseTuple(args, "d:setswitchinterval", &d)) |
|---|
| 571 | n/a | return NULL; |
|---|
| 572 | n/a | if (d <= 0.0) { |
|---|
| 573 | n/a | PyErr_SetString(PyExc_ValueError, |
|---|
| 574 | n/a | "switch interval must be strictly positive"); |
|---|
| 575 | n/a | return NULL; |
|---|
| 576 | n/a | } |
|---|
| 577 | n/a | _PyEval_SetSwitchInterval((unsigned long) (1e6 * d)); |
|---|
| 578 | n/a | Py_RETURN_NONE; |
|---|
| 579 | n/a | } |
|---|
| 580 | n/a | |
|---|
| 581 | n/a | PyDoc_STRVAR(setswitchinterval_doc, |
|---|
| 582 | n/a | "setswitchinterval(n)\n\ |
|---|
| 583 | n/a | \n\ |
|---|
| 584 | n/a | Set the ideal thread switching delay inside the Python interpreter\n\ |
|---|
| 585 | n/a | The actual frequency of switching threads can be lower if the\n\ |
|---|
| 586 | n/a | interpreter executes long sequences of uninterruptible code\n\ |
|---|
| 587 | n/a | (this is implementation-specific and workload-dependent).\n\ |
|---|
| 588 | n/a | \n\ |
|---|
| 589 | n/a | The parameter must represent the desired switching delay in seconds\n\ |
|---|
| 590 | n/a | A typical value is 0.005 (5 milliseconds)." |
|---|
| 591 | n/a | ); |
|---|
| 592 | n/a | |
|---|
| 593 | n/a | static PyObject * |
|---|
| 594 | n/a | sys_getswitchinterval(PyObject *self, PyObject *args) |
|---|
| 595 | n/a | { |
|---|
| 596 | n/a | return PyFloat_FromDouble(1e-6 * _PyEval_GetSwitchInterval()); |
|---|
| 597 | n/a | } |
|---|
| 598 | n/a | |
|---|
| 599 | n/a | PyDoc_STRVAR(getswitchinterval_doc, |
|---|
| 600 | n/a | "getswitchinterval() -> current thread switch interval; see setswitchinterval()." |
|---|
| 601 | n/a | ); |
|---|
| 602 | n/a | |
|---|
| 603 | n/a | #endif /* WITH_THREAD */ |
|---|
| 604 | n/a | |
|---|
| 605 | n/a | static PyObject * |
|---|
| 606 | n/a | sys_setrecursionlimit(PyObject *self, PyObject *args) |
|---|
| 607 | n/a | { |
|---|
| 608 | n/a | int new_limit, mark; |
|---|
| 609 | n/a | PyThreadState *tstate; |
|---|
| 610 | n/a | |
|---|
| 611 | n/a | if (!PyArg_ParseTuple(args, "i:setrecursionlimit", &new_limit)) |
|---|
| 612 | n/a | return NULL; |
|---|
| 613 | n/a | |
|---|
| 614 | n/a | if (new_limit < 1) { |
|---|
| 615 | n/a | PyErr_SetString(PyExc_ValueError, |
|---|
| 616 | n/a | "recursion limit must be greater or equal than 1"); |
|---|
| 617 | n/a | return NULL; |
|---|
| 618 | n/a | } |
|---|
| 619 | n/a | |
|---|
| 620 | n/a | /* Issue #25274: When the recursion depth hits the recursion limit in |
|---|
| 621 | n/a | _Py_CheckRecursiveCall(), the overflowed flag of the thread state is |
|---|
| 622 | n/a | set to 1 and a RecursionError is raised. The overflowed flag is reset |
|---|
| 623 | n/a | to 0 when the recursion depth goes below the low-water mark: see |
|---|
| 624 | n/a | Py_LeaveRecursiveCall(). |
|---|
| 625 | n/a | |
|---|
| 626 | n/a | Reject too low new limit if the current recursion depth is higher than |
|---|
| 627 | n/a | the new low-water mark. Otherwise it may not be possible anymore to |
|---|
| 628 | n/a | reset the overflowed flag to 0. */ |
|---|
| 629 | n/a | mark = _Py_RecursionLimitLowerWaterMark(new_limit); |
|---|
| 630 | n/a | tstate = PyThreadState_GET(); |
|---|
| 631 | n/a | if (tstate->recursion_depth >= mark) { |
|---|
| 632 | n/a | PyErr_Format(PyExc_RecursionError, |
|---|
| 633 | n/a | "cannot set the recursion limit to %i at " |
|---|
| 634 | n/a | "the recursion depth %i: the limit is too low", |
|---|
| 635 | n/a | new_limit, tstate->recursion_depth); |
|---|
| 636 | n/a | return NULL; |
|---|
| 637 | n/a | } |
|---|
| 638 | n/a | |
|---|
| 639 | n/a | Py_SetRecursionLimit(new_limit); |
|---|
| 640 | n/a | Py_RETURN_NONE; |
|---|
| 641 | n/a | } |
|---|
| 642 | n/a | |
|---|
| 643 | n/a | static PyObject * |
|---|
| 644 | n/a | sys_set_coroutine_wrapper(PyObject *self, PyObject *wrapper) |
|---|
| 645 | n/a | { |
|---|
| 646 | n/a | if (wrapper != Py_None) { |
|---|
| 647 | n/a | if (!PyCallable_Check(wrapper)) { |
|---|
| 648 | n/a | PyErr_Format(PyExc_TypeError, |
|---|
| 649 | n/a | "callable expected, got %.50s", |
|---|
| 650 | n/a | Py_TYPE(wrapper)->tp_name); |
|---|
| 651 | n/a | return NULL; |
|---|
| 652 | n/a | } |
|---|
| 653 | n/a | _PyEval_SetCoroutineWrapper(wrapper); |
|---|
| 654 | n/a | } |
|---|
| 655 | n/a | else { |
|---|
| 656 | n/a | _PyEval_SetCoroutineWrapper(NULL); |
|---|
| 657 | n/a | } |
|---|
| 658 | n/a | Py_RETURN_NONE; |
|---|
| 659 | n/a | } |
|---|
| 660 | n/a | |
|---|
| 661 | n/a | PyDoc_STRVAR(set_coroutine_wrapper_doc, |
|---|
| 662 | n/a | "set_coroutine_wrapper(wrapper)\n\ |
|---|
| 663 | n/a | \n\ |
|---|
| 664 | n/a | Set a wrapper for coroutine objects." |
|---|
| 665 | n/a | ); |
|---|
| 666 | n/a | |
|---|
| 667 | n/a | static PyObject * |
|---|
| 668 | n/a | sys_get_coroutine_wrapper(PyObject *self, PyObject *args) |
|---|
| 669 | n/a | { |
|---|
| 670 | n/a | PyObject *wrapper = _PyEval_GetCoroutineWrapper(); |
|---|
| 671 | n/a | if (wrapper == NULL) { |
|---|
| 672 | n/a | wrapper = Py_None; |
|---|
| 673 | n/a | } |
|---|
| 674 | n/a | Py_INCREF(wrapper); |
|---|
| 675 | n/a | return wrapper; |
|---|
| 676 | n/a | } |
|---|
| 677 | n/a | |
|---|
| 678 | n/a | PyDoc_STRVAR(get_coroutine_wrapper_doc, |
|---|
| 679 | n/a | "get_coroutine_wrapper()\n\ |
|---|
| 680 | n/a | \n\ |
|---|
| 681 | n/a | Return the wrapper for coroutine objects set by sys.set_coroutine_wrapper." |
|---|
| 682 | n/a | ); |
|---|
| 683 | n/a | |
|---|
| 684 | n/a | |
|---|
| 685 | n/a | static PyTypeObject AsyncGenHooksType; |
|---|
| 686 | n/a | |
|---|
| 687 | n/a | PyDoc_STRVAR(asyncgen_hooks_doc, |
|---|
| 688 | n/a | "asyncgen_hooks\n\ |
|---|
| 689 | n/a | \n\ |
|---|
| 690 | n/a | A struct sequence providing information about asynhronous\n\ |
|---|
| 691 | n/a | generators hooks. The attributes are read only."); |
|---|
| 692 | n/a | |
|---|
| 693 | n/a | static PyStructSequence_Field asyncgen_hooks_fields[] = { |
|---|
| 694 | n/a | {"firstiter", "Hook to intercept first iteration"}, |
|---|
| 695 | n/a | {"finalizer", "Hook to intercept finalization"}, |
|---|
| 696 | n/a | {0} |
|---|
| 697 | n/a | }; |
|---|
| 698 | n/a | |
|---|
| 699 | n/a | static PyStructSequence_Desc asyncgen_hooks_desc = { |
|---|
| 700 | n/a | "asyncgen_hooks", /* name */ |
|---|
| 701 | n/a | asyncgen_hooks_doc, /* doc */ |
|---|
| 702 | n/a | asyncgen_hooks_fields , /* fields */ |
|---|
| 703 | n/a | 2 |
|---|
| 704 | n/a | }; |
|---|
| 705 | n/a | |
|---|
| 706 | n/a | |
|---|
| 707 | n/a | static PyObject * |
|---|
| 708 | n/a | sys_set_asyncgen_hooks(PyObject *self, PyObject *args, PyObject *kw) |
|---|
| 709 | n/a | { |
|---|
| 710 | n/a | static char *keywords[] = {"firstiter", "finalizer", NULL}; |
|---|
| 711 | n/a | PyObject *firstiter = NULL; |
|---|
| 712 | n/a | PyObject *finalizer = NULL; |
|---|
| 713 | n/a | |
|---|
| 714 | n/a | if (!PyArg_ParseTupleAndKeywords( |
|---|
| 715 | n/a | args, kw, "|OO", keywords, |
|---|
| 716 | n/a | &firstiter, &finalizer)) { |
|---|
| 717 | n/a | return NULL; |
|---|
| 718 | n/a | } |
|---|
| 719 | n/a | |
|---|
| 720 | n/a | if (finalizer && finalizer != Py_None) { |
|---|
| 721 | n/a | if (!PyCallable_Check(finalizer)) { |
|---|
| 722 | n/a | PyErr_Format(PyExc_TypeError, |
|---|
| 723 | n/a | "callable finalizer expected, got %.50s", |
|---|
| 724 | n/a | Py_TYPE(finalizer)->tp_name); |
|---|
| 725 | n/a | return NULL; |
|---|
| 726 | n/a | } |
|---|
| 727 | n/a | _PyEval_SetAsyncGenFinalizer(finalizer); |
|---|
| 728 | n/a | } |
|---|
| 729 | n/a | else if (finalizer == Py_None) { |
|---|
| 730 | n/a | _PyEval_SetAsyncGenFinalizer(NULL); |
|---|
| 731 | n/a | } |
|---|
| 732 | n/a | |
|---|
| 733 | n/a | if (firstiter && firstiter != Py_None) { |
|---|
| 734 | n/a | if (!PyCallable_Check(firstiter)) { |
|---|
| 735 | n/a | PyErr_Format(PyExc_TypeError, |
|---|
| 736 | n/a | "callable firstiter expected, got %.50s", |
|---|
| 737 | n/a | Py_TYPE(firstiter)->tp_name); |
|---|
| 738 | n/a | return NULL; |
|---|
| 739 | n/a | } |
|---|
| 740 | n/a | _PyEval_SetAsyncGenFirstiter(firstiter); |
|---|
| 741 | n/a | } |
|---|
| 742 | n/a | else if (firstiter == Py_None) { |
|---|
| 743 | n/a | _PyEval_SetAsyncGenFirstiter(NULL); |
|---|
| 744 | n/a | } |
|---|
| 745 | n/a | |
|---|
| 746 | n/a | Py_RETURN_NONE; |
|---|
| 747 | n/a | } |
|---|
| 748 | n/a | |
|---|
| 749 | n/a | PyDoc_STRVAR(set_asyncgen_hooks_doc, |
|---|
| 750 | n/a | "set_asyncgen_hooks(*, firstiter=None, finalizer=None)\n\ |
|---|
| 751 | n/a | \n\ |
|---|
| 752 | n/a | Set a finalizer for async generators objects." |
|---|
| 753 | n/a | ); |
|---|
| 754 | n/a | |
|---|
| 755 | n/a | static PyObject * |
|---|
| 756 | n/a | sys_get_asyncgen_hooks(PyObject *self, PyObject *args) |
|---|
| 757 | n/a | { |
|---|
| 758 | n/a | PyObject *res; |
|---|
| 759 | n/a | PyObject *firstiter = _PyEval_GetAsyncGenFirstiter(); |
|---|
| 760 | n/a | PyObject *finalizer = _PyEval_GetAsyncGenFinalizer(); |
|---|
| 761 | n/a | |
|---|
| 762 | n/a | res = PyStructSequence_New(&AsyncGenHooksType); |
|---|
| 763 | n/a | if (res == NULL) { |
|---|
| 764 | n/a | return NULL; |
|---|
| 765 | n/a | } |
|---|
| 766 | n/a | |
|---|
| 767 | n/a | if (firstiter == NULL) { |
|---|
| 768 | n/a | firstiter = Py_None; |
|---|
| 769 | n/a | } |
|---|
| 770 | n/a | |
|---|
| 771 | n/a | if (finalizer == NULL) { |
|---|
| 772 | n/a | finalizer = Py_None; |
|---|
| 773 | n/a | } |
|---|
| 774 | n/a | |
|---|
| 775 | n/a | Py_INCREF(firstiter); |
|---|
| 776 | n/a | PyStructSequence_SET_ITEM(res, 0, firstiter); |
|---|
| 777 | n/a | |
|---|
| 778 | n/a | Py_INCREF(finalizer); |
|---|
| 779 | n/a | PyStructSequence_SET_ITEM(res, 1, finalizer); |
|---|
| 780 | n/a | |
|---|
| 781 | n/a | return res; |
|---|
| 782 | n/a | } |
|---|
| 783 | n/a | |
|---|
| 784 | n/a | PyDoc_STRVAR(get_asyncgen_hooks_doc, |
|---|
| 785 | n/a | "get_asyncgen_hooks()\n\ |
|---|
| 786 | n/a | \n\ |
|---|
| 787 | n/a | Return a namedtuple of installed asynchronous generators hooks \ |
|---|
| 788 | n/a | (firstiter, finalizer)." |
|---|
| 789 | n/a | ); |
|---|
| 790 | n/a | |
|---|
| 791 | n/a | |
|---|
| 792 | n/a | static PyTypeObject Hash_InfoType; |
|---|
| 793 | n/a | |
|---|
| 794 | n/a | PyDoc_STRVAR(hash_info_doc, |
|---|
| 795 | n/a | "hash_info\n\ |
|---|
| 796 | n/a | \n\ |
|---|
| 797 | n/a | A struct sequence providing parameters used for computing\n\ |
|---|
| 798 | n/a | hashes. The attributes are read only."); |
|---|
| 799 | n/a | |
|---|
| 800 | n/a | static PyStructSequence_Field hash_info_fields[] = { |
|---|
| 801 | n/a | {"width", "width of the type used for hashing, in bits"}, |
|---|
| 802 | n/a | {"modulus", "prime number giving the modulus on which the hash " |
|---|
| 803 | n/a | "function is based"}, |
|---|
| 804 | n/a | {"inf", "value to be used for hash of a positive infinity"}, |
|---|
| 805 | n/a | {"nan", "value to be used for hash of a nan"}, |
|---|
| 806 | n/a | {"imag", "multiplier used for the imaginary part of a complex number"}, |
|---|
| 807 | n/a | {"algorithm", "name of the algorithm for hashing of str, bytes and " |
|---|
| 808 | n/a | "memoryviews"}, |
|---|
| 809 | n/a | {"hash_bits", "internal output size of hash algorithm"}, |
|---|
| 810 | n/a | {"seed_bits", "seed size of hash algorithm"}, |
|---|
| 811 | n/a | {"cutoff", "small string optimization cutoff"}, |
|---|
| 812 | n/a | {NULL, NULL} |
|---|
| 813 | n/a | }; |
|---|
| 814 | n/a | |
|---|
| 815 | n/a | static PyStructSequence_Desc hash_info_desc = { |
|---|
| 816 | n/a | "sys.hash_info", |
|---|
| 817 | n/a | hash_info_doc, |
|---|
| 818 | n/a | hash_info_fields, |
|---|
| 819 | n/a | 9, |
|---|
| 820 | n/a | }; |
|---|
| 821 | n/a | |
|---|
| 822 | n/a | static PyObject * |
|---|
| 823 | n/a | get_hash_info(void) |
|---|
| 824 | n/a | { |
|---|
| 825 | n/a | PyObject *hash_info; |
|---|
| 826 | n/a | int field = 0; |
|---|
| 827 | n/a | PyHash_FuncDef *hashfunc; |
|---|
| 828 | n/a | hash_info = PyStructSequence_New(&Hash_InfoType); |
|---|
| 829 | n/a | if (hash_info == NULL) |
|---|
| 830 | n/a | return NULL; |
|---|
| 831 | n/a | hashfunc = PyHash_GetFuncDef(); |
|---|
| 832 | n/a | PyStructSequence_SET_ITEM(hash_info, field++, |
|---|
| 833 | n/a | PyLong_FromLong(8*sizeof(Py_hash_t))); |
|---|
| 834 | n/a | PyStructSequence_SET_ITEM(hash_info, field++, |
|---|
| 835 | n/a | PyLong_FromSsize_t(_PyHASH_MODULUS)); |
|---|
| 836 | n/a | PyStructSequence_SET_ITEM(hash_info, field++, |
|---|
| 837 | n/a | PyLong_FromLong(_PyHASH_INF)); |
|---|
| 838 | n/a | PyStructSequence_SET_ITEM(hash_info, field++, |
|---|
| 839 | n/a | PyLong_FromLong(_PyHASH_NAN)); |
|---|
| 840 | n/a | PyStructSequence_SET_ITEM(hash_info, field++, |
|---|
| 841 | n/a | PyLong_FromLong(_PyHASH_IMAG)); |
|---|
| 842 | n/a | PyStructSequence_SET_ITEM(hash_info, field++, |
|---|
| 843 | n/a | PyUnicode_FromString(hashfunc->name)); |
|---|
| 844 | n/a | PyStructSequence_SET_ITEM(hash_info, field++, |
|---|
| 845 | n/a | PyLong_FromLong(hashfunc->hash_bits)); |
|---|
| 846 | n/a | PyStructSequence_SET_ITEM(hash_info, field++, |
|---|
| 847 | n/a | PyLong_FromLong(hashfunc->seed_bits)); |
|---|
| 848 | n/a | PyStructSequence_SET_ITEM(hash_info, field++, |
|---|
| 849 | n/a | PyLong_FromLong(Py_HASH_CUTOFF)); |
|---|
| 850 | n/a | if (PyErr_Occurred()) { |
|---|
| 851 | n/a | Py_CLEAR(hash_info); |
|---|
| 852 | n/a | return NULL; |
|---|
| 853 | n/a | } |
|---|
| 854 | n/a | return hash_info; |
|---|
| 855 | n/a | } |
|---|
| 856 | n/a | |
|---|
| 857 | n/a | |
|---|
| 858 | n/a | PyDoc_STRVAR(setrecursionlimit_doc, |
|---|
| 859 | n/a | "setrecursionlimit(n)\n\ |
|---|
| 860 | n/a | \n\ |
|---|
| 861 | n/a | Set the maximum depth of the Python interpreter stack to n. This\n\ |
|---|
| 862 | n/a | limit prevents infinite recursion from causing an overflow of the C\n\ |
|---|
| 863 | n/a | stack and crashing Python. The highest possible limit is platform-\n\ |
|---|
| 864 | n/a | dependent." |
|---|
| 865 | n/a | ); |
|---|
| 866 | n/a | |
|---|
| 867 | n/a | static PyObject * |
|---|
| 868 | n/a | sys_getrecursionlimit(PyObject *self) |
|---|
| 869 | n/a | { |
|---|
| 870 | n/a | return PyLong_FromLong(Py_GetRecursionLimit()); |
|---|
| 871 | n/a | } |
|---|
| 872 | n/a | |
|---|
| 873 | n/a | PyDoc_STRVAR(getrecursionlimit_doc, |
|---|
| 874 | n/a | "getrecursionlimit()\n\ |
|---|
| 875 | n/a | \n\ |
|---|
| 876 | n/a | Return the current value of the recursion limit, the maximum depth\n\ |
|---|
| 877 | n/a | of the Python interpreter stack. This limit prevents infinite\n\ |
|---|
| 878 | n/a | recursion from causing an overflow of the C stack and crashing Python." |
|---|
| 879 | n/a | ); |
|---|
| 880 | n/a | |
|---|
| 881 | n/a | #ifdef MS_WINDOWS |
|---|
| 882 | n/a | PyDoc_STRVAR(getwindowsversion_doc, |
|---|
| 883 | n/a | "getwindowsversion()\n\ |
|---|
| 884 | n/a | \n\ |
|---|
| 885 | n/a | Return information about the running version of Windows as a named tuple.\n\ |
|---|
| 886 | n/a | The members are named: major, minor, build, platform, service_pack,\n\ |
|---|
| 887 | n/a | service_pack_major, service_pack_minor, suite_mask, and product_type. For\n\ |
|---|
| 888 | n/a | backward compatibility, only the first 5 items are available by indexing.\n\ |
|---|
| 889 | n/a | All elements are numbers, except service_pack and platform_type which are\n\ |
|---|
| 890 | n/a | strings, and platform_version which is a 3-tuple. Platform is always 2.\n\ |
|---|
| 891 | n/a | Product_type may be 1 for a workstation, 2 for a domain controller, 3 for a\n\ |
|---|
| 892 | n/a | server. Platform_version is a 3-tuple containing a version number that is\n\ |
|---|
| 893 | n/a | intended for identifying the OS rather than feature detection." |
|---|
| 894 | n/a | ); |
|---|
| 895 | n/a | |
|---|
| 896 | n/a | static PyTypeObject WindowsVersionType = {0, 0, 0, 0, 0, 0}; |
|---|
| 897 | n/a | |
|---|
| 898 | n/a | static PyStructSequence_Field windows_version_fields[] = { |
|---|
| 899 | n/a | {"major", "Major version number"}, |
|---|
| 900 | n/a | {"minor", "Minor version number"}, |
|---|
| 901 | n/a | {"build", "Build number"}, |
|---|
| 902 | n/a | {"platform", "Operating system platform"}, |
|---|
| 903 | n/a | {"service_pack", "Latest Service Pack installed on the system"}, |
|---|
| 904 | n/a | {"service_pack_major", "Service Pack major version number"}, |
|---|
| 905 | n/a | {"service_pack_minor", "Service Pack minor version number"}, |
|---|
| 906 | n/a | {"suite_mask", "Bit mask identifying available product suites"}, |
|---|
| 907 | n/a | {"product_type", "System product type"}, |
|---|
| 908 | n/a | {"platform_version", "Diagnostic version number"}, |
|---|
| 909 | n/a | {0} |
|---|
| 910 | n/a | }; |
|---|
| 911 | n/a | |
|---|
| 912 | n/a | static PyStructSequence_Desc windows_version_desc = { |
|---|
| 913 | n/a | "sys.getwindowsversion", /* name */ |
|---|
| 914 | n/a | getwindowsversion_doc, /* doc */ |
|---|
| 915 | n/a | windows_version_fields, /* fields */ |
|---|
| 916 | n/a | 5 /* For backward compatibility, |
|---|
| 917 | n/a | only the first 5 items are accessible |
|---|
| 918 | n/a | via indexing, the rest are name only */ |
|---|
| 919 | n/a | }; |
|---|
| 920 | n/a | |
|---|
| 921 | n/a | /* Disable deprecation warnings about GetVersionEx as the result is |
|---|
| 922 | n/a | being passed straight through to the caller, who is responsible for |
|---|
| 923 | n/a | using it correctly. */ |
|---|
| 924 | n/a | #pragma warning(push) |
|---|
| 925 | n/a | #pragma warning(disable:4996) |
|---|
| 926 | n/a | |
|---|
| 927 | n/a | static PyObject * |
|---|
| 928 | n/a | sys_getwindowsversion(PyObject *self) |
|---|
| 929 | n/a | { |
|---|
| 930 | n/a | PyObject *version; |
|---|
| 931 | n/a | int pos = 0; |
|---|
| 932 | n/a | OSVERSIONINFOEX ver; |
|---|
| 933 | n/a | DWORD realMajor, realMinor, realBuild; |
|---|
| 934 | n/a | HANDLE hKernel32; |
|---|
| 935 | n/a | wchar_t kernel32_path[MAX_PATH]; |
|---|
| 936 | n/a | LPVOID verblock; |
|---|
| 937 | n/a | DWORD verblock_size; |
|---|
| 938 | n/a | |
|---|
| 939 | n/a | ver.dwOSVersionInfoSize = sizeof(ver); |
|---|
| 940 | n/a | if (!GetVersionEx((OSVERSIONINFO*) &ver)) |
|---|
| 941 | n/a | return PyErr_SetFromWindowsErr(0); |
|---|
| 942 | n/a | |
|---|
| 943 | n/a | version = PyStructSequence_New(&WindowsVersionType); |
|---|
| 944 | n/a | if (version == NULL) |
|---|
| 945 | n/a | return NULL; |
|---|
| 946 | n/a | |
|---|
| 947 | n/a | PyStructSequence_SET_ITEM(version, pos++, PyLong_FromLong(ver.dwMajorVersion)); |
|---|
| 948 | n/a | PyStructSequence_SET_ITEM(version, pos++, PyLong_FromLong(ver.dwMinorVersion)); |
|---|
| 949 | n/a | PyStructSequence_SET_ITEM(version, pos++, PyLong_FromLong(ver.dwBuildNumber)); |
|---|
| 950 | n/a | PyStructSequence_SET_ITEM(version, pos++, PyLong_FromLong(ver.dwPlatformId)); |
|---|
| 951 | n/a | PyStructSequence_SET_ITEM(version, pos++, PyUnicode_FromString(ver.szCSDVersion)); |
|---|
| 952 | n/a | PyStructSequence_SET_ITEM(version, pos++, PyLong_FromLong(ver.wServicePackMajor)); |
|---|
| 953 | n/a | PyStructSequence_SET_ITEM(version, pos++, PyLong_FromLong(ver.wServicePackMinor)); |
|---|
| 954 | n/a | PyStructSequence_SET_ITEM(version, pos++, PyLong_FromLong(ver.wSuiteMask)); |
|---|
| 955 | n/a | PyStructSequence_SET_ITEM(version, pos++, PyLong_FromLong(ver.wProductType)); |
|---|
| 956 | n/a | |
|---|
| 957 | n/a | realMajor = ver.dwMajorVersion; |
|---|
| 958 | n/a | realMinor = ver.dwMinorVersion; |
|---|
| 959 | n/a | realBuild = ver.dwBuildNumber; |
|---|
| 960 | n/a | |
|---|
| 961 | n/a | // GetVersion will lie if we are running in a compatibility mode. |
|---|
| 962 | n/a | // We need to read the version info from a system file resource |
|---|
| 963 | n/a | // to accurately identify the OS version. If we fail for any reason, |
|---|
| 964 | n/a | // just return whatever GetVersion said. |
|---|
| 965 | n/a | hKernel32 = GetModuleHandleW(L"kernel32.dll"); |
|---|
| 966 | n/a | if (hKernel32 && GetModuleFileNameW(hKernel32, kernel32_path, MAX_PATH) && |
|---|
| 967 | n/a | (verblock_size = GetFileVersionInfoSizeW(kernel32_path, NULL)) && |
|---|
| 968 | n/a | (verblock = PyMem_RawMalloc(verblock_size))) { |
|---|
| 969 | n/a | VS_FIXEDFILEINFO *ffi; |
|---|
| 970 | n/a | UINT ffi_len; |
|---|
| 971 | n/a | |
|---|
| 972 | n/a | if (GetFileVersionInfoW(kernel32_path, 0, verblock_size, verblock) && |
|---|
| 973 | n/a | VerQueryValueW(verblock, L"", (LPVOID)&ffi, &ffi_len)) { |
|---|
| 974 | n/a | realMajor = HIWORD(ffi->dwProductVersionMS); |
|---|
| 975 | n/a | realMinor = LOWORD(ffi->dwProductVersionMS); |
|---|
| 976 | n/a | realBuild = HIWORD(ffi->dwProductVersionLS); |
|---|
| 977 | n/a | } |
|---|
| 978 | n/a | PyMem_RawFree(verblock); |
|---|
| 979 | n/a | } |
|---|
| 980 | n/a | PyStructSequence_SET_ITEM(version, pos++, PyTuple_Pack(3, |
|---|
| 981 | n/a | PyLong_FromLong(realMajor), |
|---|
| 982 | n/a | PyLong_FromLong(realMinor), |
|---|
| 983 | n/a | PyLong_FromLong(realBuild) |
|---|
| 984 | n/a | )); |
|---|
| 985 | n/a | |
|---|
| 986 | n/a | if (PyErr_Occurred()) { |
|---|
| 987 | n/a | Py_DECREF(version); |
|---|
| 988 | n/a | return NULL; |
|---|
| 989 | n/a | } |
|---|
| 990 | n/a | |
|---|
| 991 | n/a | return version; |
|---|
| 992 | n/a | } |
|---|
| 993 | n/a | |
|---|
| 994 | n/a | #pragma warning(pop) |
|---|
| 995 | n/a | |
|---|
| 996 | n/a | PyDoc_STRVAR(enablelegacywindowsfsencoding_doc, |
|---|
| 997 | n/a | "_enablelegacywindowsfsencoding()\n\ |
|---|
| 998 | n/a | \n\ |
|---|
| 999 | n/a | Changes the default filesystem encoding to mbcs:replace for consistency\n\ |
|---|
| 1000 | n/a | with earlier versions of Python. See PEP 529 for more information.\n\ |
|---|
| 1001 | n/a | \n\ |
|---|
| 1002 | n/a | This is equivalent to defining the PYTHONLEGACYWINDOWSFSENCODING \n\ |
|---|
| 1003 | n/a | environment variable before launching Python." |
|---|
| 1004 | n/a | ); |
|---|
| 1005 | n/a | |
|---|
| 1006 | n/a | static PyObject * |
|---|
| 1007 | n/a | sys_enablelegacywindowsfsencoding(PyObject *self) |
|---|
| 1008 | n/a | { |
|---|
| 1009 | n/a | Py_FileSystemDefaultEncoding = "mbcs"; |
|---|
| 1010 | n/a | Py_FileSystemDefaultEncodeErrors = "replace"; |
|---|
| 1011 | n/a | Py_RETURN_NONE; |
|---|
| 1012 | n/a | } |
|---|
| 1013 | n/a | |
|---|
| 1014 | n/a | #endif /* MS_WINDOWS */ |
|---|
| 1015 | n/a | |
|---|
| 1016 | n/a | #ifdef HAVE_DLOPEN |
|---|
| 1017 | n/a | static PyObject * |
|---|
| 1018 | n/a | sys_setdlopenflags(PyObject *self, PyObject *args) |
|---|
| 1019 | n/a | { |
|---|
| 1020 | n/a | int new_val; |
|---|
| 1021 | n/a | PyThreadState *tstate = PyThreadState_GET(); |
|---|
| 1022 | n/a | if (!PyArg_ParseTuple(args, "i:setdlopenflags", &new_val)) |
|---|
| 1023 | n/a | return NULL; |
|---|
| 1024 | n/a | if (!tstate) |
|---|
| 1025 | n/a | return NULL; |
|---|
| 1026 | n/a | tstate->interp->dlopenflags = new_val; |
|---|
| 1027 | n/a | Py_RETURN_NONE; |
|---|
| 1028 | n/a | } |
|---|
| 1029 | n/a | |
|---|
| 1030 | n/a | PyDoc_STRVAR(setdlopenflags_doc, |
|---|
| 1031 | n/a | "setdlopenflags(n) -> None\n\ |
|---|
| 1032 | n/a | \n\ |
|---|
| 1033 | n/a | Set the flags used by the interpreter for dlopen calls, such as when the\n\ |
|---|
| 1034 | n/a | interpreter loads extension modules. Among other things, this will enable\n\ |
|---|
| 1035 | n/a | a lazy resolving of symbols when importing a module, if called as\n\ |
|---|
| 1036 | n/a | sys.setdlopenflags(0). To share symbols across extension modules, call as\n\ |
|---|
| 1037 | n/a | sys.setdlopenflags(os.RTLD_GLOBAL). Symbolic names for the flag modules\n\ |
|---|
| 1038 | n/a | can be found in the os module (RTLD_xxx constants, e.g. os.RTLD_LAZY)."); |
|---|
| 1039 | n/a | |
|---|
| 1040 | n/a | static PyObject * |
|---|
| 1041 | n/a | sys_getdlopenflags(PyObject *self, PyObject *args) |
|---|
| 1042 | n/a | { |
|---|
| 1043 | n/a | PyThreadState *tstate = PyThreadState_GET(); |
|---|
| 1044 | n/a | if (!tstate) |
|---|
| 1045 | n/a | return NULL; |
|---|
| 1046 | n/a | return PyLong_FromLong(tstate->interp->dlopenflags); |
|---|
| 1047 | n/a | } |
|---|
| 1048 | n/a | |
|---|
| 1049 | n/a | PyDoc_STRVAR(getdlopenflags_doc, |
|---|
| 1050 | n/a | "getdlopenflags() -> int\n\ |
|---|
| 1051 | n/a | \n\ |
|---|
| 1052 | n/a | Return the current value of the flags that are used for dlopen calls.\n\ |
|---|
| 1053 | n/a | The flag constants are defined in the os module."); |
|---|
| 1054 | n/a | |
|---|
| 1055 | n/a | #endif /* HAVE_DLOPEN */ |
|---|
| 1056 | n/a | |
|---|
| 1057 | n/a | #ifdef USE_MALLOPT |
|---|
| 1058 | n/a | /* Link with -lmalloc (or -lmpc) on an SGI */ |
|---|
| 1059 | n/a | #include <malloc.h> |
|---|
| 1060 | n/a | |
|---|
| 1061 | n/a | static PyObject * |
|---|
| 1062 | n/a | sys_mdebug(PyObject *self, PyObject *args) |
|---|
| 1063 | n/a | { |
|---|
| 1064 | n/a | int flag; |
|---|
| 1065 | n/a | if (!PyArg_ParseTuple(args, "i:mdebug", &flag)) |
|---|
| 1066 | n/a | return NULL; |
|---|
| 1067 | n/a | mallopt(M_DEBUG, flag); |
|---|
| 1068 | n/a | Py_RETURN_NONE; |
|---|
| 1069 | n/a | } |
|---|
| 1070 | n/a | #endif /* USE_MALLOPT */ |
|---|
| 1071 | n/a | |
|---|
| 1072 | n/a | size_t |
|---|
| 1073 | n/a | _PySys_GetSizeOf(PyObject *o) |
|---|
| 1074 | n/a | { |
|---|
| 1075 | n/a | PyObject *res = NULL; |
|---|
| 1076 | n/a | PyObject *method; |
|---|
| 1077 | n/a | Py_ssize_t size; |
|---|
| 1078 | n/a | |
|---|
| 1079 | n/a | /* Make sure the type is initialized. float gets initialized late */ |
|---|
| 1080 | n/a | if (PyType_Ready(Py_TYPE(o)) < 0) |
|---|
| 1081 | n/a | return (size_t)-1; |
|---|
| 1082 | n/a | |
|---|
| 1083 | n/a | method = _PyObject_LookupSpecial(o, &PyId___sizeof__); |
|---|
| 1084 | n/a | if (method == NULL) { |
|---|
| 1085 | n/a | if (!PyErr_Occurred()) |
|---|
| 1086 | n/a | PyErr_Format(PyExc_TypeError, |
|---|
| 1087 | n/a | "Type %.100s doesn't define __sizeof__", |
|---|
| 1088 | n/a | Py_TYPE(o)->tp_name); |
|---|
| 1089 | n/a | } |
|---|
| 1090 | n/a | else { |
|---|
| 1091 | n/a | res = _PyObject_CallNoArg(method); |
|---|
| 1092 | n/a | Py_DECREF(method); |
|---|
| 1093 | n/a | } |
|---|
| 1094 | n/a | |
|---|
| 1095 | n/a | if (res == NULL) |
|---|
| 1096 | n/a | return (size_t)-1; |
|---|
| 1097 | n/a | |
|---|
| 1098 | n/a | size = PyLong_AsSsize_t(res); |
|---|
| 1099 | n/a | Py_DECREF(res); |
|---|
| 1100 | n/a | if (size == -1 && PyErr_Occurred()) |
|---|
| 1101 | n/a | return (size_t)-1; |
|---|
| 1102 | n/a | |
|---|
| 1103 | n/a | if (size < 0) { |
|---|
| 1104 | n/a | PyErr_SetString(PyExc_ValueError, "__sizeof__() should return >= 0"); |
|---|
| 1105 | n/a | return (size_t)-1; |
|---|
| 1106 | n/a | } |
|---|
| 1107 | n/a | |
|---|
| 1108 | n/a | /* add gc_head size */ |
|---|
| 1109 | n/a | if (PyObject_IS_GC(o)) |
|---|
| 1110 | n/a | return ((size_t)size) + sizeof(PyGC_Head); |
|---|
| 1111 | n/a | return (size_t)size; |
|---|
| 1112 | n/a | } |
|---|
| 1113 | n/a | |
|---|
| 1114 | n/a | static PyObject * |
|---|
| 1115 | n/a | sys_getsizeof(PyObject *self, PyObject *args, PyObject *kwds) |
|---|
| 1116 | n/a | { |
|---|
| 1117 | n/a | static char *kwlist[] = {"object", "default", 0}; |
|---|
| 1118 | n/a | size_t size; |
|---|
| 1119 | n/a | PyObject *o, *dflt = NULL; |
|---|
| 1120 | n/a | |
|---|
| 1121 | n/a | if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O:getsizeof", |
|---|
| 1122 | n/a | kwlist, &o, &dflt)) |
|---|
| 1123 | n/a | return NULL; |
|---|
| 1124 | n/a | |
|---|
| 1125 | n/a | size = _PySys_GetSizeOf(o); |
|---|
| 1126 | n/a | |
|---|
| 1127 | n/a | if (size == (size_t)-1 && PyErr_Occurred()) { |
|---|
| 1128 | n/a | /* Has a default value been given */ |
|---|
| 1129 | n/a | if (dflt != NULL && PyErr_ExceptionMatches(PyExc_TypeError)) { |
|---|
| 1130 | n/a | PyErr_Clear(); |
|---|
| 1131 | n/a | Py_INCREF(dflt); |
|---|
| 1132 | n/a | return dflt; |
|---|
| 1133 | n/a | } |
|---|
| 1134 | n/a | else |
|---|
| 1135 | n/a | return NULL; |
|---|
| 1136 | n/a | } |
|---|
| 1137 | n/a | |
|---|
| 1138 | n/a | return PyLong_FromSize_t(size); |
|---|
| 1139 | n/a | } |
|---|
| 1140 | n/a | |
|---|
| 1141 | n/a | PyDoc_STRVAR(getsizeof_doc, |
|---|
| 1142 | n/a | "getsizeof(object, default) -> int\n\ |
|---|
| 1143 | n/a | \n\ |
|---|
| 1144 | n/a | Return the size of object in bytes."); |
|---|
| 1145 | n/a | |
|---|
| 1146 | n/a | static PyObject * |
|---|
| 1147 | n/a | sys_getrefcount(PyObject *self, PyObject *arg) |
|---|
| 1148 | n/a | { |
|---|
| 1149 | n/a | return PyLong_FromSsize_t(arg->ob_refcnt); |
|---|
| 1150 | n/a | } |
|---|
| 1151 | n/a | |
|---|
| 1152 | n/a | #ifdef Py_REF_DEBUG |
|---|
| 1153 | n/a | static PyObject * |
|---|
| 1154 | n/a | sys_gettotalrefcount(PyObject *self) |
|---|
| 1155 | n/a | { |
|---|
| 1156 | n/a | return PyLong_FromSsize_t(_Py_GetRefTotal()); |
|---|
| 1157 | n/a | } |
|---|
| 1158 | n/a | #endif /* Py_REF_DEBUG */ |
|---|
| 1159 | n/a | |
|---|
| 1160 | n/a | PyDoc_STRVAR(getrefcount_doc, |
|---|
| 1161 | n/a | "getrefcount(object) -> integer\n\ |
|---|
| 1162 | n/a | \n\ |
|---|
| 1163 | n/a | Return the reference count of object. The count returned is generally\n\ |
|---|
| 1164 | n/a | one higher than you might expect, because it includes the (temporary)\n\ |
|---|
| 1165 | n/a | reference as an argument to getrefcount()." |
|---|
| 1166 | n/a | ); |
|---|
| 1167 | n/a | |
|---|
| 1168 | n/a | static PyObject * |
|---|
| 1169 | n/a | sys_getallocatedblocks(PyObject *self) |
|---|
| 1170 | n/a | { |
|---|
| 1171 | n/a | return PyLong_FromSsize_t(_Py_GetAllocatedBlocks()); |
|---|
| 1172 | n/a | } |
|---|
| 1173 | n/a | |
|---|
| 1174 | n/a | PyDoc_STRVAR(getallocatedblocks_doc, |
|---|
| 1175 | n/a | "getallocatedblocks() -> integer\n\ |
|---|
| 1176 | n/a | \n\ |
|---|
| 1177 | n/a | Return the number of memory blocks currently allocated, regardless of their\n\ |
|---|
| 1178 | n/a | size." |
|---|
| 1179 | n/a | ); |
|---|
| 1180 | n/a | |
|---|
| 1181 | n/a | #ifdef COUNT_ALLOCS |
|---|
| 1182 | n/a | static PyObject * |
|---|
| 1183 | n/a | sys_getcounts(PyObject *self) |
|---|
| 1184 | n/a | { |
|---|
| 1185 | n/a | extern PyObject *get_counts(void); |
|---|
| 1186 | n/a | |
|---|
| 1187 | n/a | return get_counts(); |
|---|
| 1188 | n/a | } |
|---|
| 1189 | n/a | #endif |
|---|
| 1190 | n/a | |
|---|
| 1191 | n/a | PyDoc_STRVAR(getframe_doc, |
|---|
| 1192 | n/a | "_getframe([depth]) -> frameobject\n\ |
|---|
| 1193 | n/a | \n\ |
|---|
| 1194 | n/a | Return a frame object from the call stack. If optional integer depth is\n\ |
|---|
| 1195 | n/a | given, return the frame object that many calls below the top of the stack.\n\ |
|---|
| 1196 | n/a | If that is deeper than the call stack, ValueError is raised. The default\n\ |
|---|
| 1197 | n/a | for depth is zero, returning the frame at the top of the call stack.\n\ |
|---|
| 1198 | n/a | \n\ |
|---|
| 1199 | n/a | This function should be used for internal and specialized\n\ |
|---|
| 1200 | n/a | purposes only." |
|---|
| 1201 | n/a | ); |
|---|
| 1202 | n/a | |
|---|
| 1203 | n/a | static PyObject * |
|---|
| 1204 | n/a | sys_getframe(PyObject *self, PyObject *args) |
|---|
| 1205 | n/a | { |
|---|
| 1206 | n/a | PyFrameObject *f = PyThreadState_GET()->frame; |
|---|
| 1207 | n/a | int depth = -1; |
|---|
| 1208 | n/a | |
|---|
| 1209 | n/a | if (!PyArg_ParseTuple(args, "|i:_getframe", &depth)) |
|---|
| 1210 | n/a | return NULL; |
|---|
| 1211 | n/a | |
|---|
| 1212 | n/a | while (depth > 0 && f != NULL) { |
|---|
| 1213 | n/a | f = f->f_back; |
|---|
| 1214 | n/a | --depth; |
|---|
| 1215 | n/a | } |
|---|
| 1216 | n/a | if (f == NULL) { |
|---|
| 1217 | n/a | PyErr_SetString(PyExc_ValueError, |
|---|
| 1218 | n/a | "call stack is not deep enough"); |
|---|
| 1219 | n/a | return NULL; |
|---|
| 1220 | n/a | } |
|---|
| 1221 | n/a | Py_INCREF(f); |
|---|
| 1222 | n/a | return (PyObject*)f; |
|---|
| 1223 | n/a | } |
|---|
| 1224 | n/a | |
|---|
| 1225 | n/a | PyDoc_STRVAR(current_frames_doc, |
|---|
| 1226 | n/a | "_current_frames() -> dictionary\n\ |
|---|
| 1227 | n/a | \n\ |
|---|
| 1228 | n/a | Return a dictionary mapping each current thread T's thread id to T's\n\ |
|---|
| 1229 | n/a | current stack frame.\n\ |
|---|
| 1230 | n/a | \n\ |
|---|
| 1231 | n/a | This function should be used for specialized purposes only." |
|---|
| 1232 | n/a | ); |
|---|
| 1233 | n/a | |
|---|
| 1234 | n/a | static PyObject * |
|---|
| 1235 | n/a | sys_current_frames(PyObject *self, PyObject *noargs) |
|---|
| 1236 | n/a | { |
|---|
| 1237 | n/a | return _PyThread_CurrentFrames(); |
|---|
| 1238 | n/a | } |
|---|
| 1239 | n/a | |
|---|
| 1240 | n/a | PyDoc_STRVAR(call_tracing_doc, |
|---|
| 1241 | n/a | "call_tracing(func, args) -> object\n\ |
|---|
| 1242 | n/a | \n\ |
|---|
| 1243 | n/a | Call func(*args), while tracing is enabled. The tracing state is\n\ |
|---|
| 1244 | n/a | saved, and restored afterwards. This is intended to be called from\n\ |
|---|
| 1245 | n/a | a debugger from a checkpoint, to recursively debug some other code." |
|---|
| 1246 | n/a | ); |
|---|
| 1247 | n/a | |
|---|
| 1248 | n/a | static PyObject * |
|---|
| 1249 | n/a | sys_call_tracing(PyObject *self, PyObject *args) |
|---|
| 1250 | n/a | { |
|---|
| 1251 | n/a | PyObject *func, *funcargs; |
|---|
| 1252 | n/a | if (!PyArg_ParseTuple(args, "OO!:call_tracing", &func, &PyTuple_Type, &funcargs)) |
|---|
| 1253 | n/a | return NULL; |
|---|
| 1254 | n/a | return _PyEval_CallTracing(func, funcargs); |
|---|
| 1255 | n/a | } |
|---|
| 1256 | n/a | |
|---|
| 1257 | n/a | PyDoc_STRVAR(callstats_doc, |
|---|
| 1258 | n/a | "callstats() -> tuple of integers\n\ |
|---|
| 1259 | n/a | \n\ |
|---|
| 1260 | n/a | Return a tuple of function call statistics, if CALL_PROFILE was defined\n\ |
|---|
| 1261 | n/a | when Python was built. Otherwise, return None.\n\ |
|---|
| 1262 | n/a | \n\ |
|---|
| 1263 | n/a | When enabled, this function returns detailed, implementation-specific\n\ |
|---|
| 1264 | n/a | details about the number of function calls executed. The return value is\n\ |
|---|
| 1265 | n/a | a 11-tuple where the entries in the tuple are counts of:\n\ |
|---|
| 1266 | n/a | 0. all function calls\n\ |
|---|
| 1267 | n/a | 1. calls to PyFunction_Type objects\n\ |
|---|
| 1268 | n/a | 2. PyFunction calls that do not create an argument tuple\n\ |
|---|
| 1269 | n/a | 3. PyFunction calls that do not create an argument tuple\n\ |
|---|
| 1270 | n/a | and bypass PyEval_EvalCodeEx()\n\ |
|---|
| 1271 | n/a | 4. PyMethod calls\n\ |
|---|
| 1272 | n/a | 5. PyMethod calls on bound methods\n\ |
|---|
| 1273 | n/a | 6. PyType calls\n\ |
|---|
| 1274 | n/a | 7. PyCFunction calls\n\ |
|---|
| 1275 | n/a | 8. generator calls\n\ |
|---|
| 1276 | n/a | 9. All other calls\n\ |
|---|
| 1277 | n/a | 10. Number of stack pops performed by call_function()" |
|---|
| 1278 | n/a | ); |
|---|
| 1279 | n/a | |
|---|
| 1280 | n/a | static PyObject * |
|---|
| 1281 | n/a | sys_callstats(PyObject *self) |
|---|
| 1282 | n/a | { |
|---|
| 1283 | n/a | if (PyErr_WarnEx(PyExc_DeprecationWarning, |
|---|
| 1284 | n/a | "sys.callstats() has been deprecated in Python 3.7 " |
|---|
| 1285 | n/a | "and will be removed in the future", 1) < 0) { |
|---|
| 1286 | n/a | return NULL; |
|---|
| 1287 | n/a | } |
|---|
| 1288 | n/a | |
|---|
| 1289 | n/a | Py_RETURN_NONE; |
|---|
| 1290 | n/a | } |
|---|
| 1291 | n/a | |
|---|
| 1292 | n/a | |
|---|
| 1293 | n/a | #ifdef __cplusplus |
|---|
| 1294 | n/a | extern "C" { |
|---|
| 1295 | n/a | #endif |
|---|
| 1296 | n/a | |
|---|
| 1297 | n/a | static PyObject * |
|---|
| 1298 | n/a | sys_debugmallocstats(PyObject *self, PyObject *args) |
|---|
| 1299 | n/a | { |
|---|
| 1300 | n/a | #ifdef WITH_PYMALLOC |
|---|
| 1301 | n/a | if (_PyMem_PymallocEnabled()) { |
|---|
| 1302 | n/a | _PyObject_DebugMallocStats(stderr); |
|---|
| 1303 | n/a | fputc('\n', stderr); |
|---|
| 1304 | n/a | } |
|---|
| 1305 | n/a | #endif |
|---|
| 1306 | n/a | _PyObject_DebugTypeStats(stderr); |
|---|
| 1307 | n/a | |
|---|
| 1308 | n/a | Py_RETURN_NONE; |
|---|
| 1309 | n/a | } |
|---|
| 1310 | n/a | PyDoc_STRVAR(debugmallocstats_doc, |
|---|
| 1311 | n/a | "_debugmallocstats()\n\ |
|---|
| 1312 | n/a | \n\ |
|---|
| 1313 | n/a | Print summary info to stderr about the state of\n\ |
|---|
| 1314 | n/a | pymalloc's structures.\n\ |
|---|
| 1315 | n/a | \n\ |
|---|
| 1316 | n/a | In Py_DEBUG mode, also perform some expensive internal consistency\n\ |
|---|
| 1317 | n/a | checks.\n\ |
|---|
| 1318 | n/a | "); |
|---|
| 1319 | n/a | |
|---|
| 1320 | n/a | #ifdef Py_TRACE_REFS |
|---|
| 1321 | n/a | /* Defined in objects.c because it uses static globals if that file */ |
|---|
| 1322 | n/a | extern PyObject *_Py_GetObjects(PyObject *, PyObject *); |
|---|
| 1323 | n/a | #endif |
|---|
| 1324 | n/a | |
|---|
| 1325 | n/a | #ifdef DYNAMIC_EXECUTION_PROFILE |
|---|
| 1326 | n/a | /* Defined in ceval.c because it uses static globals if that file */ |
|---|
| 1327 | n/a | extern PyObject *_Py_GetDXProfile(PyObject *, PyObject *); |
|---|
| 1328 | n/a | #endif |
|---|
| 1329 | n/a | |
|---|
| 1330 | n/a | #ifdef __cplusplus |
|---|
| 1331 | n/a | } |
|---|
| 1332 | n/a | #endif |
|---|
| 1333 | n/a | |
|---|
| 1334 | n/a | static PyObject * |
|---|
| 1335 | n/a | sys_clear_type_cache(PyObject* self, PyObject* args) |
|---|
| 1336 | n/a | { |
|---|
| 1337 | n/a | PyType_ClearCache(); |
|---|
| 1338 | n/a | Py_RETURN_NONE; |
|---|
| 1339 | n/a | } |
|---|
| 1340 | n/a | |
|---|
| 1341 | n/a | PyDoc_STRVAR(sys_clear_type_cache__doc__, |
|---|
| 1342 | n/a | "_clear_type_cache() -> None\n\ |
|---|
| 1343 | n/a | Clear the internal type lookup cache."); |
|---|
| 1344 | n/a | |
|---|
| 1345 | n/a | static PyObject * |
|---|
| 1346 | n/a | sys_is_finalizing(PyObject* self, PyObject* args) |
|---|
| 1347 | n/a | { |
|---|
| 1348 | n/a | return PyBool_FromLong(_Py_Finalizing != NULL); |
|---|
| 1349 | n/a | } |
|---|
| 1350 | n/a | |
|---|
| 1351 | n/a | PyDoc_STRVAR(is_finalizing_doc, |
|---|
| 1352 | n/a | "is_finalizing()\n\ |
|---|
| 1353 | n/a | Return True if Python is exiting."); |
|---|
| 1354 | n/a | |
|---|
| 1355 | n/a | |
|---|
| 1356 | n/a | #ifdef ANDROID_API_LEVEL |
|---|
| 1357 | n/a | PyDoc_STRVAR(getandroidapilevel_doc, |
|---|
| 1358 | n/a | "getandroidapilevel()\n\ |
|---|
| 1359 | n/a | \n\ |
|---|
| 1360 | n/a | Return the build time API version of Android as an integer."); |
|---|
| 1361 | n/a | |
|---|
| 1362 | n/a | static PyObject * |
|---|
| 1363 | n/a | sys_getandroidapilevel(PyObject *self) |
|---|
| 1364 | n/a | { |
|---|
| 1365 | n/a | return PyLong_FromLong(ANDROID_API_LEVEL); |
|---|
| 1366 | n/a | } |
|---|
| 1367 | n/a | #endif /* ANDROID_API_LEVEL */ |
|---|
| 1368 | n/a | |
|---|
| 1369 | n/a | |
|---|
| 1370 | n/a | static PyMethodDef sys_methods[] = { |
|---|
| 1371 | n/a | /* Might as well keep this in alphabetic order */ |
|---|
| 1372 | n/a | {"callstats", (PyCFunction)sys_callstats, METH_NOARGS, |
|---|
| 1373 | n/a | callstats_doc}, |
|---|
| 1374 | n/a | {"_clear_type_cache", sys_clear_type_cache, METH_NOARGS, |
|---|
| 1375 | n/a | sys_clear_type_cache__doc__}, |
|---|
| 1376 | n/a | {"_current_frames", sys_current_frames, METH_NOARGS, |
|---|
| 1377 | n/a | current_frames_doc}, |
|---|
| 1378 | n/a | {"displayhook", sys_displayhook, METH_O, displayhook_doc}, |
|---|
| 1379 | n/a | {"exc_info", sys_exc_info, METH_NOARGS, exc_info_doc}, |
|---|
| 1380 | n/a | {"excepthook", sys_excepthook, METH_VARARGS, excepthook_doc}, |
|---|
| 1381 | n/a | {"exit", sys_exit, METH_VARARGS, exit_doc}, |
|---|
| 1382 | n/a | {"getdefaultencoding", (PyCFunction)sys_getdefaultencoding, |
|---|
| 1383 | n/a | METH_NOARGS, getdefaultencoding_doc}, |
|---|
| 1384 | n/a | #ifdef HAVE_DLOPEN |
|---|
| 1385 | n/a | {"getdlopenflags", (PyCFunction)sys_getdlopenflags, METH_NOARGS, |
|---|
| 1386 | n/a | getdlopenflags_doc}, |
|---|
| 1387 | n/a | #endif |
|---|
| 1388 | n/a | {"getallocatedblocks", (PyCFunction)sys_getallocatedblocks, METH_NOARGS, |
|---|
| 1389 | n/a | getallocatedblocks_doc}, |
|---|
| 1390 | n/a | #ifdef COUNT_ALLOCS |
|---|
| 1391 | n/a | {"getcounts", (PyCFunction)sys_getcounts, METH_NOARGS}, |
|---|
| 1392 | n/a | #endif |
|---|
| 1393 | n/a | #ifdef DYNAMIC_EXECUTION_PROFILE |
|---|
| 1394 | n/a | {"getdxp", _Py_GetDXProfile, METH_VARARGS}, |
|---|
| 1395 | n/a | #endif |
|---|
| 1396 | n/a | {"getfilesystemencoding", (PyCFunction)sys_getfilesystemencoding, |
|---|
| 1397 | n/a | METH_NOARGS, getfilesystemencoding_doc}, |
|---|
| 1398 | n/a | { "getfilesystemencodeerrors", (PyCFunction)sys_getfilesystemencodeerrors, |
|---|
| 1399 | n/a | METH_NOARGS, getfilesystemencodeerrors_doc }, |
|---|
| 1400 | n/a | #ifdef Py_TRACE_REFS |
|---|
| 1401 | n/a | {"getobjects", _Py_GetObjects, METH_VARARGS}, |
|---|
| 1402 | n/a | #endif |
|---|
| 1403 | n/a | #ifdef Py_REF_DEBUG |
|---|
| 1404 | n/a | {"gettotalrefcount", (PyCFunction)sys_gettotalrefcount, METH_NOARGS}, |
|---|
| 1405 | n/a | #endif |
|---|
| 1406 | n/a | {"getrefcount", (PyCFunction)sys_getrefcount, METH_O, getrefcount_doc}, |
|---|
| 1407 | n/a | {"getrecursionlimit", (PyCFunction)sys_getrecursionlimit, METH_NOARGS, |
|---|
| 1408 | n/a | getrecursionlimit_doc}, |
|---|
| 1409 | n/a | {"getsizeof", (PyCFunction)sys_getsizeof, |
|---|
| 1410 | n/a | METH_VARARGS | METH_KEYWORDS, getsizeof_doc}, |
|---|
| 1411 | n/a | {"_getframe", sys_getframe, METH_VARARGS, getframe_doc}, |
|---|
| 1412 | n/a | #ifdef MS_WINDOWS |
|---|
| 1413 | n/a | {"getwindowsversion", (PyCFunction)sys_getwindowsversion, METH_NOARGS, |
|---|
| 1414 | n/a | getwindowsversion_doc}, |
|---|
| 1415 | n/a | {"_enablelegacywindowsfsencoding", (PyCFunction)sys_enablelegacywindowsfsencoding, |
|---|
| 1416 | n/a | METH_NOARGS, enablelegacywindowsfsencoding_doc }, |
|---|
| 1417 | n/a | #endif /* MS_WINDOWS */ |
|---|
| 1418 | n/a | {"intern", sys_intern, METH_VARARGS, intern_doc}, |
|---|
| 1419 | n/a | {"is_finalizing", sys_is_finalizing, METH_NOARGS, is_finalizing_doc}, |
|---|
| 1420 | n/a | #ifdef USE_MALLOPT |
|---|
| 1421 | n/a | {"mdebug", sys_mdebug, METH_VARARGS}, |
|---|
| 1422 | n/a | #endif |
|---|
| 1423 | n/a | {"setcheckinterval", sys_setcheckinterval, METH_VARARGS, |
|---|
| 1424 | n/a | setcheckinterval_doc}, |
|---|
| 1425 | n/a | {"getcheckinterval", sys_getcheckinterval, METH_NOARGS, |
|---|
| 1426 | n/a | getcheckinterval_doc}, |
|---|
| 1427 | n/a | #ifdef WITH_THREAD |
|---|
| 1428 | n/a | {"setswitchinterval", sys_setswitchinterval, METH_VARARGS, |
|---|
| 1429 | n/a | setswitchinterval_doc}, |
|---|
| 1430 | n/a | {"getswitchinterval", sys_getswitchinterval, METH_NOARGS, |
|---|
| 1431 | n/a | getswitchinterval_doc}, |
|---|
| 1432 | n/a | #endif |
|---|
| 1433 | n/a | #ifdef HAVE_DLOPEN |
|---|
| 1434 | n/a | {"setdlopenflags", sys_setdlopenflags, METH_VARARGS, |
|---|
| 1435 | n/a | setdlopenflags_doc}, |
|---|
| 1436 | n/a | #endif |
|---|
| 1437 | n/a | {"setprofile", sys_setprofile, METH_O, setprofile_doc}, |
|---|
| 1438 | n/a | {"getprofile", sys_getprofile, METH_NOARGS, getprofile_doc}, |
|---|
| 1439 | n/a | {"setrecursionlimit", sys_setrecursionlimit, METH_VARARGS, |
|---|
| 1440 | n/a | setrecursionlimit_doc}, |
|---|
| 1441 | n/a | {"settrace", sys_settrace, METH_O, settrace_doc}, |
|---|
| 1442 | n/a | {"gettrace", sys_gettrace, METH_NOARGS, gettrace_doc}, |
|---|
| 1443 | n/a | {"call_tracing", sys_call_tracing, METH_VARARGS, call_tracing_doc}, |
|---|
| 1444 | n/a | {"_debugmallocstats", sys_debugmallocstats, METH_NOARGS, |
|---|
| 1445 | n/a | debugmallocstats_doc}, |
|---|
| 1446 | n/a | {"set_coroutine_wrapper", sys_set_coroutine_wrapper, METH_O, |
|---|
| 1447 | n/a | set_coroutine_wrapper_doc}, |
|---|
| 1448 | n/a | {"get_coroutine_wrapper", sys_get_coroutine_wrapper, METH_NOARGS, |
|---|
| 1449 | n/a | get_coroutine_wrapper_doc}, |
|---|
| 1450 | n/a | {"set_asyncgen_hooks", (PyCFunction)sys_set_asyncgen_hooks, |
|---|
| 1451 | n/a | METH_VARARGS | METH_KEYWORDS, set_asyncgen_hooks_doc}, |
|---|
| 1452 | n/a | {"get_asyncgen_hooks", sys_get_asyncgen_hooks, METH_NOARGS, |
|---|
| 1453 | n/a | get_asyncgen_hooks_doc}, |
|---|
| 1454 | n/a | #ifdef ANDROID_API_LEVEL |
|---|
| 1455 | n/a | {"getandroidapilevel", (PyCFunction)sys_getandroidapilevel, METH_NOARGS, |
|---|
| 1456 | n/a | getandroidapilevel_doc}, |
|---|
| 1457 | n/a | #endif |
|---|
| 1458 | n/a | {NULL, NULL} /* sentinel */ |
|---|
| 1459 | n/a | }; |
|---|
| 1460 | n/a | |
|---|
| 1461 | n/a | static PyObject * |
|---|
| 1462 | n/a | list_builtin_module_names(void) |
|---|
| 1463 | n/a | { |
|---|
| 1464 | n/a | PyObject *list = PyList_New(0); |
|---|
| 1465 | n/a | int i; |
|---|
| 1466 | n/a | if (list == NULL) |
|---|
| 1467 | n/a | return NULL; |
|---|
| 1468 | n/a | for (i = 0; PyImport_Inittab[i].name != NULL; i++) { |
|---|
| 1469 | n/a | PyObject *name = PyUnicode_FromString( |
|---|
| 1470 | n/a | PyImport_Inittab[i].name); |
|---|
| 1471 | n/a | if (name == NULL) |
|---|
| 1472 | n/a | break; |
|---|
| 1473 | n/a | PyList_Append(list, name); |
|---|
| 1474 | n/a | Py_DECREF(name); |
|---|
| 1475 | n/a | } |
|---|
| 1476 | n/a | if (PyList_Sort(list) != 0) { |
|---|
| 1477 | n/a | Py_DECREF(list); |
|---|
| 1478 | n/a | list = NULL; |
|---|
| 1479 | n/a | } |
|---|
| 1480 | n/a | if (list) { |
|---|
| 1481 | n/a | PyObject *v = PyList_AsTuple(list); |
|---|
| 1482 | n/a | Py_DECREF(list); |
|---|
| 1483 | n/a | list = v; |
|---|
| 1484 | n/a | } |
|---|
| 1485 | n/a | return list; |
|---|
| 1486 | n/a | } |
|---|
| 1487 | n/a | |
|---|
| 1488 | n/a | static PyObject *warnoptions = NULL; |
|---|
| 1489 | n/a | |
|---|
| 1490 | n/a | void |
|---|
| 1491 | n/a | PySys_ResetWarnOptions(void) |
|---|
| 1492 | n/a | { |
|---|
| 1493 | n/a | if (warnoptions == NULL || !PyList_Check(warnoptions)) |
|---|
| 1494 | n/a | return; |
|---|
| 1495 | n/a | PyList_SetSlice(warnoptions, 0, PyList_GET_SIZE(warnoptions), NULL); |
|---|
| 1496 | n/a | } |
|---|
| 1497 | n/a | |
|---|
| 1498 | n/a | void |
|---|
| 1499 | n/a | PySys_AddWarnOptionUnicode(PyObject *unicode) |
|---|
| 1500 | n/a | { |
|---|
| 1501 | n/a | if (warnoptions == NULL || !PyList_Check(warnoptions)) { |
|---|
| 1502 | n/a | Py_XDECREF(warnoptions); |
|---|
| 1503 | n/a | warnoptions = PyList_New(0); |
|---|
| 1504 | n/a | if (warnoptions == NULL) |
|---|
| 1505 | n/a | return; |
|---|
| 1506 | n/a | } |
|---|
| 1507 | n/a | PyList_Append(warnoptions, unicode); |
|---|
| 1508 | n/a | } |
|---|
| 1509 | n/a | |
|---|
| 1510 | n/a | void |
|---|
| 1511 | n/a | PySys_AddWarnOption(const wchar_t *s) |
|---|
| 1512 | n/a | { |
|---|
| 1513 | n/a | PyObject *unicode; |
|---|
| 1514 | n/a | unicode = PyUnicode_FromWideChar(s, -1); |
|---|
| 1515 | n/a | if (unicode == NULL) |
|---|
| 1516 | n/a | return; |
|---|
| 1517 | n/a | PySys_AddWarnOptionUnicode(unicode); |
|---|
| 1518 | n/a | Py_DECREF(unicode); |
|---|
| 1519 | n/a | } |
|---|
| 1520 | n/a | |
|---|
| 1521 | n/a | int |
|---|
| 1522 | n/a | PySys_HasWarnOptions(void) |
|---|
| 1523 | n/a | { |
|---|
| 1524 | n/a | return (warnoptions != NULL && (PyList_Size(warnoptions) > 0)) ? 1 : 0; |
|---|
| 1525 | n/a | } |
|---|
| 1526 | n/a | |
|---|
| 1527 | n/a | static PyObject *xoptions = NULL; |
|---|
| 1528 | n/a | |
|---|
| 1529 | n/a | static PyObject * |
|---|
| 1530 | n/a | get_xoptions(void) |
|---|
| 1531 | n/a | { |
|---|
| 1532 | n/a | if (xoptions == NULL || !PyDict_Check(xoptions)) { |
|---|
| 1533 | n/a | Py_XDECREF(xoptions); |
|---|
| 1534 | n/a | xoptions = PyDict_New(); |
|---|
| 1535 | n/a | } |
|---|
| 1536 | n/a | return xoptions; |
|---|
| 1537 | n/a | } |
|---|
| 1538 | n/a | |
|---|
| 1539 | n/a | void |
|---|
| 1540 | n/a | PySys_AddXOption(const wchar_t *s) |
|---|
| 1541 | n/a | { |
|---|
| 1542 | n/a | PyObject *opts; |
|---|
| 1543 | n/a | PyObject *name = NULL, *value = NULL; |
|---|
| 1544 | n/a | const wchar_t *name_end; |
|---|
| 1545 | n/a | |
|---|
| 1546 | n/a | opts = get_xoptions(); |
|---|
| 1547 | n/a | if (opts == NULL) |
|---|
| 1548 | n/a | goto error; |
|---|
| 1549 | n/a | |
|---|
| 1550 | n/a | name_end = wcschr(s, L'='); |
|---|
| 1551 | n/a | if (!name_end) { |
|---|
| 1552 | n/a | name = PyUnicode_FromWideChar(s, -1); |
|---|
| 1553 | n/a | value = Py_True; |
|---|
| 1554 | n/a | Py_INCREF(value); |
|---|
| 1555 | n/a | } |
|---|
| 1556 | n/a | else { |
|---|
| 1557 | n/a | name = PyUnicode_FromWideChar(s, name_end - s); |
|---|
| 1558 | n/a | value = PyUnicode_FromWideChar(name_end + 1, -1); |
|---|
| 1559 | n/a | } |
|---|
| 1560 | n/a | if (name == NULL || value == NULL) |
|---|
| 1561 | n/a | goto error; |
|---|
| 1562 | n/a | PyDict_SetItem(opts, name, value); |
|---|
| 1563 | n/a | Py_DECREF(name); |
|---|
| 1564 | n/a | Py_DECREF(value); |
|---|
| 1565 | n/a | return; |
|---|
| 1566 | n/a | |
|---|
| 1567 | n/a | error: |
|---|
| 1568 | n/a | Py_XDECREF(name); |
|---|
| 1569 | n/a | Py_XDECREF(value); |
|---|
| 1570 | n/a | /* No return value, therefore clear error state if possible */ |
|---|
| 1571 | n/a | if (_PyThreadState_UncheckedGet()) { |
|---|
| 1572 | n/a | PyErr_Clear(); |
|---|
| 1573 | n/a | } |
|---|
| 1574 | n/a | } |
|---|
| 1575 | n/a | |
|---|
| 1576 | n/a | PyObject * |
|---|
| 1577 | n/a | PySys_GetXOptions(void) |
|---|
| 1578 | n/a | { |
|---|
| 1579 | n/a | return get_xoptions(); |
|---|
| 1580 | n/a | } |
|---|
| 1581 | n/a | |
|---|
| 1582 | n/a | /* XXX This doc string is too long to be a single string literal in VC++ 5.0. |
|---|
| 1583 | n/a | Two literals concatenated works just fine. If you have a K&R compiler |
|---|
| 1584 | n/a | or other abomination that however *does* understand longer strings, |
|---|
| 1585 | n/a | get rid of the !!! comment in the middle and the quotes that surround it. */ |
|---|
| 1586 | n/a | PyDoc_VAR(sys_doc) = |
|---|
| 1587 | n/a | PyDoc_STR( |
|---|
| 1588 | n/a | "This module provides access to some objects used or maintained by the\n\ |
|---|
| 1589 | n/a | interpreter and to functions that interact strongly with the interpreter.\n\ |
|---|
| 1590 | n/a | \n\ |
|---|
| 1591 | n/a | Dynamic objects:\n\ |
|---|
| 1592 | n/a | \n\ |
|---|
| 1593 | n/a | argv -- command line arguments; argv[0] is the script pathname if known\n\ |
|---|
| 1594 | n/a | path -- module search path; path[0] is the script directory, else ''\n\ |
|---|
| 1595 | n/a | modules -- dictionary of loaded modules\n\ |
|---|
| 1596 | n/a | \n\ |
|---|
| 1597 | n/a | displayhook -- called to show results in an interactive session\n\ |
|---|
| 1598 | n/a | excepthook -- called to handle any uncaught exception other than SystemExit\n\ |
|---|
| 1599 | n/a | To customize printing in an interactive session or to install a custom\n\ |
|---|
| 1600 | n/a | top-level exception handler, assign other functions to replace these.\n\ |
|---|
| 1601 | n/a | \n\ |
|---|
| 1602 | n/a | stdin -- standard input file object; used by input()\n\ |
|---|
| 1603 | n/a | stdout -- standard output file object; used by print()\n\ |
|---|
| 1604 | n/a | stderr -- standard error object; used for error messages\n\ |
|---|
| 1605 | n/a | By assigning other file objects (or objects that behave like files)\n\ |
|---|
| 1606 | n/a | to these, it is possible to redirect all of the interpreter's I/O.\n\ |
|---|
| 1607 | n/a | \n\ |
|---|
| 1608 | n/a | last_type -- type of last uncaught exception\n\ |
|---|
| 1609 | n/a | last_value -- value of last uncaught exception\n\ |
|---|
| 1610 | n/a | last_traceback -- traceback of last uncaught exception\n\ |
|---|
| 1611 | n/a | These three are only available in an interactive session after a\n\ |
|---|
| 1612 | n/a | traceback has been printed.\n\ |
|---|
| 1613 | n/a | " |
|---|
| 1614 | n/a | ) |
|---|
| 1615 | n/a | /* concatenating string here */ |
|---|
| 1616 | n/a | PyDoc_STR( |
|---|
| 1617 | n/a | "\n\ |
|---|
| 1618 | n/a | Static objects:\n\ |
|---|
| 1619 | n/a | \n\ |
|---|
| 1620 | n/a | builtin_module_names -- tuple of module names built into this interpreter\n\ |
|---|
| 1621 | n/a | copyright -- copyright notice pertaining to this interpreter\n\ |
|---|
| 1622 | n/a | exec_prefix -- prefix used to find the machine-specific Python library\n\ |
|---|
| 1623 | n/a | executable -- absolute path of the executable binary of the Python interpreter\n\ |
|---|
| 1624 | n/a | float_info -- a struct sequence with information about the float implementation.\n\ |
|---|
| 1625 | n/a | float_repr_style -- string indicating the style of repr() output for floats\n\ |
|---|
| 1626 | n/a | hash_info -- a struct sequence with information about the hash algorithm.\n\ |
|---|
| 1627 | n/a | hexversion -- version information encoded as a single integer\n\ |
|---|
| 1628 | n/a | implementation -- Python implementation information.\n\ |
|---|
| 1629 | n/a | int_info -- a struct sequence with information about the int implementation.\n\ |
|---|
| 1630 | n/a | maxsize -- the largest supported length of containers.\n\ |
|---|
| 1631 | n/a | maxunicode -- the value of the largest Unicode code point\n\ |
|---|
| 1632 | n/a | platform -- platform identifier\n\ |
|---|
| 1633 | n/a | prefix -- prefix used to find the Python library\n\ |
|---|
| 1634 | n/a | thread_info -- a struct sequence with information about the thread implementation.\n\ |
|---|
| 1635 | n/a | version -- the version of this interpreter as a string\n\ |
|---|
| 1636 | n/a | version_info -- version information as a named tuple\n\ |
|---|
| 1637 | n/a | " |
|---|
| 1638 | n/a | ) |
|---|
| 1639 | n/a | #ifdef MS_COREDLL |
|---|
| 1640 | n/a | /* concatenating string here */ |
|---|
| 1641 | n/a | PyDoc_STR( |
|---|
| 1642 | n/a | "dllhandle -- [Windows only] integer handle of the Python DLL\n\ |
|---|
| 1643 | n/a | winver -- [Windows only] version number of the Python DLL\n\ |
|---|
| 1644 | n/a | " |
|---|
| 1645 | n/a | ) |
|---|
| 1646 | n/a | #endif /* MS_COREDLL */ |
|---|
| 1647 | n/a | #ifdef MS_WINDOWS |
|---|
| 1648 | n/a | /* concatenating string here */ |
|---|
| 1649 | n/a | PyDoc_STR( |
|---|
| 1650 | n/a | "_enablelegacywindowsfsencoding -- [Windows only] \n\ |
|---|
| 1651 | n/a | " |
|---|
| 1652 | n/a | ) |
|---|
| 1653 | n/a | #endif |
|---|
| 1654 | n/a | PyDoc_STR( |
|---|
| 1655 | n/a | "__stdin__ -- the original stdin; don't touch!\n\ |
|---|
| 1656 | n/a | __stdout__ -- the original stdout; don't touch!\n\ |
|---|
| 1657 | n/a | __stderr__ -- the original stderr; don't touch!\n\ |
|---|
| 1658 | n/a | __displayhook__ -- the original displayhook; don't touch!\n\ |
|---|
| 1659 | n/a | __excepthook__ -- the original excepthook; don't touch!\n\ |
|---|
| 1660 | n/a | \n\ |
|---|
| 1661 | n/a | Functions:\n\ |
|---|
| 1662 | n/a | \n\ |
|---|
| 1663 | n/a | displayhook() -- print an object to the screen, and save it in builtins._\n\ |
|---|
| 1664 | n/a | excepthook() -- print an exception and its traceback to sys.stderr\n\ |
|---|
| 1665 | n/a | exc_info() -- return thread-safe information about the current exception\n\ |
|---|
| 1666 | n/a | exit() -- exit the interpreter by raising SystemExit\n\ |
|---|
| 1667 | n/a | getdlopenflags() -- returns flags to be used for dlopen() calls\n\ |
|---|
| 1668 | n/a | getprofile() -- get the global profiling function\n\ |
|---|
| 1669 | n/a | getrefcount() -- return the reference count for an object (plus one :-)\n\ |
|---|
| 1670 | n/a | getrecursionlimit() -- return the max recursion depth for the interpreter\n\ |
|---|
| 1671 | n/a | getsizeof() -- return the size of an object in bytes\n\ |
|---|
| 1672 | n/a | gettrace() -- get the global debug tracing function\n\ |
|---|
| 1673 | n/a | setcheckinterval() -- control how often the interpreter checks for events\n\ |
|---|
| 1674 | n/a | setdlopenflags() -- set the flags to be used for dlopen() calls\n\ |
|---|
| 1675 | n/a | setprofile() -- set the global profiling function\n\ |
|---|
| 1676 | n/a | setrecursionlimit() -- set the max recursion depth for the interpreter\n\ |
|---|
| 1677 | n/a | settrace() -- set the global debug tracing function\n\ |
|---|
| 1678 | n/a | " |
|---|
| 1679 | n/a | ) |
|---|
| 1680 | n/a | /* end of sys_doc */ ; |
|---|
| 1681 | n/a | |
|---|
| 1682 | n/a | |
|---|
| 1683 | n/a | PyDoc_STRVAR(flags__doc__, |
|---|
| 1684 | n/a | "sys.flags\n\ |
|---|
| 1685 | n/a | \n\ |
|---|
| 1686 | n/a | Flags provided through command line arguments or environment vars."); |
|---|
| 1687 | n/a | |
|---|
| 1688 | n/a | static PyTypeObject FlagsType; |
|---|
| 1689 | n/a | |
|---|
| 1690 | n/a | static PyStructSequence_Field flags_fields[] = { |
|---|
| 1691 | n/a | {"debug", "-d"}, |
|---|
| 1692 | n/a | {"inspect", "-i"}, |
|---|
| 1693 | n/a | {"interactive", "-i"}, |
|---|
| 1694 | n/a | {"optimize", "-O or -OO"}, |
|---|
| 1695 | n/a | {"dont_write_bytecode", "-B"}, |
|---|
| 1696 | n/a | {"no_user_site", "-s"}, |
|---|
| 1697 | n/a | {"no_site", "-S"}, |
|---|
| 1698 | n/a | {"ignore_environment", "-E"}, |
|---|
| 1699 | n/a | {"verbose", "-v"}, |
|---|
| 1700 | n/a | /* {"unbuffered", "-u"}, */ |
|---|
| 1701 | n/a | /* {"skip_first", "-x"}, */ |
|---|
| 1702 | n/a | {"bytes_warning", "-b"}, |
|---|
| 1703 | n/a | {"quiet", "-q"}, |
|---|
| 1704 | n/a | {"hash_randomization", "-R"}, |
|---|
| 1705 | n/a | {"isolated", "-I"}, |
|---|
| 1706 | n/a | {0} |
|---|
| 1707 | n/a | }; |
|---|
| 1708 | n/a | |
|---|
| 1709 | n/a | static PyStructSequence_Desc flags_desc = { |
|---|
| 1710 | n/a | "sys.flags", /* name */ |
|---|
| 1711 | n/a | flags__doc__, /* doc */ |
|---|
| 1712 | n/a | flags_fields, /* fields */ |
|---|
| 1713 | n/a | 13 |
|---|
| 1714 | n/a | }; |
|---|
| 1715 | n/a | |
|---|
| 1716 | n/a | static PyObject* |
|---|
| 1717 | n/a | make_flags(void) |
|---|
| 1718 | n/a | { |
|---|
| 1719 | n/a | int pos = 0; |
|---|
| 1720 | n/a | PyObject *seq; |
|---|
| 1721 | n/a | |
|---|
| 1722 | n/a | seq = PyStructSequence_New(&FlagsType); |
|---|
| 1723 | n/a | if (seq == NULL) |
|---|
| 1724 | n/a | return NULL; |
|---|
| 1725 | n/a | |
|---|
| 1726 | n/a | #define SetFlag(flag) \ |
|---|
| 1727 | n/a | PyStructSequence_SET_ITEM(seq, pos++, PyLong_FromLong(flag)) |
|---|
| 1728 | n/a | |
|---|
| 1729 | n/a | SetFlag(Py_DebugFlag); |
|---|
| 1730 | n/a | SetFlag(Py_InspectFlag); |
|---|
| 1731 | n/a | SetFlag(Py_InteractiveFlag); |
|---|
| 1732 | n/a | SetFlag(Py_OptimizeFlag); |
|---|
| 1733 | n/a | SetFlag(Py_DontWriteBytecodeFlag); |
|---|
| 1734 | n/a | SetFlag(Py_NoUserSiteDirectory); |
|---|
| 1735 | n/a | SetFlag(Py_NoSiteFlag); |
|---|
| 1736 | n/a | SetFlag(Py_IgnoreEnvironmentFlag); |
|---|
| 1737 | n/a | SetFlag(Py_VerboseFlag); |
|---|
| 1738 | n/a | /* SetFlag(saw_unbuffered_flag); */ |
|---|
| 1739 | n/a | /* SetFlag(skipfirstline); */ |
|---|
| 1740 | n/a | SetFlag(Py_BytesWarningFlag); |
|---|
| 1741 | n/a | SetFlag(Py_QuietFlag); |
|---|
| 1742 | n/a | SetFlag(Py_HashRandomizationFlag); |
|---|
| 1743 | n/a | SetFlag(Py_IsolatedFlag); |
|---|
| 1744 | n/a | #undef SetFlag |
|---|
| 1745 | n/a | |
|---|
| 1746 | n/a | if (PyErr_Occurred()) { |
|---|
| 1747 | n/a | Py_DECREF(seq); |
|---|
| 1748 | n/a | return NULL; |
|---|
| 1749 | n/a | } |
|---|
| 1750 | n/a | return seq; |
|---|
| 1751 | n/a | } |
|---|
| 1752 | n/a | |
|---|
| 1753 | n/a | PyDoc_STRVAR(version_info__doc__, |
|---|
| 1754 | n/a | "sys.version_info\n\ |
|---|
| 1755 | n/a | \n\ |
|---|
| 1756 | n/a | Version information as a named tuple."); |
|---|
| 1757 | n/a | |
|---|
| 1758 | n/a | static PyTypeObject VersionInfoType; |
|---|
| 1759 | n/a | |
|---|
| 1760 | n/a | static PyStructSequence_Field version_info_fields[] = { |
|---|
| 1761 | n/a | {"major", "Major release number"}, |
|---|
| 1762 | n/a | {"minor", "Minor release number"}, |
|---|
| 1763 | n/a | {"micro", "Patch release number"}, |
|---|
| 1764 | n/a | {"releaselevel", "'alpha', 'beta', 'candidate', or 'final'"}, |
|---|
| 1765 | n/a | {"serial", "Serial release number"}, |
|---|
| 1766 | n/a | {0} |
|---|
| 1767 | n/a | }; |
|---|
| 1768 | n/a | |
|---|
| 1769 | n/a | static PyStructSequence_Desc version_info_desc = { |
|---|
| 1770 | n/a | "sys.version_info", /* name */ |
|---|
| 1771 | n/a | version_info__doc__, /* doc */ |
|---|
| 1772 | n/a | version_info_fields, /* fields */ |
|---|
| 1773 | n/a | 5 |
|---|
| 1774 | n/a | }; |
|---|
| 1775 | n/a | |
|---|
| 1776 | n/a | static PyObject * |
|---|
| 1777 | n/a | make_version_info(void) |
|---|
| 1778 | n/a | { |
|---|
| 1779 | n/a | PyObject *version_info; |
|---|
| 1780 | n/a | char *s; |
|---|
| 1781 | n/a | int pos = 0; |
|---|
| 1782 | n/a | |
|---|
| 1783 | n/a | version_info = PyStructSequence_New(&VersionInfoType); |
|---|
| 1784 | n/a | if (version_info == NULL) { |
|---|
| 1785 | n/a | return NULL; |
|---|
| 1786 | n/a | } |
|---|
| 1787 | n/a | |
|---|
| 1788 | n/a | /* |
|---|
| 1789 | n/a | * These release level checks are mutually exclusive and cover |
|---|
| 1790 | n/a | * the field, so don't get too fancy with the pre-processor! |
|---|
| 1791 | n/a | */ |
|---|
| 1792 | n/a | #if PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_ALPHA |
|---|
| 1793 | n/a | s = "alpha"; |
|---|
| 1794 | n/a | #elif PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_BETA |
|---|
| 1795 | n/a | s = "beta"; |
|---|
| 1796 | n/a | #elif PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_GAMMA |
|---|
| 1797 | n/a | s = "candidate"; |
|---|
| 1798 | n/a | #elif PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_FINAL |
|---|
| 1799 | n/a | s = "final"; |
|---|
| 1800 | n/a | #endif |
|---|
| 1801 | n/a | |
|---|
| 1802 | n/a | #define SetIntItem(flag) \ |
|---|
| 1803 | n/a | PyStructSequence_SET_ITEM(version_info, pos++, PyLong_FromLong(flag)) |
|---|
| 1804 | n/a | #define SetStrItem(flag) \ |
|---|
| 1805 | n/a | PyStructSequence_SET_ITEM(version_info, pos++, PyUnicode_FromString(flag)) |
|---|
| 1806 | n/a | |
|---|
| 1807 | n/a | SetIntItem(PY_MAJOR_VERSION); |
|---|
| 1808 | n/a | SetIntItem(PY_MINOR_VERSION); |
|---|
| 1809 | n/a | SetIntItem(PY_MICRO_VERSION); |
|---|
| 1810 | n/a | SetStrItem(s); |
|---|
| 1811 | n/a | SetIntItem(PY_RELEASE_SERIAL); |
|---|
| 1812 | n/a | #undef SetIntItem |
|---|
| 1813 | n/a | #undef SetStrItem |
|---|
| 1814 | n/a | |
|---|
| 1815 | n/a | if (PyErr_Occurred()) { |
|---|
| 1816 | n/a | Py_CLEAR(version_info); |
|---|
| 1817 | n/a | return NULL; |
|---|
| 1818 | n/a | } |
|---|
| 1819 | n/a | return version_info; |
|---|
| 1820 | n/a | } |
|---|
| 1821 | n/a | |
|---|
| 1822 | n/a | /* sys.implementation values */ |
|---|
| 1823 | n/a | #define NAME "cpython" |
|---|
| 1824 | n/a | const char *_PySys_ImplName = NAME; |
|---|
| 1825 | n/a | #define MAJOR Py_STRINGIFY(PY_MAJOR_VERSION) |
|---|
| 1826 | n/a | #define MINOR Py_STRINGIFY(PY_MINOR_VERSION) |
|---|
| 1827 | n/a | #define TAG NAME "-" MAJOR MINOR |
|---|
| 1828 | n/a | const char *_PySys_ImplCacheTag = TAG; |
|---|
| 1829 | n/a | #undef NAME |
|---|
| 1830 | n/a | #undef MAJOR |
|---|
| 1831 | n/a | #undef MINOR |
|---|
| 1832 | n/a | #undef TAG |
|---|
| 1833 | n/a | |
|---|
| 1834 | n/a | static PyObject * |
|---|
| 1835 | n/a | make_impl_info(PyObject *version_info) |
|---|
| 1836 | n/a | { |
|---|
| 1837 | n/a | int res; |
|---|
| 1838 | n/a | PyObject *impl_info, *value, *ns; |
|---|
| 1839 | n/a | |
|---|
| 1840 | n/a | impl_info = PyDict_New(); |
|---|
| 1841 | n/a | if (impl_info == NULL) |
|---|
| 1842 | n/a | return NULL; |
|---|
| 1843 | n/a | |
|---|
| 1844 | n/a | /* populate the dict */ |
|---|
| 1845 | n/a | |
|---|
| 1846 | n/a | value = PyUnicode_FromString(_PySys_ImplName); |
|---|
| 1847 | n/a | if (value == NULL) |
|---|
| 1848 | n/a | goto error; |
|---|
| 1849 | n/a | res = PyDict_SetItemString(impl_info, "name", value); |
|---|
| 1850 | n/a | Py_DECREF(value); |
|---|
| 1851 | n/a | if (res < 0) |
|---|
| 1852 | n/a | goto error; |
|---|
| 1853 | n/a | |
|---|
| 1854 | n/a | value = PyUnicode_FromString(_PySys_ImplCacheTag); |
|---|
| 1855 | n/a | if (value == NULL) |
|---|
| 1856 | n/a | goto error; |
|---|
| 1857 | n/a | res = PyDict_SetItemString(impl_info, "cache_tag", value); |
|---|
| 1858 | n/a | Py_DECREF(value); |
|---|
| 1859 | n/a | if (res < 0) |
|---|
| 1860 | n/a | goto error; |
|---|
| 1861 | n/a | |
|---|
| 1862 | n/a | res = PyDict_SetItemString(impl_info, "version", version_info); |
|---|
| 1863 | n/a | if (res < 0) |
|---|
| 1864 | n/a | goto error; |
|---|
| 1865 | n/a | |
|---|
| 1866 | n/a | value = PyLong_FromLong(PY_VERSION_HEX); |
|---|
| 1867 | n/a | if (value == NULL) |
|---|
| 1868 | n/a | goto error; |
|---|
| 1869 | n/a | res = PyDict_SetItemString(impl_info, "hexversion", value); |
|---|
| 1870 | n/a | Py_DECREF(value); |
|---|
| 1871 | n/a | if (res < 0) |
|---|
| 1872 | n/a | goto error; |
|---|
| 1873 | n/a | |
|---|
| 1874 | n/a | #ifdef MULTIARCH |
|---|
| 1875 | n/a | value = PyUnicode_FromString(MULTIARCH); |
|---|
| 1876 | n/a | if (value == NULL) |
|---|
| 1877 | n/a | goto error; |
|---|
| 1878 | n/a | res = PyDict_SetItemString(impl_info, "_multiarch", value); |
|---|
| 1879 | n/a | Py_DECREF(value); |
|---|
| 1880 | n/a | if (res < 0) |
|---|
| 1881 | n/a | goto error; |
|---|
| 1882 | n/a | #endif |
|---|
| 1883 | n/a | |
|---|
| 1884 | n/a | /* dict ready */ |
|---|
| 1885 | n/a | |
|---|
| 1886 | n/a | ns = _PyNamespace_New(impl_info); |
|---|
| 1887 | n/a | Py_DECREF(impl_info); |
|---|
| 1888 | n/a | return ns; |
|---|
| 1889 | n/a | |
|---|
| 1890 | n/a | error: |
|---|
| 1891 | n/a | Py_CLEAR(impl_info); |
|---|
| 1892 | n/a | return NULL; |
|---|
| 1893 | n/a | } |
|---|
| 1894 | n/a | |
|---|
| 1895 | n/a | static struct PyModuleDef sysmodule = { |
|---|
| 1896 | n/a | PyModuleDef_HEAD_INIT, |
|---|
| 1897 | n/a | "sys", |
|---|
| 1898 | n/a | sys_doc, |
|---|
| 1899 | n/a | -1, /* multiple "initialization" just copies the module dict. */ |
|---|
| 1900 | n/a | sys_methods, |
|---|
| 1901 | n/a | NULL, |
|---|
| 1902 | n/a | NULL, |
|---|
| 1903 | n/a | NULL, |
|---|
| 1904 | n/a | NULL |
|---|
| 1905 | n/a | }; |
|---|
| 1906 | n/a | |
|---|
| 1907 | n/a | PyObject * |
|---|
| 1908 | n/a | _PySys_Init(void) |
|---|
| 1909 | n/a | { |
|---|
| 1910 | n/a | PyObject *m, *sysdict, *version_info; |
|---|
| 1911 | n/a | int res; |
|---|
| 1912 | n/a | |
|---|
| 1913 | n/a | m = PyModule_Create(&sysmodule); |
|---|
| 1914 | n/a | if (m == NULL) |
|---|
| 1915 | n/a | return NULL; |
|---|
| 1916 | n/a | sysdict = PyModule_GetDict(m); |
|---|
| 1917 | n/a | #define SET_SYS_FROM_STRING_BORROW(key, value) \ |
|---|
| 1918 | n/a | do { \ |
|---|
| 1919 | n/a | PyObject *v = (value); \ |
|---|
| 1920 | n/a | if (v == NULL) \ |
|---|
| 1921 | n/a | return NULL; \ |
|---|
| 1922 | n/a | res = PyDict_SetItemString(sysdict, key, v); \ |
|---|
| 1923 | n/a | if (res < 0) { \ |
|---|
| 1924 | n/a | return NULL; \ |
|---|
| 1925 | n/a | } \ |
|---|
| 1926 | n/a | } while (0) |
|---|
| 1927 | n/a | #define SET_SYS_FROM_STRING(key, value) \ |
|---|
| 1928 | n/a | do { \ |
|---|
| 1929 | n/a | PyObject *v = (value); \ |
|---|
| 1930 | n/a | if (v == NULL) \ |
|---|
| 1931 | n/a | return NULL; \ |
|---|
| 1932 | n/a | res = PyDict_SetItemString(sysdict, key, v); \ |
|---|
| 1933 | n/a | Py_DECREF(v); \ |
|---|
| 1934 | n/a | if (res < 0) { \ |
|---|
| 1935 | n/a | return NULL; \ |
|---|
| 1936 | n/a | } \ |
|---|
| 1937 | n/a | } while (0) |
|---|
| 1938 | n/a | |
|---|
| 1939 | n/a | /* Check that stdin is not a directory |
|---|
| 1940 | n/a | Using shell redirection, you can redirect stdin to a directory, |
|---|
| 1941 | n/a | crashing the Python interpreter. Catch this common mistake here |
|---|
| 1942 | n/a | and output a useful error message. Note that under MS Windows, |
|---|
| 1943 | n/a | the shell already prevents that. */ |
|---|
| 1944 | n/a | #if !defined(MS_WINDOWS) |
|---|
| 1945 | n/a | { |
|---|
| 1946 | n/a | struct _Py_stat_struct sb; |
|---|
| 1947 | n/a | if (_Py_fstat_noraise(fileno(stdin), &sb) == 0 && |
|---|
| 1948 | n/a | S_ISDIR(sb.st_mode)) { |
|---|
| 1949 | n/a | /* There's nothing more we can do. */ |
|---|
| 1950 | n/a | /* Py_FatalError() will core dump, so just exit. */ |
|---|
| 1951 | n/a | PySys_WriteStderr("Python error: <stdin> is a directory, cannot continue\n"); |
|---|
| 1952 | n/a | exit(EXIT_FAILURE); |
|---|
| 1953 | n/a | } |
|---|
| 1954 | n/a | } |
|---|
| 1955 | n/a | #endif |
|---|
| 1956 | n/a | |
|---|
| 1957 | n/a | /* stdin/stdout/stderr are set in pylifecycle.c */ |
|---|
| 1958 | n/a | |
|---|
| 1959 | n/a | SET_SYS_FROM_STRING_BORROW("__displayhook__", |
|---|
| 1960 | n/a | PyDict_GetItemString(sysdict, "displayhook")); |
|---|
| 1961 | n/a | SET_SYS_FROM_STRING_BORROW("__excepthook__", |
|---|
| 1962 | n/a | PyDict_GetItemString(sysdict, "excepthook")); |
|---|
| 1963 | n/a | SET_SYS_FROM_STRING("version", |
|---|
| 1964 | n/a | PyUnicode_FromString(Py_GetVersion())); |
|---|
| 1965 | n/a | SET_SYS_FROM_STRING("hexversion", |
|---|
| 1966 | n/a | PyLong_FromLong(PY_VERSION_HEX)); |
|---|
| 1967 | n/a | SET_SYS_FROM_STRING("_mercurial", |
|---|
| 1968 | n/a | Py_BuildValue("(szz)", "CPython", _Py_hgidentifier(), |
|---|
| 1969 | n/a | _Py_hgversion())); |
|---|
| 1970 | n/a | SET_SYS_FROM_STRING("dont_write_bytecode", |
|---|
| 1971 | n/a | PyBool_FromLong(Py_DontWriteBytecodeFlag)); |
|---|
| 1972 | n/a | SET_SYS_FROM_STRING("api_version", |
|---|
| 1973 | n/a | PyLong_FromLong(PYTHON_API_VERSION)); |
|---|
| 1974 | n/a | SET_SYS_FROM_STRING("copyright", |
|---|
| 1975 | n/a | PyUnicode_FromString(Py_GetCopyright())); |
|---|
| 1976 | n/a | SET_SYS_FROM_STRING("platform", |
|---|
| 1977 | n/a | PyUnicode_FromString(Py_GetPlatform())); |
|---|
| 1978 | n/a | SET_SYS_FROM_STRING("executable", |
|---|
| 1979 | n/a | PyUnicode_FromWideChar( |
|---|
| 1980 | n/a | Py_GetProgramFullPath(), -1)); |
|---|
| 1981 | n/a | SET_SYS_FROM_STRING("prefix", |
|---|
| 1982 | n/a | PyUnicode_FromWideChar(Py_GetPrefix(), -1)); |
|---|
| 1983 | n/a | SET_SYS_FROM_STRING("exec_prefix", |
|---|
| 1984 | n/a | PyUnicode_FromWideChar(Py_GetExecPrefix(), -1)); |
|---|
| 1985 | n/a | SET_SYS_FROM_STRING("base_prefix", |
|---|
| 1986 | n/a | PyUnicode_FromWideChar(Py_GetPrefix(), -1)); |
|---|
| 1987 | n/a | SET_SYS_FROM_STRING("base_exec_prefix", |
|---|
| 1988 | n/a | PyUnicode_FromWideChar(Py_GetExecPrefix(), -1)); |
|---|
| 1989 | n/a | SET_SYS_FROM_STRING("maxsize", |
|---|
| 1990 | n/a | PyLong_FromSsize_t(PY_SSIZE_T_MAX)); |
|---|
| 1991 | n/a | SET_SYS_FROM_STRING("float_info", |
|---|
| 1992 | n/a | PyFloat_GetInfo()); |
|---|
| 1993 | n/a | SET_SYS_FROM_STRING("int_info", |
|---|
| 1994 | n/a | PyLong_GetInfo()); |
|---|
| 1995 | n/a | /* initialize hash_info */ |
|---|
| 1996 | n/a | if (Hash_InfoType.tp_name == NULL) { |
|---|
| 1997 | n/a | if (PyStructSequence_InitType2(&Hash_InfoType, &hash_info_desc) < 0) |
|---|
| 1998 | n/a | return NULL; |
|---|
| 1999 | n/a | } |
|---|
| 2000 | n/a | SET_SYS_FROM_STRING("hash_info", |
|---|
| 2001 | n/a | get_hash_info()); |
|---|
| 2002 | n/a | SET_SYS_FROM_STRING("maxunicode", |
|---|
| 2003 | n/a | PyLong_FromLong(0x10FFFF)); |
|---|
| 2004 | n/a | SET_SYS_FROM_STRING("builtin_module_names", |
|---|
| 2005 | n/a | list_builtin_module_names()); |
|---|
| 2006 | n/a | #if PY_BIG_ENDIAN |
|---|
| 2007 | n/a | SET_SYS_FROM_STRING("byteorder", |
|---|
| 2008 | n/a | PyUnicode_FromString("big")); |
|---|
| 2009 | n/a | #else |
|---|
| 2010 | n/a | SET_SYS_FROM_STRING("byteorder", |
|---|
| 2011 | n/a | PyUnicode_FromString("little")); |
|---|
| 2012 | n/a | #endif |
|---|
| 2013 | n/a | |
|---|
| 2014 | n/a | #ifdef MS_COREDLL |
|---|
| 2015 | n/a | SET_SYS_FROM_STRING("dllhandle", |
|---|
| 2016 | n/a | PyLong_FromVoidPtr(PyWin_DLLhModule)); |
|---|
| 2017 | n/a | SET_SYS_FROM_STRING("winver", |
|---|
| 2018 | n/a | PyUnicode_FromString(PyWin_DLLVersionString)); |
|---|
| 2019 | n/a | #endif |
|---|
| 2020 | n/a | #ifdef ABIFLAGS |
|---|
| 2021 | n/a | SET_SYS_FROM_STRING("abiflags", |
|---|
| 2022 | n/a | PyUnicode_FromString(ABIFLAGS)); |
|---|
| 2023 | n/a | #endif |
|---|
| 2024 | n/a | if (warnoptions == NULL) { |
|---|
| 2025 | n/a | warnoptions = PyList_New(0); |
|---|
| 2026 | n/a | if (warnoptions == NULL) |
|---|
| 2027 | n/a | return NULL; |
|---|
| 2028 | n/a | } |
|---|
| 2029 | n/a | else { |
|---|
| 2030 | n/a | Py_INCREF(warnoptions); |
|---|
| 2031 | n/a | } |
|---|
| 2032 | n/a | SET_SYS_FROM_STRING_BORROW("warnoptions", warnoptions); |
|---|
| 2033 | n/a | |
|---|
| 2034 | n/a | SET_SYS_FROM_STRING_BORROW("_xoptions", get_xoptions()); |
|---|
| 2035 | n/a | |
|---|
| 2036 | n/a | /* version_info */ |
|---|
| 2037 | n/a | if (VersionInfoType.tp_name == NULL) { |
|---|
| 2038 | n/a | if (PyStructSequence_InitType2(&VersionInfoType, |
|---|
| 2039 | n/a | &version_info_desc) < 0) |
|---|
| 2040 | n/a | return NULL; |
|---|
| 2041 | n/a | } |
|---|
| 2042 | n/a | version_info = make_version_info(); |
|---|
| 2043 | n/a | SET_SYS_FROM_STRING("version_info", version_info); |
|---|
| 2044 | n/a | /* prevent user from creating new instances */ |
|---|
| 2045 | n/a | VersionInfoType.tp_init = NULL; |
|---|
| 2046 | n/a | VersionInfoType.tp_new = NULL; |
|---|
| 2047 | n/a | res = PyDict_DelItemString(VersionInfoType.tp_dict, "__new__"); |
|---|
| 2048 | n/a | if (res < 0 && PyErr_ExceptionMatches(PyExc_KeyError)) |
|---|
| 2049 | n/a | PyErr_Clear(); |
|---|
| 2050 | n/a | |
|---|
| 2051 | n/a | /* implementation */ |
|---|
| 2052 | n/a | SET_SYS_FROM_STRING("implementation", make_impl_info(version_info)); |
|---|
| 2053 | n/a | |
|---|
| 2054 | n/a | /* flags */ |
|---|
| 2055 | n/a | if (FlagsType.tp_name == 0) { |
|---|
| 2056 | n/a | if (PyStructSequence_InitType2(&FlagsType, &flags_desc) < 0) |
|---|
| 2057 | n/a | return NULL; |
|---|
| 2058 | n/a | } |
|---|
| 2059 | n/a | SET_SYS_FROM_STRING("flags", make_flags()); |
|---|
| 2060 | n/a | /* prevent user from creating new instances */ |
|---|
| 2061 | n/a | FlagsType.tp_init = NULL; |
|---|
| 2062 | n/a | FlagsType.tp_new = NULL; |
|---|
| 2063 | n/a | res = PyDict_DelItemString(FlagsType.tp_dict, "__new__"); |
|---|
| 2064 | n/a | if (res < 0 && PyErr_ExceptionMatches(PyExc_KeyError)) |
|---|
| 2065 | n/a | PyErr_Clear(); |
|---|
| 2066 | n/a | |
|---|
| 2067 | n/a | #if defined(MS_WINDOWS) |
|---|
| 2068 | n/a | /* getwindowsversion */ |
|---|
| 2069 | n/a | if (WindowsVersionType.tp_name == 0) |
|---|
| 2070 | n/a | if (PyStructSequence_InitType2(&WindowsVersionType, |
|---|
| 2071 | n/a | &windows_version_desc) < 0) |
|---|
| 2072 | n/a | return NULL; |
|---|
| 2073 | n/a | /* prevent user from creating new instances */ |
|---|
| 2074 | n/a | WindowsVersionType.tp_init = NULL; |
|---|
| 2075 | n/a | WindowsVersionType.tp_new = NULL; |
|---|
| 2076 | n/a | res = PyDict_DelItemString(WindowsVersionType.tp_dict, "__new__"); |
|---|
| 2077 | n/a | if (res < 0 && PyErr_ExceptionMatches(PyExc_KeyError)) |
|---|
| 2078 | n/a | PyErr_Clear(); |
|---|
| 2079 | n/a | #endif |
|---|
| 2080 | n/a | |
|---|
| 2081 | n/a | /* float repr style: 0.03 (short) vs 0.029999999999999999 (legacy) */ |
|---|
| 2082 | n/a | #ifndef PY_NO_SHORT_FLOAT_REPR |
|---|
| 2083 | n/a | SET_SYS_FROM_STRING("float_repr_style", |
|---|
| 2084 | n/a | PyUnicode_FromString("short")); |
|---|
| 2085 | n/a | #else |
|---|
| 2086 | n/a | SET_SYS_FROM_STRING("float_repr_style", |
|---|
| 2087 | n/a | PyUnicode_FromString("legacy")); |
|---|
| 2088 | n/a | #endif |
|---|
| 2089 | n/a | |
|---|
| 2090 | n/a | #ifdef WITH_THREAD |
|---|
| 2091 | n/a | SET_SYS_FROM_STRING("thread_info", PyThread_GetInfo()); |
|---|
| 2092 | n/a | #endif |
|---|
| 2093 | n/a | |
|---|
| 2094 | n/a | /* initialize asyncgen_hooks */ |
|---|
| 2095 | n/a | if (AsyncGenHooksType.tp_name == NULL) { |
|---|
| 2096 | n/a | if (PyStructSequence_InitType2( |
|---|
| 2097 | n/a | &AsyncGenHooksType, &asyncgen_hooks_desc) < 0) { |
|---|
| 2098 | n/a | return NULL; |
|---|
| 2099 | n/a | } |
|---|
| 2100 | n/a | } |
|---|
| 2101 | n/a | |
|---|
| 2102 | n/a | #undef SET_SYS_FROM_STRING |
|---|
| 2103 | n/a | #undef SET_SYS_FROM_STRING_BORROW |
|---|
| 2104 | n/a | if (PyErr_Occurred()) |
|---|
| 2105 | n/a | return NULL; |
|---|
| 2106 | n/a | return m; |
|---|
| 2107 | n/a | } |
|---|
| 2108 | n/a | |
|---|
| 2109 | n/a | static PyObject * |
|---|
| 2110 | n/a | makepathobject(const wchar_t *path, wchar_t delim) |
|---|
| 2111 | n/a | { |
|---|
| 2112 | n/a | int i, n; |
|---|
| 2113 | n/a | const wchar_t *p; |
|---|
| 2114 | n/a | PyObject *v, *w; |
|---|
| 2115 | n/a | |
|---|
| 2116 | n/a | n = 1; |
|---|
| 2117 | n/a | p = path; |
|---|
| 2118 | n/a | while ((p = wcschr(p, delim)) != NULL) { |
|---|
| 2119 | n/a | n++; |
|---|
| 2120 | n/a | p++; |
|---|
| 2121 | n/a | } |
|---|
| 2122 | n/a | v = PyList_New(n); |
|---|
| 2123 | n/a | if (v == NULL) |
|---|
| 2124 | n/a | return NULL; |
|---|
| 2125 | n/a | for (i = 0; ; i++) { |
|---|
| 2126 | n/a | p = wcschr(path, delim); |
|---|
| 2127 | n/a | if (p == NULL) |
|---|
| 2128 | n/a | p = path + wcslen(path); /* End of string */ |
|---|
| 2129 | n/a | w = PyUnicode_FromWideChar(path, (Py_ssize_t)(p - path)); |
|---|
| 2130 | n/a | if (w == NULL) { |
|---|
| 2131 | n/a | Py_DECREF(v); |
|---|
| 2132 | n/a | return NULL; |
|---|
| 2133 | n/a | } |
|---|
| 2134 | n/a | PyList_SetItem(v, i, w); |
|---|
| 2135 | n/a | if (*p == '\0') |
|---|
| 2136 | n/a | break; |
|---|
| 2137 | n/a | path = p+1; |
|---|
| 2138 | n/a | } |
|---|
| 2139 | n/a | return v; |
|---|
| 2140 | n/a | } |
|---|
| 2141 | n/a | |
|---|
| 2142 | n/a | void |
|---|
| 2143 | n/a | PySys_SetPath(const wchar_t *path) |
|---|
| 2144 | n/a | { |
|---|
| 2145 | n/a | PyObject *v; |
|---|
| 2146 | n/a | if ((v = makepathobject(path, DELIM)) == NULL) |
|---|
| 2147 | n/a | Py_FatalError("can't create sys.path"); |
|---|
| 2148 | n/a | if (_PySys_SetObjectId(&PyId_path, v) != 0) |
|---|
| 2149 | n/a | Py_FatalError("can't assign sys.path"); |
|---|
| 2150 | n/a | Py_DECREF(v); |
|---|
| 2151 | n/a | } |
|---|
| 2152 | n/a | |
|---|
| 2153 | n/a | static PyObject * |
|---|
| 2154 | n/a | makeargvobject(int argc, wchar_t **argv) |
|---|
| 2155 | n/a | { |
|---|
| 2156 | n/a | PyObject *av; |
|---|
| 2157 | n/a | if (argc <= 0 || argv == NULL) { |
|---|
| 2158 | n/a | /* Ensure at least one (empty) argument is seen */ |
|---|
| 2159 | n/a | static wchar_t *empty_argv[1] = {L""}; |
|---|
| 2160 | n/a | argv = empty_argv; |
|---|
| 2161 | n/a | argc = 1; |
|---|
| 2162 | n/a | } |
|---|
| 2163 | n/a | av = PyList_New(argc); |
|---|
| 2164 | n/a | if (av != NULL) { |
|---|
| 2165 | n/a | int i; |
|---|
| 2166 | n/a | for (i = 0; i < argc; i++) { |
|---|
| 2167 | n/a | PyObject *v = PyUnicode_FromWideChar(argv[i], -1); |
|---|
| 2168 | n/a | if (v == NULL) { |
|---|
| 2169 | n/a | Py_DECREF(av); |
|---|
| 2170 | n/a | av = NULL; |
|---|
| 2171 | n/a | break; |
|---|
| 2172 | n/a | } |
|---|
| 2173 | n/a | PyList_SetItem(av, i, v); |
|---|
| 2174 | n/a | } |
|---|
| 2175 | n/a | } |
|---|
| 2176 | n/a | return av; |
|---|
| 2177 | n/a | } |
|---|
| 2178 | n/a | |
|---|
| 2179 | n/a | #define _HAVE_SCRIPT_ARGUMENT(argc, argv) \ |
|---|
| 2180 | n/a | (argc > 0 && argv0 != NULL && \ |
|---|
| 2181 | n/a | wcscmp(argv0, L"-c") != 0 && wcscmp(argv0, L"-m") != 0) |
|---|
| 2182 | n/a | |
|---|
| 2183 | n/a | static void |
|---|
| 2184 | n/a | sys_update_path(int argc, wchar_t **argv) |
|---|
| 2185 | n/a | { |
|---|
| 2186 | n/a | wchar_t *argv0; |
|---|
| 2187 | n/a | wchar_t *p = NULL; |
|---|
| 2188 | n/a | Py_ssize_t n = 0; |
|---|
| 2189 | n/a | PyObject *a; |
|---|
| 2190 | n/a | PyObject *path; |
|---|
| 2191 | n/a | #ifdef HAVE_READLINK |
|---|
| 2192 | n/a | wchar_t link[MAXPATHLEN+1]; |
|---|
| 2193 | n/a | wchar_t argv0copy[2*MAXPATHLEN+1]; |
|---|
| 2194 | n/a | int nr = 0; |
|---|
| 2195 | n/a | #endif |
|---|
| 2196 | n/a | #if defined(HAVE_REALPATH) |
|---|
| 2197 | n/a | wchar_t fullpath[MAXPATHLEN]; |
|---|
| 2198 | n/a | #elif defined(MS_WINDOWS) |
|---|
| 2199 | n/a | wchar_t fullpath[MAX_PATH]; |
|---|
| 2200 | n/a | #endif |
|---|
| 2201 | n/a | |
|---|
| 2202 | n/a | path = _PySys_GetObjectId(&PyId_path); |
|---|
| 2203 | n/a | if (path == NULL) |
|---|
| 2204 | n/a | return; |
|---|
| 2205 | n/a | |
|---|
| 2206 | n/a | argv0 = argv[0]; |
|---|
| 2207 | n/a | |
|---|
| 2208 | n/a | #ifdef HAVE_READLINK |
|---|
| 2209 | n/a | if (_HAVE_SCRIPT_ARGUMENT(argc, argv)) |
|---|
| 2210 | n/a | nr = _Py_wreadlink(argv0, link, MAXPATHLEN); |
|---|
| 2211 | n/a | if (nr > 0) { |
|---|
| 2212 | n/a | /* It's a symlink */ |
|---|
| 2213 | n/a | link[nr] = '\0'; |
|---|
| 2214 | n/a | if (link[0] == SEP) |
|---|
| 2215 | n/a | argv0 = link; /* Link to absolute path */ |
|---|
| 2216 | n/a | else if (wcschr(link, SEP) == NULL) |
|---|
| 2217 | n/a | ; /* Link without path */ |
|---|
| 2218 | n/a | else { |
|---|
| 2219 | n/a | /* Must join(dirname(argv0), link) */ |
|---|
| 2220 | n/a | wchar_t *q = wcsrchr(argv0, SEP); |
|---|
| 2221 | n/a | if (q == NULL) |
|---|
| 2222 | n/a | argv0 = link; /* argv0 without path */ |
|---|
| 2223 | n/a | else { |
|---|
| 2224 | n/a | /* Must make a copy, argv0copy has room for 2 * MAXPATHLEN */ |
|---|
| 2225 | n/a | wcsncpy(argv0copy, argv0, MAXPATHLEN); |
|---|
| 2226 | n/a | q = wcsrchr(argv0copy, SEP); |
|---|
| 2227 | n/a | wcsncpy(q+1, link, MAXPATHLEN); |
|---|
| 2228 | n/a | q[MAXPATHLEN + 1] = L'\0'; |
|---|
| 2229 | n/a | argv0 = argv0copy; |
|---|
| 2230 | n/a | } |
|---|
| 2231 | n/a | } |
|---|
| 2232 | n/a | } |
|---|
| 2233 | n/a | #endif /* HAVE_READLINK */ |
|---|
| 2234 | n/a | #if SEP == '\\' /* Special case for MS filename syntax */ |
|---|
| 2235 | n/a | if (_HAVE_SCRIPT_ARGUMENT(argc, argv)) { |
|---|
| 2236 | n/a | wchar_t *q; |
|---|
| 2237 | n/a | #if defined(MS_WINDOWS) |
|---|
| 2238 | n/a | /* Replace the first element in argv with the full path. */ |
|---|
| 2239 | n/a | wchar_t *ptemp; |
|---|
| 2240 | n/a | if (GetFullPathNameW(argv0, |
|---|
| 2241 | n/a | Py_ARRAY_LENGTH(fullpath), |
|---|
| 2242 | n/a | fullpath, |
|---|
| 2243 | n/a | &ptemp)) { |
|---|
| 2244 | n/a | argv0 = fullpath; |
|---|
| 2245 | n/a | } |
|---|
| 2246 | n/a | #endif |
|---|
| 2247 | n/a | p = wcsrchr(argv0, SEP); |
|---|
| 2248 | n/a | /* Test for alternate separator */ |
|---|
| 2249 | n/a | q = wcsrchr(p ? p : argv0, '/'); |
|---|
| 2250 | n/a | if (q != NULL) |
|---|
| 2251 | n/a | p = q; |
|---|
| 2252 | n/a | if (p != NULL) { |
|---|
| 2253 | n/a | n = p + 1 - argv0; |
|---|
| 2254 | n/a | if (n > 1 && p[-1] != ':') |
|---|
| 2255 | n/a | n--; /* Drop trailing separator */ |
|---|
| 2256 | n/a | } |
|---|
| 2257 | n/a | } |
|---|
| 2258 | n/a | #else /* All other filename syntaxes */ |
|---|
| 2259 | n/a | if (_HAVE_SCRIPT_ARGUMENT(argc, argv)) { |
|---|
| 2260 | n/a | #if defined(HAVE_REALPATH) |
|---|
| 2261 | n/a | if (_Py_wrealpath(argv0, fullpath, Py_ARRAY_LENGTH(fullpath))) { |
|---|
| 2262 | n/a | argv0 = fullpath; |
|---|
| 2263 | n/a | } |
|---|
| 2264 | n/a | #endif |
|---|
| 2265 | n/a | p = wcsrchr(argv0, SEP); |
|---|
| 2266 | n/a | } |
|---|
| 2267 | n/a | if (p != NULL) { |
|---|
| 2268 | n/a | n = p + 1 - argv0; |
|---|
| 2269 | n/a | #if SEP == '/' /* Special case for Unix filename syntax */ |
|---|
| 2270 | n/a | if (n > 1) |
|---|
| 2271 | n/a | n--; /* Drop trailing separator */ |
|---|
| 2272 | n/a | #endif /* Unix */ |
|---|
| 2273 | n/a | } |
|---|
| 2274 | n/a | #endif /* All others */ |
|---|
| 2275 | n/a | a = PyUnicode_FromWideChar(argv0, n); |
|---|
| 2276 | n/a | if (a == NULL) |
|---|
| 2277 | n/a | Py_FatalError("no mem for sys.path insertion"); |
|---|
| 2278 | n/a | if (PyList_Insert(path, 0, a) < 0) |
|---|
| 2279 | n/a | Py_FatalError("sys.path.insert(0) failed"); |
|---|
| 2280 | n/a | Py_DECREF(a); |
|---|
| 2281 | n/a | } |
|---|
| 2282 | n/a | |
|---|
| 2283 | n/a | void |
|---|
| 2284 | n/a | PySys_SetArgvEx(int argc, wchar_t **argv, int updatepath) |
|---|
| 2285 | n/a | { |
|---|
| 2286 | n/a | PyObject *av = makeargvobject(argc, argv); |
|---|
| 2287 | n/a | if (av == NULL) |
|---|
| 2288 | n/a | Py_FatalError("no mem for sys.argv"); |
|---|
| 2289 | n/a | if (PySys_SetObject("argv", av) != 0) |
|---|
| 2290 | n/a | Py_FatalError("can't assign sys.argv"); |
|---|
| 2291 | n/a | Py_DECREF(av); |
|---|
| 2292 | n/a | if (updatepath) |
|---|
| 2293 | n/a | sys_update_path(argc, argv); |
|---|
| 2294 | n/a | } |
|---|
| 2295 | n/a | |
|---|
| 2296 | n/a | void |
|---|
| 2297 | n/a | PySys_SetArgv(int argc, wchar_t **argv) |
|---|
| 2298 | n/a | { |
|---|
| 2299 | n/a | PySys_SetArgvEx(argc, argv, Py_IsolatedFlag == 0); |
|---|
| 2300 | n/a | } |
|---|
| 2301 | n/a | |
|---|
| 2302 | n/a | /* Reimplementation of PyFile_WriteString() no calling indirectly |
|---|
| 2303 | n/a | PyErr_CheckSignals(): avoid the call to PyObject_Str(). */ |
|---|
| 2304 | n/a | |
|---|
| 2305 | n/a | static int |
|---|
| 2306 | n/a | sys_pyfile_write_unicode(PyObject *unicode, PyObject *file) |
|---|
| 2307 | n/a | { |
|---|
| 2308 | n/a | PyObject *writer = NULL, *result = NULL; |
|---|
| 2309 | n/a | int err; |
|---|
| 2310 | n/a | |
|---|
| 2311 | n/a | if (file == NULL) |
|---|
| 2312 | n/a | return -1; |
|---|
| 2313 | n/a | |
|---|
| 2314 | n/a | writer = _PyObject_GetAttrId(file, &PyId_write); |
|---|
| 2315 | n/a | if (writer == NULL) |
|---|
| 2316 | n/a | goto error; |
|---|
| 2317 | n/a | |
|---|
| 2318 | n/a | result = PyObject_CallFunctionObjArgs(writer, unicode, NULL); |
|---|
| 2319 | n/a | if (result == NULL) { |
|---|
| 2320 | n/a | goto error; |
|---|
| 2321 | n/a | } else { |
|---|
| 2322 | n/a | err = 0; |
|---|
| 2323 | n/a | goto finally; |
|---|
| 2324 | n/a | } |
|---|
| 2325 | n/a | |
|---|
| 2326 | n/a | error: |
|---|
| 2327 | n/a | err = -1; |
|---|
| 2328 | n/a | finally: |
|---|
| 2329 | n/a | Py_XDECREF(writer); |
|---|
| 2330 | n/a | Py_XDECREF(result); |
|---|
| 2331 | n/a | return err; |
|---|
| 2332 | n/a | } |
|---|
| 2333 | n/a | |
|---|
| 2334 | n/a | static int |
|---|
| 2335 | n/a | sys_pyfile_write(const char *text, PyObject *file) |
|---|
| 2336 | n/a | { |
|---|
| 2337 | n/a | PyObject *unicode = NULL; |
|---|
| 2338 | n/a | int err; |
|---|
| 2339 | n/a | |
|---|
| 2340 | n/a | if (file == NULL) |
|---|
| 2341 | n/a | return -1; |
|---|
| 2342 | n/a | |
|---|
| 2343 | n/a | unicode = PyUnicode_FromString(text); |
|---|
| 2344 | n/a | if (unicode == NULL) |
|---|
| 2345 | n/a | return -1; |
|---|
| 2346 | n/a | |
|---|
| 2347 | n/a | err = sys_pyfile_write_unicode(unicode, file); |
|---|
| 2348 | n/a | Py_DECREF(unicode); |
|---|
| 2349 | n/a | return err; |
|---|
| 2350 | n/a | } |
|---|
| 2351 | n/a | |
|---|
| 2352 | n/a | /* APIs to write to sys.stdout or sys.stderr using a printf-like interface. |
|---|
| 2353 | n/a | Adapted from code submitted by Just van Rossum. |
|---|
| 2354 | n/a | |
|---|
| 2355 | n/a | PySys_WriteStdout(format, ...) |
|---|
| 2356 | n/a | PySys_WriteStderr(format, ...) |
|---|
| 2357 | n/a | |
|---|
| 2358 | n/a | The first function writes to sys.stdout; the second to sys.stderr. When |
|---|
| 2359 | n/a | there is a problem, they write to the real (C level) stdout or stderr; |
|---|
| 2360 | n/a | no exceptions are raised. |
|---|
| 2361 | n/a | |
|---|
| 2362 | n/a | PyErr_CheckSignals() is not called to avoid the execution of the Python |
|---|
| 2363 | n/a | signal handlers: they may raise a new exception whereas sys_write() |
|---|
| 2364 | n/a | ignores all exceptions. |
|---|
| 2365 | n/a | |
|---|
| 2366 | n/a | Both take a printf-style format string as their first argument followed |
|---|
| 2367 | n/a | by a variable length argument list determined by the format string. |
|---|
| 2368 | n/a | |
|---|
| 2369 | n/a | *** WARNING *** |
|---|
| 2370 | n/a | |
|---|
| 2371 | n/a | The format should limit the total size of the formatted output string to |
|---|
| 2372 | n/a | 1000 bytes. In particular, this means that no unrestricted "%s" formats |
|---|
| 2373 | n/a | should occur; these should be limited using "%.<N>s where <N> is a |
|---|
| 2374 | n/a | decimal number calculated so that <N> plus the maximum size of other |
|---|
| 2375 | n/a | formatted text does not exceed 1000 bytes. Also watch out for "%f", |
|---|
| 2376 | n/a | which can print hundreds of digits for very large numbers. |
|---|
| 2377 | n/a | |
|---|
| 2378 | n/a | */ |
|---|
| 2379 | n/a | |
|---|
| 2380 | n/a | static void |
|---|
| 2381 | n/a | sys_write(_Py_Identifier *key, FILE *fp, const char *format, va_list va) |
|---|
| 2382 | n/a | { |
|---|
| 2383 | n/a | PyObject *file; |
|---|
| 2384 | n/a | PyObject *error_type, *error_value, *error_traceback; |
|---|
| 2385 | n/a | char buffer[1001]; |
|---|
| 2386 | n/a | int written; |
|---|
| 2387 | n/a | |
|---|
| 2388 | n/a | PyErr_Fetch(&error_type, &error_value, &error_traceback); |
|---|
| 2389 | n/a | file = _PySys_GetObjectId(key); |
|---|
| 2390 | n/a | written = PyOS_vsnprintf(buffer, sizeof(buffer), format, va); |
|---|
| 2391 | n/a | if (sys_pyfile_write(buffer, file) != 0) { |
|---|
| 2392 | n/a | PyErr_Clear(); |
|---|
| 2393 | n/a | fputs(buffer, fp); |
|---|
| 2394 | n/a | } |
|---|
| 2395 | n/a | if (written < 0 || (size_t)written >= sizeof(buffer)) { |
|---|
| 2396 | n/a | const char *truncated = "... truncated"; |
|---|
| 2397 | n/a | if (sys_pyfile_write(truncated, file) != 0) |
|---|
| 2398 | n/a | fputs(truncated, fp); |
|---|
| 2399 | n/a | } |
|---|
| 2400 | n/a | PyErr_Restore(error_type, error_value, error_traceback); |
|---|
| 2401 | n/a | } |
|---|
| 2402 | n/a | |
|---|
| 2403 | n/a | void |
|---|
| 2404 | n/a | PySys_WriteStdout(const char *format, ...) |
|---|
| 2405 | n/a | { |
|---|
| 2406 | n/a | va_list va; |
|---|
| 2407 | n/a | |
|---|
| 2408 | n/a | va_start(va, format); |
|---|
| 2409 | n/a | sys_write(&PyId_stdout, stdout, format, va); |
|---|
| 2410 | n/a | va_end(va); |
|---|
| 2411 | n/a | } |
|---|
| 2412 | n/a | |
|---|
| 2413 | n/a | void |
|---|
| 2414 | n/a | PySys_WriteStderr(const char *format, ...) |
|---|
| 2415 | n/a | { |
|---|
| 2416 | n/a | va_list va; |
|---|
| 2417 | n/a | |
|---|
| 2418 | n/a | va_start(va, format); |
|---|
| 2419 | n/a | sys_write(&PyId_stderr, stderr, format, va); |
|---|
| 2420 | n/a | va_end(va); |
|---|
| 2421 | n/a | } |
|---|
| 2422 | n/a | |
|---|
| 2423 | n/a | static void |
|---|
| 2424 | n/a | sys_format(_Py_Identifier *key, FILE *fp, const char *format, va_list va) |
|---|
| 2425 | n/a | { |
|---|
| 2426 | n/a | PyObject *file, *message; |
|---|
| 2427 | n/a | PyObject *error_type, *error_value, *error_traceback; |
|---|
| 2428 | n/a | const char *utf8; |
|---|
| 2429 | n/a | |
|---|
| 2430 | n/a | PyErr_Fetch(&error_type, &error_value, &error_traceback); |
|---|
| 2431 | n/a | file = _PySys_GetObjectId(key); |
|---|
| 2432 | n/a | message = PyUnicode_FromFormatV(format, va); |
|---|
| 2433 | n/a | if (message != NULL) { |
|---|
| 2434 | n/a | if (sys_pyfile_write_unicode(message, file) != 0) { |
|---|
| 2435 | n/a | PyErr_Clear(); |
|---|
| 2436 | n/a | utf8 = PyUnicode_AsUTF8(message); |
|---|
| 2437 | n/a | if (utf8 != NULL) |
|---|
| 2438 | n/a | fputs(utf8, fp); |
|---|
| 2439 | n/a | } |
|---|
| 2440 | n/a | Py_DECREF(message); |
|---|
| 2441 | n/a | } |
|---|
| 2442 | n/a | PyErr_Restore(error_type, error_value, error_traceback); |
|---|
| 2443 | n/a | } |
|---|
| 2444 | n/a | |
|---|
| 2445 | n/a | void |
|---|
| 2446 | n/a | PySys_FormatStdout(const char *format, ...) |
|---|
| 2447 | n/a | { |
|---|
| 2448 | n/a | va_list va; |
|---|
| 2449 | n/a | |
|---|
| 2450 | n/a | va_start(va, format); |
|---|
| 2451 | n/a | sys_format(&PyId_stdout, stdout, format, va); |
|---|
| 2452 | n/a | va_end(va); |
|---|
| 2453 | n/a | } |
|---|
| 2454 | n/a | |
|---|
| 2455 | n/a | void |
|---|
| 2456 | n/a | PySys_FormatStderr(const char *format, ...) |
|---|
| 2457 | n/a | { |
|---|
| 2458 | n/a | va_list va; |
|---|
| 2459 | n/a | |
|---|
| 2460 | n/a | va_start(va, format); |
|---|
| 2461 | n/a | sys_format(&PyId_stderr, stderr, format, va); |
|---|
| 2462 | n/a | va_end(va); |
|---|
| 2463 | n/a | } |
|---|