Ignore:
Timestamp:
Mar 19, 2014, 11:31:01 PM (11 years ago)
Author:
dmik
Message:

python: Merge vendor 2.7.6 to trunk.

Location:
python/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • python/trunk

  • python/trunk/PC/_subprocess.c

    r2 r391  
    5050
    5151typedef struct {
    52         PyObject_HEAD
    53         HANDLE handle;
     52    PyObject_HEAD
     53    HANDLE handle;
    5454} sp_handle_object;
    5555
     
    5959sp_handle_new(HANDLE handle)
    6060{
    61         sp_handle_object* self;
    62 
    63         self = PyObject_NEW(sp_handle_object, &sp_handle_type);
    64         if (self == NULL)
    65                 return NULL;
    66 
    67         self->handle = handle;
    68 
    69         return (PyObject*) self;
     61    sp_handle_object* self;
     62
     63    self = PyObject_NEW(sp_handle_object, &sp_handle_type);
     64    if (self == NULL)
     65        return NULL;
     66
     67    self->handle = handle;
     68
     69    return (PyObject*) self;
    7070}
    7171
    7272#if defined(MS_WIN32) && !defined(MS_WIN64)
    73 #define HANDLE_TO_PYNUM(handle) PyInt_FromLong((long) handle)
    74 #define PY_HANDLE_PARAM "l"
     73#define HANDLE_TO_PYNUM(handle) PyInt_FromLong((long) handle)
     74#define PY_HANDLE_PARAM "l"
    7575#else
    76 #define HANDLE_TO_PYNUM(handle) PyLong_FromLongLong((long long) handle)
    77 #define PY_HANDLE_PARAM "L"
     76#define HANDLE_TO_PYNUM(handle) PyLong_FromLongLong((long long) handle)
     77#define PY_HANDLE_PARAM "L"
    7878#endif
    7979
     
    8181sp_handle_detach(sp_handle_object* self, PyObject* args)
    8282{
    83         HANDLE handle;
    84 
    85         if (! PyArg_ParseTuple(args, ":Detach"))
    86                 return NULL;
    87 
    88         handle = self->handle;
    89 
    90         self->handle = INVALID_HANDLE_VALUE;
    91 
    92         /* note: return the current handle, as an integer */
    93         return HANDLE_TO_PYNUM(handle);
     83    HANDLE handle;
     84
     85    if (! PyArg_ParseTuple(args, ":Detach"))
     86        return NULL;
     87
     88    handle = self->handle;
     89
     90    self->handle = INVALID_HANDLE_VALUE;
     91
     92    /* note: return the current handle, as an integer */
     93    return HANDLE_TO_PYNUM(handle);
    9494}
    9595
     
    9797sp_handle_close(sp_handle_object* self, PyObject* args)
    9898{
    99         if (! PyArg_ParseTuple(args, ":Close"))
    100                 return NULL;
    101 
    102         if (self->handle != INVALID_HANDLE_VALUE) {
    103                 CloseHandle(self->handle);
    104                 self->handle = INVALID_HANDLE_VALUE;
    105         }
    106         Py_INCREF(Py_None);
    107         return Py_None;
     99    if (! PyArg_ParseTuple(args, ":Close"))
     100        return NULL;
     101
     102    if (self->handle != INVALID_HANDLE_VALUE) {
     103        CloseHandle(self->handle);
     104        self->handle = INVALID_HANDLE_VALUE;
     105    }
     106    Py_INCREF(Py_None);
     107    return Py_None;
    108108}
    109109
     
    111111sp_handle_dealloc(sp_handle_object* self)
    112112{
    113         if (self->handle != INVALID_HANDLE_VALUE)
    114                 CloseHandle(self->handle);
    115         PyObject_FREE(self);
     113    if (self->handle != INVALID_HANDLE_VALUE)
     114        CloseHandle(self->handle);
     115    PyObject_FREE(self);
    116116}
    117117
    118118static PyMethodDef sp_handle_methods[] = {
    119         {"Detach", (PyCFunction) sp_handle_detach, METH_VARARGS},
    120         {"Close",  (PyCFunction) sp_handle_close,  METH_VARARGS},
    121         {NULL, NULL}
     119    {"Detach", (PyCFunction) sp_handle_detach, METH_VARARGS},
     120    {"Close",  (PyCFunction) sp_handle_close,  METH_VARARGS},
     121    {NULL, NULL}
    122122};
    123123
     
    125125sp_handle_getattr(sp_handle_object* self, char* name)
    126126{
    127         return Py_FindMethod(sp_handle_methods, (PyObject*) self, name);
     127    return Py_FindMethod(sp_handle_methods, (PyObject*) self, name);
    128128}
    129129
     
    131131sp_handle_as_int(sp_handle_object* self)
    132132{
    133         return HANDLE_TO_PYNUM(self->handle);
     133    return HANDLE_TO_PYNUM(self->handle);
    134134}
    135135
     
    137137
    138138statichere PyTypeObject sp_handle_type = {
    139         PyObject_HEAD_INIT(NULL)
    140         0,                              /*ob_size*/
    141         "_subprocess_handle", sizeof(sp_handle_object), 0,
    142         (destructor) sp_handle_dealloc, /*tp_dealloc*/
    143         0, /*tp_print*/
    144         (getattrfunc) sp_handle_getattr,/*tp_getattr*/
    145         0,                              /*tp_setattr*/
    146         0,                              /*tp_compare*/
    147         0,                              /*tp_repr*/
    148         &sp_handle_as_number,           /*tp_as_number */
    149         0,                              /*tp_as_sequence */
    150         0,                              /*tp_as_mapping */
    151         0                               /*tp_hash*/
     139    PyObject_HEAD_INIT(NULL)
     140    0,                                  /*ob_size*/
     141    "_subprocess_handle", sizeof(sp_handle_object), 0,
     142    (destructor) sp_handle_dealloc, /*tp_dealloc*/
     143    0, /*tp_print*/
     144    (getattrfunc) sp_handle_getattr,/*tp_getattr*/
     145    0,                                  /*tp_setattr*/
     146    0,                                  /*tp_compare*/
     147    0,                                  /*tp_repr*/
     148    &sp_handle_as_number,               /*tp_as_number */
     149    0,                                  /*tp_as_sequence */
     150    0,                                  /*tp_as_mapping */
     151    0                                   /*tp_hash*/
    152152};
    153153
     
    155155/* windows API functions */
    156156
     157PyDoc_STRVAR(GetStdHandle_doc,
     158"GetStdHandle(handle) -> integer\n\
     159\n\
     160Return a handle to the specified standard device\n\
     161(STD_INPUT_HANDLE, STD_OUTPUT_HANDLE, STD_ERROR_HANDLE).\n\
     162The integer associated with the handle object is returned.");
     163
    157164static PyObject *
    158165sp_GetStdHandle(PyObject* self, PyObject* args)
    159166{
    160         HANDLE handle;
    161         int std_handle;
    162 
    163         if (! PyArg_ParseTuple(args, "i:GetStdHandle", &std_handle))
    164                 return NULL;
    165 
    166         Py_BEGIN_ALLOW_THREADS
    167         handle = GetStdHandle((DWORD) std_handle);
    168         Py_END_ALLOW_THREADS
    169 
    170         if (handle == INVALID_HANDLE_VALUE)
    171                 return PyErr_SetFromWindowsErr(GetLastError());
    172 
    173         if (! handle) {
    174                 Py_INCREF(Py_None);
    175                 return Py_None;
    176         }
    177 
    178         /* note: returns integer, not handle object */
    179         return HANDLE_TO_PYNUM(handle);
    180 }
     167    HANDLE handle;
     168    int std_handle;
     169
     170    if (! PyArg_ParseTuple(args, "i:GetStdHandle", &std_handle))
     171        return NULL;
     172
     173    Py_BEGIN_ALLOW_THREADS
     174    handle = GetStdHandle((DWORD) std_handle);
     175    Py_END_ALLOW_THREADS
     176
     177    if (handle == INVALID_HANDLE_VALUE)
     178        return PyErr_SetFromWindowsErr(GetLastError());
     179
     180    if (! handle) {
     181        Py_INCREF(Py_None);
     182        return Py_None;
     183    }
     184
     185    /* note: returns integer, not handle object */
     186    return HANDLE_TO_PYNUM(handle);
     187}
     188
     189PyDoc_STRVAR(GetCurrentProcess_doc,
     190"GetCurrentProcess() -> handle\n\
     191\n\
     192Return a handle object for the current process.");
    181193
    182194static PyObject *
    183195sp_GetCurrentProcess(PyObject* self, PyObject* args)
    184196{
    185         if (! PyArg_ParseTuple(args, ":GetCurrentProcess"))
    186                 return NULL;
    187 
    188         return sp_handle_new(GetCurrentProcess());
    189 }
     197    if (! PyArg_ParseTuple(args, ":GetCurrentProcess"))
     198        return NULL;
     199
     200    return sp_handle_new(GetCurrentProcess());
     201}
     202
     203PyDoc_STRVAR(DuplicateHandle_doc,
     204"DuplicateHandle(source_proc_handle, source_handle,\n\
     205                 target_proc_handle, target_handle, access,\n\
     206                 inherit[, options]) -> handle\n\
     207\n\
     208Return a duplicate handle object.\n\
     209\n\
     210The duplicate handle refers to the same object as the original\n\
     211handle. Therefore, any changes to the object are reflected\n\
     212through both handles.");
    190213
    191214static PyObject *
    192215sp_DuplicateHandle(PyObject* self, PyObject* args)
    193216{
    194         HANDLE target_handle;
    195         BOOL result;
    196 
    197         HANDLE source_process_handle;
    198         HANDLE source_handle;
    199         HANDLE target_process_handle;
    200         int desired_access;
    201         int inherit_handle;
    202         int options = 0;
    203 
    204         if (! PyArg_ParseTuple(args,
    205                                PY_HANDLE_PARAM PY_HANDLE_PARAM PY_HANDLE_PARAM
    206                                "ii|i:DuplicateHandle",
    207                                &source_process_handle,
    208                                &source_handle,
    209                                &target_process_handle,
    210                                &desired_access,
    211                                &inherit_handle,
    212                                &options))
    213                 return NULL;
    214 
    215         Py_BEGIN_ALLOW_THREADS
    216         result = DuplicateHandle(
    217                 source_process_handle,
    218                 source_handle,
    219                 target_process_handle,
    220                 &target_handle,
    221                 desired_access,
    222                 inherit_handle,
    223                 options
    224         );
    225         Py_END_ALLOW_THREADS
    226 
    227         if (! result)
    228                 return PyErr_SetFromWindowsErr(GetLastError());
    229 
    230         return sp_handle_new(target_handle);
    231 }
     217    HANDLE target_handle;
     218    BOOL result;
     219
     220    HANDLE source_process_handle;
     221    HANDLE source_handle;
     222    HANDLE target_process_handle;
     223    int desired_access;
     224    int inherit_handle;
     225    int options = 0;
     226
     227    if (! PyArg_ParseTuple(args,
     228                           PY_HANDLE_PARAM PY_HANDLE_PARAM PY_HANDLE_PARAM
     229                           "ii|i:DuplicateHandle",
     230                           &source_process_handle,
     231                           &source_handle,
     232                           &target_process_handle,
     233                           &desired_access,
     234                           &inherit_handle,
     235                           &options))
     236        return NULL;
     237
     238    Py_BEGIN_ALLOW_THREADS
     239    result = DuplicateHandle(
     240        source_process_handle,
     241        source_handle,
     242        target_process_handle,
     243        &target_handle,
     244        desired_access,
     245        inherit_handle,
     246        options
     247    );
     248    Py_END_ALLOW_THREADS
     249
     250    if (! result)
     251        return PyErr_SetFromWindowsErr(GetLastError());
     252
     253    return sp_handle_new(target_handle);
     254}
     255
     256PyDoc_STRVAR(CreatePipe_doc,
     257"CreatePipe(pipe_attrs, size) -> (read_handle, write_handle)\n\
     258\n\
     259Create an anonymous pipe, and return handles to the read and\n\
     260write ends of the pipe.\n\
     261\n\
     262pipe_attrs is ignored internally and can be None.");
    232263
    233264static PyObject *
    234265sp_CreatePipe(PyObject* self, PyObject* args)
    235266{
    236         HANDLE read_pipe;
    237         HANDLE write_pipe;
    238         BOOL result;
    239 
    240         PyObject* pipe_attributes; /* ignored */
    241         int size;
    242 
    243         if (! PyArg_ParseTuple(args, "Oi:CreatePipe", &pipe_attributes, &size))
    244                 return NULL;
    245 
    246         Py_BEGIN_ALLOW_THREADS
    247         result = CreatePipe(&read_pipe, &write_pipe, NULL, size);
    248         Py_END_ALLOW_THREADS
    249 
    250         if (! result)
    251                 return PyErr_SetFromWindowsErr(GetLastError());
    252 
    253         return Py_BuildValue(
    254                 "NN", sp_handle_new(read_pipe), sp_handle_new(write_pipe));
     267    HANDLE read_pipe;
     268    HANDLE write_pipe;
     269    BOOL result;
     270
     271    PyObject* pipe_attributes; /* ignored */
     272    int size;
     273
     274    if (! PyArg_ParseTuple(args, "Oi:CreatePipe", &pipe_attributes, &size))
     275        return NULL;
     276
     277    Py_BEGIN_ALLOW_THREADS
     278    result = CreatePipe(&read_pipe, &write_pipe, NULL, size);
     279    Py_END_ALLOW_THREADS
     280
     281    if (! result)
     282        return PyErr_SetFromWindowsErr(GetLastError());
     283
     284    return Py_BuildValue(
     285        "NN", sp_handle_new(read_pipe), sp_handle_new(write_pipe));
    255286}
    256287
     
    260291getint(PyObject* obj, char* name)
    261292{
    262         PyObject* value;
    263         int ret;
    264 
    265         value = PyObject_GetAttrString(obj, name);
    266         if (! value) {
    267                 PyErr_Clear(); /* FIXME: propagate error? */
    268                 return 0;
    269         }
    270         ret = (int) PyInt_AsLong(value);
    271         Py_DECREF(value);
    272         return ret;
     293    PyObject* value;
     294    int ret;
     295
     296    value = PyObject_GetAttrString(obj, name);
     297    if (! value) {
     298        PyErr_Clear(); /* FIXME: propagate error? */
     299        return 0;
     300    }
     301    ret = (int) PyInt_AsLong(value);
     302    Py_DECREF(value);
     303    return ret;
    273304}
    274305
     
    276307gethandle(PyObject* obj, char* name)
    277308{
    278         sp_handle_object* value;
    279         HANDLE ret;
    280 
    281         value = (sp_handle_object*) PyObject_GetAttrString(obj, name);
    282         if (! value) {
    283                 PyErr_Clear(); /* FIXME: propagate error? */
    284                 return NULL;
    285         }
    286         if (value->ob_type != &sp_handle_type)
    287                 ret = NULL;
    288         else
    289                 ret = value->handle;
    290         Py_DECREF(value);
    291         return ret;
     309    sp_handle_object* value;
     310    HANDLE ret;
     311
     312    value = (sp_handle_object*) PyObject_GetAttrString(obj, name);
     313    if (! value) {
     314        PyErr_Clear(); /* FIXME: propagate error? */
     315        return NULL;
     316    }
     317    if (value->ob_type != &sp_handle_type)
     318        ret = NULL;
     319    else
     320        ret = value->handle;
     321    Py_DECREF(value);
     322    return ret;
    292323}
    293324
     
    295326getenvironment(PyObject* environment)
    296327{
    297         int i, envsize;
    298         PyObject* out = NULL;
    299         PyObject* keys;
    300         PyObject* values;
    301         char* p;
    302 
    303         /* convert environment dictionary to windows enviroment string */
    304         if (! PyMapping_Check(environment)) {
    305                 PyErr_SetString(
    306                     PyExc_TypeError, "environment must be dictionary or None");
    307                 return NULL;
    308         }
    309 
    310         envsize = PyMapping_Length(environment);
    311 
    312         keys = PyMapping_Keys(environment);
    313         values = PyMapping_Values(environment);
    314         if (!keys || !values)
    315                 goto error;
    316 
    317         out = PyString_FromStringAndSize(NULL, 2048);
    318         if (! out)
    319                 goto error;
    320 
    321         p = PyString_AS_STRING(out);
    322 
    323         for (i = 0; i < envsize; i++) {
    324                 int ksize, vsize, totalsize;
    325                 PyObject* key = PyList_GET_ITEM(keys, i);
    326                 PyObject* value = PyList_GET_ITEM(values, i);
    327 
    328                 if (! PyString_Check(key) || ! PyString_Check(value)) {
    329                         PyErr_SetString(PyExc_TypeError,
    330                                 "environment can only contain strings");
    331                         goto error;
    332                 }
    333                 ksize = PyString_GET_SIZE(key);
    334                 vsize = PyString_GET_SIZE(value);
    335                 totalsize = (p - PyString_AS_STRING(out)) + ksize + 1 +
    336                                                              vsize + 1 + 1;
    337                 if (totalsize > PyString_GET_SIZE(out)) {
    338                         int offset = p - PyString_AS_STRING(out);
    339                         _PyString_Resize(&out, totalsize + 1024);
    340                         p = PyString_AS_STRING(out) + offset;
    341                 }
    342                 memcpy(p, PyString_AS_STRING(key), ksize);
    343                 p += ksize;
    344                 *p++ = '=';
    345                 memcpy(p, PyString_AS_STRING(value), vsize);
    346                 p += vsize;
    347                 *p++ = '\0';
    348         }
    349 
    350         /* add trailing null byte */
    351         *p++ = '\0';
    352         _PyString_Resize(&out, p - PyString_AS_STRING(out));
    353 
    354         /* PyObject_Print(out, stdout, 0); */
    355 
    356         Py_XDECREF(keys);
    357         Py_XDECREF(values);
    358 
    359         return out;
     328    int i, envsize;
     329    PyObject* out = NULL;
     330    PyObject* keys;
     331    PyObject* values;
     332    char* p;
     333
     334    /* convert environment dictionary to windows environment string */
     335    if (! PyMapping_Check(environment)) {
     336        PyErr_SetString(
     337            PyExc_TypeError, "environment must be dictionary or None");
     338        return NULL;
     339    }
     340
     341    envsize = PyMapping_Length(environment);
     342
     343    keys = PyMapping_Keys(environment);
     344    values = PyMapping_Values(environment);
     345    if (!keys || !values)
     346        goto error;
     347
     348    out = PyString_FromStringAndSize(NULL, 2048);
     349    if (! out)
     350        goto error;
     351
     352    p = PyString_AS_STRING(out);
     353
     354    for (i = 0; i < envsize; i++) {
     355        int ksize, vsize, totalsize;
     356        PyObject* key = PyList_GET_ITEM(keys, i);
     357        PyObject* value = PyList_GET_ITEM(values, i);
     358
     359        if (! PyString_Check(key) || ! PyString_Check(value)) {
     360            PyErr_SetString(PyExc_TypeError,
     361                "environment can only contain strings");
     362            goto error;
     363        }
     364        ksize = PyString_GET_SIZE(key);
     365        vsize = PyString_GET_SIZE(value);
     366        totalsize = (p - PyString_AS_STRING(out)) + ksize + 1 +
     367                                                     vsize + 1 + 1;
     368        if (totalsize > PyString_GET_SIZE(out)) {
     369            int offset = p - PyString_AS_STRING(out);
     370            _PyString_Resize(&out, totalsize + 1024);
     371            p = PyString_AS_STRING(out) + offset;
     372        }
     373        memcpy(p, PyString_AS_STRING(key), ksize);
     374        p += ksize;
     375        *p++ = '=';
     376        memcpy(p, PyString_AS_STRING(value), vsize);
     377        p += vsize;
     378        *p++ = '\0';
     379    }
     380
     381    /* add trailing null byte */
     382    *p++ = '\0';
     383    _PyString_Resize(&out, p - PyString_AS_STRING(out));
     384
     385    /* PyObject_Print(out, stdout, 0); */
     386
     387    Py_XDECREF(keys);
     388    Py_XDECREF(values);
     389
     390    return out;
    360391
    361392 error:
    362         Py_XDECREF(out);
    363         Py_XDECREF(keys);
    364         Py_XDECREF(values);
    365         return NULL;
    366 }
     393    Py_XDECREF(out);
     394    Py_XDECREF(keys);
     395    Py_XDECREF(values);
     396    return NULL;
     397}
     398
     399PyDoc_STRVAR(CreateProcess_doc,
     400"CreateProcess(app_name, cmd_line, proc_attrs, thread_attrs,\n\
     401               inherit, flags, env_mapping, curdir,\n\
     402               startup_info) -> (proc_handle, thread_handle,\n\
     403                                 pid, tid)\n\
     404\n\
     405Create a new process and its primary thread. The return\n\
     406value is a tuple of the process handle, thread handle,\n\
     407process ID, and thread ID.\n\
     408\n\
     409proc_attrs and thread_attrs are ignored internally and can be None.");
    367410
    368411static PyObject *
    369412sp_CreateProcess(PyObject* self, PyObject* args)
    370413{
    371         BOOL result;
    372         PROCESS_INFORMATION pi;
    373         STARTUPINFO si;
    374         PyObject* environment;
    375 
    376         char* application_name;
    377         char* command_line;
    378         PyObject* process_attributes; /* ignored */
    379         PyObject* thread_attributes; /* ignored */
    380         int inherit_handles;
    381         int creation_flags;
    382         PyObject* env_mapping;
    383         char* current_directory;
    384         PyObject* startup_info;
    385 
    386         if (! PyArg_ParseTuple(args, "zzOOiiOzO:CreateProcess",
    387                                &application_name,
    388                                &command_line,
    389                                &process_attributes,
    390                                &thread_attributes,
    391                                &inherit_handles,
    392                                &creation_flags,
    393                                &env_mapping,
    394                                &current_directory,
    395                                &startup_info))
    396                 return NULL;
    397 
    398         ZeroMemory(&si, sizeof(si));
    399         si.cb = sizeof(si);
    400 
    401         /* note: we only support a small subset of all SI attributes */
    402         si.dwFlags = getint(startup_info, "dwFlags");
    403         si.wShowWindow = getint(startup_info, "wShowWindow");
    404         si.hStdInput = gethandle(startup_info, "hStdInput");
    405         si.hStdOutput = gethandle(startup_info, "hStdOutput");
    406         si.hStdError = gethandle(startup_info, "hStdError");
    407 
    408         if (PyErr_Occurred())
    409                 return NULL;
    410 
    411         if (env_mapping == Py_None)
    412                 environment = NULL;
    413         else {
    414                 environment = getenvironment(env_mapping);
    415                 if (! environment)
    416                         return NULL;
    417         }
    418 
    419         Py_BEGIN_ALLOW_THREADS
    420         result = CreateProcess(application_name,
    421                                command_line,
    422                                NULL,
    423                                NULL,
    424                                inherit_handles,
    425                                creation_flags,
    426                                environment ? PyString_AS_STRING(environment) : NULL,
    427                                current_directory,
    428                                &si,
    429                                &pi);
    430         Py_END_ALLOW_THREADS
    431 
    432         Py_XDECREF(environment);
    433 
    434         if (! result)
    435                 return PyErr_SetFromWindowsErr(GetLastError());
    436 
    437         return Py_BuildValue("NNii",
    438                              sp_handle_new(pi.hProcess),
    439                              sp_handle_new(pi.hThread),
    440                              pi.dwProcessId,
    441                              pi.dwThreadId);
    442 }
     414    BOOL result;
     415    PROCESS_INFORMATION pi;
     416    STARTUPINFO si;
     417    PyObject* environment;
     418
     419    char* application_name;
     420    char* command_line;
     421    PyObject* process_attributes; /* ignored */
     422    PyObject* thread_attributes; /* ignored */
     423    int inherit_handles;
     424    int creation_flags;
     425    PyObject* env_mapping;
     426    char* current_directory;
     427    PyObject* startup_info;
     428
     429    if (! PyArg_ParseTuple(args, "zzOOiiOzO:CreateProcess",
     430                           &application_name,
     431                           &command_line,
     432                           &process_attributes,
     433                           &thread_attributes,
     434                           &inherit_handles,
     435                           &creation_flags,
     436                           &env_mapping,
     437                           &current_directory,
     438                           &startup_info))
     439        return NULL;
     440
     441    ZeroMemory(&si, sizeof(si));
     442    si.cb = sizeof(si);
     443
     444    /* note: we only support a small subset of all SI attributes */
     445    si.dwFlags = getint(startup_info, "dwFlags");
     446    si.wShowWindow = getint(startup_info, "wShowWindow");
     447    si.hStdInput = gethandle(startup_info, "hStdInput");
     448    si.hStdOutput = gethandle(startup_info, "hStdOutput");
     449    si.hStdError = gethandle(startup_info, "hStdError");
     450
     451    if (PyErr_Occurred())
     452        return NULL;
     453
     454    if (env_mapping == Py_None)
     455        environment = NULL;
     456    else {
     457        environment = getenvironment(env_mapping);
     458        if (! environment)
     459            return NULL;
     460    }
     461
     462    Py_BEGIN_ALLOW_THREADS
     463    result = CreateProcess(application_name,
     464                           command_line,
     465                           NULL,
     466                           NULL,
     467                           inherit_handles,
     468                           creation_flags,
     469                           environment ? PyString_AS_STRING(environment) : NULL,
     470                           current_directory,
     471                           &si,
     472                           &pi);
     473    Py_END_ALLOW_THREADS
     474
     475    Py_XDECREF(environment);
     476
     477    if (! result)
     478        return PyErr_SetFromWindowsErr(GetLastError());
     479
     480    return Py_BuildValue("NNii",
     481                         sp_handle_new(pi.hProcess),
     482                         sp_handle_new(pi.hThread),
     483                         pi.dwProcessId,
     484                         pi.dwThreadId);
     485}
     486
     487PyDoc_STRVAR(TerminateProcess_doc,
     488"TerminateProcess(handle, exit_code) -> None\n\
     489\n\
     490Terminate the specified process and all of its threads.");
    443491
    444492static PyObject *
    445493sp_TerminateProcess(PyObject* self, PyObject* args)
    446494{
    447         BOOL result;
    448 
    449         HANDLE process;
    450         int exit_code;
    451         if (! PyArg_ParseTuple(args, PY_HANDLE_PARAM "i:TerminateProcess",
    452                                &process, &exit_code))
    453                 return NULL;
    454 
    455         result = TerminateProcess(process, exit_code);
    456 
    457         if (! result)
    458                 return PyErr_SetFromWindowsErr(GetLastError());
    459 
    460         Py_INCREF(Py_None);
    461         return Py_None;
    462 }
     495    BOOL result;
     496
     497    HANDLE process;
     498    int exit_code;
     499    if (! PyArg_ParseTuple(args, PY_HANDLE_PARAM "i:TerminateProcess",
     500                           &process, &exit_code))
     501        return NULL;
     502
     503    result = TerminateProcess(process, exit_code);
     504
     505    if (! result)
     506        return PyErr_SetFromWindowsErr(GetLastError());
     507
     508    Py_INCREF(Py_None);
     509    return Py_None;
     510}
     511
     512PyDoc_STRVAR(GetExitCodeProcess_doc,
     513"GetExitCodeProcess(handle) -> Exit code\n\
     514\n\
     515Return the termination status of the specified process.");
    463516
    464517static PyObject *
    465518sp_GetExitCodeProcess(PyObject* self, PyObject* args)
    466519{
    467         DWORD exit_code;
    468         BOOL result;
    469 
    470         HANDLE process;
    471         if (! PyArg_ParseTuple(args, PY_HANDLE_PARAM ":GetExitCodeProcess", &process))
    472                 return NULL;
    473 
    474         result = GetExitCodeProcess(process, &exit_code);
    475 
    476         if (! result)
    477                 return PyErr_SetFromWindowsErr(GetLastError());
    478 
    479         return PyInt_FromLong(exit_code);
    480 }
     520    DWORD exit_code;
     521    BOOL result;
     522
     523    HANDLE process;
     524    if (! PyArg_ParseTuple(args, PY_HANDLE_PARAM ":GetExitCodeProcess", &process))
     525        return NULL;
     526
     527    result = GetExitCodeProcess(process, &exit_code);
     528
     529    if (! result)
     530        return PyErr_SetFromWindowsErr(GetLastError());
     531
     532    return PyInt_FromLong(exit_code);
     533}
     534
     535PyDoc_STRVAR(WaitForSingleObject_doc,
     536"WaitForSingleObject(handle, timeout) -> result\n\
     537\n\
     538Wait until the specified object is in the signaled state or\n\
     539the time-out interval elapses. The timeout value is specified\n\
     540in milliseconds.");
    481541
    482542static PyObject *
    483543sp_WaitForSingleObject(PyObject* self, PyObject* args)
    484544{
    485         DWORD result;
    486 
    487         HANDLE handle;
    488         int milliseconds;
    489         if (! PyArg_ParseTuple(args, PY_HANDLE_PARAM "i:WaitForSingleObject",
    490                                      &handle,
    491                                      &milliseconds))
    492                 return NULL;
    493 
    494         Py_BEGIN_ALLOW_THREADS
    495         result = WaitForSingleObject(handle, (DWORD) milliseconds);
    496         Py_END_ALLOW_THREADS
    497 
    498         if (result == WAIT_FAILED)
    499                 return PyErr_SetFromWindowsErr(GetLastError());
    500 
    501         return PyInt_FromLong((int) result);
    502 }
     545    DWORD result;
     546
     547    HANDLE handle;
     548    int milliseconds;
     549    if (! PyArg_ParseTuple(args, PY_HANDLE_PARAM "i:WaitForSingleObject",
     550                                 &handle,
     551                                 &milliseconds))
     552        return NULL;
     553
     554    Py_BEGIN_ALLOW_THREADS
     555    result = WaitForSingleObject(handle, (DWORD) milliseconds);
     556    Py_END_ALLOW_THREADS
     557
     558    if (result == WAIT_FAILED)
     559        return PyErr_SetFromWindowsErr(GetLastError());
     560
     561    return PyInt_FromLong((int) result);
     562}
     563
     564PyDoc_STRVAR(GetVersion_doc,
     565"GetVersion() -> version\n\
     566\n\
     567Return the version number of the current operating system.");
    503568
    504569static PyObject *
    505570sp_GetVersion(PyObject* self, PyObject* args)
    506571{
    507         if (! PyArg_ParseTuple(args, ":GetVersion"))
    508                 return NULL;
    509 
    510         return PyInt_FromLong((int) GetVersion());
    511 }
     572    if (! PyArg_ParseTuple(args, ":GetVersion"))
     573        return NULL;
     574
     575    return PyInt_FromLong((int) GetVersion());
     576}
     577
     578PyDoc_STRVAR(GetModuleFileName_doc,
     579"GetModuleFileName(module) -> path\n\
     580\n\
     581Return the fully-qualified path for the file that contains\n\
     582the specified module. The module must have been loaded by the\n\
     583current process.\n\
     584\n\
     585The module parameter should be a handle to the loaded module\n\
     586whose path is being requested. If this parameter is 0, \n\
     587GetModuleFileName retrieves the path of the executable file\n\
     588of the current process.");
    512589
    513590static PyObject *
    514591sp_GetModuleFileName(PyObject* self, PyObject* args)
    515592{
    516         BOOL result;
    517         HMODULE module;
    518         TCHAR filename[MAX_PATH];
    519 
    520         if (! PyArg_ParseTuple(args, PY_HANDLE_PARAM ":GetModuleFileName",
    521                                &module))
    522                 return NULL;
    523 
    524         result = GetModuleFileName(module, filename, MAX_PATH);
    525         filename[MAX_PATH-1] = '\0';
    526 
    527         if (! result)
    528                 return PyErr_SetFromWindowsErr(GetLastError());
    529 
    530         return PyString_FromString(filename);
     593    BOOL result;
     594    HMODULE module;
     595    TCHAR filename[MAX_PATH];
     596
     597    if (! PyArg_ParseTuple(args, PY_HANDLE_PARAM ":GetModuleFileName",
     598                           &module))
     599        return NULL;
     600
     601    result = GetModuleFileName(module, filename, MAX_PATH);
     602    filename[MAX_PATH-1] = '\0';
     603
     604    if (! result)
     605        return PyErr_SetFromWindowsErr(GetLastError());
     606
     607    return PyString_FromString(filename);
    531608}
    532609
    533610static PyMethodDef sp_functions[] = {
    534         {"GetStdHandle",        sp_GetStdHandle,        METH_VARARGS},
    535         {"GetCurrentProcess",   sp_GetCurrentProcess,   METH_VARARGS},
    536         {"DuplicateHandle",     sp_DuplicateHandle,     METH_VARARGS},
    537         {"CreatePipe",          sp_CreatePipe,          METH_VARARGS},
    538         {"CreateProcess",       sp_CreateProcess,       METH_VARARGS},
    539         {"TerminateProcess",    sp_TerminateProcess,    METH_VARARGS},
    540         {"GetExitCodeProcess",  sp_GetExitCodeProcess,  METH_VARARGS},
    541         {"WaitForSingleObject", sp_WaitForSingleObject, METH_VARARGS},
    542         {"GetVersion",          sp_GetVersion,          METH_VARARGS},
    543         {"GetModuleFileName",   sp_GetModuleFileName,   METH_VARARGS},
    544         {NULL, NULL}
     611    {"GetStdHandle", sp_GetStdHandle, METH_VARARGS, GetStdHandle_doc},
     612    {"GetCurrentProcess", sp_GetCurrentProcess,         METH_VARARGS,
     613                                              GetCurrentProcess_doc},
     614    {"DuplicateHandle",         sp_DuplicateHandle,     METH_VARARGS,
     615                                            DuplicateHandle_doc},
     616    {"CreatePipe", sp_CreatePipe, METH_VARARGS, CreatePipe_doc},
     617    {"CreateProcess", sp_CreateProcess, METH_VARARGS, CreateProcess_doc},
     618    {"TerminateProcess", sp_TerminateProcess, METH_VARARGS,
     619                                             TerminateProcess_doc},
     620    {"GetExitCodeProcess", sp_GetExitCodeProcess, METH_VARARGS,
     621                                               GetExitCodeProcess_doc},
     622    {"WaitForSingleObject", sp_WaitForSingleObject, METH_VARARGS,
     623                                                    WaitForSingleObject_doc},
     624    {"GetVersion", sp_GetVersion, METH_VARARGS, GetVersion_doc},
     625    {"GetModuleFileName", sp_GetModuleFileName, METH_VARARGS,
     626                                              GetModuleFileName_doc},
     627    {NULL, NULL}
    545628};
    546629
     
    550633defint(PyObject* d, const char* name, int value)
    551634{
    552         PyObject* v = PyInt_FromLong((long) value);
    553         if (v) {
    554                 PyDict_SetItemString(d, (char*) name, v);
    555                 Py_DECREF(v);
    556         }
     635    PyObject* v = PyInt_FromLong((long) value);
     636    if (v) {
     637        PyDict_SetItemString(d, (char*) name, v);
     638        Py_DECREF(v);
     639    }
    557640}
    558641
     
    564647init_subprocess()
    565648{
    566         PyObject *d;
    567         PyObject *m;
    568 
    569         /* patch up object descriptors */
    570         sp_handle_type.ob_type = &PyType_Type;
    571         sp_handle_as_number.nb_int = (unaryfunc) sp_handle_as_int;
    572 
    573         m = Py_InitModule("_subprocess", sp_functions);
    574         if (m == NULL)
    575                 return;
    576         d = PyModule_GetDict(m);
    577 
    578         /* constants */
    579         defint(d, "STD_INPUT_HANDLE", STD_INPUT_HANDLE);
    580         defint(d, "STD_OUTPUT_HANDLE", STD_OUTPUT_HANDLE);
    581         defint(d, "STD_ERROR_HANDLE", STD_ERROR_HANDLE);
    582         defint(d, "DUPLICATE_SAME_ACCESS", DUPLICATE_SAME_ACCESS);
    583         defint(d, "STARTF_USESTDHANDLES", STARTF_USESTDHANDLES);
    584         defint(d, "STARTF_USESHOWWINDOW", STARTF_USESHOWWINDOW);
    585         defint(d, "SW_HIDE", SW_HIDE);
    586         defint(d, "INFINITE", INFINITE);
    587         defint(d, "WAIT_OBJECT_0", WAIT_OBJECT_0);
    588         defint(d, "CREATE_NEW_CONSOLE", CREATE_NEW_CONSOLE);
    589 }
     649    PyObject *d;
     650    PyObject *m;
     651
     652    /* patch up object descriptors */
     653    sp_handle_type.ob_type = &PyType_Type;
     654    sp_handle_as_number.nb_int = (unaryfunc) sp_handle_as_int;
     655
     656    m = Py_InitModule("_subprocess", sp_functions);
     657    if (m == NULL)
     658        return;
     659    d = PyModule_GetDict(m);
     660
     661    /* constants */
     662    defint(d, "STD_INPUT_HANDLE", STD_INPUT_HANDLE);
     663    defint(d, "STD_OUTPUT_HANDLE", STD_OUTPUT_HANDLE);
     664    defint(d, "STD_ERROR_HANDLE", STD_ERROR_HANDLE);
     665    defint(d, "DUPLICATE_SAME_ACCESS", DUPLICATE_SAME_ACCESS);
     666    defint(d, "STARTF_USESTDHANDLES", STARTF_USESTDHANDLES);
     667    defint(d, "STARTF_USESHOWWINDOW", STARTF_USESHOWWINDOW);
     668    defint(d, "SW_HIDE", SW_HIDE);
     669    defint(d, "INFINITE", INFINITE);
     670    defint(d, "WAIT_OBJECT_0", WAIT_OBJECT_0);
     671    defint(d, "CREATE_NEW_CONSOLE", CREATE_NEW_CONSOLE);
     672    defint(d, "CREATE_NEW_PROCESS_GROUP", CREATE_NEW_PROCESS_GROUP);
     673    defint(d, "STILL_ACTIVE", STILL_ACTIVE);
     674}
Note: See TracChangeset for help on using the changeset viewer.