_winapi.c 77 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676
  1. /*
  2. * Support routines from the Windows API
  3. *
  4. * This module was originally created by merging PC/_subprocess.c with
  5. * Modules/_multiprocessing/win32_functions.c.
  6. *
  7. * Copyright (c) 2004 by Fredrik Lundh <fredrik@pythonware.com>
  8. * Copyright (c) 2004 by Secret Labs AB, http://www.pythonware.com
  9. * Copyright (c) 2004 by Peter Astrand <astrand@lysator.liu.se>
  10. *
  11. * By obtaining, using, and/or copying this software and/or its
  12. * associated documentation, you agree that you have read, understood,
  13. * and will comply with the following terms and conditions:
  14. *
  15. * Permission to use, copy, modify, and distribute this software and
  16. * its associated documentation for any purpose and without fee is
  17. * hereby granted, provided that the above copyright notice appears in
  18. * all copies, and that both that copyright notice and this permission
  19. * notice appear in supporting documentation, and that the name of the
  20. * authors not be used in advertising or publicity pertaining to
  21. * distribution of the software without specific, written prior
  22. * permission.
  23. *
  24. * THE AUTHORS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  25. * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
  26. * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  27. * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
  28. * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
  29. * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
  30. * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  31. *
  32. */
  33. /* Licensed to PSF under a Contributor Agreement. */
  34. /* See https://www.python.org/2.4/license for licensing details. */
  35. #include "Python.h"
  36. #include "pycore_moduleobject.h" // _PyModule_GetState()
  37. #include "structmember.h" // PyMemberDef
  38. #ifndef WINDOWS_LEAN_AND_MEAN
  39. #define WINDOWS_LEAN_AND_MEAN
  40. #endif
  41. #include "windows.h"
  42. #include <winioctl.h>
  43. #include <crtdbg.h>
  44. #include "winreparse.h"
  45. #if defined(MS_WIN32) && !defined(MS_WIN64)
  46. #define HANDLE_TO_PYNUM(handle) \
  47. PyLong_FromUnsignedLong((unsigned long) handle)
  48. #define PYNUM_TO_HANDLE(obj) ((HANDLE)PyLong_AsUnsignedLong(obj))
  49. #define F_POINTER "k"
  50. #define T_POINTER T_ULONG
  51. #else
  52. #define HANDLE_TO_PYNUM(handle) \
  53. PyLong_FromUnsignedLongLong((unsigned long long) handle)
  54. #define PYNUM_TO_HANDLE(obj) ((HANDLE)PyLong_AsUnsignedLongLong(obj))
  55. #define F_POINTER "K"
  56. #define T_POINTER T_ULONGLONG
  57. #endif
  58. #define F_HANDLE F_POINTER
  59. #define F_DWORD "k"
  60. #define T_HANDLE T_POINTER
  61. // winbase.h limits the STARTF_* flags to the desktop API as of 10.0.19041.
  62. #ifndef STARTF_USESHOWWINDOW
  63. #define STARTF_USESHOWWINDOW 0x00000001
  64. #endif
  65. #ifndef STARTF_USESTDHANDLES
  66. #define STARTF_USESTDHANDLES 0x00000100
  67. #endif
  68. typedef struct {
  69. PyTypeObject *overlapped_type;
  70. } WinApiState;
  71. static inline WinApiState*
  72. winapi_get_state(PyObject *module)
  73. {
  74. void *state = _PyModule_GetState(module);
  75. assert(state != NULL);
  76. return (WinApiState *)state;
  77. }
  78. /*
  79. * A Python object wrapping an OVERLAPPED structure and other useful data
  80. * for overlapped I/O
  81. */
  82. typedef struct {
  83. PyObject_HEAD
  84. OVERLAPPED overlapped;
  85. /* For convenience, we store the file handle too */
  86. HANDLE handle;
  87. /* Whether there's I/O in flight */
  88. int pending;
  89. /* Whether I/O completed successfully */
  90. int completed;
  91. /* Buffer used for reading (optional) */
  92. PyObject *read_buffer;
  93. /* Buffer used for writing (optional) */
  94. Py_buffer write_buffer;
  95. } OverlappedObject;
  96. /*
  97. Note: tp_clear (overlapped_clear) is not implemented because it
  98. requires cancelling the IO operation if it's pending and the cancellation is
  99. quite complex and can fail (see: overlapped_dealloc).
  100. */
  101. static int
  102. overlapped_traverse(OverlappedObject *self, visitproc visit, void *arg)
  103. {
  104. Py_VISIT(self->read_buffer);
  105. Py_VISIT(self->write_buffer.obj);
  106. Py_VISIT(Py_TYPE(self));
  107. return 0;
  108. }
  109. static void
  110. overlapped_dealloc(OverlappedObject *self)
  111. {
  112. DWORD bytes;
  113. int err = GetLastError();
  114. PyObject_GC_UnTrack(self);
  115. if (self->pending) {
  116. if (CancelIoEx(self->handle, &self->overlapped) &&
  117. GetOverlappedResult(self->handle, &self->overlapped, &bytes, TRUE))
  118. {
  119. /* The operation is no longer pending -- nothing to do. */
  120. }
  121. else if (_Py_IsInterpreterFinalizing(PyInterpreterState_Get()))
  122. {
  123. /* The operation is still pending -- give a warning. This
  124. will probably only happen on Windows XP. */
  125. PyErr_SetString(PyExc_RuntimeError,
  126. "I/O operations still in flight while destroying "
  127. "Overlapped object, the process may crash");
  128. PyErr_WriteUnraisable(NULL);
  129. }
  130. else
  131. {
  132. /* The operation is still pending, but the process is
  133. probably about to exit, so we need not worry too much
  134. about memory leaks. Leaking self prevents a potential
  135. crash. This can happen when a daemon thread is cleaned
  136. up at exit -- see #19565. We only expect to get here
  137. on Windows XP. */
  138. CloseHandle(self->overlapped.hEvent);
  139. SetLastError(err);
  140. return;
  141. }
  142. }
  143. CloseHandle(self->overlapped.hEvent);
  144. SetLastError(err);
  145. if (self->write_buffer.obj)
  146. PyBuffer_Release(&self->write_buffer);
  147. Py_CLEAR(self->read_buffer);
  148. PyTypeObject *tp = Py_TYPE(self);
  149. tp->tp_free(self);
  150. Py_DECREF(tp);
  151. }
  152. /*[clinic input]
  153. module _winapi
  154. class _winapi.Overlapped "OverlappedObject *" "&OverlappedType"
  155. [clinic start generated code]*/
  156. /*[clinic end generated code: output=da39a3ee5e6b4b0d input=c13d3f5fd1dabb84]*/
  157. /*[python input]
  158. def create_converter(type_, format_unit):
  159. name = type_ + '_converter'
  160. # registered upon creation by CConverter's metaclass
  161. type(name, (CConverter,), {'type': type_, 'format_unit': format_unit})
  162. # format unit differs between platforms for these
  163. create_converter('HANDLE', '" F_HANDLE "')
  164. create_converter('HMODULE', '" F_HANDLE "')
  165. create_converter('LPSECURITY_ATTRIBUTES', '" F_POINTER "')
  166. create_converter('LPCVOID', '" F_POINTER "')
  167. create_converter('BOOL', 'i') # F_BOOL used previously (always 'i')
  168. create_converter('DWORD', 'k') # F_DWORD is always "k" (which is much shorter)
  169. create_converter('UINT', 'I') # F_UINT used previously (always 'I')
  170. class LPCWSTR_converter(Py_UNICODE_converter):
  171. type = 'LPCWSTR'
  172. class HANDLE_return_converter(CReturnConverter):
  173. type = 'HANDLE'
  174. def render(self, function, data):
  175. self.declare(data)
  176. self.err_occurred_if("_return_value == INVALID_HANDLE_VALUE", data)
  177. data.return_conversion.append(
  178. 'if (_return_value == NULL) {\n Py_RETURN_NONE;\n}\n')
  179. data.return_conversion.append(
  180. 'return_value = HANDLE_TO_PYNUM(_return_value);\n')
  181. class DWORD_return_converter(CReturnConverter):
  182. type = 'DWORD'
  183. def render(self, function, data):
  184. self.declare(data)
  185. self.err_occurred_if("_return_value == PY_DWORD_MAX", data)
  186. data.return_conversion.append(
  187. 'return_value = Py_BuildValue("k", _return_value);\n')
  188. class LPVOID_return_converter(CReturnConverter):
  189. type = 'LPVOID'
  190. def render(self, function, data):
  191. self.declare(data)
  192. self.err_occurred_if("_return_value == NULL", data)
  193. data.return_conversion.append(
  194. 'return_value = HANDLE_TO_PYNUM(_return_value);\n')
  195. [python start generated code]*/
  196. /*[python end generated code: output=da39a3ee5e6b4b0d input=ae30321c4cb150dd]*/
  197. #include "clinic/_winapi.c.h"
  198. /*[clinic input]
  199. _winapi.Overlapped.GetOverlappedResult
  200. wait: bool
  201. /
  202. [clinic start generated code]*/
  203. static PyObject *
  204. _winapi_Overlapped_GetOverlappedResult_impl(OverlappedObject *self, int wait)
  205. /*[clinic end generated code: output=bdd0c1ed6518cd03 input=194505ee8e0e3565]*/
  206. {
  207. BOOL res;
  208. DWORD transferred = 0;
  209. DWORD err;
  210. Py_BEGIN_ALLOW_THREADS
  211. res = GetOverlappedResult(self->handle, &self->overlapped, &transferred,
  212. wait != 0);
  213. Py_END_ALLOW_THREADS
  214. err = res ? ERROR_SUCCESS : GetLastError();
  215. switch (err) {
  216. case ERROR_SUCCESS:
  217. case ERROR_MORE_DATA:
  218. case ERROR_OPERATION_ABORTED:
  219. self->completed = 1;
  220. self->pending = 0;
  221. break;
  222. case ERROR_IO_INCOMPLETE:
  223. break;
  224. default:
  225. self->pending = 0;
  226. return PyErr_SetExcFromWindowsErr(PyExc_OSError, err);
  227. }
  228. if (self->completed && self->read_buffer != NULL) {
  229. assert(PyBytes_CheckExact(self->read_buffer));
  230. if (transferred != PyBytes_GET_SIZE(self->read_buffer) &&
  231. _PyBytes_Resize(&self->read_buffer, transferred))
  232. return NULL;
  233. }
  234. return Py_BuildValue("II", (unsigned) transferred, (unsigned) err);
  235. }
  236. /*[clinic input]
  237. _winapi.Overlapped.getbuffer
  238. [clinic start generated code]*/
  239. static PyObject *
  240. _winapi_Overlapped_getbuffer_impl(OverlappedObject *self)
  241. /*[clinic end generated code: output=95a3eceefae0f748 input=347fcfd56b4ceabd]*/
  242. {
  243. PyObject *res;
  244. if (!self->completed) {
  245. PyErr_SetString(PyExc_ValueError,
  246. "can't get read buffer before GetOverlappedResult() "
  247. "signals the operation completed");
  248. return NULL;
  249. }
  250. res = self->read_buffer ? self->read_buffer : Py_None;
  251. return Py_NewRef(res);
  252. }
  253. /*[clinic input]
  254. _winapi.Overlapped.cancel
  255. [clinic start generated code]*/
  256. static PyObject *
  257. _winapi_Overlapped_cancel_impl(OverlappedObject *self)
  258. /*[clinic end generated code: output=fcb9ab5df4ebdae5 input=cbf3da142290039f]*/
  259. {
  260. BOOL res = TRUE;
  261. if (self->pending) {
  262. Py_BEGIN_ALLOW_THREADS
  263. res = CancelIoEx(self->handle, &self->overlapped);
  264. Py_END_ALLOW_THREADS
  265. }
  266. /* CancelIoEx returns ERROR_NOT_FOUND if the I/O completed in-between */
  267. if (!res && GetLastError() != ERROR_NOT_FOUND)
  268. return PyErr_SetExcFromWindowsErr(PyExc_OSError, 0);
  269. self->pending = 0;
  270. Py_RETURN_NONE;
  271. }
  272. static PyMethodDef overlapped_methods[] = {
  273. _WINAPI_OVERLAPPED_GETOVERLAPPEDRESULT_METHODDEF
  274. _WINAPI_OVERLAPPED_GETBUFFER_METHODDEF
  275. _WINAPI_OVERLAPPED_CANCEL_METHODDEF
  276. {NULL}
  277. };
  278. static PyMemberDef overlapped_members[] = {
  279. {"event", T_HANDLE,
  280. offsetof(OverlappedObject, overlapped) + offsetof(OVERLAPPED, hEvent),
  281. READONLY, "overlapped event handle"},
  282. {NULL}
  283. };
  284. static PyType_Slot winapi_overlapped_type_slots[] = {
  285. {Py_tp_traverse, overlapped_traverse},
  286. {Py_tp_dealloc, overlapped_dealloc},
  287. {Py_tp_doc, "OVERLAPPED structure wrapper"},
  288. {Py_tp_methods, overlapped_methods},
  289. {Py_tp_members, overlapped_members},
  290. {0,0}
  291. };
  292. static PyType_Spec winapi_overlapped_type_spec = {
  293. .name = "_winapi.Overlapped",
  294. .basicsize = sizeof(OverlappedObject),
  295. .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_DISALLOW_INSTANTIATION |
  296. Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_IMMUTABLETYPE),
  297. .slots = winapi_overlapped_type_slots,
  298. };
  299. static OverlappedObject *
  300. new_overlapped(PyObject *module, HANDLE handle)
  301. {
  302. WinApiState *st = winapi_get_state(module);
  303. OverlappedObject *self = PyObject_GC_New(OverlappedObject, st->overlapped_type);
  304. if (!self)
  305. return NULL;
  306. self->handle = handle;
  307. self->read_buffer = NULL;
  308. self->pending = 0;
  309. self->completed = 0;
  310. memset(&self->overlapped, 0, sizeof(OVERLAPPED));
  311. memset(&self->write_buffer, 0, sizeof(Py_buffer));
  312. /* Manual reset, initially non-signalled */
  313. self->overlapped.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
  314. PyObject_GC_Track(self);
  315. return self;
  316. }
  317. /* -------------------------------------------------------------------- */
  318. /* windows API functions */
  319. /*[clinic input]
  320. _winapi.CloseHandle
  321. handle: HANDLE
  322. /
  323. Close handle.
  324. [clinic start generated code]*/
  325. static PyObject *
  326. _winapi_CloseHandle_impl(PyObject *module, HANDLE handle)
  327. /*[clinic end generated code: output=7ad37345f07bd782 input=7f0e4ac36e0352b8]*/
  328. {
  329. BOOL success;
  330. Py_BEGIN_ALLOW_THREADS
  331. success = CloseHandle(handle);
  332. Py_END_ALLOW_THREADS
  333. if (!success)
  334. return PyErr_SetFromWindowsErr(0);
  335. Py_RETURN_NONE;
  336. }
  337. /*[clinic input]
  338. _winapi.ConnectNamedPipe
  339. handle: HANDLE
  340. overlapped as use_overlapped: bool = False
  341. [clinic start generated code]*/
  342. static PyObject *
  343. _winapi_ConnectNamedPipe_impl(PyObject *module, HANDLE handle,
  344. int use_overlapped)
  345. /*[clinic end generated code: output=335a0e7086800671 input=a80e56e8bd370e31]*/
  346. {
  347. BOOL success;
  348. OverlappedObject *overlapped = NULL;
  349. if (use_overlapped) {
  350. overlapped = new_overlapped(module, handle);
  351. if (!overlapped)
  352. return NULL;
  353. }
  354. Py_BEGIN_ALLOW_THREADS
  355. success = ConnectNamedPipe(handle,
  356. overlapped ? &overlapped->overlapped : NULL);
  357. Py_END_ALLOW_THREADS
  358. if (overlapped) {
  359. int err = GetLastError();
  360. /* Overlapped ConnectNamedPipe never returns a success code */
  361. assert(success == 0);
  362. if (err == ERROR_IO_PENDING)
  363. overlapped->pending = 1;
  364. else if (err == ERROR_PIPE_CONNECTED)
  365. SetEvent(overlapped->overlapped.hEvent);
  366. else {
  367. Py_DECREF(overlapped);
  368. return PyErr_SetFromWindowsErr(err);
  369. }
  370. return (PyObject *) overlapped;
  371. }
  372. if (!success)
  373. return PyErr_SetFromWindowsErr(0);
  374. Py_RETURN_NONE;
  375. }
  376. /*[clinic input]
  377. _winapi.CreateFile -> HANDLE
  378. file_name: LPCWSTR
  379. desired_access: DWORD
  380. share_mode: DWORD
  381. security_attributes: LPSECURITY_ATTRIBUTES
  382. creation_disposition: DWORD
  383. flags_and_attributes: DWORD
  384. template_file: HANDLE
  385. /
  386. [clinic start generated code]*/
  387. static HANDLE
  388. _winapi_CreateFile_impl(PyObject *module, LPCWSTR file_name,
  389. DWORD desired_access, DWORD share_mode,
  390. LPSECURITY_ATTRIBUTES security_attributes,
  391. DWORD creation_disposition,
  392. DWORD flags_and_attributes, HANDLE template_file)
  393. /*[clinic end generated code: output=818c811e5e04d550 input=1fa870ed1c2e3d69]*/
  394. {
  395. HANDLE handle;
  396. if (PySys_Audit("_winapi.CreateFile", "ukkkk",
  397. file_name, desired_access, share_mode,
  398. creation_disposition, flags_and_attributes) < 0) {
  399. return INVALID_HANDLE_VALUE;
  400. }
  401. Py_BEGIN_ALLOW_THREADS
  402. handle = CreateFileW(file_name, desired_access,
  403. share_mode, security_attributes,
  404. creation_disposition,
  405. flags_and_attributes, template_file);
  406. Py_END_ALLOW_THREADS
  407. if (handle == INVALID_HANDLE_VALUE) {
  408. PyErr_SetFromWindowsErr(0);
  409. }
  410. return handle;
  411. }
  412. /*[clinic input]
  413. _winapi.CreateFileMapping -> HANDLE
  414. file_handle: HANDLE
  415. security_attributes: LPSECURITY_ATTRIBUTES
  416. protect: DWORD
  417. max_size_high: DWORD
  418. max_size_low: DWORD
  419. name: LPCWSTR
  420. /
  421. [clinic start generated code]*/
  422. static HANDLE
  423. _winapi_CreateFileMapping_impl(PyObject *module, HANDLE file_handle,
  424. LPSECURITY_ATTRIBUTES security_attributes,
  425. DWORD protect, DWORD max_size_high,
  426. DWORD max_size_low, LPCWSTR name)
  427. /*[clinic end generated code: output=6c0a4d5cf7f6fcc6 input=3dc5cf762a74dee8]*/
  428. {
  429. HANDLE handle;
  430. Py_BEGIN_ALLOW_THREADS
  431. handle = CreateFileMappingW(file_handle, security_attributes,
  432. protect, max_size_high, max_size_low,
  433. name);
  434. Py_END_ALLOW_THREADS
  435. if (handle == NULL) {
  436. PyObject *temp = PyUnicode_FromWideChar(name, -1);
  437. PyErr_SetExcFromWindowsErrWithFilenameObject(PyExc_OSError, 0, temp);
  438. Py_XDECREF(temp);
  439. handle = INVALID_HANDLE_VALUE;
  440. }
  441. return handle;
  442. }
  443. /*[clinic input]
  444. _winapi.CreateJunction
  445. src_path: LPCWSTR
  446. dst_path: LPCWSTR
  447. /
  448. [clinic start generated code]*/
  449. static PyObject *
  450. _winapi_CreateJunction_impl(PyObject *module, LPCWSTR src_path,
  451. LPCWSTR dst_path)
  452. /*[clinic end generated code: output=44b3f5e9bbcc4271 input=963d29b44b9384a7]*/
  453. {
  454. /* Privilege adjustment */
  455. HANDLE token = NULL;
  456. struct {
  457. TOKEN_PRIVILEGES base;
  458. /* overallocate by a few array elements */
  459. LUID_AND_ATTRIBUTES privs[4];
  460. } tp, previousTp;
  461. int previousTpSize = 0;
  462. /* Reparse data buffer */
  463. const USHORT prefix_len = 4;
  464. USHORT print_len = 0;
  465. USHORT rdb_size = 0;
  466. _Py_PREPARSE_DATA_BUFFER rdb = NULL;
  467. /* Junction point creation */
  468. HANDLE junction = NULL;
  469. DWORD ret = 0;
  470. if (src_path == NULL || dst_path == NULL)
  471. return PyErr_SetFromWindowsErr(ERROR_INVALID_PARAMETER);
  472. if (wcsncmp(src_path, L"\\??\\", prefix_len) == 0)
  473. return PyErr_SetFromWindowsErr(ERROR_INVALID_PARAMETER);
  474. if (PySys_Audit("_winapi.CreateJunction", "uu", src_path, dst_path) < 0) {
  475. return NULL;
  476. }
  477. /* Adjust privileges to allow rewriting directory entry as a
  478. junction point. */
  479. if (!OpenProcessToken(GetCurrentProcess(),
  480. TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &token)) {
  481. goto cleanup;
  482. }
  483. if (!LookupPrivilegeValue(NULL, SE_RESTORE_NAME, &tp.base.Privileges[0].Luid)) {
  484. goto cleanup;
  485. }
  486. tp.base.PrivilegeCount = 1;
  487. tp.base.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
  488. if (!AdjustTokenPrivileges(token, FALSE, &tp.base, sizeof(previousTp),
  489. &previousTp.base, &previousTpSize)) {
  490. goto cleanup;
  491. }
  492. if (GetFileAttributesW(src_path) == INVALID_FILE_ATTRIBUTES)
  493. goto cleanup;
  494. /* Store the absolute link target path length in print_len. */
  495. print_len = (USHORT)GetFullPathNameW(src_path, 0, NULL, NULL);
  496. if (print_len == 0)
  497. goto cleanup;
  498. /* NUL terminator should not be part of print_len. */
  499. --print_len;
  500. /* REPARSE_DATA_BUFFER usage is heavily under-documented, especially for
  501. junction points. Here's what I've learned along the way:
  502. - A junction point has two components: a print name and a substitute
  503. name. They both describe the link target, but the substitute name is
  504. the physical target and the print name is shown in directory listings.
  505. - The print name must be a native name, prefixed with "\??\".
  506. - Both names are stored after each other in the same buffer (the
  507. PathBuffer) and both must be NUL-terminated.
  508. - There are four members defining their respective offset and length
  509. inside PathBuffer: SubstituteNameOffset, SubstituteNameLength,
  510. PrintNameOffset and PrintNameLength.
  511. - The total size we need to allocate for the REPARSE_DATA_BUFFER, thus,
  512. is the sum of:
  513. - the fixed header size (REPARSE_DATA_BUFFER_HEADER_SIZE)
  514. - the size of the MountPointReparseBuffer member without the PathBuffer
  515. - the size of the prefix ("\??\") in bytes
  516. - the size of the print name in bytes
  517. - the size of the substitute name in bytes
  518. - the size of two NUL terminators in bytes */
  519. rdb_size = _Py_REPARSE_DATA_BUFFER_HEADER_SIZE +
  520. sizeof(rdb->MountPointReparseBuffer) -
  521. sizeof(rdb->MountPointReparseBuffer.PathBuffer) +
  522. /* Two +1's for NUL terminators. */
  523. (prefix_len + print_len + 1 + print_len + 1) * sizeof(WCHAR);
  524. rdb = (_Py_PREPARSE_DATA_BUFFER)PyMem_RawCalloc(1, rdb_size);
  525. if (rdb == NULL)
  526. goto cleanup;
  527. rdb->ReparseTag = IO_REPARSE_TAG_MOUNT_POINT;
  528. rdb->ReparseDataLength = rdb_size - _Py_REPARSE_DATA_BUFFER_HEADER_SIZE;
  529. rdb->MountPointReparseBuffer.SubstituteNameOffset = 0;
  530. rdb->MountPointReparseBuffer.SubstituteNameLength =
  531. (prefix_len + print_len) * sizeof(WCHAR);
  532. rdb->MountPointReparseBuffer.PrintNameOffset =
  533. rdb->MountPointReparseBuffer.SubstituteNameLength + sizeof(WCHAR);
  534. rdb->MountPointReparseBuffer.PrintNameLength = print_len * sizeof(WCHAR);
  535. /* Store the full native path of link target at the substitute name
  536. offset (0). */
  537. wcscpy(rdb->MountPointReparseBuffer.PathBuffer, L"\\??\\");
  538. if (GetFullPathNameW(src_path, print_len + 1,
  539. rdb->MountPointReparseBuffer.PathBuffer + prefix_len,
  540. NULL) == 0)
  541. goto cleanup;
  542. /* Copy everything but the native prefix to the print name offset. */
  543. wcscpy(rdb->MountPointReparseBuffer.PathBuffer +
  544. prefix_len + print_len + 1,
  545. rdb->MountPointReparseBuffer.PathBuffer + prefix_len);
  546. /* Create a directory for the junction point. */
  547. if (!CreateDirectoryW(dst_path, NULL))
  548. goto cleanup;
  549. junction = CreateFileW(dst_path, GENERIC_READ | GENERIC_WRITE, 0, NULL,
  550. OPEN_EXISTING,
  551. FILE_FLAG_OPEN_REPARSE_POINT | FILE_FLAG_BACKUP_SEMANTICS, NULL);
  552. if (junction == INVALID_HANDLE_VALUE)
  553. goto cleanup;
  554. /* Make the directory entry a junction point. */
  555. if (!DeviceIoControl(junction, FSCTL_SET_REPARSE_POINT, rdb, rdb_size,
  556. NULL, 0, &ret, NULL))
  557. goto cleanup;
  558. cleanup:
  559. ret = GetLastError();
  560. if (previousTpSize) {
  561. AdjustTokenPrivileges(token, FALSE, &previousTp.base, previousTpSize,
  562. NULL, NULL);
  563. }
  564. if (token != NULL)
  565. CloseHandle(token);
  566. if (junction != NULL)
  567. CloseHandle(junction);
  568. PyMem_RawFree(rdb);
  569. if (ret != 0)
  570. return PyErr_SetFromWindowsErr(ret);
  571. Py_RETURN_NONE;
  572. }
  573. /*[clinic input]
  574. _winapi.CreateNamedPipe -> HANDLE
  575. name: LPCWSTR
  576. open_mode: DWORD
  577. pipe_mode: DWORD
  578. max_instances: DWORD
  579. out_buffer_size: DWORD
  580. in_buffer_size: DWORD
  581. default_timeout: DWORD
  582. security_attributes: LPSECURITY_ATTRIBUTES
  583. /
  584. [clinic start generated code]*/
  585. static HANDLE
  586. _winapi_CreateNamedPipe_impl(PyObject *module, LPCWSTR name, DWORD open_mode,
  587. DWORD pipe_mode, DWORD max_instances,
  588. DWORD out_buffer_size, DWORD in_buffer_size,
  589. DWORD default_timeout,
  590. LPSECURITY_ATTRIBUTES security_attributes)
  591. /*[clinic end generated code: output=7d6fde93227680ba input=5bd4e4a55639ee02]*/
  592. {
  593. HANDLE handle;
  594. if (PySys_Audit("_winapi.CreateNamedPipe", "ukk",
  595. name, open_mode, pipe_mode) < 0) {
  596. return INVALID_HANDLE_VALUE;
  597. }
  598. Py_BEGIN_ALLOW_THREADS
  599. handle = CreateNamedPipeW(name, open_mode, pipe_mode,
  600. max_instances, out_buffer_size,
  601. in_buffer_size, default_timeout,
  602. security_attributes);
  603. Py_END_ALLOW_THREADS
  604. if (handle == INVALID_HANDLE_VALUE)
  605. PyErr_SetFromWindowsErr(0);
  606. return handle;
  607. }
  608. /*[clinic input]
  609. _winapi.CreatePipe
  610. pipe_attrs: object
  611. Ignored internally, can be None.
  612. size: DWORD
  613. /
  614. Create an anonymous pipe.
  615. Returns a 2-tuple of handles, to the read and write ends of the pipe.
  616. [clinic start generated code]*/
  617. static PyObject *
  618. _winapi_CreatePipe_impl(PyObject *module, PyObject *pipe_attrs, DWORD size)
  619. /*[clinic end generated code: output=1c4411d8699f0925 input=c4f2cfa56ef68d90]*/
  620. {
  621. HANDLE read_pipe;
  622. HANDLE write_pipe;
  623. BOOL result;
  624. if (PySys_Audit("_winapi.CreatePipe", NULL) < 0) {
  625. return NULL;
  626. }
  627. Py_BEGIN_ALLOW_THREADS
  628. result = CreatePipe(&read_pipe, &write_pipe, NULL, size);
  629. Py_END_ALLOW_THREADS
  630. if (! result)
  631. return PyErr_SetFromWindowsErr(GetLastError());
  632. return Py_BuildValue(
  633. "NN", HANDLE_TO_PYNUM(read_pipe), HANDLE_TO_PYNUM(write_pipe));
  634. }
  635. /* helpers for createprocess */
  636. static unsigned long
  637. getulong(PyObject* obj, const char* name)
  638. {
  639. PyObject* value;
  640. unsigned long ret;
  641. value = PyObject_GetAttrString(obj, name);
  642. if (! value) {
  643. PyErr_Clear(); /* FIXME: propagate error? */
  644. return 0;
  645. }
  646. ret = PyLong_AsUnsignedLong(value);
  647. Py_DECREF(value);
  648. return ret;
  649. }
  650. static HANDLE
  651. gethandle(PyObject* obj, const char* name)
  652. {
  653. PyObject* value;
  654. HANDLE ret;
  655. value = PyObject_GetAttrString(obj, name);
  656. if (! value) {
  657. PyErr_Clear(); /* FIXME: propagate error? */
  658. return NULL;
  659. }
  660. if (value == Py_None)
  661. ret = NULL;
  662. else
  663. ret = PYNUM_TO_HANDLE(value);
  664. Py_DECREF(value);
  665. return ret;
  666. }
  667. static PyObject *
  668. sortenvironmentkey(PyObject *module, PyObject *item)
  669. {
  670. return _winapi_LCMapStringEx_impl(NULL, LOCALE_NAME_INVARIANT,
  671. LCMAP_UPPERCASE, item);
  672. }
  673. static PyMethodDef sortenvironmentkey_def = {
  674. "sortenvironmentkey", _PyCFunction_CAST(sortenvironmentkey), METH_O, "",
  675. };
  676. static int
  677. sort_environment_keys(PyObject *keys)
  678. {
  679. PyObject *keyfunc = PyCFunction_New(&sortenvironmentkey_def, NULL);
  680. if (keyfunc == NULL) {
  681. return -1;
  682. }
  683. PyObject *kwnames = Py_BuildValue("(s)", "key");
  684. if (kwnames == NULL) {
  685. Py_DECREF(keyfunc);
  686. return -1;
  687. }
  688. PyObject *args[] = { keys, keyfunc };
  689. PyObject *ret = PyObject_VectorcallMethod(&_Py_ID(sort), args, 1, kwnames);
  690. Py_DECREF(keyfunc);
  691. Py_DECREF(kwnames);
  692. if (ret == NULL) {
  693. return -1;
  694. }
  695. Py_DECREF(ret);
  696. return 0;
  697. }
  698. static int
  699. compare_string_ordinal(PyObject *str1, PyObject *str2, int *result)
  700. {
  701. wchar_t *s1 = PyUnicode_AsWideCharString(str1, NULL);
  702. if (s1 == NULL) {
  703. return -1;
  704. }
  705. wchar_t *s2 = PyUnicode_AsWideCharString(str2, NULL);
  706. if (s2 == NULL) {
  707. PyMem_Free(s1);
  708. return -1;
  709. }
  710. *result = CompareStringOrdinal(s1, -1, s2, -1, TRUE);
  711. PyMem_Free(s1);
  712. PyMem_Free(s2);
  713. return 0;
  714. }
  715. static PyObject *
  716. dedup_environment_keys(PyObject *keys)
  717. {
  718. PyObject *result = PyList_New(0);
  719. if (result == NULL) {
  720. return NULL;
  721. }
  722. // Iterate over the pre-ordered keys, check whether the current key is equal
  723. // to the next key (ignoring case), if different, insert the current value
  724. // into the result list. If they are equal, do nothing because we always
  725. // want to keep the last inserted one.
  726. for (Py_ssize_t i = 0; i < PyList_GET_SIZE(keys); i++) {
  727. PyObject *key = PyList_GET_ITEM(keys, i);
  728. // The last key will always be kept.
  729. if (i + 1 == PyList_GET_SIZE(keys)) {
  730. if (PyList_Append(result, key) < 0) {
  731. Py_DECREF(result);
  732. return NULL;
  733. }
  734. continue;
  735. }
  736. PyObject *next_key = PyList_GET_ITEM(keys, i + 1);
  737. int compare_result;
  738. if (compare_string_ordinal(key, next_key, &compare_result) < 0) {
  739. Py_DECREF(result);
  740. return NULL;
  741. }
  742. if (compare_result == CSTR_EQUAL) {
  743. continue;
  744. }
  745. if (PyList_Append(result, key) < 0) {
  746. Py_DECREF(result);
  747. return NULL;
  748. }
  749. }
  750. return result;
  751. }
  752. static PyObject *
  753. normalize_environment(PyObject *environment)
  754. {
  755. PyObject *keys = PyMapping_Keys(environment);
  756. if (keys == NULL) {
  757. return NULL;
  758. }
  759. if (sort_environment_keys(keys) < 0) {
  760. Py_DECREF(keys);
  761. return NULL;
  762. }
  763. PyObject *normalized_keys = dedup_environment_keys(keys);
  764. Py_DECREF(keys);
  765. if (normalized_keys == NULL) {
  766. return NULL;
  767. }
  768. PyObject *result = PyDict_New();
  769. if (result == NULL) {
  770. Py_DECREF(normalized_keys);
  771. return NULL;
  772. }
  773. for (int i = 0; i < PyList_GET_SIZE(normalized_keys); i++) {
  774. PyObject *key = PyList_GET_ITEM(normalized_keys, i);
  775. PyObject *value = PyObject_GetItem(environment, key);
  776. if (value == NULL) {
  777. Py_DECREF(normalized_keys);
  778. Py_DECREF(result);
  779. return NULL;
  780. }
  781. int ret = PyObject_SetItem(result, key, value);
  782. Py_DECREF(value);
  783. if (ret < 0) {
  784. Py_DECREF(normalized_keys);
  785. Py_DECREF(result);
  786. return NULL;
  787. }
  788. }
  789. Py_DECREF(normalized_keys);
  790. return result;
  791. }
  792. static wchar_t *
  793. getenvironment(PyObject* environment)
  794. {
  795. Py_ssize_t i, envsize, totalsize;
  796. wchar_t *buffer = NULL, *p, *end;
  797. PyObject *normalized_environment = NULL;
  798. PyObject *keys = NULL;
  799. PyObject *values = NULL;
  800. /* convert environment dictionary to windows environment string */
  801. if (! PyMapping_Check(environment)) {
  802. PyErr_SetString(
  803. PyExc_TypeError, "environment must be dictionary or None");
  804. return NULL;
  805. }
  806. normalized_environment = normalize_environment(environment);
  807. if (normalize_environment == NULL) {
  808. return NULL;
  809. }
  810. keys = PyMapping_Keys(normalized_environment);
  811. if (!keys) {
  812. goto error;
  813. }
  814. values = PyMapping_Values(normalized_environment);
  815. if (!values) {
  816. goto error;
  817. }
  818. envsize = PyList_GET_SIZE(keys);
  819. if (envsize == 0) {
  820. // A environment block must be terminated by two null characters --
  821. // one for the last string and one for the block.
  822. buffer = PyMem_Calloc(2, sizeof(wchar_t));
  823. if (!buffer) {
  824. PyErr_NoMemory();
  825. }
  826. goto cleanup;
  827. }
  828. if (PyList_GET_SIZE(values) != envsize) {
  829. PyErr_SetString(PyExc_RuntimeError,
  830. "environment changed size during iteration");
  831. goto error;
  832. }
  833. totalsize = 1; /* trailing null character */
  834. for (i = 0; i < envsize; i++) {
  835. PyObject* key = PyList_GET_ITEM(keys, i);
  836. PyObject* value = PyList_GET_ITEM(values, i);
  837. Py_ssize_t size;
  838. if (! PyUnicode_Check(key) || ! PyUnicode_Check(value)) {
  839. PyErr_SetString(PyExc_TypeError,
  840. "environment can only contain strings");
  841. goto error;
  842. }
  843. if (PyUnicode_FindChar(key, '\0', 0, PyUnicode_GET_LENGTH(key), 1) != -1 ||
  844. PyUnicode_FindChar(value, '\0', 0, PyUnicode_GET_LENGTH(value), 1) != -1)
  845. {
  846. PyErr_SetString(PyExc_ValueError, "embedded null character");
  847. goto error;
  848. }
  849. /* Search from index 1 because on Windows starting '=' is allowed for
  850. defining hidden environment variables. */
  851. if (PyUnicode_GET_LENGTH(key) == 0 ||
  852. PyUnicode_FindChar(key, '=', 1, PyUnicode_GET_LENGTH(key), 1) != -1)
  853. {
  854. PyErr_SetString(PyExc_ValueError, "illegal environment variable name");
  855. goto error;
  856. }
  857. size = PyUnicode_AsWideChar(key, NULL, 0);
  858. assert(size > 1);
  859. if (totalsize > PY_SSIZE_T_MAX - size) {
  860. PyErr_SetString(PyExc_OverflowError, "environment too long");
  861. goto error;
  862. }
  863. totalsize += size; /* including '=' */
  864. size = PyUnicode_AsWideChar(value, NULL, 0);
  865. assert(size > 0);
  866. if (totalsize > PY_SSIZE_T_MAX - size) {
  867. PyErr_SetString(PyExc_OverflowError, "environment too long");
  868. goto error;
  869. }
  870. totalsize += size; /* including trailing '\0' */
  871. }
  872. buffer = PyMem_NEW(wchar_t, totalsize);
  873. if (! buffer) {
  874. PyErr_NoMemory();
  875. goto error;
  876. }
  877. p = buffer;
  878. end = buffer + totalsize;
  879. for (i = 0; i < envsize; i++) {
  880. PyObject* key = PyList_GET_ITEM(keys, i);
  881. PyObject* value = PyList_GET_ITEM(values, i);
  882. Py_ssize_t size = PyUnicode_AsWideChar(key, p, end - p);
  883. assert(1 <= size && size < end - p);
  884. p += size;
  885. *p++ = L'=';
  886. size = PyUnicode_AsWideChar(value, p, end - p);
  887. assert(0 <= size && size < end - p);
  888. p += size + 1;
  889. }
  890. /* add trailing null character */
  891. *p++ = L'\0';
  892. assert(p == end);
  893. cleanup:
  894. error:
  895. Py_XDECREF(normalized_environment);
  896. Py_XDECREF(keys);
  897. Py_XDECREF(values);
  898. return buffer;
  899. }
  900. static LPHANDLE
  901. gethandlelist(PyObject *mapping, const char *name, Py_ssize_t *size)
  902. {
  903. LPHANDLE ret = NULL;
  904. PyObject *value_fast = NULL;
  905. PyObject *value;
  906. Py_ssize_t i;
  907. value = PyMapping_GetItemString(mapping, name);
  908. if (!value) {
  909. PyErr_Clear();
  910. return NULL;
  911. }
  912. if (value == Py_None) {
  913. goto cleanup;
  914. }
  915. value_fast = PySequence_Fast(value, "handle_list must be a sequence or None");
  916. if (value_fast == NULL)
  917. goto cleanup;
  918. *size = PySequence_Fast_GET_SIZE(value_fast) * sizeof(HANDLE);
  919. /* Passing an empty array causes CreateProcess to fail so just don't set it */
  920. if (*size == 0) {
  921. goto cleanup;
  922. }
  923. ret = PyMem_Malloc(*size);
  924. if (ret == NULL)
  925. goto cleanup;
  926. for (i = 0; i < PySequence_Fast_GET_SIZE(value_fast); i++) {
  927. ret[i] = PYNUM_TO_HANDLE(PySequence_Fast_GET_ITEM(value_fast, i));
  928. if (ret[i] == (HANDLE)-1 && PyErr_Occurred()) {
  929. PyMem_Free(ret);
  930. ret = NULL;
  931. goto cleanup;
  932. }
  933. }
  934. cleanup:
  935. Py_DECREF(value);
  936. Py_XDECREF(value_fast);
  937. return ret;
  938. }
  939. typedef struct {
  940. LPPROC_THREAD_ATTRIBUTE_LIST attribute_list;
  941. LPHANDLE handle_list;
  942. } AttributeList;
  943. static void
  944. freeattributelist(AttributeList *attribute_list)
  945. {
  946. if (attribute_list->attribute_list != NULL) {
  947. DeleteProcThreadAttributeList(attribute_list->attribute_list);
  948. PyMem_Free(attribute_list->attribute_list);
  949. }
  950. PyMem_Free(attribute_list->handle_list);
  951. memset(attribute_list, 0, sizeof(*attribute_list));
  952. }
  953. static int
  954. getattributelist(PyObject *obj, const char *name, AttributeList *attribute_list)
  955. {
  956. int ret = 0;
  957. DWORD err;
  958. BOOL result;
  959. PyObject *value;
  960. Py_ssize_t handle_list_size;
  961. DWORD attribute_count = 0;
  962. SIZE_T attribute_list_size = 0;
  963. value = PyObject_GetAttrString(obj, name);
  964. if (!value) {
  965. PyErr_Clear(); /* FIXME: propagate error? */
  966. return 0;
  967. }
  968. if (value == Py_None) {
  969. ret = 0;
  970. goto cleanup;
  971. }
  972. if (!PyMapping_Check(value)) {
  973. ret = -1;
  974. PyErr_Format(PyExc_TypeError, "%s must be a mapping or None", name);
  975. goto cleanup;
  976. }
  977. attribute_list->handle_list = gethandlelist(value, "handle_list", &handle_list_size);
  978. if (attribute_list->handle_list == NULL && PyErr_Occurred()) {
  979. ret = -1;
  980. goto cleanup;
  981. }
  982. if (attribute_list->handle_list != NULL)
  983. ++attribute_count;
  984. /* Get how many bytes we need for the attribute list */
  985. result = InitializeProcThreadAttributeList(NULL, attribute_count, 0, &attribute_list_size);
  986. if (result || GetLastError() != ERROR_INSUFFICIENT_BUFFER) {
  987. ret = -1;
  988. PyErr_SetFromWindowsErr(GetLastError());
  989. goto cleanup;
  990. }
  991. attribute_list->attribute_list = PyMem_Malloc(attribute_list_size);
  992. if (attribute_list->attribute_list == NULL) {
  993. ret = -1;
  994. goto cleanup;
  995. }
  996. result = InitializeProcThreadAttributeList(
  997. attribute_list->attribute_list,
  998. attribute_count,
  999. 0,
  1000. &attribute_list_size);
  1001. if (!result) {
  1002. err = GetLastError();
  1003. /* So that we won't call DeleteProcThreadAttributeList */
  1004. PyMem_Free(attribute_list->attribute_list);
  1005. attribute_list->attribute_list = NULL;
  1006. ret = -1;
  1007. PyErr_SetFromWindowsErr(err);
  1008. goto cleanup;
  1009. }
  1010. if (attribute_list->handle_list != NULL) {
  1011. result = UpdateProcThreadAttribute(
  1012. attribute_list->attribute_list,
  1013. 0,
  1014. PROC_THREAD_ATTRIBUTE_HANDLE_LIST,
  1015. attribute_list->handle_list,
  1016. handle_list_size,
  1017. NULL,
  1018. NULL);
  1019. if (!result) {
  1020. ret = -1;
  1021. PyErr_SetFromWindowsErr(GetLastError());
  1022. goto cleanup;
  1023. }
  1024. }
  1025. cleanup:
  1026. Py_DECREF(value);
  1027. if (ret < 0)
  1028. freeattributelist(attribute_list);
  1029. return ret;
  1030. }
  1031. /*[clinic input]
  1032. _winapi.CreateProcess
  1033. application_name: Py_UNICODE(accept={str, NoneType})
  1034. command_line: object
  1035. Can be str or None
  1036. proc_attrs: object
  1037. Ignored internally, can be None.
  1038. thread_attrs: object
  1039. Ignored internally, can be None.
  1040. inherit_handles: BOOL
  1041. creation_flags: DWORD
  1042. env_mapping: object
  1043. current_directory: Py_UNICODE(accept={str, NoneType})
  1044. startup_info: object
  1045. /
  1046. Create a new process and its primary thread.
  1047. The return value is a tuple of the process handle, thread handle,
  1048. process ID, and thread ID.
  1049. [clinic start generated code]*/
  1050. static PyObject *
  1051. _winapi_CreateProcess_impl(PyObject *module,
  1052. const Py_UNICODE *application_name,
  1053. PyObject *command_line, PyObject *proc_attrs,
  1054. PyObject *thread_attrs, BOOL inherit_handles,
  1055. DWORD creation_flags, PyObject *env_mapping,
  1056. const Py_UNICODE *current_directory,
  1057. PyObject *startup_info)
  1058. /*[clinic end generated code: output=9b2423a609230132 input=42ac293eaea03fc4]*/
  1059. {
  1060. PyObject *ret = NULL;
  1061. BOOL result;
  1062. PROCESS_INFORMATION pi;
  1063. STARTUPINFOEXW si;
  1064. wchar_t *wenvironment = NULL;
  1065. wchar_t *command_line_copy = NULL;
  1066. AttributeList attribute_list = {0};
  1067. if (PySys_Audit("_winapi.CreateProcess", "uuu", application_name,
  1068. command_line, current_directory) < 0) {
  1069. return NULL;
  1070. }
  1071. ZeroMemory(&si, sizeof(si));
  1072. si.StartupInfo.cb = sizeof(si);
  1073. /* note: we only support a small subset of all SI attributes */
  1074. si.StartupInfo.dwFlags = getulong(startup_info, "dwFlags");
  1075. si.StartupInfo.wShowWindow = (WORD)getulong(startup_info, "wShowWindow");
  1076. si.StartupInfo.hStdInput = gethandle(startup_info, "hStdInput");
  1077. si.StartupInfo.hStdOutput = gethandle(startup_info, "hStdOutput");
  1078. si.StartupInfo.hStdError = gethandle(startup_info, "hStdError");
  1079. if (PyErr_Occurred())
  1080. goto cleanup;
  1081. if (env_mapping != Py_None) {
  1082. wenvironment = getenvironment(env_mapping);
  1083. if (wenvironment == NULL) {
  1084. goto cleanup;
  1085. }
  1086. }
  1087. if (getattributelist(startup_info, "lpAttributeList", &attribute_list) < 0)
  1088. goto cleanup;
  1089. si.lpAttributeList = attribute_list.attribute_list;
  1090. if (PyUnicode_Check(command_line)) {
  1091. command_line_copy = PyUnicode_AsWideCharString(command_line, NULL);
  1092. if (command_line_copy == NULL) {
  1093. goto cleanup;
  1094. }
  1095. }
  1096. else if (command_line != Py_None) {
  1097. PyErr_Format(PyExc_TypeError,
  1098. "CreateProcess() argument 2 must be str or None, not %s",
  1099. Py_TYPE(command_line)->tp_name);
  1100. goto cleanup;
  1101. }
  1102. Py_BEGIN_ALLOW_THREADS
  1103. result = CreateProcessW(application_name,
  1104. command_line_copy,
  1105. NULL,
  1106. NULL,
  1107. inherit_handles,
  1108. creation_flags | EXTENDED_STARTUPINFO_PRESENT |
  1109. CREATE_UNICODE_ENVIRONMENT,
  1110. wenvironment,
  1111. current_directory,
  1112. (LPSTARTUPINFOW)&si,
  1113. &pi);
  1114. Py_END_ALLOW_THREADS
  1115. if (!result) {
  1116. PyErr_SetFromWindowsErr(GetLastError());
  1117. goto cleanup;
  1118. }
  1119. ret = Py_BuildValue("NNkk",
  1120. HANDLE_TO_PYNUM(pi.hProcess),
  1121. HANDLE_TO_PYNUM(pi.hThread),
  1122. pi.dwProcessId,
  1123. pi.dwThreadId);
  1124. cleanup:
  1125. PyMem_Free(command_line_copy);
  1126. PyMem_Free(wenvironment);
  1127. freeattributelist(&attribute_list);
  1128. return ret;
  1129. }
  1130. /*[clinic input]
  1131. _winapi.DuplicateHandle -> HANDLE
  1132. source_process_handle: HANDLE
  1133. source_handle: HANDLE
  1134. target_process_handle: HANDLE
  1135. desired_access: DWORD
  1136. inherit_handle: BOOL
  1137. options: DWORD = 0
  1138. /
  1139. Return a duplicate handle object.
  1140. The duplicate handle refers to the same object as the original
  1141. handle. Therefore, any changes to the object are reflected
  1142. through both handles.
  1143. [clinic start generated code]*/
  1144. static HANDLE
  1145. _winapi_DuplicateHandle_impl(PyObject *module, HANDLE source_process_handle,
  1146. HANDLE source_handle,
  1147. HANDLE target_process_handle,
  1148. DWORD desired_access, BOOL inherit_handle,
  1149. DWORD options)
  1150. /*[clinic end generated code: output=ad9711397b5dcd4e input=b933e3f2356a8c12]*/
  1151. {
  1152. HANDLE target_handle;
  1153. BOOL result;
  1154. Py_BEGIN_ALLOW_THREADS
  1155. result = DuplicateHandle(
  1156. source_process_handle,
  1157. source_handle,
  1158. target_process_handle,
  1159. &target_handle,
  1160. desired_access,
  1161. inherit_handle,
  1162. options
  1163. );
  1164. Py_END_ALLOW_THREADS
  1165. if (! result) {
  1166. PyErr_SetFromWindowsErr(GetLastError());
  1167. return INVALID_HANDLE_VALUE;
  1168. }
  1169. return target_handle;
  1170. }
  1171. /*[clinic input]
  1172. _winapi.ExitProcess
  1173. ExitCode: UINT
  1174. /
  1175. [clinic start generated code]*/
  1176. static PyObject *
  1177. _winapi_ExitProcess_impl(PyObject *module, UINT ExitCode)
  1178. /*[clinic end generated code: output=a387deb651175301 input=4f05466a9406c558]*/
  1179. {
  1180. #if defined(Py_DEBUG)
  1181. #ifdef MS_WINDOWS_DESKTOP
  1182. SetErrorMode(SEM_FAILCRITICALERRORS|SEM_NOALIGNMENTFAULTEXCEPT|
  1183. SEM_NOGPFAULTERRORBOX|SEM_NOOPENFILEERRORBOX);
  1184. #endif
  1185. _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_DEBUG);
  1186. #endif
  1187. ExitProcess(ExitCode);
  1188. return NULL;
  1189. }
  1190. /*[clinic input]
  1191. _winapi.GetCurrentProcess -> HANDLE
  1192. Return a handle object for the current process.
  1193. [clinic start generated code]*/
  1194. static HANDLE
  1195. _winapi_GetCurrentProcess_impl(PyObject *module)
  1196. /*[clinic end generated code: output=ddeb4dd2ffadf344 input=b213403fd4b96b41]*/
  1197. {
  1198. return GetCurrentProcess();
  1199. }
  1200. /*[clinic input]
  1201. _winapi.GetExitCodeProcess -> DWORD
  1202. process: HANDLE
  1203. /
  1204. Return the termination status of the specified process.
  1205. [clinic start generated code]*/
  1206. static DWORD
  1207. _winapi_GetExitCodeProcess_impl(PyObject *module, HANDLE process)
  1208. /*[clinic end generated code: output=b4620bdf2bccf36b input=61b6bfc7dc2ee374]*/
  1209. {
  1210. DWORD exit_code;
  1211. BOOL result;
  1212. result = GetExitCodeProcess(process, &exit_code);
  1213. if (! result) {
  1214. PyErr_SetFromWindowsErr(GetLastError());
  1215. exit_code = PY_DWORD_MAX;
  1216. }
  1217. return exit_code;
  1218. }
  1219. /*[clinic input]
  1220. _winapi.GetLastError -> DWORD
  1221. [clinic start generated code]*/
  1222. static DWORD
  1223. _winapi_GetLastError_impl(PyObject *module)
  1224. /*[clinic end generated code: output=8585b827cb1a92c5 input=62d47fb9bce038ba]*/
  1225. {
  1226. return GetLastError();
  1227. }
  1228. /*[clinic input]
  1229. _winapi.GetLongPathName
  1230. path: LPCWSTR
  1231. Return the long version of the provided path.
  1232. If the path is already in its long form, returns the same value.
  1233. The path must already be a 'str'. If the type is not known, use
  1234. os.fsdecode before calling this function.
  1235. [clinic start generated code]*/
  1236. static PyObject *
  1237. _winapi_GetLongPathName_impl(PyObject *module, LPCWSTR path)
  1238. /*[clinic end generated code: output=c4774b080275a2d0 input=9872e211e3a4a88f]*/
  1239. {
  1240. DWORD cchBuffer;
  1241. PyObject *result = NULL;
  1242. Py_BEGIN_ALLOW_THREADS
  1243. cchBuffer = GetLongPathNameW(path, NULL, 0);
  1244. Py_END_ALLOW_THREADS
  1245. if (cchBuffer) {
  1246. WCHAR *buffer = (WCHAR *)PyMem_Malloc(cchBuffer * sizeof(WCHAR));
  1247. if (buffer) {
  1248. Py_BEGIN_ALLOW_THREADS
  1249. cchBuffer = GetLongPathNameW(path, buffer, cchBuffer);
  1250. Py_END_ALLOW_THREADS
  1251. if (cchBuffer) {
  1252. result = PyUnicode_FromWideChar(buffer, cchBuffer);
  1253. } else {
  1254. PyErr_SetFromWindowsErr(0);
  1255. }
  1256. PyMem_Free((void *)buffer);
  1257. }
  1258. } else {
  1259. PyErr_SetFromWindowsErr(0);
  1260. }
  1261. return result;
  1262. }
  1263. /*[clinic input]
  1264. _winapi.GetModuleFileName
  1265. module_handle: HMODULE
  1266. /
  1267. Return the fully-qualified path for the file that contains module.
  1268. The module must have been loaded by the current process.
  1269. The module parameter should be a handle to the loaded module
  1270. whose path is being requested. If this parameter is 0,
  1271. GetModuleFileName retrieves the path of the executable file
  1272. of the current process.
  1273. [clinic start generated code]*/
  1274. static PyObject *
  1275. _winapi_GetModuleFileName_impl(PyObject *module, HMODULE module_handle)
  1276. /*[clinic end generated code: output=85b4b728c5160306 input=6d66ff7deca5d11f]*/
  1277. {
  1278. BOOL result;
  1279. WCHAR filename[MAX_PATH];
  1280. Py_BEGIN_ALLOW_THREADS
  1281. result = GetModuleFileNameW(module_handle, filename, MAX_PATH);
  1282. filename[MAX_PATH-1] = '\0';
  1283. Py_END_ALLOW_THREADS
  1284. if (! result)
  1285. return PyErr_SetFromWindowsErr(GetLastError());
  1286. return PyUnicode_FromWideChar(filename, wcslen(filename));
  1287. }
  1288. /*[clinic input]
  1289. _winapi.GetShortPathName
  1290. path: LPCWSTR
  1291. Return the short version of the provided path.
  1292. If the path is already in its short form, returns the same value.
  1293. The path must already be a 'str'. If the type is not known, use
  1294. os.fsdecode before calling this function.
  1295. [clinic start generated code]*/
  1296. static PyObject *
  1297. _winapi_GetShortPathName_impl(PyObject *module, LPCWSTR path)
  1298. /*[clinic end generated code: output=dab6ae494c621e81 input=43fa349aaf2ac718]*/
  1299. {
  1300. DWORD cchBuffer;
  1301. PyObject *result = NULL;
  1302. Py_BEGIN_ALLOW_THREADS
  1303. cchBuffer = GetShortPathNameW(path, NULL, 0);
  1304. Py_END_ALLOW_THREADS
  1305. if (cchBuffer) {
  1306. WCHAR *buffer = (WCHAR *)PyMem_Malloc(cchBuffer * sizeof(WCHAR));
  1307. if (buffer) {
  1308. Py_BEGIN_ALLOW_THREADS
  1309. cchBuffer = GetShortPathNameW(path, buffer, cchBuffer);
  1310. Py_END_ALLOW_THREADS
  1311. if (cchBuffer) {
  1312. result = PyUnicode_FromWideChar(buffer, cchBuffer);
  1313. } else {
  1314. PyErr_SetFromWindowsErr(0);
  1315. }
  1316. PyMem_Free((void *)buffer);
  1317. }
  1318. } else {
  1319. PyErr_SetFromWindowsErr(0);
  1320. }
  1321. return result;
  1322. }
  1323. /*[clinic input]
  1324. _winapi.GetStdHandle -> HANDLE
  1325. std_handle: DWORD
  1326. One of STD_INPUT_HANDLE, STD_OUTPUT_HANDLE, or STD_ERROR_HANDLE.
  1327. /
  1328. Return a handle to the specified standard device.
  1329. The integer associated with the handle object is returned.
  1330. [clinic start generated code]*/
  1331. static HANDLE
  1332. _winapi_GetStdHandle_impl(PyObject *module, DWORD std_handle)
  1333. /*[clinic end generated code: output=0e613001e73ab614 input=07016b06a2fc8826]*/
  1334. {
  1335. HANDLE handle;
  1336. Py_BEGIN_ALLOW_THREADS
  1337. handle = GetStdHandle(std_handle);
  1338. Py_END_ALLOW_THREADS
  1339. if (handle == INVALID_HANDLE_VALUE)
  1340. PyErr_SetFromWindowsErr(GetLastError());
  1341. return handle;
  1342. }
  1343. /*[clinic input]
  1344. _winapi.GetVersion -> long
  1345. Return the version number of the current operating system.
  1346. [clinic start generated code]*/
  1347. static long
  1348. _winapi_GetVersion_impl(PyObject *module)
  1349. /*[clinic end generated code: output=e41f0db5a3b82682 input=e21dff8d0baeded2]*/
  1350. /* Disable deprecation warnings about GetVersionEx as the result is
  1351. being passed straight through to the caller, who is responsible for
  1352. using it correctly. */
  1353. #pragma warning(push)
  1354. #pragma warning(disable:4996)
  1355. {
  1356. return GetVersion();
  1357. }
  1358. #pragma warning(pop)
  1359. /*[clinic input]
  1360. _winapi.MapViewOfFile -> LPVOID
  1361. file_map: HANDLE
  1362. desired_access: DWORD
  1363. file_offset_high: DWORD
  1364. file_offset_low: DWORD
  1365. number_bytes: size_t
  1366. /
  1367. [clinic start generated code]*/
  1368. static LPVOID
  1369. _winapi_MapViewOfFile_impl(PyObject *module, HANDLE file_map,
  1370. DWORD desired_access, DWORD file_offset_high,
  1371. DWORD file_offset_low, size_t number_bytes)
  1372. /*[clinic end generated code: output=f23b1ee4823663e3 input=177471073be1a103]*/
  1373. {
  1374. LPVOID address;
  1375. Py_BEGIN_ALLOW_THREADS
  1376. address = MapViewOfFile(file_map, desired_access, file_offset_high,
  1377. file_offset_low, number_bytes);
  1378. Py_END_ALLOW_THREADS
  1379. if (address == NULL)
  1380. PyErr_SetFromWindowsErr(0);
  1381. return address;
  1382. }
  1383. /*[clinic input]
  1384. _winapi.UnmapViewOfFile
  1385. address: LPCVOID
  1386. /
  1387. [clinic start generated code]*/
  1388. static PyObject *
  1389. _winapi_UnmapViewOfFile_impl(PyObject *module, LPCVOID address)
  1390. /*[clinic end generated code: output=4f7e18ac75d19744 input=8c4b6119ad9288a3]*/
  1391. {
  1392. BOOL success;
  1393. Py_BEGIN_ALLOW_THREADS
  1394. success = UnmapViewOfFile(address);
  1395. Py_END_ALLOW_THREADS
  1396. if (!success) {
  1397. return PyErr_SetFromWindowsErr(0);
  1398. }
  1399. Py_RETURN_NONE;
  1400. }
  1401. /*[clinic input]
  1402. _winapi.OpenFileMapping -> HANDLE
  1403. desired_access: DWORD
  1404. inherit_handle: BOOL
  1405. name: LPCWSTR
  1406. /
  1407. [clinic start generated code]*/
  1408. static HANDLE
  1409. _winapi_OpenFileMapping_impl(PyObject *module, DWORD desired_access,
  1410. BOOL inherit_handle, LPCWSTR name)
  1411. /*[clinic end generated code: output=08cc44def1cb11f1 input=131f2a405359de7f]*/
  1412. {
  1413. HANDLE handle;
  1414. Py_BEGIN_ALLOW_THREADS
  1415. handle = OpenFileMappingW(desired_access, inherit_handle, name);
  1416. Py_END_ALLOW_THREADS
  1417. if (handle == NULL) {
  1418. PyObject *temp = PyUnicode_FromWideChar(name, -1);
  1419. PyErr_SetExcFromWindowsErrWithFilenameObject(PyExc_OSError, 0, temp);
  1420. Py_XDECREF(temp);
  1421. handle = INVALID_HANDLE_VALUE;
  1422. }
  1423. return handle;
  1424. }
  1425. /*[clinic input]
  1426. _winapi.OpenProcess -> HANDLE
  1427. desired_access: DWORD
  1428. inherit_handle: BOOL
  1429. process_id: DWORD
  1430. /
  1431. [clinic start generated code]*/
  1432. static HANDLE
  1433. _winapi_OpenProcess_impl(PyObject *module, DWORD desired_access,
  1434. BOOL inherit_handle, DWORD process_id)
  1435. /*[clinic end generated code: output=b42b6b81ea5a0fc3 input=ec98c4cf4ea2ec36]*/
  1436. {
  1437. HANDLE handle;
  1438. if (PySys_Audit("_winapi.OpenProcess", "kk",
  1439. process_id, desired_access) < 0) {
  1440. return INVALID_HANDLE_VALUE;
  1441. }
  1442. Py_BEGIN_ALLOW_THREADS
  1443. handle = OpenProcess(desired_access, inherit_handle, process_id);
  1444. Py_END_ALLOW_THREADS
  1445. if (handle == NULL) {
  1446. PyErr_SetFromWindowsErr(GetLastError());
  1447. handle = INVALID_HANDLE_VALUE;
  1448. }
  1449. return handle;
  1450. }
  1451. /*[clinic input]
  1452. _winapi.PeekNamedPipe
  1453. handle: HANDLE
  1454. size: int = 0
  1455. /
  1456. [clinic start generated code]*/
  1457. static PyObject *
  1458. _winapi_PeekNamedPipe_impl(PyObject *module, HANDLE handle, int size)
  1459. /*[clinic end generated code: output=d0c3e29e49d323dd input=c7aa53bfbce69d70]*/
  1460. {
  1461. PyObject *buf = NULL;
  1462. DWORD nread, navail, nleft;
  1463. BOOL ret;
  1464. if (size < 0) {
  1465. PyErr_SetString(PyExc_ValueError, "negative size");
  1466. return NULL;
  1467. }
  1468. if (size) {
  1469. buf = PyBytes_FromStringAndSize(NULL, size);
  1470. if (!buf)
  1471. return NULL;
  1472. Py_BEGIN_ALLOW_THREADS
  1473. ret = PeekNamedPipe(handle, PyBytes_AS_STRING(buf), size, &nread,
  1474. &navail, &nleft);
  1475. Py_END_ALLOW_THREADS
  1476. if (!ret) {
  1477. Py_DECREF(buf);
  1478. return PyErr_SetExcFromWindowsErr(PyExc_OSError, 0);
  1479. }
  1480. if (_PyBytes_Resize(&buf, nread))
  1481. return NULL;
  1482. return Py_BuildValue("NII", buf, navail, nleft);
  1483. }
  1484. else {
  1485. Py_BEGIN_ALLOW_THREADS
  1486. ret = PeekNamedPipe(handle, NULL, 0, NULL, &navail, &nleft);
  1487. Py_END_ALLOW_THREADS
  1488. if (!ret) {
  1489. return PyErr_SetExcFromWindowsErr(PyExc_OSError, 0);
  1490. }
  1491. return Py_BuildValue("II", navail, nleft);
  1492. }
  1493. }
  1494. /*[clinic input]
  1495. _winapi.LCMapStringEx
  1496. locale: LPCWSTR
  1497. flags: DWORD
  1498. src: unicode
  1499. [clinic start generated code]*/
  1500. static PyObject *
  1501. _winapi_LCMapStringEx_impl(PyObject *module, LPCWSTR locale, DWORD flags,
  1502. PyObject *src)
  1503. /*[clinic end generated code: output=b90e6b26e028ff0a input=3e3dcd9b8164012f]*/
  1504. {
  1505. if (flags & (LCMAP_SORTHANDLE | LCMAP_HASH | LCMAP_BYTEREV |
  1506. LCMAP_SORTKEY)) {
  1507. return PyErr_Format(PyExc_ValueError, "unsupported flags");
  1508. }
  1509. Py_ssize_t src_size;
  1510. wchar_t *src_ = PyUnicode_AsWideCharString(src, &src_size);
  1511. if (!src_) {
  1512. return NULL;
  1513. }
  1514. if (src_size > INT_MAX) {
  1515. PyMem_Free(src_);
  1516. PyErr_SetString(PyExc_OverflowError, "input string is too long");
  1517. return NULL;
  1518. }
  1519. int dest_size = LCMapStringEx(locale, flags, src_, (int)src_size, NULL, 0,
  1520. NULL, NULL, 0);
  1521. if (dest_size <= 0) {
  1522. DWORD error = GetLastError();
  1523. PyMem_Free(src_);
  1524. return PyErr_SetFromWindowsErr(error);
  1525. }
  1526. wchar_t* dest = PyMem_NEW(wchar_t, dest_size);
  1527. if (dest == NULL) {
  1528. PyMem_Free(src_);
  1529. return PyErr_NoMemory();
  1530. }
  1531. int nmapped = LCMapStringEx(locale, flags, src_, (int)src_size, dest, dest_size,
  1532. NULL, NULL, 0);
  1533. if (nmapped <= 0) {
  1534. DWORD error = GetLastError();
  1535. PyMem_Free(src_);
  1536. PyMem_DEL(dest);
  1537. return PyErr_SetFromWindowsErr(error);
  1538. }
  1539. PyMem_Free(src_);
  1540. PyObject *ret = PyUnicode_FromWideChar(dest, nmapped);
  1541. PyMem_DEL(dest);
  1542. return ret;
  1543. }
  1544. /*[clinic input]
  1545. _winapi.ReadFile
  1546. handle: HANDLE
  1547. size: DWORD
  1548. overlapped as use_overlapped: bool = False
  1549. [clinic start generated code]*/
  1550. static PyObject *
  1551. _winapi_ReadFile_impl(PyObject *module, HANDLE handle, DWORD size,
  1552. int use_overlapped)
  1553. /*[clinic end generated code: output=d3d5b44a8201b944 input=4f82f8e909ad91ad]*/
  1554. {
  1555. DWORD nread;
  1556. PyObject *buf;
  1557. BOOL ret;
  1558. DWORD err;
  1559. OverlappedObject *overlapped = NULL;
  1560. buf = PyBytes_FromStringAndSize(NULL, size);
  1561. if (!buf)
  1562. return NULL;
  1563. if (use_overlapped) {
  1564. overlapped = new_overlapped(module, handle);
  1565. if (!overlapped) {
  1566. Py_DECREF(buf);
  1567. return NULL;
  1568. }
  1569. /* Steals reference to buf */
  1570. overlapped->read_buffer = buf;
  1571. }
  1572. Py_BEGIN_ALLOW_THREADS
  1573. ret = ReadFile(handle, PyBytes_AS_STRING(buf), size, &nread,
  1574. overlapped ? &overlapped->overlapped : NULL);
  1575. Py_END_ALLOW_THREADS
  1576. err = ret ? 0 : GetLastError();
  1577. if (overlapped) {
  1578. if (!ret) {
  1579. if (err == ERROR_IO_PENDING)
  1580. overlapped->pending = 1;
  1581. else if (err != ERROR_MORE_DATA) {
  1582. Py_DECREF(overlapped);
  1583. return PyErr_SetExcFromWindowsErr(PyExc_OSError, 0);
  1584. }
  1585. }
  1586. return Py_BuildValue("NI", (PyObject *) overlapped, err);
  1587. }
  1588. if (!ret && err != ERROR_MORE_DATA) {
  1589. Py_DECREF(buf);
  1590. return PyErr_SetExcFromWindowsErr(PyExc_OSError, 0);
  1591. }
  1592. if (_PyBytes_Resize(&buf, nread))
  1593. return NULL;
  1594. return Py_BuildValue("NI", buf, err);
  1595. }
  1596. /*[clinic input]
  1597. _winapi.SetNamedPipeHandleState
  1598. named_pipe: HANDLE
  1599. mode: object
  1600. max_collection_count: object
  1601. collect_data_timeout: object
  1602. /
  1603. [clinic start generated code]*/
  1604. static PyObject *
  1605. _winapi_SetNamedPipeHandleState_impl(PyObject *module, HANDLE named_pipe,
  1606. PyObject *mode,
  1607. PyObject *max_collection_count,
  1608. PyObject *collect_data_timeout)
  1609. /*[clinic end generated code: output=f2129d222cbfa095 input=9142d72163d0faa6]*/
  1610. {
  1611. PyObject *oArgs[3] = {mode, max_collection_count, collect_data_timeout};
  1612. DWORD dwArgs[3], *pArgs[3] = {NULL, NULL, NULL};
  1613. int i;
  1614. BOOL b;
  1615. for (i = 0 ; i < 3 ; i++) {
  1616. if (oArgs[i] != Py_None) {
  1617. dwArgs[i] = PyLong_AsUnsignedLongMask(oArgs[i]);
  1618. if (PyErr_Occurred())
  1619. return NULL;
  1620. pArgs[i] = &dwArgs[i];
  1621. }
  1622. }
  1623. Py_BEGIN_ALLOW_THREADS
  1624. b = SetNamedPipeHandleState(named_pipe, pArgs[0], pArgs[1], pArgs[2]);
  1625. Py_END_ALLOW_THREADS
  1626. if (!b)
  1627. return PyErr_SetFromWindowsErr(0);
  1628. Py_RETURN_NONE;
  1629. }
  1630. /*[clinic input]
  1631. _winapi.TerminateProcess
  1632. handle: HANDLE
  1633. exit_code: UINT
  1634. /
  1635. Terminate the specified process and all of its threads.
  1636. [clinic start generated code]*/
  1637. static PyObject *
  1638. _winapi_TerminateProcess_impl(PyObject *module, HANDLE handle,
  1639. UINT exit_code)
  1640. /*[clinic end generated code: output=f4e99ac3f0b1f34a input=d6bc0aa1ee3bb4df]*/
  1641. {
  1642. BOOL result;
  1643. if (PySys_Audit("_winapi.TerminateProcess", "nI",
  1644. (Py_ssize_t)handle, exit_code) < 0) {
  1645. return NULL;
  1646. }
  1647. result = TerminateProcess(handle, exit_code);
  1648. if (! result)
  1649. return PyErr_SetFromWindowsErr(GetLastError());
  1650. Py_RETURN_NONE;
  1651. }
  1652. /*[clinic input]
  1653. _winapi.VirtualQuerySize -> size_t
  1654. address: LPCVOID
  1655. /
  1656. [clinic start generated code]*/
  1657. static size_t
  1658. _winapi_VirtualQuerySize_impl(PyObject *module, LPCVOID address)
  1659. /*[clinic end generated code: output=40c8e0ff5ec964df input=6b784a69755d0bb6]*/
  1660. {
  1661. SIZE_T size_of_buf;
  1662. MEMORY_BASIC_INFORMATION mem_basic_info;
  1663. SIZE_T region_size;
  1664. Py_BEGIN_ALLOW_THREADS
  1665. size_of_buf = VirtualQuery(address, &mem_basic_info, sizeof(mem_basic_info));
  1666. Py_END_ALLOW_THREADS
  1667. if (size_of_buf == 0)
  1668. PyErr_SetFromWindowsErr(0);
  1669. region_size = mem_basic_info.RegionSize;
  1670. return region_size;
  1671. }
  1672. /*[clinic input]
  1673. _winapi.WaitNamedPipe
  1674. name: LPCWSTR
  1675. timeout: DWORD
  1676. /
  1677. [clinic start generated code]*/
  1678. static PyObject *
  1679. _winapi_WaitNamedPipe_impl(PyObject *module, LPCWSTR name, DWORD timeout)
  1680. /*[clinic end generated code: output=e161e2e630b3e9c2 input=099a4746544488fa]*/
  1681. {
  1682. BOOL success;
  1683. Py_BEGIN_ALLOW_THREADS
  1684. success = WaitNamedPipeW(name, timeout);
  1685. Py_END_ALLOW_THREADS
  1686. if (!success)
  1687. return PyErr_SetFromWindowsErr(0);
  1688. Py_RETURN_NONE;
  1689. }
  1690. /*[clinic input]
  1691. _winapi.WaitForMultipleObjects
  1692. handle_seq: object
  1693. wait_flag: BOOL
  1694. milliseconds: DWORD(c_default='INFINITE') = _winapi.INFINITE
  1695. /
  1696. [clinic start generated code]*/
  1697. static PyObject *
  1698. _winapi_WaitForMultipleObjects_impl(PyObject *module, PyObject *handle_seq,
  1699. BOOL wait_flag, DWORD milliseconds)
  1700. /*[clinic end generated code: output=295e3f00b8e45899 input=36f76ca057cd28a0]*/
  1701. {
  1702. DWORD result;
  1703. HANDLE handles[MAXIMUM_WAIT_OBJECTS];
  1704. HANDLE sigint_event = NULL;
  1705. Py_ssize_t nhandles, i;
  1706. if (!PySequence_Check(handle_seq)) {
  1707. PyErr_Format(PyExc_TypeError,
  1708. "sequence type expected, got '%s'",
  1709. Py_TYPE(handle_seq)->tp_name);
  1710. return NULL;
  1711. }
  1712. nhandles = PySequence_Length(handle_seq);
  1713. if (nhandles == -1)
  1714. return NULL;
  1715. if (nhandles < 0 || nhandles > MAXIMUM_WAIT_OBJECTS - 1) {
  1716. PyErr_Format(PyExc_ValueError,
  1717. "need at most %zd handles, got a sequence of length %zd",
  1718. MAXIMUM_WAIT_OBJECTS - 1, nhandles);
  1719. return NULL;
  1720. }
  1721. for (i = 0; i < nhandles; i++) {
  1722. HANDLE h;
  1723. PyObject *v = PySequence_GetItem(handle_seq, i);
  1724. if (v == NULL)
  1725. return NULL;
  1726. if (!PyArg_Parse(v, F_HANDLE, &h)) {
  1727. Py_DECREF(v);
  1728. return NULL;
  1729. }
  1730. handles[i] = h;
  1731. Py_DECREF(v);
  1732. }
  1733. /* If this is the main thread then make the wait interruptible
  1734. by Ctrl-C unless we are waiting for *all* handles */
  1735. if (!wait_flag && _PyOS_IsMainThread()) {
  1736. sigint_event = _PyOS_SigintEvent();
  1737. assert(sigint_event != NULL);
  1738. handles[nhandles++] = sigint_event;
  1739. }
  1740. Py_BEGIN_ALLOW_THREADS
  1741. if (sigint_event != NULL)
  1742. ResetEvent(sigint_event);
  1743. result = WaitForMultipleObjects((DWORD) nhandles, handles,
  1744. wait_flag, milliseconds);
  1745. Py_END_ALLOW_THREADS
  1746. if (result == WAIT_FAILED)
  1747. return PyErr_SetExcFromWindowsErr(PyExc_OSError, 0);
  1748. else if (sigint_event != NULL && result == WAIT_OBJECT_0 + nhandles - 1) {
  1749. errno = EINTR;
  1750. return PyErr_SetFromErrno(PyExc_OSError);
  1751. }
  1752. return PyLong_FromLong((int) result);
  1753. }
  1754. /*[clinic input]
  1755. _winapi.WaitForSingleObject -> long
  1756. handle: HANDLE
  1757. milliseconds: DWORD
  1758. /
  1759. Wait for a single object.
  1760. Wait until the specified object is in the signaled state or
  1761. the time-out interval elapses. The timeout value is specified
  1762. in milliseconds.
  1763. [clinic start generated code]*/
  1764. static long
  1765. _winapi_WaitForSingleObject_impl(PyObject *module, HANDLE handle,
  1766. DWORD milliseconds)
  1767. /*[clinic end generated code: output=3c4715d8f1b39859 input=443d1ab076edc7b1]*/
  1768. {
  1769. DWORD result;
  1770. Py_BEGIN_ALLOW_THREADS
  1771. result = WaitForSingleObject(handle, milliseconds);
  1772. Py_END_ALLOW_THREADS
  1773. if (result == WAIT_FAILED) {
  1774. PyErr_SetFromWindowsErr(GetLastError());
  1775. return -1;
  1776. }
  1777. return result;
  1778. }
  1779. /*[clinic input]
  1780. _winapi.WriteFile
  1781. handle: HANDLE
  1782. buffer: object
  1783. overlapped as use_overlapped: bool = False
  1784. [clinic start generated code]*/
  1785. static PyObject *
  1786. _winapi_WriteFile_impl(PyObject *module, HANDLE handle, PyObject *buffer,
  1787. int use_overlapped)
  1788. /*[clinic end generated code: output=2ca80f6bf3fa92e3 input=2badb008c8a2e2a0]*/
  1789. {
  1790. Py_buffer _buf, *buf;
  1791. DWORD len, written;
  1792. BOOL ret;
  1793. DWORD err;
  1794. OverlappedObject *overlapped = NULL;
  1795. if (use_overlapped) {
  1796. overlapped = new_overlapped(module, handle);
  1797. if (!overlapped)
  1798. return NULL;
  1799. buf = &overlapped->write_buffer;
  1800. }
  1801. else
  1802. buf = &_buf;
  1803. if (!PyArg_Parse(buffer, "y*", buf)) {
  1804. Py_XDECREF(overlapped);
  1805. return NULL;
  1806. }
  1807. Py_BEGIN_ALLOW_THREADS
  1808. len = (DWORD)Py_MIN(buf->len, PY_DWORD_MAX);
  1809. ret = WriteFile(handle, buf->buf, len, &written,
  1810. overlapped ? &overlapped->overlapped : NULL);
  1811. Py_END_ALLOW_THREADS
  1812. err = ret ? 0 : GetLastError();
  1813. if (overlapped) {
  1814. if (!ret) {
  1815. if (err == ERROR_IO_PENDING)
  1816. overlapped->pending = 1;
  1817. else {
  1818. Py_DECREF(overlapped);
  1819. return PyErr_SetExcFromWindowsErr(PyExc_OSError, 0);
  1820. }
  1821. }
  1822. return Py_BuildValue("NI", (PyObject *) overlapped, err);
  1823. }
  1824. PyBuffer_Release(buf);
  1825. if (!ret)
  1826. return PyErr_SetExcFromWindowsErr(PyExc_OSError, 0);
  1827. return Py_BuildValue("II", written, err);
  1828. }
  1829. /*[clinic input]
  1830. _winapi.GetACP
  1831. Get the current Windows ANSI code page identifier.
  1832. [clinic start generated code]*/
  1833. static PyObject *
  1834. _winapi_GetACP_impl(PyObject *module)
  1835. /*[clinic end generated code: output=f7ee24bf705dbb88 input=1433c96d03a05229]*/
  1836. {
  1837. return PyLong_FromUnsignedLong(GetACP());
  1838. }
  1839. /*[clinic input]
  1840. _winapi.GetFileType -> DWORD
  1841. handle: HANDLE
  1842. [clinic start generated code]*/
  1843. static DWORD
  1844. _winapi_GetFileType_impl(PyObject *module, HANDLE handle)
  1845. /*[clinic end generated code: output=92b8466ac76ecc17 input=0058366bc40bbfbf]*/
  1846. {
  1847. DWORD result;
  1848. Py_BEGIN_ALLOW_THREADS
  1849. result = GetFileType(handle);
  1850. Py_END_ALLOW_THREADS
  1851. if (result == FILE_TYPE_UNKNOWN && GetLastError() != NO_ERROR) {
  1852. PyErr_SetFromWindowsErr(0);
  1853. return -1;
  1854. }
  1855. return result;
  1856. }
  1857. /*[clinic input]
  1858. _winapi._mimetypes_read_windows_registry
  1859. on_type_read: object
  1860. Optimized function for reading all known MIME types from the registry.
  1861. *on_type_read* is a callable taking *type* and *ext* arguments, as for
  1862. MimeTypes.add_type.
  1863. [clinic start generated code]*/
  1864. static PyObject *
  1865. _winapi__mimetypes_read_windows_registry_impl(PyObject *module,
  1866. PyObject *on_type_read)
  1867. /*[clinic end generated code: output=20829f00bebce55b input=cd357896d6501f68]*/
  1868. {
  1869. #define CCH_EXT 128
  1870. #define CB_TYPE 510
  1871. struct {
  1872. wchar_t ext[CCH_EXT];
  1873. wchar_t type[CB_TYPE / sizeof(wchar_t) + 1];
  1874. } entries[64];
  1875. int entry = 0;
  1876. HKEY hkcr = NULL;
  1877. LRESULT err;
  1878. Py_BEGIN_ALLOW_THREADS
  1879. err = RegOpenKeyExW(HKEY_CLASSES_ROOT, NULL, 0, KEY_READ, &hkcr);
  1880. for (DWORD i = 0; err == ERROR_SUCCESS || err == ERROR_MORE_DATA; ++i) {
  1881. LPWSTR ext = entries[entry].ext;
  1882. LPWSTR type = entries[entry].type;
  1883. DWORD cchExt = CCH_EXT;
  1884. DWORD cbType = CB_TYPE;
  1885. HKEY subkey;
  1886. DWORD regType;
  1887. err = RegEnumKeyExW(hkcr, i, ext, &cchExt, NULL, NULL, NULL, NULL);
  1888. if (err != ERROR_SUCCESS || (cchExt && ext[0] != L'.')) {
  1889. continue;
  1890. }
  1891. err = RegOpenKeyExW(hkcr, ext, 0, KEY_READ, &subkey);
  1892. if (err == ERROR_FILE_NOT_FOUND || err == ERROR_ACCESS_DENIED) {
  1893. err = ERROR_SUCCESS;
  1894. continue;
  1895. } else if (err != ERROR_SUCCESS) {
  1896. continue;
  1897. }
  1898. err = RegQueryValueExW(subkey, L"Content Type", NULL,
  1899. &regType, (LPBYTE)type, &cbType);
  1900. RegCloseKey(subkey);
  1901. if (err == ERROR_FILE_NOT_FOUND) {
  1902. err = ERROR_SUCCESS;
  1903. continue;
  1904. } else if (err != ERROR_SUCCESS) {
  1905. continue;
  1906. } else if (regType != REG_SZ || !cbType) {
  1907. continue;
  1908. }
  1909. type[cbType / sizeof(wchar_t)] = L'\0';
  1910. entry += 1;
  1911. /* Flush our cached entries if we are full */
  1912. if (entry == sizeof(entries) / sizeof(entries[0])) {
  1913. Py_BLOCK_THREADS
  1914. for (int j = 0; j < entry; ++j) {
  1915. PyObject *r = PyObject_CallFunction(
  1916. on_type_read, "uu", entries[j].type, entries[j].ext
  1917. );
  1918. if (!r) {
  1919. /* We blocked threads, so safe to return from here */
  1920. RegCloseKey(hkcr);
  1921. return NULL;
  1922. }
  1923. Py_DECREF(r);
  1924. }
  1925. Py_UNBLOCK_THREADS
  1926. entry = 0;
  1927. }
  1928. }
  1929. if (hkcr) {
  1930. RegCloseKey(hkcr);
  1931. }
  1932. Py_END_ALLOW_THREADS
  1933. if (err != ERROR_SUCCESS && err != ERROR_NO_MORE_ITEMS) {
  1934. PyErr_SetFromWindowsErr((int)err);
  1935. return NULL;
  1936. }
  1937. for (int j = 0; j < entry; ++j) {
  1938. PyObject *r = PyObject_CallFunction(
  1939. on_type_read, "uu", entries[j].type, entries[j].ext
  1940. );
  1941. if (!r) {
  1942. return NULL;
  1943. }
  1944. Py_DECREF(r);
  1945. }
  1946. Py_RETURN_NONE;
  1947. #undef CCH_EXT
  1948. #undef CB_TYPE
  1949. }
  1950. /*[clinic input]
  1951. _winapi.NeedCurrentDirectoryForExePath -> bool
  1952. exe_name: LPCWSTR
  1953. /
  1954. [clinic start generated code]*/
  1955. static int
  1956. _winapi_NeedCurrentDirectoryForExePath_impl(PyObject *module,
  1957. LPCWSTR exe_name)
  1958. /*[clinic end generated code: output=a65ec879502b58fc input=972aac88a1ec2f00]*/
  1959. {
  1960. BOOL result;
  1961. Py_BEGIN_ALLOW_THREADS
  1962. result = NeedCurrentDirectoryForExePathW(exe_name);
  1963. Py_END_ALLOW_THREADS
  1964. return result;
  1965. }
  1966. /*[clinic input]
  1967. _winapi.CopyFile2
  1968. existing_file_name: LPCWSTR
  1969. new_file_name: LPCWSTR
  1970. flags: DWORD
  1971. progress_routine: object = None
  1972. Copies a file from one name to a new name.
  1973. This is implemented using the CopyFile2 API, which preserves all stat
  1974. and metadata information apart from security attributes.
  1975. progress_routine is reserved for future use, but is currently not
  1976. implemented. Its value is ignored.
  1977. [clinic start generated code]*/
  1978. static PyObject *
  1979. _winapi_CopyFile2_impl(PyObject *module, LPCWSTR existing_file_name,
  1980. LPCWSTR new_file_name, DWORD flags,
  1981. PyObject *progress_routine)
  1982. /*[clinic end generated code: output=43d960d9df73d984 input=fb976b8d1492d130]*/
  1983. {
  1984. HRESULT hr;
  1985. COPYFILE2_EXTENDED_PARAMETERS params = { sizeof(COPYFILE2_EXTENDED_PARAMETERS) };
  1986. if (PySys_Audit("_winapi.CopyFile2", "uuk",
  1987. existing_file_name, new_file_name, flags) < 0) {
  1988. return NULL;
  1989. }
  1990. params.dwCopyFlags = flags;
  1991. /* For future implementation. We ignore the value for now so that
  1992. users only have to test for 'CopyFile2' existing and not whether
  1993. the additional parameter exists.
  1994. if (progress_routine != Py_None) {
  1995. params.pProgressRoutine = _winapi_CopyFile2ProgressRoutine;
  1996. params.pvCallbackContext = Py_NewRef(progress_routine);
  1997. }
  1998. */
  1999. Py_BEGIN_ALLOW_THREADS;
  2000. hr = CopyFile2(existing_file_name, new_file_name, &params);
  2001. Py_END_ALLOW_THREADS;
  2002. /* For future implementation.
  2003. if (progress_routine != Py_None) {
  2004. Py_DECREF(progress_routine);
  2005. }
  2006. */
  2007. if (FAILED(hr)) {
  2008. if ((hr & 0xFFFF0000) == 0x80070000) {
  2009. PyErr_SetFromWindowsErr(hr & 0xFFFF);
  2010. } else {
  2011. PyErr_SetFromWindowsErr(hr);
  2012. }
  2013. return NULL;
  2014. }
  2015. Py_RETURN_NONE;
  2016. }
  2017. static PyMethodDef winapi_functions[] = {
  2018. _WINAPI_CLOSEHANDLE_METHODDEF
  2019. _WINAPI_CONNECTNAMEDPIPE_METHODDEF
  2020. _WINAPI_CREATEFILE_METHODDEF
  2021. _WINAPI_CREATEFILEMAPPING_METHODDEF
  2022. _WINAPI_CREATENAMEDPIPE_METHODDEF
  2023. _WINAPI_CREATEPIPE_METHODDEF
  2024. _WINAPI_CREATEPROCESS_METHODDEF
  2025. _WINAPI_CREATEJUNCTION_METHODDEF
  2026. _WINAPI_DUPLICATEHANDLE_METHODDEF
  2027. _WINAPI_EXITPROCESS_METHODDEF
  2028. _WINAPI_GETCURRENTPROCESS_METHODDEF
  2029. _WINAPI_GETEXITCODEPROCESS_METHODDEF
  2030. _WINAPI_GETLASTERROR_METHODDEF
  2031. _WINAPI_GETLONGPATHNAME_METHODDEF
  2032. _WINAPI_GETMODULEFILENAME_METHODDEF
  2033. _WINAPI_GETSHORTPATHNAME_METHODDEF
  2034. _WINAPI_GETSTDHANDLE_METHODDEF
  2035. _WINAPI_GETVERSION_METHODDEF
  2036. _WINAPI_MAPVIEWOFFILE_METHODDEF
  2037. _WINAPI_OPENFILEMAPPING_METHODDEF
  2038. _WINAPI_OPENPROCESS_METHODDEF
  2039. _WINAPI_PEEKNAMEDPIPE_METHODDEF
  2040. _WINAPI_LCMAPSTRINGEX_METHODDEF
  2041. _WINAPI_READFILE_METHODDEF
  2042. _WINAPI_SETNAMEDPIPEHANDLESTATE_METHODDEF
  2043. _WINAPI_TERMINATEPROCESS_METHODDEF
  2044. _WINAPI_UNMAPVIEWOFFILE_METHODDEF
  2045. _WINAPI_VIRTUALQUERYSIZE_METHODDEF
  2046. _WINAPI_WAITNAMEDPIPE_METHODDEF
  2047. _WINAPI_WAITFORMULTIPLEOBJECTS_METHODDEF
  2048. _WINAPI_WAITFORSINGLEOBJECT_METHODDEF
  2049. _WINAPI_WRITEFILE_METHODDEF
  2050. _WINAPI_GETACP_METHODDEF
  2051. _WINAPI_GETFILETYPE_METHODDEF
  2052. _WINAPI__MIMETYPES_READ_WINDOWS_REGISTRY_METHODDEF
  2053. _WINAPI_NEEDCURRENTDIRECTORYFOREXEPATH_METHODDEF
  2054. _WINAPI_COPYFILE2_METHODDEF
  2055. {NULL, NULL}
  2056. };
  2057. #define WINAPI_CONSTANT(fmt, con) \
  2058. do { \
  2059. PyObject *value = Py_BuildValue(fmt, con); \
  2060. if (value == NULL) { \
  2061. return -1; \
  2062. } \
  2063. if (PyDict_SetItemString(d, #con, value) < 0) { \
  2064. Py_DECREF(value); \
  2065. return -1; \
  2066. } \
  2067. Py_DECREF(value); \
  2068. } while (0)
  2069. static int winapi_exec(PyObject *m)
  2070. {
  2071. WinApiState *st = winapi_get_state(m);
  2072. st->overlapped_type = (PyTypeObject *)PyType_FromModuleAndSpec(m, &winapi_overlapped_type_spec, NULL);
  2073. if (st->overlapped_type == NULL) {
  2074. return -1;
  2075. }
  2076. if (PyModule_AddType(m, st->overlapped_type) < 0) {
  2077. return -1;
  2078. }
  2079. PyObject *d = PyModule_GetDict(m);
  2080. /* constants */
  2081. WINAPI_CONSTANT(F_DWORD, CREATE_NEW_CONSOLE);
  2082. WINAPI_CONSTANT(F_DWORD, CREATE_NEW_PROCESS_GROUP);
  2083. WINAPI_CONSTANT(F_DWORD, DUPLICATE_SAME_ACCESS);
  2084. WINAPI_CONSTANT(F_DWORD, DUPLICATE_CLOSE_SOURCE);
  2085. WINAPI_CONSTANT(F_DWORD, ERROR_ACCESS_DENIED);
  2086. WINAPI_CONSTANT(F_DWORD, ERROR_ALREADY_EXISTS);
  2087. WINAPI_CONSTANT(F_DWORD, ERROR_BROKEN_PIPE);
  2088. WINAPI_CONSTANT(F_DWORD, ERROR_IO_PENDING);
  2089. WINAPI_CONSTANT(F_DWORD, ERROR_MORE_DATA);
  2090. WINAPI_CONSTANT(F_DWORD, ERROR_NETNAME_DELETED);
  2091. WINAPI_CONSTANT(F_DWORD, ERROR_NO_SYSTEM_RESOURCES);
  2092. WINAPI_CONSTANT(F_DWORD, ERROR_MORE_DATA);
  2093. WINAPI_CONSTANT(F_DWORD, ERROR_NETNAME_DELETED);
  2094. WINAPI_CONSTANT(F_DWORD, ERROR_NO_DATA);
  2095. WINAPI_CONSTANT(F_DWORD, ERROR_NO_SYSTEM_RESOURCES);
  2096. WINAPI_CONSTANT(F_DWORD, ERROR_OPERATION_ABORTED);
  2097. WINAPI_CONSTANT(F_DWORD, ERROR_PIPE_BUSY);
  2098. WINAPI_CONSTANT(F_DWORD, ERROR_PIPE_CONNECTED);
  2099. WINAPI_CONSTANT(F_DWORD, ERROR_PRIVILEGE_NOT_HELD);
  2100. WINAPI_CONSTANT(F_DWORD, ERROR_SEM_TIMEOUT);
  2101. WINAPI_CONSTANT(F_DWORD, FILE_FLAG_FIRST_PIPE_INSTANCE);
  2102. WINAPI_CONSTANT(F_DWORD, FILE_FLAG_OVERLAPPED);
  2103. WINAPI_CONSTANT(F_DWORD, FILE_GENERIC_READ);
  2104. WINAPI_CONSTANT(F_DWORD, FILE_GENERIC_WRITE);
  2105. WINAPI_CONSTANT(F_DWORD, FILE_MAP_ALL_ACCESS);
  2106. WINAPI_CONSTANT(F_DWORD, FILE_MAP_COPY);
  2107. WINAPI_CONSTANT(F_DWORD, FILE_MAP_EXECUTE);
  2108. WINAPI_CONSTANT(F_DWORD, FILE_MAP_READ);
  2109. WINAPI_CONSTANT(F_DWORD, FILE_MAP_WRITE);
  2110. WINAPI_CONSTANT(F_DWORD, GENERIC_READ);
  2111. WINAPI_CONSTANT(F_DWORD, GENERIC_WRITE);
  2112. WINAPI_CONSTANT(F_DWORD, INFINITE);
  2113. WINAPI_CONSTANT(F_HANDLE, INVALID_HANDLE_VALUE);
  2114. WINAPI_CONSTANT(F_DWORD, MEM_COMMIT);
  2115. WINAPI_CONSTANT(F_DWORD, MEM_FREE);
  2116. WINAPI_CONSTANT(F_DWORD, MEM_IMAGE);
  2117. WINAPI_CONSTANT(F_DWORD, MEM_MAPPED);
  2118. WINAPI_CONSTANT(F_DWORD, MEM_PRIVATE);
  2119. WINAPI_CONSTANT(F_DWORD, MEM_RESERVE);
  2120. WINAPI_CONSTANT(F_DWORD, NMPWAIT_WAIT_FOREVER);
  2121. WINAPI_CONSTANT(F_DWORD, OPEN_EXISTING);
  2122. WINAPI_CONSTANT(F_DWORD, PAGE_EXECUTE);
  2123. WINAPI_CONSTANT(F_DWORD, PAGE_EXECUTE_READ);
  2124. WINAPI_CONSTANT(F_DWORD, PAGE_EXECUTE_READWRITE);
  2125. WINAPI_CONSTANT(F_DWORD, PAGE_EXECUTE_WRITECOPY);
  2126. WINAPI_CONSTANT(F_DWORD, PAGE_GUARD);
  2127. WINAPI_CONSTANT(F_DWORD, PAGE_NOACCESS);
  2128. WINAPI_CONSTANT(F_DWORD, PAGE_NOCACHE);
  2129. WINAPI_CONSTANT(F_DWORD, PAGE_READONLY);
  2130. WINAPI_CONSTANT(F_DWORD, PAGE_READWRITE);
  2131. WINAPI_CONSTANT(F_DWORD, PAGE_WRITECOMBINE);
  2132. WINAPI_CONSTANT(F_DWORD, PAGE_WRITECOPY);
  2133. WINAPI_CONSTANT(F_DWORD, PIPE_ACCESS_DUPLEX);
  2134. WINAPI_CONSTANT(F_DWORD, PIPE_ACCESS_INBOUND);
  2135. WINAPI_CONSTANT(F_DWORD, PIPE_READMODE_MESSAGE);
  2136. WINAPI_CONSTANT(F_DWORD, PIPE_TYPE_MESSAGE);
  2137. WINAPI_CONSTANT(F_DWORD, PIPE_UNLIMITED_INSTANCES);
  2138. WINAPI_CONSTANT(F_DWORD, PIPE_WAIT);
  2139. WINAPI_CONSTANT(F_DWORD, PROCESS_ALL_ACCESS);
  2140. WINAPI_CONSTANT(F_DWORD, SYNCHRONIZE);
  2141. WINAPI_CONSTANT(F_DWORD, PROCESS_DUP_HANDLE);
  2142. WINAPI_CONSTANT(F_DWORD, SEC_COMMIT);
  2143. WINAPI_CONSTANT(F_DWORD, SEC_IMAGE);
  2144. WINAPI_CONSTANT(F_DWORD, SEC_LARGE_PAGES);
  2145. WINAPI_CONSTANT(F_DWORD, SEC_NOCACHE);
  2146. WINAPI_CONSTANT(F_DWORD, SEC_RESERVE);
  2147. WINAPI_CONSTANT(F_DWORD, SEC_WRITECOMBINE);
  2148. WINAPI_CONSTANT(F_DWORD, STARTF_USESHOWWINDOW);
  2149. WINAPI_CONSTANT(F_DWORD, STARTF_USESTDHANDLES);
  2150. WINAPI_CONSTANT(F_DWORD, STD_INPUT_HANDLE);
  2151. WINAPI_CONSTANT(F_DWORD, STD_OUTPUT_HANDLE);
  2152. WINAPI_CONSTANT(F_DWORD, STD_ERROR_HANDLE);
  2153. WINAPI_CONSTANT(F_DWORD, STILL_ACTIVE);
  2154. WINAPI_CONSTANT(F_DWORD, SW_HIDE);
  2155. WINAPI_CONSTANT(F_DWORD, WAIT_OBJECT_0);
  2156. WINAPI_CONSTANT(F_DWORD, WAIT_ABANDONED_0);
  2157. WINAPI_CONSTANT(F_DWORD, WAIT_TIMEOUT);
  2158. WINAPI_CONSTANT(F_DWORD, ABOVE_NORMAL_PRIORITY_CLASS);
  2159. WINAPI_CONSTANT(F_DWORD, BELOW_NORMAL_PRIORITY_CLASS);
  2160. WINAPI_CONSTANT(F_DWORD, HIGH_PRIORITY_CLASS);
  2161. WINAPI_CONSTANT(F_DWORD, IDLE_PRIORITY_CLASS);
  2162. WINAPI_CONSTANT(F_DWORD, NORMAL_PRIORITY_CLASS);
  2163. WINAPI_CONSTANT(F_DWORD, REALTIME_PRIORITY_CLASS);
  2164. WINAPI_CONSTANT(F_DWORD, CREATE_NO_WINDOW);
  2165. WINAPI_CONSTANT(F_DWORD, DETACHED_PROCESS);
  2166. WINAPI_CONSTANT(F_DWORD, CREATE_DEFAULT_ERROR_MODE);
  2167. WINAPI_CONSTANT(F_DWORD, CREATE_BREAKAWAY_FROM_JOB);
  2168. WINAPI_CONSTANT(F_DWORD, FILE_TYPE_UNKNOWN);
  2169. WINAPI_CONSTANT(F_DWORD, FILE_TYPE_DISK);
  2170. WINAPI_CONSTANT(F_DWORD, FILE_TYPE_CHAR);
  2171. WINAPI_CONSTANT(F_DWORD, FILE_TYPE_PIPE);
  2172. WINAPI_CONSTANT(F_DWORD, FILE_TYPE_REMOTE);
  2173. WINAPI_CONSTANT("u", LOCALE_NAME_INVARIANT);
  2174. WINAPI_CONSTANT(F_DWORD, LOCALE_NAME_MAX_LENGTH);
  2175. WINAPI_CONSTANT("u", LOCALE_NAME_SYSTEM_DEFAULT);
  2176. WINAPI_CONSTANT("u", LOCALE_NAME_USER_DEFAULT);
  2177. WINAPI_CONSTANT(F_DWORD, LCMAP_FULLWIDTH);
  2178. WINAPI_CONSTANT(F_DWORD, LCMAP_HALFWIDTH);
  2179. WINAPI_CONSTANT(F_DWORD, LCMAP_HIRAGANA);
  2180. WINAPI_CONSTANT(F_DWORD, LCMAP_KATAKANA);
  2181. WINAPI_CONSTANT(F_DWORD, LCMAP_LINGUISTIC_CASING);
  2182. WINAPI_CONSTANT(F_DWORD, LCMAP_LOWERCASE);
  2183. WINAPI_CONSTANT(F_DWORD, LCMAP_SIMPLIFIED_CHINESE);
  2184. WINAPI_CONSTANT(F_DWORD, LCMAP_TITLECASE);
  2185. WINAPI_CONSTANT(F_DWORD, LCMAP_TRADITIONAL_CHINESE);
  2186. WINAPI_CONSTANT(F_DWORD, LCMAP_UPPERCASE);
  2187. WINAPI_CONSTANT(F_DWORD, COPY_FILE_ALLOW_DECRYPTED_DESTINATION);
  2188. WINAPI_CONSTANT(F_DWORD, COPY_FILE_COPY_SYMLINK);
  2189. WINAPI_CONSTANT(F_DWORD, COPY_FILE_FAIL_IF_EXISTS);
  2190. WINAPI_CONSTANT(F_DWORD, COPY_FILE_NO_BUFFERING);
  2191. WINAPI_CONSTANT(F_DWORD, COPY_FILE_NO_OFFLOAD);
  2192. WINAPI_CONSTANT(F_DWORD, COPY_FILE_OPEN_SOURCE_FOR_WRITE);
  2193. WINAPI_CONSTANT(F_DWORD, COPY_FILE_RESTARTABLE);
  2194. WINAPI_CONSTANT(F_DWORD, COPY_FILE_REQUEST_SECURITY_PRIVILEGES);
  2195. WINAPI_CONSTANT(F_DWORD, COPY_FILE_RESUME_FROM_PAUSE);
  2196. #ifndef COPY_FILE_REQUEST_COMPRESSED_TRAFFIC
  2197. // Only defined in newer WinSDKs
  2198. #define COPY_FILE_REQUEST_COMPRESSED_TRAFFIC 0x10000000
  2199. #endif
  2200. WINAPI_CONSTANT(F_DWORD, COPY_FILE_REQUEST_COMPRESSED_TRAFFIC);
  2201. WINAPI_CONSTANT(F_DWORD, COPYFILE2_CALLBACK_CHUNK_STARTED);
  2202. WINAPI_CONSTANT(F_DWORD, COPYFILE2_CALLBACK_CHUNK_FINISHED);
  2203. WINAPI_CONSTANT(F_DWORD, COPYFILE2_CALLBACK_STREAM_STARTED);
  2204. WINAPI_CONSTANT(F_DWORD, COPYFILE2_CALLBACK_STREAM_FINISHED);
  2205. WINAPI_CONSTANT(F_DWORD, COPYFILE2_CALLBACK_POLL_CONTINUE);
  2206. WINAPI_CONSTANT(F_DWORD, COPYFILE2_CALLBACK_ERROR);
  2207. WINAPI_CONSTANT(F_DWORD, COPYFILE2_PROGRESS_CONTINUE);
  2208. WINAPI_CONSTANT(F_DWORD, COPYFILE2_PROGRESS_CANCEL);
  2209. WINAPI_CONSTANT(F_DWORD, COPYFILE2_PROGRESS_STOP);
  2210. WINAPI_CONSTANT(F_DWORD, COPYFILE2_PROGRESS_QUIET);
  2211. WINAPI_CONSTANT(F_DWORD, COPYFILE2_PROGRESS_PAUSE);
  2212. WINAPI_CONSTANT("i", NULL);
  2213. return 0;
  2214. }
  2215. static PyModuleDef_Slot winapi_slots[] = {
  2216. {Py_mod_exec, winapi_exec},
  2217. {Py_mod_multiple_interpreters, Py_MOD_PER_INTERPRETER_GIL_SUPPORTED},
  2218. {0, NULL}
  2219. };
  2220. static int
  2221. winapi_traverse(PyObject *module, visitproc visit, void *arg)
  2222. {
  2223. WinApiState *st = winapi_get_state(module);
  2224. Py_VISIT(st->overlapped_type);
  2225. return 0;
  2226. }
  2227. static int
  2228. winapi_clear(PyObject *module)
  2229. {
  2230. WinApiState *st = winapi_get_state(module);
  2231. Py_CLEAR(st->overlapped_type);
  2232. return 0;
  2233. }
  2234. static void
  2235. winapi_free(void *module)
  2236. {
  2237. winapi_clear((PyObject *)module);
  2238. }
  2239. static struct PyModuleDef winapi_module = {
  2240. PyModuleDef_HEAD_INIT,
  2241. .m_name = "_winapi",
  2242. .m_size = sizeof(WinApiState),
  2243. .m_methods = winapi_functions,
  2244. .m_slots = winapi_slots,
  2245. .m_traverse = winapi_traverse,
  2246. .m_clear = winapi_clear,
  2247. .m_free = winapi_free,
  2248. };
  2249. PyMODINIT_FUNC
  2250. PyInit__winapi(void)
  2251. {
  2252. return PyModuleDef_Init(&winapi_module);
  2253. }