msvcrtmodule.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  1. /*********************************************************
  2. msvcrtmodule.c
  3. A Python interface to the Microsoft Visual C Runtime
  4. Library, providing access to those non-portable, but
  5. still useful routines.
  6. Only ever compiled with an MS compiler, so no attempt
  7. has been made to avoid MS language extensions, etc...
  8. This may only work on NT or 95...
  9. Author: Mark Hammond and Guido van Rossum.
  10. Maintenance: Guido van Rossum.
  11. ***********************************************************/
  12. #include "Python.h"
  13. #include "pycore_fileutils.h" // _Py_BEGIN_SUPPRESS_IPH
  14. #include "malloc.h"
  15. #include <io.h>
  16. #include <conio.h>
  17. #include <sys/locking.h>
  18. #include <crtdbg.h>
  19. #include <windows.h>
  20. #ifdef _MSC_VER
  21. #if _MSC_VER >= 1500 && _MSC_VER < 1600
  22. #error #include <crtassem.h>
  23. #elif _MSC_VER >= 1600
  24. #include <crtversion.h>
  25. #endif
  26. #endif
  27. /*[python input]
  28. class HANDLE_converter(CConverter):
  29. type = 'void *'
  30. format_unit = '"_Py_PARSE_UINTPTR"'
  31. def parse_arg(self, argname, displayname):
  32. return """
  33. {paramname} = PyLong_AsVoidPtr({argname});
  34. if (!{paramname} && PyErr_Occurred()) {{{{
  35. goto exit;
  36. }}}}
  37. """.format(argname=argname, paramname=self.parser_name)
  38. class HANDLE_return_converter(CReturnConverter):
  39. type = 'void *'
  40. def render(self, function, data):
  41. self.declare(data)
  42. self.err_occurred_if(
  43. "_return_value == NULL || _return_value == INVALID_HANDLE_VALUE",
  44. data)
  45. data.return_conversion.append(
  46. 'return_value = PyLong_FromVoidPtr(_return_value);\n')
  47. class byte_char_return_converter(CReturnConverter):
  48. type = 'int'
  49. def render(self, function, data):
  50. data.declarations.append('char s[1];')
  51. data.return_value = 's[0]'
  52. data.return_conversion.append(
  53. 'return_value = PyBytes_FromStringAndSize(s, 1);\n')
  54. class wchar_t_return_converter(CReturnConverter):
  55. type = 'wchar_t'
  56. def render(self, function, data):
  57. self.declare(data)
  58. data.return_conversion.append(
  59. 'return_value = PyUnicode_FromOrdinal(_return_value);\n')
  60. [python start generated code]*/
  61. /*[python end generated code: output=da39a3ee5e6b4b0d input=1e8e9fa3538ec08f]*/
  62. /*[clinic input]
  63. module msvcrt
  64. [clinic start generated code]*/
  65. /*[clinic end generated code: output=da39a3ee5e6b4b0d input=f31a87a783d036cd]*/
  66. #include "clinic/msvcrtmodule.c.h"
  67. /*[clinic input]
  68. msvcrt.heapmin
  69. Minimize the malloc() heap.
  70. Force the malloc() heap to clean itself up and return unused blocks
  71. to the operating system. On failure, this raises OSError.
  72. [clinic start generated code]*/
  73. static PyObject *
  74. msvcrt_heapmin_impl(PyObject *module)
  75. /*[clinic end generated code: output=1ba00f344782dc19 input=82e1771d21bde2d8]*/
  76. {
  77. if (_heapmin() != 0)
  78. return PyErr_SetFromErrno(PyExc_OSError);
  79. Py_RETURN_NONE;
  80. }
  81. /*[clinic input]
  82. msvcrt.locking
  83. fd: int
  84. mode: int
  85. nbytes: long
  86. /
  87. Lock part of a file based on file descriptor fd from the C runtime.
  88. Raises OSError on failure. The locked region of the file extends from
  89. the current file position for nbytes bytes, and may continue beyond
  90. the end of the file. mode must be one of the LK_* constants listed
  91. below. Multiple regions in a file may be locked at the same time, but
  92. may not overlap. Adjacent regions are not merged; they must be unlocked
  93. individually.
  94. [clinic start generated code]*/
  95. static PyObject *
  96. msvcrt_locking_impl(PyObject *module, int fd, int mode, long nbytes)
  97. /*[clinic end generated code: output=a4a90deca9785a03 input=e97bd15fc4a04fef]*/
  98. {
  99. int err;
  100. if (PySys_Audit("msvcrt.locking", "iil", fd, mode, nbytes) < 0) {
  101. return NULL;
  102. }
  103. Py_BEGIN_ALLOW_THREADS
  104. _Py_BEGIN_SUPPRESS_IPH
  105. err = _locking(fd, mode, nbytes);
  106. _Py_END_SUPPRESS_IPH
  107. Py_END_ALLOW_THREADS
  108. if (err != 0)
  109. return PyErr_SetFromErrno(PyExc_OSError);
  110. Py_RETURN_NONE;
  111. }
  112. /*[clinic input]
  113. msvcrt.setmode -> long
  114. fd: int
  115. mode as flags: int
  116. /
  117. Set the line-end translation mode for the file descriptor fd.
  118. To set it to text mode, flags should be os.O_TEXT; for binary, it
  119. should be os.O_BINARY.
  120. Return value is the previous mode.
  121. [clinic start generated code]*/
  122. static long
  123. msvcrt_setmode_impl(PyObject *module, int fd, int flags)
  124. /*[clinic end generated code: output=24a9be5ea07ccb9b input=76e7c01f6b137f75]*/
  125. {
  126. _Py_BEGIN_SUPPRESS_IPH
  127. flags = _setmode(fd, flags);
  128. _Py_END_SUPPRESS_IPH
  129. if (flags == -1)
  130. PyErr_SetFromErrno(PyExc_OSError);
  131. return flags;
  132. }
  133. /*[clinic input]
  134. msvcrt.open_osfhandle -> long
  135. handle: HANDLE
  136. flags: int
  137. /
  138. Create a C runtime file descriptor from the file handle handle.
  139. The flags parameter should be a bitwise OR of os.O_APPEND, os.O_RDONLY,
  140. and os.O_TEXT. The returned file descriptor may be used as a parameter
  141. to os.fdopen() to create a file object.
  142. [clinic start generated code]*/
  143. static long
  144. msvcrt_open_osfhandle_impl(PyObject *module, void *handle, int flags)
  145. /*[clinic end generated code: output=b2fb97c4b515e4e6 input=d5db190a307cf4bb]*/
  146. {
  147. if (PySys_Audit("msvcrt.open_osfhandle", "Ki", handle, flags) < 0) {
  148. return -1;
  149. }
  150. return _Py_open_osfhandle(handle, flags);
  151. }
  152. /*[clinic input]
  153. msvcrt.get_osfhandle -> HANDLE
  154. fd: int
  155. /
  156. Return the file handle for the file descriptor fd.
  157. Raises OSError if fd is not recognized.
  158. [clinic start generated code]*/
  159. static void *
  160. msvcrt_get_osfhandle_impl(PyObject *module, int fd)
  161. /*[clinic end generated code: output=aca01dfe24637374 input=5fcfde9b17136aa2]*/
  162. {
  163. if (PySys_Audit("msvcrt.get_osfhandle", "(i)", fd) < 0) {
  164. return NULL;
  165. }
  166. return _Py_get_osfhandle(fd);
  167. }
  168. /* Console I/O */
  169. /*[clinic input]
  170. msvcrt.kbhit -> long
  171. Return true if a keypress is waiting to be read.
  172. [clinic start generated code]*/
  173. static long
  174. msvcrt_kbhit_impl(PyObject *module)
  175. /*[clinic end generated code: output=940dfce6587c1890 input=e70d678a5c2f6acc]*/
  176. {
  177. return _kbhit();
  178. }
  179. /*[clinic input]
  180. msvcrt.getch -> byte_char
  181. Read a keypress and return the resulting character as a byte string.
  182. Nothing is echoed to the console. This call will block if a keypress is
  183. not already available, but will not wait for Enter to be pressed. If the
  184. pressed key was a special function key, this will return '\000' or
  185. '\xe0'; the next call will return the keycode. The Control-C keypress
  186. cannot be read with this function.
  187. [clinic start generated code]*/
  188. static int
  189. msvcrt_getch_impl(PyObject *module)
  190. /*[clinic end generated code: output=a4e51f0565064a7d input=37a40cf0ed0d1153]*/
  191. {
  192. int ch;
  193. Py_BEGIN_ALLOW_THREADS
  194. ch = _getch();
  195. Py_END_ALLOW_THREADS
  196. return ch;
  197. }
  198. #ifdef MS_WINDOWS_DESKTOP
  199. /*[clinic input]
  200. msvcrt.getwch -> wchar_t
  201. Wide char variant of getch(), returning a Unicode value.
  202. [clinic start generated code]*/
  203. static wchar_t
  204. msvcrt_getwch_impl(PyObject *module)
  205. /*[clinic end generated code: output=be9937494e22f007 input=27b3dec8ad823d7c]*/
  206. {
  207. wchar_t ch;
  208. Py_BEGIN_ALLOW_THREADS
  209. ch = _getwch();
  210. Py_END_ALLOW_THREADS
  211. return ch;
  212. }
  213. #endif /* MS_WINDOWS_DESKTOP */
  214. /*[clinic input]
  215. msvcrt.getche -> byte_char
  216. Similar to getch(), but the keypress will be echoed if possible.
  217. [clinic start generated code]*/
  218. static int
  219. msvcrt_getche_impl(PyObject *module)
  220. /*[clinic end generated code: output=d8f7db4fd2990401 input=43311ade9ed4a9c0]*/
  221. {
  222. int ch;
  223. Py_BEGIN_ALLOW_THREADS
  224. ch = _getche();
  225. Py_END_ALLOW_THREADS
  226. return ch;
  227. }
  228. #ifdef MS_WINDOWS_DESKTOP
  229. /*[clinic input]
  230. msvcrt.getwche -> wchar_t
  231. Wide char variant of getche(), returning a Unicode value.
  232. [clinic start generated code]*/
  233. static wchar_t
  234. msvcrt_getwche_impl(PyObject *module)
  235. /*[clinic end generated code: output=d0dae5ba3829d596 input=49337d59d1a591f8]*/
  236. {
  237. wchar_t ch;
  238. Py_BEGIN_ALLOW_THREADS
  239. ch = _getwche();
  240. Py_END_ALLOW_THREADS
  241. return ch;
  242. }
  243. #endif /* MS_WINDOWS_DESKTOP */
  244. /*[clinic input]
  245. msvcrt.putch
  246. char: char
  247. /
  248. Print the byte string char to the console without buffering.
  249. [clinic start generated code]*/
  250. static PyObject *
  251. msvcrt_putch_impl(PyObject *module, char char_value)
  252. /*[clinic end generated code: output=92ec9b81012d8f60 input=ec078dd10cb054d6]*/
  253. {
  254. _Py_BEGIN_SUPPRESS_IPH
  255. _putch(char_value);
  256. _Py_END_SUPPRESS_IPH
  257. Py_RETURN_NONE;
  258. }
  259. #ifdef MS_WINDOWS_DESKTOP
  260. /*[clinic input]
  261. msvcrt.putwch
  262. unicode_char: int(accept={str})
  263. /
  264. Wide char variant of putch(), accepting a Unicode value.
  265. [clinic start generated code]*/
  266. static PyObject *
  267. msvcrt_putwch_impl(PyObject *module, int unicode_char)
  268. /*[clinic end generated code: output=a3bd1a8951d28eee input=996ccd0bbcbac4c3]*/
  269. {
  270. _Py_BEGIN_SUPPRESS_IPH
  271. _putwch(unicode_char);
  272. _Py_END_SUPPRESS_IPH
  273. Py_RETURN_NONE;
  274. }
  275. #endif /* MS_WINDOWS_DESKTOP */
  276. /*[clinic input]
  277. msvcrt.ungetch
  278. char: char
  279. /
  280. Opposite of getch.
  281. Cause the byte string char to be "pushed back" into the
  282. console buffer; it will be the next character read by
  283. getch() or getche().
  284. [clinic start generated code]*/
  285. static PyObject *
  286. msvcrt_ungetch_impl(PyObject *module, char char_value)
  287. /*[clinic end generated code: output=c6942a0efa119000 input=22f07ee9001bbf0f]*/
  288. {
  289. int res;
  290. _Py_BEGIN_SUPPRESS_IPH
  291. res = _ungetch(char_value);
  292. _Py_END_SUPPRESS_IPH
  293. if (res == EOF)
  294. return PyErr_SetFromErrno(PyExc_OSError);
  295. Py_RETURN_NONE;
  296. }
  297. #ifdef MS_WINDOWS_DESKTOP
  298. /*[clinic input]
  299. msvcrt.ungetwch
  300. unicode_char: int(accept={str})
  301. /
  302. Wide char variant of ungetch(), accepting a Unicode value.
  303. [clinic start generated code]*/
  304. static PyObject *
  305. msvcrt_ungetwch_impl(PyObject *module, int unicode_char)
  306. /*[clinic end generated code: output=e63af05438b8ba3d input=83ec0492be04d564]*/
  307. {
  308. int res;
  309. _Py_BEGIN_SUPPRESS_IPH
  310. res = _ungetwch(unicode_char);
  311. _Py_END_SUPPRESS_IPH
  312. if (res == WEOF)
  313. return PyErr_SetFromErrno(PyExc_OSError);
  314. Py_RETURN_NONE;
  315. }
  316. #endif /* MS_WINDOWS_DESKTOP */
  317. #ifdef _DEBUG
  318. /*[clinic input]
  319. msvcrt.CrtSetReportFile -> HANDLE
  320. type: int
  321. file: HANDLE
  322. /
  323. Wrapper around _CrtSetReportFile.
  324. Only available on Debug builds.
  325. [clinic start generated code]*/
  326. static void *
  327. msvcrt_CrtSetReportFile_impl(PyObject *module, int type, void *file)
  328. /*[clinic end generated code: output=9393e8c77088bbe9 input=290809b5f19e65b9]*/
  329. {
  330. HANDLE res;
  331. _Py_BEGIN_SUPPRESS_IPH
  332. res = _CrtSetReportFile(type, file);
  333. _Py_END_SUPPRESS_IPH
  334. return res;
  335. }
  336. /*[clinic input]
  337. msvcrt.CrtSetReportMode -> long
  338. type: int
  339. mode: int
  340. /
  341. Wrapper around _CrtSetReportMode.
  342. Only available on Debug builds.
  343. [clinic start generated code]*/
  344. static long
  345. msvcrt_CrtSetReportMode_impl(PyObject *module, int type, int mode)
  346. /*[clinic end generated code: output=b2863761523de317 input=9319d29b4319426b]*/
  347. {
  348. int res;
  349. _Py_BEGIN_SUPPRESS_IPH
  350. res = _CrtSetReportMode(type, mode);
  351. _Py_END_SUPPRESS_IPH
  352. if (res == -1)
  353. PyErr_SetFromErrno(PyExc_OSError);
  354. return res;
  355. }
  356. /*[clinic input]
  357. msvcrt.set_error_mode -> long
  358. mode: int
  359. /
  360. Wrapper around _set_error_mode.
  361. Only available on Debug builds.
  362. [clinic start generated code]*/
  363. static long
  364. msvcrt_set_error_mode_impl(PyObject *module, int mode)
  365. /*[clinic end generated code: output=ac4a09040d8ac4e3 input=046fca59c0f20872]*/
  366. {
  367. long res;
  368. _Py_BEGIN_SUPPRESS_IPH
  369. res = _set_error_mode(mode);
  370. _Py_END_SUPPRESS_IPH
  371. return res;
  372. }
  373. #endif /* _DEBUG */
  374. #if defined(MS_WINDOWS_DESKTOP) || defined(MS_WINDOWS_APP) || defined(MS_WINDOWS_SYSTEM)
  375. /*[clinic input]
  376. msvcrt.GetErrorMode
  377. Wrapper around GetErrorMode.
  378. [clinic start generated code]*/
  379. static PyObject *
  380. msvcrt_GetErrorMode_impl(PyObject *module)
  381. /*[clinic end generated code: output=3103fc6145913591 input=5a7fb083b6dd71fd]*/
  382. {
  383. unsigned int res;
  384. _Py_BEGIN_SUPPRESS_IPH
  385. res = GetErrorMode();
  386. _Py_END_SUPPRESS_IPH
  387. return PyLong_FromUnsignedLong(res);
  388. }
  389. #endif /* MS_WINDOWS_APP || MS_WINDOWS_SYSTEM */
  390. /*[clinic input]
  391. msvcrt.SetErrorMode
  392. mode: unsigned_int(bitwise=True)
  393. /
  394. Wrapper around SetErrorMode.
  395. [clinic start generated code]*/
  396. static PyObject *
  397. msvcrt_SetErrorMode_impl(PyObject *module, unsigned int mode)
  398. /*[clinic end generated code: output=01d529293f00da8f input=d8b167258d32d907]*/
  399. {
  400. unsigned int res;
  401. _Py_BEGIN_SUPPRESS_IPH
  402. res = SetErrorMode(mode);
  403. _Py_END_SUPPRESS_IPH
  404. return PyLong_FromUnsignedLong(res);
  405. }
  406. /*[clinic input]
  407. [clinic start generated code]*/
  408. /*[clinic end generated code: output=da39a3ee5e6b4b0d input=da39a3ee5e6b4b0d]*/
  409. /* List of functions exported by this module */
  410. static struct PyMethodDef msvcrt_functions[] = {
  411. MSVCRT_HEAPMIN_METHODDEF
  412. MSVCRT_LOCKING_METHODDEF
  413. MSVCRT_SETMODE_METHODDEF
  414. MSVCRT_OPEN_OSFHANDLE_METHODDEF
  415. MSVCRT_GET_OSFHANDLE_METHODDEF
  416. MSVCRT_KBHIT_METHODDEF
  417. MSVCRT_GETCH_METHODDEF
  418. MSVCRT_GETCHE_METHODDEF
  419. MSVCRT_PUTCH_METHODDEF
  420. MSVCRT_UNGETCH_METHODDEF
  421. MSVCRT_GETERRORMODE_METHODDEF
  422. MSVCRT_SETERRORMODE_METHODDEF
  423. MSVCRT_CRTSETREPORTFILE_METHODDEF
  424. MSVCRT_CRTSETREPORTMODE_METHODDEF
  425. MSVCRT_SET_ERROR_MODE_METHODDEF
  426. MSVCRT_GETWCH_METHODDEF
  427. MSVCRT_GETWCHE_METHODDEF
  428. MSVCRT_PUTWCH_METHODDEF
  429. MSVCRT_UNGETWCH_METHODDEF
  430. {NULL, NULL}
  431. };
  432. static int
  433. insertptr(PyObject *mod, char *name, void *value)
  434. {
  435. PyObject *v = PyLong_FromVoidPtr(value);
  436. if (v == NULL) {
  437. return -1;
  438. }
  439. int rc = PyModule_AddObjectRef(mod, name, v);
  440. Py_DECREF(v);
  441. return rc;
  442. }
  443. #define INSERTINT(MOD, NAME, VAL) do { \
  444. if (PyModule_AddIntConstant(MOD, NAME, VAL) < 0) { \
  445. return -1; \
  446. } \
  447. } while (0)
  448. #define INSERTPTR(MOD, NAME, PTR) do { \
  449. if (insertptr(MOD, NAME, PTR) < 0) { \
  450. return -1; \
  451. } \
  452. } while (0)
  453. #define INSERTSTR(MOD, NAME, CONST) do { \
  454. if (PyModule_AddStringConstant(MOD, NAME, CONST) < 0) { \
  455. return -1; \
  456. } \
  457. } while (0)
  458. static int
  459. exec_module(PyObject* m)
  460. {
  461. /* constants for the locking() function's mode argument */
  462. INSERTINT(m, "LK_LOCK", _LK_LOCK);
  463. INSERTINT(m, "LK_NBLCK", _LK_NBLCK);
  464. INSERTINT(m, "LK_NBRLCK", _LK_NBRLCK);
  465. INSERTINT(m, "LK_RLCK", _LK_RLCK);
  466. INSERTINT(m, "LK_UNLCK", _LK_UNLCK);
  467. #ifdef MS_WINDOWS_DESKTOP
  468. INSERTINT(m, "SEM_FAILCRITICALERRORS", SEM_FAILCRITICALERRORS);
  469. INSERTINT(m, "SEM_NOALIGNMENTFAULTEXCEPT", SEM_NOALIGNMENTFAULTEXCEPT);
  470. INSERTINT(m, "SEM_NOGPFAULTERRORBOX", SEM_NOGPFAULTERRORBOX);
  471. INSERTINT(m, "SEM_NOOPENFILEERRORBOX", SEM_NOOPENFILEERRORBOX);
  472. #endif
  473. #ifdef _DEBUG
  474. INSERTINT(m, "CRT_WARN", _CRT_WARN);
  475. INSERTINT(m, "CRT_ERROR", _CRT_ERROR);
  476. INSERTINT(m, "CRT_ASSERT", _CRT_ASSERT);
  477. INSERTINT(m, "CRTDBG_MODE_DEBUG", _CRTDBG_MODE_DEBUG);
  478. INSERTINT(m, "CRTDBG_MODE_FILE", _CRTDBG_MODE_FILE);
  479. INSERTINT(m, "CRTDBG_MODE_WNDW", _CRTDBG_MODE_WNDW);
  480. INSERTINT(m, "CRTDBG_REPORT_MODE", _CRTDBG_REPORT_MODE);
  481. INSERTPTR(m, "CRTDBG_FILE_STDERR", _CRTDBG_FILE_STDERR);
  482. INSERTPTR(m, "CRTDBG_FILE_STDOUT", _CRTDBG_FILE_STDOUT);
  483. INSERTPTR(m, "CRTDBG_REPORT_FILE", _CRTDBG_REPORT_FILE);
  484. #endif
  485. #undef INSERTINT
  486. #undef INSERTPTR
  487. /* constants for the crt versions */
  488. #ifdef _VC_ASSEMBLY_PUBLICKEYTOKEN
  489. INSERTSTR(m, "VC_ASSEMBLY_PUBLICKEYTOKEN", _VC_ASSEMBLY_PUBLICKEYTOKEN);
  490. #endif
  491. #ifdef _CRT_ASSEMBLY_VERSION
  492. INSERTSTR(m, "CRT_ASSEMBLY_VERSION", _CRT_ASSEMBLY_VERSION);
  493. #endif
  494. #ifdef __LIBRARIES_ASSEMBLY_NAME_PREFIX
  495. INSERTSTR(m, "LIBRARIES_ASSEMBLY_NAME_PREFIX",
  496. __LIBRARIES_ASSEMBLY_NAME_PREFIX);
  497. #endif
  498. #undef INSERTSTR
  499. /* constants for the 2010 crt versions */
  500. #if defined(_VC_CRT_MAJOR_VERSION) && defined (_VC_CRT_MINOR_VERSION) && defined(_VC_CRT_BUILD_VERSION) && defined(_VC_CRT_RBUILD_VERSION)
  501. PyObject *version = PyUnicode_FromFormat("%d.%d.%d.%d",
  502. _VC_CRT_MAJOR_VERSION,
  503. _VC_CRT_MINOR_VERSION,
  504. _VC_CRT_BUILD_VERSION,
  505. _VC_CRT_RBUILD_VERSION);
  506. if (version == NULL) {
  507. return -1;
  508. }
  509. int st = PyModule_AddObjectRef(m, "CRT_ASSEMBLY_VERSION", version);
  510. Py_DECREF(version);
  511. if (st < 0) {
  512. return -1;
  513. }
  514. #endif
  515. return 0;
  516. }
  517. static PyModuleDef_Slot msvcrt_slots[] = {
  518. {Py_mod_exec, exec_module},
  519. {Py_mod_multiple_interpreters, Py_MOD_PER_INTERPRETER_GIL_SUPPORTED},
  520. {0, NULL}
  521. };
  522. static struct PyModuleDef msvcrtmodule = {
  523. .m_base = PyModuleDef_HEAD_INIT,
  524. .m_name = "msvcrt",
  525. .m_methods = msvcrt_functions,
  526. .m_slots = msvcrt_slots,
  527. };
  528. PyMODINIT_FUNC
  529. PyInit_msvcrt(void)
  530. {
  531. return PyModuleDef_Init(&msvcrtmodule);
  532. }