Advertisement
Guest User

Untitled

a guest
Jul 21st, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.05 KB | None | 0 0
  1. void
  2. Py_InitializeEx(int install_sigs)
  3. {
  4. PyInterpreterState *interp;
  5. PyThreadState *tstate;
  6. PyObject *bimod, *sysmod;
  7. char *p;
  8. char *icodeset = NULL; /* On Windows, input codeset may theoretically
  9. differ from output codeset. */
  10. char *codeset = NULL;
  11. char *errors = NULL;
  12. int free_codeset = 0;
  13. int overridden = 0;
  14. PyObject *sys_stream;
  15. #if defined(Py_USING_UNICODE) && defined(HAVE_LANGINFO_H) && defined(CODESET)
  16. char *saved_locale, *loc_codeset;
  17. #endif
  18. #ifdef MS_WINDOWS
  19. char ibuf[128];
  20. char buf[128];
  21. #endif
  22. extern void _Py_ReadyTypes(void);
  23.  
  24. if (initialized)
  25. return;
  26. initialized = 1;
  27.  
  28. if ((p = Py_GETENV("PYTHONDEBUG")) && *p != '\0')
  29. Py_DebugFlag = add_flag(Py_DebugFlag, p);
  30. if ((p = Py_GETENV("PYTHONVERBOSE")) && *p != '\0')
  31. Py_VerboseFlag = add_flag(Py_VerboseFlag, p);
  32. if ((p = Py_GETENV("PYTHONOPTIMIZE")) && *p != '\0')
  33. Py_OptimizeFlag = add_flag(Py_OptimizeFlag, p);
  34. if ((p = Py_GETENV("PYTHONDONTWRITEBYTECODE")) && *p != '\0')
  35. Py_DontWriteBytecodeFlag = add_flag(Py_DontWriteBytecodeFlag, p);
  36. /* The variable is only tested for existence here; _PyRandom_Init will
  37. check its value further. */
  38. if ((p = Py_GETENV("PYTHONHASHSEED")) && *p != '\0')
  39. Py_HashRandomizationFlag = add_flag(Py_HashRandomizationFlag, p);
  40.  
  41. _PyRandom_Init();
  42.  
  43. interp = PyInterpreterState_New();
  44. if (interp == NULL)
  45. Py_FatalError("Py_Initialize: can't make first interpreter");
  46.  
  47. tstate = PyThreadState_New(interp);
  48. if (tstate == NULL)
  49. Py_FatalError("Py_Initialize: can't make first thread");
  50. (void) PyThreadState_Swap(tstate);
  51.  
  52. _Py_ReadyTypes();
  53.  
  54. if (!_PyFrame_Init())
  55. Py_FatalError("Py_Initialize: can't init frames");
  56.  
  57. if (!_PyInt_Init())
  58. Py_FatalError("Py_Initialize: can't init ints");
  59.  
  60. if (!_PyLong_Init())
  61. Py_FatalError("Py_Initialize: can't init longs");
  62.  
  63. if (!PyByteArray_Init())
  64. Py_FatalError("Py_Initialize: can't init bytearray");
  65.  
  66. _PyFloat_Init();
  67.  
  68. interp->modules = PyDict_New();
  69. if (interp->modules == NULL)
  70. Py_FatalError("Py_Initialize: can't make modules dictionary");
  71. interp->modules_reloading = PyDict_New();
  72. if (interp->modules_reloading == NULL)
  73. Py_FatalError("Py_Initialize: can't make modules_reloading dictionary");
  74.  
  75. #ifdef Py_USING_UNICODE
  76. /* Init Unicode implementation; relies on the codec registry */
  77. _PyUnicode_Init();
  78. #endif
  79.  
  80. bimod = _PyBuiltin_Init();
  81. if (bimod == NULL)
  82. Py_FatalError("Py_Initialize: can't initialize __builtin__");
  83. interp->builtins = PyModule_GetDict(bimod);
  84. if (interp->builtins == NULL)
  85. Py_FatalError("Py_Initialize: can't initialize builtins dict");
  86. Py_INCREF(interp->builtins);
  87.  
  88. sysmod = _PySys_Init();
  89. if (sysmod == NULL)
  90. Py_FatalError("Py_Initialize: can't initialize sys");
  91. interp->sysdict = PyModule_GetDict(sysmod);
  92. if (interp->sysdict == NULL)
  93. Py_FatalError("Py_Initialize: can't initialize sys dict");
  94. Py_INCREF(interp->sysdict);
  95. _PyImport_FixupExtension("sys", "sys");
  96. PySys_SetPath(Py_GetPath());
  97. PyDict_SetItemString(interp->sysdict, "modules",
  98. interp->modules);
  99.  
  100. _PyImport_Init();
  101.  
  102. /* initialize builtin exceptions */
  103. _PyExc_Init();
  104. _PyImport_FixupExtension("exceptions", "exceptions");
  105.  
  106. /* phase 2 of builtins */
  107. _PyImport_FixupExtension("__builtin__", "__builtin__");
  108.  
  109. _PyImportHooks_Init();
  110.  
  111. if (install_sigs)
  112. initsigs(); /* Signal handling stuff, including initintr() */
  113.  
  114. /* Initialize warnings. */
  115. _PyWarnings_Init();
  116. if (PySys_HasWarnOptions()) {
  117. PyObject *warnings_module = PyImport_ImportModule("warnings");
  118. if (!warnings_module)
  119. PyErr_Clear();
  120. Py_XDECREF(warnings_module);
  121. }
  122.  
  123. initmain(); /* Module __main__ */
  124.  
  125. /* auto-thread-state API, if available */
  126. #ifdef WITH_THREAD
  127. _PyGILState_Init(interp, tstate);
  128. #endif /* WITH_THREAD */
  129.  
  130. if (!Py_NoSiteFlag)
  131. initsite(); /* Module site */
  132.  
  133. if ((p = Py_GETENV("PYTHONIOENCODING")) && *p != '\0') {
  134. p = icodeset = codeset = strdup(p);
  135. free_codeset = 1;
  136. errors = strchr(p, ':');
  137. if (errors) {
  138. *errors = '\0';
  139. errors++;
  140. }
  141. overridden = 1;
  142. }
  143.  
  144. #if defined(Py_USING_UNICODE) && defined(HAVE_LANGINFO_H) && defined(CODESET)
  145. /* On Unix, set the file system encoding according to the
  146. user's preference, if the CODESET names a well-known
  147. Python codec, and Py_FileSystemDefaultEncoding isn't
  148. initialized by other means. Also set the encoding of
  149. stdin and stdout if these are terminals, unless overridden. */
  150.  
  151. if (!overridden || !Py_FileSystemDefaultEncoding) {
  152. saved_locale = strdup(setlocale(LC_CTYPE, NULL));
  153. setlocale(LC_CTYPE, "");
  154. loc_codeset = nl_langinfo(CODESET);
  155. if (loc_codeset && *loc_codeset) {
  156. PyObject *enc = PyCodec_Encoder(loc_codeset);
  157. if (enc) {
  158. loc_codeset = strdup(loc_codeset);
  159. Py_DECREF(enc);
  160. } else {
  161. if (PyErr_ExceptionMatches(PyExc_LookupError)) {
  162. PyErr_Clear();
  163. loc_codeset = NULL;
  164. } else {
  165. PyErr_Print();
  166. exit(1);
  167. }
  168. }
  169. } else
  170. loc_codeset = NULL;
  171. setlocale(LC_CTYPE, saved_locale);
  172. free(saved_locale);
  173.  
  174. if (!overridden) {
  175. codeset = icodeset = loc_codeset;
  176. free_codeset = 1;
  177. }
  178.  
  179. /* Initialize Py_FileSystemDefaultEncoding from
  180. locale even if PYTHONIOENCODING is set. */
  181. if (!Py_FileSystemDefaultEncoding) {
  182. Py_FileSystemDefaultEncoding = loc_codeset;
  183. if (!overridden)
  184. free_codeset = 0;
  185. }
  186. }
  187. #endif
  188.  
  189. #ifdef MS_WINDOWS
  190. if (!overridden) {
  191. icodeset = ibuf;
  192. codeset = buf;
  193. sprintf(ibuf, "cp%d", GetConsoleCP());
  194. sprintf(buf, "cp%d", GetConsoleOutputCP());
  195. }
  196. #endif
  197.  
  198. if (codeset) {
  199. sys_stream = PySys_GetObject("stdin");
  200. if ((overridden || isatty_no_error(sys_stream)) &&
  201. PyFile_Check(sys_stream)) {
  202. if (!PyFile_SetEncodingAndErrors(sys_stream, icodeset, errors))
  203. Py_FatalError("Cannot set codeset of stdin");
  204. }
  205.  
  206. sys_stream = PySys_GetObject("stdout");
  207. if ((overridden || isatty_no_error(sys_stream)) &&
  208. PyFile_Check(sys_stream)) {
  209. if (!PyFile_SetEncodingAndErrors(sys_stream, codeset, errors))
  210. Py_FatalError("Cannot set codeset of stdout");
  211. }
  212.  
  213. sys_stream = PySys_GetObject("stderr");
  214. if ((overridden || isatty_no_error(sys_stream)) &&
  215. PyFile_Check(sys_stream)) {
  216. if (!PyFile_SetEncodingAndErrors(sys_stream, codeset, errors))
  217. Py_FatalError("Cannot set codeset of stderr");
  218. }
  219.  
  220. if (free_codeset)
  221. free(codeset);
  222. }
  223. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement