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