Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(16)

Delta Between Two Patch Sets: PC/msvcrtmodule.c

Issue 3871: cross and native build of python for mingw32 with distutils
Left Patch Set: Created 9 years, 8 months ago
Right Patch Set: Created 7 years, 4 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « PC/getpathp.c ('k') | PC/pyconfig.h » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 /********************************************************* 1 /*********************************************************
2 2
3 msvcrtmodule.c 3 msvcrtmodule.c
4 4
5 A Python interface to the Microsoft Visual C Runtime 5 A Python interface to the Microsoft Visual C Runtime
6 Library, providing access to those non-portable, but 6 Library, providing access to those non-portable, but
7 still useful routines. 7 still useful routines.
8 8
9 Only ever compiled with an MS compiler, so no attempt 9 Only ever compiled with an MS compiler, so no attempt
10 has been made to avoid MS language extensions, etc... 10 has been made to avoid MS language extensions, etc...
11 11
12 This may only work on NT or 95... 12 This may only work on NT or 95...
13 13
14 Author: Mark Hammond and Guido van Rossum. 14 Author: Mark Hammond and Guido van Rossum.
15 Maintenance: Guido van Rossum. 15 Maintenance: Guido van Rossum.
16 16
17 ***********************************************************/ 17 ***********************************************************/
18 18
19 #include "Python.h" 19 #include "Python.h"
20 #include "malloc.h" 20 #include "malloc.h"
21 #include <io.h> 21 #include <io.h>
22 #include <conio.h> 22 #include <conio.h>
23 #include <sys/locking.h> 23 #include <sys/locking.h>
24 #ifndef __MINGW32__ 24 #ifdef _MSC_VER
25 # define HAVE_CRTDBG_H
26 #endif
27 #ifdef HAVE_CRTDBG_H
25 #include <crtdbg.h> 28 #include <crtdbg.h>
26 #endif 29 #endif
27 #include <windows.h> 30 #include <windows.h>
31
32 #if defined(__MINGW32__)
33 #if __MSVCRT_VERSION__ >= 0x0700
34 # define _WCONIO_DEFINED
35 /* NOTE: Up to version ?.?? mingw don't define functions
36 * listed below. Also this require module to be linked with
37 * ms-vcrt at least verion 7.
38 * To build with different runtimes see:
39 * http://www.mingw.org/wiki/HOWTO_Use_the_GCC_specs_file
40 *
41 * Also note that NT5.1(XP), shiped with msvcrt version 7.0,
42 * contain all those functions, but library name is msvcrt.dll.
43 * So if you like module to run on w2k as is you must define
44 * appropriate __MSVCRT_VERSION__ .
45 * If you like those functions even on w2k you must link
46 * with appropriate runtime and to pack it in distributions.
47 * This is what MSVC build do - it is build and packed
48 * with version 9.0 of Microsoft C-runtime.
49 */
50 _CRTIMP wint_t __cdecl __MINGW_NOTHROW _getwch (void);
51 _CRTIMP wint_t __cdecl __MINGW_NOTHROW _getwche (void);
52 _CRTIMP wint_t __cdecl __MINGW_NOTHROW _putwch (wchar_t);
53 _CRTIMP wint_t __cdecl __MINGW_NOTHROW _ungetwch(wint_t);
54 #endif /* __MSVCRT_VERSION__ >= 0x0700 */
55 #endif
28 56
29 #ifdef _MSC_VER 57 #ifdef _MSC_VER
30 #if _MSC_VER >= 1500 && _MSC_VER < 1600 58 #if _MSC_VER >= 1500 && _MSC_VER < 1600
31 #include <crtassem.h> 59 #include <crtassem.h>
60 #elif _MSC_VER >= 1600
61 #include <crtversion.h>
32 #endif 62 #endif
33 #endif 63 #endif
34 64
35 // Force the malloc heap to clean itself up, and free unused blocks 65 // Force the malloc heap to clean itself up, and free unused blocks
36 // back to the OS. (According to the docs, only works on NT.) 66 // back to the OS. (According to the docs, only works on NT.)
37 static PyObject * 67 static PyObject *
38 msvcrt_heapmin(PyObject *self, PyObject *args) 68 msvcrt_heapmin(PyObject *self, PyObject *args)
39 { 69 {
40 if (!PyArg_ParseTuple(args, ":heapmin")) 70 if (!PyArg_ParseTuple(args, ":heapmin"))
41 return NULL; 71 return NULL;
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after
138 // Convert a C runtime file descriptor to an OS file handle. 168 // Convert a C runtime file descriptor to an OS file handle.
139 static PyObject * 169 static PyObject *
140 msvcrt_get_osfhandle(PyObject *self, PyObject *args) 170 msvcrt_get_osfhandle(PyObject *self, PyObject *args)
141 { 171 {
142 int fd; 172 int fd;
143 Py_intptr_t handle; 173 Py_intptr_t handle;
144 174
145 if (!PyArg_ParseTuple(args,"i:get_osfhandle", &fd)) 175 if (!PyArg_ParseTuple(args,"i:get_osfhandle", &fd))
146 return NULL; 176 return NULL;
147 177
178 if (!_PyVerify_fd(fd))
179 return PyErr_SetFromErrno(PyExc_IOError);
180
148 handle = _get_osfhandle(fd); 181 handle = _get_osfhandle(fd);
149 if (handle == -1) 182 if (handle == -1)
150 return PyErr_SetFromErrno(PyExc_IOError); 183 return PyErr_SetFromErrno(PyExc_IOError);
151 184
152 /* technically 'handle' is not a pointer, but a integer as 185 /* technically 'handle' is not a pointer, but a integer as
153 large as a pointer, Python's *VoidPtr interface is the 186 large as a pointer, Python's *VoidPtr interface is the
154 most appropriate here */ 187 most appropriate here */
155 return PyLong_FromVoidPtr((void*)handle); 188 return PyLong_FromVoidPtr((void*)handle);
156 } 189 }
157 190
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
192 Py_BEGIN_ALLOW_THREADS 225 Py_BEGIN_ALLOW_THREADS
193 ch = _getch(); 226 ch = _getch();
194 Py_END_ALLOW_THREADS 227 Py_END_ALLOW_THREADS
195 s[0] = ch; 228 s[0] = ch;
196 return PyBytes_FromStringAndSize(s, 1); 229 return PyBytes_FromStringAndSize(s, 1);
197 } 230 }
198 231
199 PyDoc_STRVAR(getch_doc, 232 PyDoc_STRVAR(getch_doc,
200 "getch() -> key character\n\ 233 "getch() -> key character\n\
201 \n\ 234 \n\
202 Read a keypress and return the resulting character. Nothing is echoed to\n\ 235 Read a keypress and return the resulting character as a byte string.\n\
203 the console. This call will block if a keypress is not already\n\ 236 Nothing is echoed to the console. This call will block if a keypress is\n\
204 available, but will not wait for Enter to be pressed. If the pressed key\n\ 237 not already available, but will not wait for Enter to be pressed. If the\n\
205 was a special function key, this will return '\\000' or '\\xe0'; the next\n\ 238 pressed key was a special function key, this will return '\\000' or\n\
206 call will return the keycode. The Control-C keypress cannot be read with\n\ 239 '\\xe0'; the next call will return the keycode. The Control-C keypress\n\
207 this function."); 240 cannot be read with this function.");
208 241
209 #ifdef _WCONIO_DEFINED 242 #ifdef _WCONIO_DEFINED
210 static PyObject * 243 static PyObject *
211 msvcrt_getwch(PyObject *self, PyObject *args) 244 msvcrt_getwch(PyObject *self, PyObject *args)
212 { 245 {
213 Py_UNICODE ch; 246 wchar_t ch;
214 Py_UNICODE u[1];
215 247
216 if (!PyArg_ParseTuple(args, ":getwch")) 248 if (!PyArg_ParseTuple(args, ":getwch"))
217 return NULL; 249 return NULL;
218 250
219 Py_BEGIN_ALLOW_THREADS 251 Py_BEGIN_ALLOW_THREADS
220 ch = _getwch(); 252 ch = _getwch();
221 Py_END_ALLOW_THREADS 253 Py_END_ALLOW_THREADS
222 u[0] = ch; 254 return PyUnicode_FromOrdinal(ch);
223 return PyUnicode_FromUnicode(u, 1);
224 } 255 }
225 256
226 PyDoc_STRVAR(getwch_doc, 257 PyDoc_STRVAR(getwch_doc,
227 "getwch() -> Unicode key character\n\ 258 "getwch() -> Unicode key character\n\
228 \n\ 259 \n\
229 Wide char variant of getch(), returning a Unicode value."); 260 Wide char variant of getch(), returning a Unicode value.");
230 #endif 261 #endif
231 262
232 static PyObject * 263 static PyObject *
233 msvcrt_getche(PyObject *self, PyObject *args) 264 msvcrt_getche(PyObject *self, PyObject *args)
(...skipping 14 matching lines...) Expand all
248 PyDoc_STRVAR(getche_doc, 279 PyDoc_STRVAR(getche_doc,
249 "getche() -> key character\n\ 280 "getche() -> key character\n\
250 \n\ 281 \n\
251 Similar to getch(), but the keypress will be echoed if it represents\n\ 282 Similar to getch(), but the keypress will be echoed if it represents\n\
252 a printable character."); 283 a printable character.");
253 284
254 #ifdef _WCONIO_DEFINED 285 #ifdef _WCONIO_DEFINED
255 static PyObject * 286 static PyObject *
256 msvcrt_getwche(PyObject *self, PyObject *args) 287 msvcrt_getwche(PyObject *self, PyObject *args)
257 { 288 {
258 Py_UNICODE ch; 289 wchar_t ch;
259 Py_UNICODE s[1];
260 290
261 if (!PyArg_ParseTuple(args, ":getwche")) 291 if (!PyArg_ParseTuple(args, ":getwche"))
262 return NULL; 292 return NULL;
263 293
264 Py_BEGIN_ALLOW_THREADS 294 Py_BEGIN_ALLOW_THREADS
265 ch = _getwche(); 295 ch = _getwche();
266 Py_END_ALLOW_THREADS 296 Py_END_ALLOW_THREADS
267 s[0] = ch; 297 return PyUnicode_FromOrdinal(ch);
268 return PyUnicode_FromUnicode(s, 1);
269 } 298 }
270 299
271 PyDoc_STRVAR(getwche_doc, 300 PyDoc_STRVAR(getwche_doc,
272 "getwche() -> Unicode key character\n\ 301 "getwche() -> Unicode key character\n\
273 \n\ 302 \n\
274 Wide char variant of getche(), returning a Unicode value."); 303 Wide char variant of getche(), returning a Unicode value.");
275 #endif 304 #endif
276 305
277 static PyObject * 306 static PyObject *
278 msvcrt_putch(PyObject *self, PyObject *args) 307 msvcrt_putch(PyObject *self, PyObject *args)
279 { 308 {
280 char ch; 309 char ch;
281 310
282 if (!PyArg_ParseTuple(args, "c:putch", &ch)) 311 if (!PyArg_ParseTuple(args, "c:putch", &ch))
283 return NULL; 312 return NULL;
284 313
285 _putch(ch); 314 _putch(ch);
286 Py_INCREF(Py_None); 315 Py_INCREF(Py_None);
287 return Py_None; 316 return Py_None;
288 } 317 }
289 318
290 PyDoc_STRVAR(putch_doc, 319 PyDoc_STRVAR(putch_doc,
291 "putch(char) -> None\n\ 320 "putch(char) -> None\n\
292 \n\ 321 \n\
293 Print the character char to the console without buffering."); 322 Print the byte string char to the console without buffering.");
294 323
295 #ifdef _WCONIO_DEFINED 324 #ifdef _WCONIO_DEFINED
296 static PyObject * 325 static PyObject *
297 msvcrt_putwch(PyObject *self, PyObject *args) 326 msvcrt_putwch(PyObject *self, PyObject *args)
298 { 327 {
299 int ch; 328 int ch;
300 329
301 if (!PyArg_ParseTuple(args, "C:putwch", &ch)) 330 if (!PyArg_ParseTuple(args, "C:putwch", &ch))
302 return NULL; 331 return NULL;
303 332
304 _putwch(ch); 333 _putwch(ch);
305 Py_RETURN_NONE; 334 Py_RETURN_NONE;
306 335
307 } 336 }
308 337
309 PyDoc_STRVAR(putwch_doc, 338 PyDoc_STRVAR(putwch_doc,
310 "putwch(unicode_char) -> None\n\ 339 "putwch(unicode_char) -> None\n\
311 \n\ 340 \n\
312 Wide char variant of putch(), accepting a Unicode value."); 341 Wide char variant of putch(), accepting a Unicode value.");
313 #endif 342 #endif
314 343
315 static PyObject * 344 static PyObject *
316 msvcrt_ungetch(PyObject *self, PyObject *args) 345 msvcrt_ungetch(PyObject *self, PyObject *args)
317 { 346 {
318 char ch; 347 char ch;
319 348
320 if (!PyArg_ParseTuple(args, "c:ungetch", &ch)) 349 if (!PyArg_ParseTuple(args, "c:ungetch", &ch))
321 return NULL; 350 return NULL;
322 351
352 /* FIXME: why _ungetch is called instead _ungetwch */
323 if (_ungetch(ch) == EOF) 353 if (_ungetch(ch) == EOF)
324 return PyErr_SetFromErrno(PyExc_IOError); 354 return PyErr_SetFromErrno(PyExc_IOError);
325 Py_INCREF(Py_None); 355 Py_INCREF(Py_None);
326 return Py_None; 356 return Py_None;
327 } 357 }
328 358
329 PyDoc_STRVAR(ungetch_doc, 359 PyDoc_STRVAR(ungetch_doc,
330 "ungetch(char) -> None\n\ 360 "ungetch(char) -> None\n\
331 \n\ 361 \n\
332 Cause the character char to be \"pushed back\" into the console buffer;\n\ 362 Cause the byte string char to be \"pushed back\" into the\n\
333 it will be the next character read by getch() or getche()."); 363 console buffer; it will be the next character read by\n\
364 getch() or getche().");
334 365
335 #ifdef _WCONIO_DEFINED 366 #ifdef _WCONIO_DEFINED
336 static PyObject * 367 static PyObject *
337 msvcrt_ungetwch(PyObject *self, PyObject *args) 368 msvcrt_ungetwch(PyObject *self, PyObject *args)
338 { 369 {
339 int ch; 370 int ch;
340 371
341 if (!PyArg_ParseTuple(args, "C:ungetwch", &ch)) 372 if (!PyArg_ParseTuple(args, "C:ungetwch", &ch))
342 return NULL; 373 return NULL;
343 374
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after
459 NULL, 490 NULL,
460 NULL, 491 NULL,
461 NULL, 492 NULL,
462 NULL 493 NULL
463 }; 494 };
464 495
465 PyMODINIT_FUNC 496 PyMODINIT_FUNC
466 PyInit_msvcrt(void) 497 PyInit_msvcrt(void)
467 { 498 {
468 int st; 499 int st;
469 PyObject *d; 500 PyObject *d, *version;
470 PyObject *m = PyModule_Create(&msvcrtmodule); 501 PyObject *m = PyModule_Create(&msvcrtmodule);
471 if (m == NULL) 502 if (m == NULL)
472 return NULL; 503 return NULL;
473 d = PyModule_GetDict(m); 504 d = PyModule_GetDict(m);
474 505
475 /* constants for the locking() function's mode argument */ 506 /* constants for the locking() function's mode argument */
476 insertint(d, "LK_LOCK", _LK_LOCK); 507 insertint(d, "LK_LOCK", _LK_LOCK);
477 insertint(d, "LK_NBLCK", _LK_NBLCK); 508 insertint(d, "LK_NBLCK", _LK_NBLCK);
478 insertint(d, "LK_NBRLCK", _LK_NBRLCK); 509 insertint(d, "LK_NBRLCK", _LK_NBRLCK);
479 insertint(d, "LK_RLCK", _LK_RLCK); 510 insertint(d, "LK_RLCK", _LK_RLCK);
480 insertint(d, "LK_UNLCK", _LK_UNLCK); 511 insertint(d, "LK_UNLCK", _LK_UNLCK);
481 insertint(d, "SEM_FAILCRITICALERRORS", SEM_FAILCRITICALERRORS); 512 insertint(d, "SEM_FAILCRITICALERRORS", SEM_FAILCRITICALERRORS);
482 insertint(d, "SEM_NOALIGNMENTFAULTEXCEPT", SEM_NOALIGNMENTFAULTEXCEPT); 513 insertint(d, "SEM_NOALIGNMENTFAULTEXCEPT", SEM_NOALIGNMENTFAULTEXCEPT);
483 insertint(d, "SEM_NOGPFAULTERRORBOX", SEM_NOGPFAULTERRORBOX); 514 insertint(d, "SEM_NOGPFAULTERRORBOX", SEM_NOGPFAULTERRORBOX);
484 insertint(d, "SEM_NOOPENFILEERRORBOX", SEM_NOOPENFILEERRORBOX); 515 insertint(d, "SEM_NOOPENFILEERRORBOX", SEM_NOOPENFILEERRORBOX);
485 #ifdef _DEBUG 516 #ifdef _DEBUG
486 insertint(d, "CRT_WARN", _CRT_WARN); 517 insertint(d, "CRT_WARN", _CRT_WARN);
487 insertint(d, "CRT_ERROR", _CRT_ERROR); 518 insertint(d, "CRT_ERROR", _CRT_ERROR);
488 insertint(d, "CRT_ASSERT", _CRT_ASSERT); 519 insertint(d, "CRT_ASSERT", _CRT_ASSERT);
489 insertint(d, "CRTDBG_MODE_DEBUG", _CRTDBG_MODE_DEBUG); 520 insertint(d, "CRTDBG_MODE_DEBUG", _CRTDBG_MODE_DEBUG);
490 insertint(d, "CRTDBG_MODE_FILE", _CRTDBG_MODE_FILE); 521 insertint(d, "CRTDBG_MODE_FILE", _CRTDBG_MODE_FILE);
491 insertint(d, "CRTDBG_MODE_WNDW", _CRTDBG_MODE_WNDW); 522 insertint(d, "CRTDBG_MODE_WNDW", _CRTDBG_MODE_WNDW);
492 insertint(d, "CRTDBG_REPORT_MODE", _CRTDBG_REPORT_MODE); 523 insertint(d, "CRTDBG_REPORT_MODE", _CRTDBG_REPORT_MODE);
493 insertint(d, "CRTDBG_FILE_STDERR", (int)_CRTDBG_FILE_STDERR); 524 insertint(d, "CRTDBG_FILE_STDERR", (int)_CRTDBG_FILE_STDERR);
494 insertint(d, "CRTDBG_FILE_STDOUT", (int)_CRTDBG_FILE_STDOUT); 525 insertint(d, "CRTDBG_FILE_STDOUT", (int)_CRTDBG_FILE_STDOUT);
495 insertint(d, "CRTDBG_REPORT_FILE", (int)_CRTDBG_REPORT_FILE); 526 insertint(d, "CRTDBG_REPORT_FILE", (int)_CRTDBG_REPORT_FILE);
496 #endif 527 #endif
497 528
498 /* constants for the crt versions */ 529 /* constants for the crt versions */
530 (void)st;
499 #ifdef _VC_ASSEMBLY_PUBLICKEYTOKEN 531 #ifdef _VC_ASSEMBLY_PUBLICKEYTOKEN
500 st = PyModule_AddStringConstant(m, "VC_ASSEMBLY_PUBLICKEYTOKEN", 532 st = PyModule_AddStringConstant(m, "VC_ASSEMBLY_PUBLICKEYTOKEN",
501 _VC_ASSEMBLY_PUBLICKEYTOKEN); 533 _VC_ASSEMBLY_PUBLICKEYTOKEN);
502 if (st < 0) return NULL; 534 if (st < 0) return NULL;
503 #endif 535 #endif
504 #ifdef _CRT_ASSEMBLY_VERSION 536 #ifdef _CRT_ASSEMBLY_VERSION
505 st = PyModule_AddStringConstant(m, "CRT_ASSEMBLY_VERSION", 537 st = PyModule_AddStringConstant(m, "CRT_ASSEMBLY_VERSION",
506 _CRT_ASSEMBLY_VERSION); 538 _CRT_ASSEMBLY_VERSION);
507 if (st < 0) return NULL; 539 if (st < 0) return NULL;
508 #endif 540 #endif
509 #ifdef __LIBRARIES_ASSEMBLY_NAME_PREFIX 541 #ifdef __LIBRARIES_ASSEMBLY_NAME_PREFIX
510 st = PyModule_AddStringConstant(m, "LIBRARIES_ASSEMBLY_NAME_PREFIX", 542 st = PyModule_AddStringConstant(m, "LIBRARIES_ASSEMBLY_NAME_PREFIX",
511 __LIBRARIES_ASSEMBLY_NAME_PREFIX); 543 __LIBRARIES_ASSEMBLY_NAME_PREFIX);
512 if (st < 0) return NULL; 544 if (st < 0) return NULL;
513 #endif 545 #endif
514 546
547 /* constants for the 2010 crt versions */
548 #if defined(_VC_CRT_MAJOR_VERSION) && defined (_VC_CRT_MINOR_VERSION) && defined (_VC_CRT_BUILD_VERSION) && defined(_VC_CRT_RBUILD_VERSION)
549 version = PyUnicode_FromFormat("%d.%d.%d.%d", _VC_CRT_MAJOR_VERSION,
550 _VC_CRT_MINOR_VERSION,
551 _VC_CRT_BUILD_VERSION,
552 _VC_CRT_RBUILD_VERSION);
553 st = PyModule_AddObject(m, "CRT_ASSEMBLY_VERSION", version);
554 if (st < 0) return NULL;
555 #endif
556
515 return m; 557 return m;
516 } 558 }
LEFTRIGHT

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+