2121#include <io.h>
2222#include <conio.h>
2323#include <sys/locking.h>
24+ #include <crtdbg.h>
25+ #include <windows.h>
2426
2527// Force the malloc heap to clean itself up, and free unused blocks
2628// back to the OS. (According to the docs, only works on NT.)
@@ -201,6 +203,60 @@ insertint(PyObject *d, char *name, int value)
201203 }
202204}
203205
206+ #ifdef _DEBUG
207+
208+ static PyObject *
209+ msvcrt_setreportfile (PyObject * self , PyObject * args )
210+ {
211+ int type , file ;
212+ _HFILE res ;
213+
214+ if (!PyArg_ParseTuple (args , "ii" , & type , & file ))
215+ return NULL ;
216+ res = _CrtSetReportFile (type , (_HFILE )file );
217+ return PyInt_FromLong ((long )res );
218+ Py_INCREF (Py_None );
219+ return Py_None ;
220+ }
221+
222+ static PyObject *
223+ msvcrt_setreportmode (PyObject * self , PyObject * args )
224+ {
225+ int type , mode ;
226+ int res ;
227+
228+ if (!PyArg_ParseTuple (args , "ii" , & type , & mode ))
229+ return NULL ;
230+ res = _CrtSetReportMode (type , mode );
231+ if (res == -1 )
232+ return PyErr_SetFromErrno (PyExc_IOError );
233+ return PyLong_FromLong (res );
234+ }
235+
236+ static PyObject *
237+ msvcrt_seterrormode (PyObject * self , PyObject * args )
238+ {
239+ int mode , res ;
240+
241+ if (!PyArg_ParseTuple (args , "i" , & mode ))
242+ return NULL ;
243+ res = _set_error_mode (mode );
244+ return PyLong_FromLong (res );
245+ }
246+
247+ #endif
248+
249+ static PyObject *
250+ seterrormode (PyObject * self , PyObject * args )
251+ {
252+ unsigned int mode , res ;
253+
254+ if (!PyArg_ParseTuple (args , "I" , & mode ))
255+ return NULL ;
256+ res = SetErrorMode (mode );
257+ return PyLong_FromUnsignedLong (res );
258+ }
259+
204260
205261/* List of functions exported by this module */
206262static struct PyMethodDef msvcrt_functions [] = {
@@ -214,6 +270,12 @@ static struct PyMethodDef msvcrt_functions[] = {
214270 {"getche" , msvcrt_getche , METH_VARARGS },
215271 {"putch" , msvcrt_putch , METH_VARARGS },
216272 {"ungetch" , msvcrt_ungetch , METH_VARARGS },
273+ {"SetErrorMode" , seterrormode , METH_VARARGS },
274+ #ifdef _DEBUG
275+ {"CrtSetReportFile" , msvcrt_setreportfile , METH_VARARGS },
276+ {"CrtSetReportMode" , msvcrt_setreportmode , METH_VARARGS },
277+ {"set_error_mode" , msvcrt_seterrormode , METH_VARARGS },
278+ #endif
217279 {NULL , NULL }
218280};
219281
@@ -232,4 +294,20 @@ initmsvcrt(void)
232294 insertint (d , "LK_NBRLCK" , _LK_NBRLCK );
233295 insertint (d , "LK_RLCK" , _LK_RLCK );
234296 insertint (d , "LK_UNLCK" , _LK_UNLCK );
297+ insertint (d , "SEM_FAILCRITICALERRORS" , SEM_FAILCRITICALERRORS );
298+ insertint (d , "SEM_NOALIGNMENTFAULTEXCEPT" , SEM_NOALIGNMENTFAULTEXCEPT );
299+ insertint (d , "SEM_NOGPFAULTERRORBOX" , SEM_NOGPFAULTERRORBOX );
300+ insertint (d , "SEM_NOOPENFILEERRORBOX" , SEM_NOOPENFILEERRORBOX );
301+ #ifdef _DEBUG
302+ insertint (d , "CRT_WARN" , _CRT_WARN );
303+ insertint (d , "CRT_ERROR" , _CRT_ERROR );
304+ insertint (d , "CRT_ASSERT" , _CRT_ASSERT );
305+ insertint (d , "CRTDBG_MODE_DEBUG" , _CRTDBG_MODE_DEBUG );
306+ insertint (d , "CRTDBG_MODE_FILE" , _CRTDBG_MODE_FILE );
307+ insertint (d , "CRTDBG_MODE_WNDW" , _CRTDBG_MODE_WNDW );
308+ insertint (d , "CRTDBG_REPORT_MODE" , _CRTDBG_REPORT_MODE );
309+ insertint (d , "CRTDBG_FILE_STDERR" , (int )_CRTDBG_FILE_STDERR );
310+ insertint (d , "CRTDBG_FILE_STDOUT" , (int )_CRTDBG_FILE_STDOUT );
311+ insertint (d , "CRTDBG_REPORT_FILE" , (int )_CRTDBG_REPORT_FILE );
312+ #endif
235313}
0 commit comments