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/_winreg.c

    r2 r391  
    55
    66  * Simple registry access written by Mark Hammond in win32api
    7         module circa 1995.
     7    module circa 1995.
    88  * Bill Tutt expanded the support significantly not long after.
    99  * Numerous other people have submitted patches since then.
     
    3030*/
    3131#define PyErr_SetFromWindowsErrWithFunction(rc, fnname) \
    32         PyErr_SetFromWindowsErr(rc)
     32    PyErr_SetFromWindowsErr(rc)
    3333
    3434/* Forward declares */
     
    9090"\n"
    9191"The return value is the handle of the opened key.\n"
    92 "If the function fails, an EnvironmentError exception is raised.");
     92"If the function fails, a WindowsError exception is raised.");
    9393
    9494PyDoc_STRVAR(CreateKey_doc,
     
    105105"If the function fails, an exception is raised.");
    106106
     107PyDoc_STRVAR(CreateKeyEx_doc,
     108"key = CreateKeyEx(key, sub_key, res, sam) - Creates or opens the specified key.\n"
     109"\n"
     110"key is an already open key, or one of the predefined HKEY_* constants\n"
     111"sub_key is a string that names the key this method opens or creates.\n"
     112"res is a reserved integer, and must be zero.  Default is zero.\n"
     113"sam is an integer that specifies an access mask that describes the desired\n"
     114" If key is one of the predefined keys, sub_key may be None. In that case,\n"
     115" the handle returned is the same key handle passed in to the function.\n"
     116"\n"
     117"If the key already exists, this function opens the existing key\n"
     118"\n"
     119"The return value is the handle of the opened key.\n"
     120"If the function fails, an exception is raised.");
     121
    107122PyDoc_STRVAR(DeleteKey_doc,
    108123"DeleteKey(key, sub_key) - Deletes the specified key.\n"
     
    115130"\n"
    116131"If the method succeeds, the entire key, including all of its values,\n"
    117 "is removed.  If the method fails, an EnvironmentError exception is raised.");
     132"is removed.  If the method fails, a WindowsError exception is raised.");
     133
     134PyDoc_STRVAR(DeleteKeyEx_doc,
     135"DeleteKeyEx(key, sub_key, sam, res) - Deletes the specified key.\n"
     136"\n"
     137"key is an already open key, or any one of the predefined HKEY_* constants.\n"
     138"sub_key is a string that must be a subkey of the key identified by the key parameter.\n"
     139"res is a reserved integer, and must be zero.  Default is zero.\n"
     140"sam is an integer that specifies an access mask that describes the desired\n"
     141" This value must not be None, and the key may not have subkeys.\n"
     142"\n"
     143"This method can not delete keys with subkeys.\n"
     144"\n"
     145"If the method succeeds, the entire key, including all of its values,\n"
     146"is removed.  If the method fails, a WindowsError exception is raised.\n"
     147"On unsupported Windows versions, NotImplementedError is raised.");
    118148
    119149PyDoc_STRVAR(DeleteValue_doc,
     
    130160"\n"
    131161"The function retrieves the name of one subkey each time it is called.\n"
    132 "It is typically called repeatedly until an EnvironmentError exception is\n"
     162"It is typically called repeatedly until a WindowsError exception is\n"
    133163"raised, indicating no more values are available.");
    134164
     
    139169"\n"
    140170"The function retrieves the name of one subkey each time it is called.\n"
    141 "It is typically called repeatedly, until an EnvironmentError exception\n"
     171"It is typically called repeatedly, until a WindowsError exception\n"
    142172"is raised, indicating no more values.\n"
    143173"\n"
     
    193223"\n"
    194224"The result is a new handle to the specified key\n"
    195 "If the function fails, an EnvironmentError exception is raised.");
     225"If the function fails, a WindowsError exception is raised.");
    196226
    197227PyDoc_STRVAR(OpenKeyEx_doc, "See OpenKey()");
     
    355385************************************************************************/
    356386typedef struct {
    357         PyObject_VAR_HEAD
    358         HKEY hkey;
     387    PyObject_VAR_HEAD
     388    HKEY hkey;
    359389} PyHKEYObject;
    360390
     
    366396PyHKEY_unaryFailureFunc(PyObject *ob)
    367397{
    368         PyErr_SetString(PyExc_TypeError, failMsg);
    369         return NULL;
     398    PyErr_SetString(PyExc_TypeError, failMsg);
     399    return NULL;
    370400}
    371401static PyObject *
    372402PyHKEY_binaryFailureFunc(PyObject *ob1, PyObject *ob2)
    373403{
    374         PyErr_SetString(PyExc_TypeError, failMsg);
    375         return NULL;
     404    PyErr_SetString(PyExc_TypeError, failMsg);
     405    return NULL;
    376406}
    377407static PyObject *
    378408PyHKEY_ternaryFailureFunc(PyObject *ob1, PyObject *ob2, PyObject *ob3)
    379409{
    380         PyErr_SetString(PyExc_TypeError, failMsg);
    381         return NULL;
     410    PyErr_SetString(PyExc_TypeError, failMsg);
     411    return NULL;
    382412}
    383413
     
    385415PyHKEY_deallocFunc(PyObject *ob)
    386416{
    387         /* Can not call PyHKEY_Close, as the ob->tp_type
    388            has already been cleared, thus causing the type
    389            check to fail!
    390         */
    391         PyHKEYObject *obkey = (PyHKEYObject *)ob;
    392         if (obkey->hkey)
    393                 RegCloseKey((HKEY)obkey->hkey);
    394         PyObject_DEL(ob);
     417    /* Can not call PyHKEY_Close, as the ob->tp_type
     418       has already been cleared, thus causing the type
     419       check to fail!
     420    */
     421    PyHKEYObject *obkey = (PyHKEYObject *)ob;
     422    if (obkey->hkey)
     423        RegCloseKey((HKEY)obkey->hkey);
     424    PyObject_DEL(ob);
    395425}
    396426
     
    398428PyHKEY_nonzeroFunc(PyObject *ob)
    399429{
    400         return ((PyHKEYObject *)ob)->hkey != 0;
     430    return ((PyHKEYObject *)ob)->hkey != 0;
    401431}
    402432
     
    404434PyHKEY_intFunc(PyObject *ob)
    405435{
    406         PyHKEYObject *pyhkey = (PyHKEYObject *)ob;
    407         return PyLong_FromVoidPtr(pyhkey->hkey);
     436    PyHKEYObject *pyhkey = (PyHKEYObject *)ob;
     437    return PyLong_FromVoidPtr(pyhkey->hkey);
    408438}
    409439
     
    411441PyHKEY_printFunc(PyObject *ob, FILE *fp, int flags)
    412442{
    413         PyHKEYObject *pyhkey = (PyHKEYObject *)ob;
    414         char resBuf[160];
    415         wsprintf(resBuf, "<PyHKEY at %p (%p)>",
    416                  ob, pyhkey->hkey);
    417         fputs(resBuf, fp);
    418         return 0;
     443    PyHKEYObject *pyhkey = (PyHKEYObject *)ob;
     444    fprintf(fp, "<PyHKEY at %p (%p)>",
     445        ob, pyhkey->hkey);
     446    return 0;
    419447}
    420448
     
    422450PyHKEY_strFunc(PyObject *ob)
    423451{
    424         PyHKEYObject *pyhkey = (PyHKEYObject *)ob;
    425         char resBuf[160];
    426         wsprintf(resBuf, "<PyHKEY:%p>", pyhkey->hkey);
    427         return PyString_FromString(resBuf);
     452    PyHKEYObject *pyhkey = (PyHKEYObject *)ob;
     453    return PyString_FromFormat("<PyHKEY:%p>", pyhkey->hkey);
    428454}
    429455
     
    431457PyHKEY_compareFunc(PyObject *ob1, PyObject *ob2)
    432458{
    433         PyHKEYObject *pyhkey1 = (PyHKEYObject *)ob1;
    434         PyHKEYObject *pyhkey2 = (PyHKEYObject *)ob2;
    435         return pyhkey1 == pyhkey2 ? 0 :
    436                 (pyhkey1 < pyhkey2 ? -1 : 1);
     459    PyHKEYObject *pyhkey1 = (PyHKEYObject *)ob1;
     460    PyHKEYObject *pyhkey2 = (PyHKEYObject *)ob2;
     461    return pyhkey1 == pyhkey2 ? 0 :
     462        (pyhkey1 < pyhkey2 ? -1 : 1);
    437463}
    438464
     
    440466PyHKEY_hashFunc(PyObject *ob)
    441467{
    442         /* Just use the address.
    443            XXX - should we use the handle value?
    444         */
    445         return _Py_HashPointer(ob);
     468    /* Just use the address.
     469       XXX - should we use the handle value?
     470    */
     471    return _Py_HashPointer(ob);
    446472}
    447473
     
    449475static PyNumberMethods PyHKEY_NumberMethods =
    450476{
    451         PyHKEY_binaryFailureFunc,       /* nb_add */
    452         PyHKEY_binaryFailureFunc,       /* nb_subtract */
    453         PyHKEY_binaryFailureFunc,       /* nb_multiply */
    454         PyHKEY_binaryFailureFunc,       /* nb_divide */
    455         PyHKEY_binaryFailureFunc,       /* nb_remainder */
    456         PyHKEY_binaryFailureFunc,       /* nb_divmod */
    457         PyHKEY_ternaryFailureFunc,      /* nb_power */
    458         PyHKEY_unaryFailureFunc,        /* nb_negative */
    459         PyHKEY_unaryFailureFunc,        /* nb_positive */
    460         PyHKEY_unaryFailureFunc,        /* nb_absolute */
    461         PyHKEY_nonzeroFunc,             /* nb_nonzero */
    462         PyHKEY_unaryFailureFunc,        /* nb_invert */
    463         PyHKEY_binaryFailureFunc,       /* nb_lshift */
    464         PyHKEY_binaryFailureFunc,       /* nb_rshift */
    465         PyHKEY_binaryFailureFunc,       /* nb_and */
    466         PyHKEY_binaryFailureFunc,       /* nb_xor */
    467         PyHKEY_binaryFailureFunc,       /* nb_or */
    468         0,              /* nb_coerce (allowed to be zero) */
    469         PyHKEY_intFunc,                 /* nb_int */
    470         PyHKEY_unaryFailureFunc,        /* nb_long */
    471         PyHKEY_unaryFailureFunc,        /* nb_float */
    472         PyHKEY_unaryFailureFunc,        /* nb_oct */
    473         PyHKEY_unaryFailureFunc,        /* nb_hex */
     477    PyHKEY_binaryFailureFunc,           /* nb_add */
     478    PyHKEY_binaryFailureFunc,           /* nb_subtract */
     479    PyHKEY_binaryFailureFunc,           /* nb_multiply */
     480    PyHKEY_binaryFailureFunc,           /* nb_divide */
     481    PyHKEY_binaryFailureFunc,           /* nb_remainder */
     482    PyHKEY_binaryFailureFunc,           /* nb_divmod */
     483    PyHKEY_ternaryFailureFunc,          /* nb_power */
     484    PyHKEY_unaryFailureFunc,            /* nb_negative */
     485    PyHKEY_unaryFailureFunc,            /* nb_positive */
     486    PyHKEY_unaryFailureFunc,            /* nb_absolute */
     487    PyHKEY_nonzeroFunc,                 /* nb_nonzero */
     488    PyHKEY_unaryFailureFunc,            /* nb_invert */
     489    PyHKEY_binaryFailureFunc,           /* nb_lshift */
     490    PyHKEY_binaryFailureFunc,           /* nb_rshift */
     491    PyHKEY_binaryFailureFunc,           /* nb_and */
     492    PyHKEY_binaryFailureFunc,           /* nb_xor */
     493    PyHKEY_binaryFailureFunc,           /* nb_or */
     494    0,                  /* nb_coerce (allowed to be zero) */
     495    PyHKEY_intFunc,                     /* nb_int */
     496    PyHKEY_unaryFailureFunc,            /* nb_long */
     497    PyHKEY_unaryFailureFunc,            /* nb_float */
     498    PyHKEY_unaryFailureFunc,            /* nb_oct */
     499    PyHKEY_unaryFailureFunc,            /* nb_hex */
    474500};
    475501
    476 
    477 /* fwd declare __getattr__ */
    478 static PyObject *PyHKEY_getattr(PyObject *self, const char *name);
     502static PyObject *PyHKEY_CloseMethod(PyObject *self, PyObject *args);
     503static PyObject *PyHKEY_DetachMethod(PyObject *self, PyObject *args);
     504static PyObject *PyHKEY_Enter(PyObject *self);
     505static PyObject *PyHKEY_Exit(PyObject *self, PyObject *args);
     506
     507static struct PyMethodDef PyHKEY_methods[] = {
     508    {"Close",  PyHKEY_CloseMethod, METH_VARARGS, PyHKEY_Close_doc},
     509    {"Detach", PyHKEY_DetachMethod, METH_VARARGS, PyHKEY_Detach_doc},
     510    {"__enter__", (PyCFunction)PyHKEY_Enter, METH_NOARGS, NULL},
     511    {"__exit__", PyHKEY_Exit, METH_VARARGS, NULL},
     512    {NULL}
     513};
     514
     515static PyMemberDef PyHKEY_memberlist[] = {
     516    {"handle", T_PYSSIZET, offsetof(PyHKEYObject, hkey), READONLY},
     517    {NULL}    /* Sentinel */
     518};
    479519
    480520/* The type itself */
    481521PyTypeObject PyHKEY_Type =
    482522{
    483         PyVarObject_HEAD_INIT(0, 0) /* fill in type at module init */
    484         "PyHKEY",
    485         sizeof(PyHKEYObject),
    486         0,
    487         PyHKEY_deallocFunc,             /* tp_dealloc */
    488         PyHKEY_printFunc,               /* tp_print */
    489         PyHKEY_getattr,                 /* tp_getattr */
    490         0,                              /* tp_setattr */
    491         PyHKEY_compareFunc,             /* tp_compare */
    492         0,                              /* tp_repr */
    493         &PyHKEY_NumberMethods,          /* tp_as_number */
    494         0,                              /* tp_as_sequence */
    495         0,                              /* tp_as_mapping */
    496         PyHKEY_hashFunc,                /* tp_hash */
    497         0,                              /* tp_call */
    498         PyHKEY_strFunc,                 /* tp_str */
    499         0,                              /* tp_getattro */
    500         0,                              /* tp_setattro */
    501         0,                              /* tp_as_buffer */
    502         0,                              /* tp_flags */
    503         PyHKEY_doc,                     /* tp_doc */
     523    PyVarObject_HEAD_INIT(0, 0) /* fill in type at module init */
     524    "PyHKEY",
     525    sizeof(PyHKEYObject),
     526    0,
     527    PyHKEY_deallocFunc,                 /* tp_dealloc */
     528    PyHKEY_printFunc,                   /* tp_print */
     529    0,                                  /* tp_getattr */
     530    0,                                  /* tp_setattr */
     531    PyHKEY_compareFunc,                 /* tp_compare */
     532    0,                                  /* tp_repr */
     533    &PyHKEY_NumberMethods,              /* tp_as_number */
     534    0,                                  /* tp_as_sequence */
     535    0,                                  /* tp_as_mapping */
     536    PyHKEY_hashFunc,                    /* tp_hash */
     537    0,                                  /* tp_call */
     538    PyHKEY_strFunc,                     /* tp_str */
     539    0,                                  /* tp_getattro */
     540    0,                                  /* tp_setattro */
     541    0,                                  /* tp_as_buffer */
     542    Py_TPFLAGS_DEFAULT,                 /* tp_flags */
     543    PyHKEY_doc,                         /* tp_doc */
     544    0,                                  /* tp_traverse */
     545    0,                                  /* tp_clear */
     546    0,                                  /* tp_richcompare */
     547    0,                                  /* tp_weaklistoffset */
     548    0,                                  /* tp_iter */
     549    0,                                  /* tp_iternext */
     550    PyHKEY_methods,                     /* tp_methods */
     551    PyHKEY_memberlist,                  /* tp_members */
    504552};
    505553
    506 #define OFF(e) offsetof(PyHKEYObject, e)
    507 
    508 static struct memberlist PyHKEY_memberlist[] = {
    509         {"handle",      T_INT,      OFF(hkey)},
    510         {NULL}    /* Sentinel */
    511 };
    512 
    513554/************************************************************************
    514555
     
    519560PyHKEY_CloseMethod(PyObject *self, PyObject *args)
    520561{
    521         if (!PyArg_ParseTuple(args, ":Close"))
    522                 return NULL;
    523         if (!PyHKEY_Close(self))
    524                 return NULL;
    525         Py_INCREF(Py_None);
    526         return Py_None;
     562    if (!PyArg_ParseTuple(args, ":Close"))
     563        return NULL;
     564    if (!PyHKEY_Close(self))
     565        return NULL;
     566    Py_INCREF(Py_None);
     567    return Py_None;
    527568}
    528569
     
    530571PyHKEY_DetachMethod(PyObject *self, PyObject *args)
    531572{
    532         void* ret;
    533         PyHKEYObject *pThis = (PyHKEYObject *)self;
    534         if (!PyArg_ParseTuple(args, ":Detach"))
    535                 return NULL;
    536         ret = (void*)pThis->hkey;
    537         pThis->hkey = 0;
    538         return PyLong_FromVoidPtr(ret);
     573    void* ret;
     574    PyHKEYObject *pThis = (PyHKEYObject *)self;
     575    if (!PyArg_ParseTuple(args, ":Detach"))
     576        return NULL;
     577    ret = (void*)pThis->hkey;
     578    pThis->hkey = 0;
     579    return PyLong_FromVoidPtr(ret);
    539580}
    540581
     
    542583PyHKEY_Enter(PyObject *self)
    543584{
    544         Py_XINCREF(self);
    545         return self;
     585    Py_XINCREF(self);
     586    return self;
    546587}
    547588
     
    549590PyHKEY_Exit(PyObject *self, PyObject *args)
    550591{
    551         if (!PyHKEY_Close(self))
    552                 return NULL;
    553         Py_RETURN_NONE;
    554 }
    555 
    556 
    557 static struct PyMethodDef PyHKEY_methods[] = {
    558         {"Close",  PyHKEY_CloseMethod, METH_VARARGS, PyHKEY_Close_doc},
    559         {"Detach", PyHKEY_DetachMethod, METH_VARARGS, PyHKEY_Detach_doc},
    560         {"__enter__", (PyCFunction)PyHKEY_Enter, METH_NOARGS, NULL},
    561         {"__exit__", PyHKEY_Exit, METH_VARARGS, NULL},
    562         {NULL}
    563 };
    564 
    565 /*static*/ PyObject *
    566 PyHKEY_getattr(PyObject *self, const char *name)
    567 {
    568         PyObject *res;
    569 
    570         res = Py_FindMethod(PyHKEY_methods, self, name);
    571         if (res != NULL)
    572                 return res;
    573         PyErr_Clear();
    574         if (strcmp(name, "handle") == 0)
    575                 return PyLong_FromVoidPtr(((PyHKEYObject *)self)->hkey);
    576         return PyMember_Get((char *)self, PyHKEY_memberlist, name);
    577 }
     592    if (!PyHKEY_Close(self))
     593        return NULL;
     594    Py_RETURN_NONE;
     595}
     596
    578597
    579598/************************************************************************
     
    583602PyHKEY_New(HKEY hInit)
    584603{
    585         PyHKEYObject *key = PyObject_NEW(PyHKEYObject, &PyHKEY_Type);
    586         if (key)
    587                 key->hkey = hInit;
    588         return (PyObject *)key;
     604    PyHKEYObject *key = PyObject_NEW(PyHKEYObject, &PyHKEY_Type);
     605    if (key)
     606        key->hkey = hInit;
     607    return (PyObject *)key;
    589608}
    590609
     
    592611PyHKEY_Close(PyObject *ob_handle)
    593612{
    594         LONG rc;
    595         PyHKEYObject *key;
    596 
    597         if (!PyHKEY_Check(ob_handle)) {
    598                 PyErr_SetString(PyExc_TypeError, "bad operand type");
    599                 return FALSE;
    600         }
    601         key = (PyHKEYObject *)ob_handle;
    602         rc = key->hkey ? RegCloseKey((HKEY)key->hkey) : ERROR_SUCCESS;
    603         key->hkey = 0;
    604         if (rc != ERROR_SUCCESS)
    605                 PyErr_SetFromWindowsErrWithFunction(rc, "RegCloseKey");
    606         return rc == ERROR_SUCCESS;
     613    LONG rc;
     614    PyHKEYObject *key;
     615
     616    if (!PyHKEY_Check(ob_handle)) {
     617        PyErr_SetString(PyExc_TypeError, "bad operand type");
     618        return FALSE;
     619    }
     620    key = (PyHKEYObject *)ob_handle;
     621    rc = key->hkey ? RegCloseKey((HKEY)key->hkey) : ERROR_SUCCESS;
     622    key->hkey = 0;
     623    if (rc != ERROR_SUCCESS)
     624        PyErr_SetFromWindowsErrWithFunction(rc, "RegCloseKey");
     625    return rc == ERROR_SUCCESS;
    607626}
    608627
     
    610629PyHKEY_AsHKEY(PyObject *ob, HKEY *pHANDLE, BOOL bNoneOK)
    611630{
    612         if (ob == Py_None) {
    613                 if (!bNoneOK) {
    614                         PyErr_SetString(
    615                                   PyExc_TypeError,
    616                                   "None is not a valid HKEY in this context");
    617                         return FALSE;
    618                 }
    619                 *pHANDLE = (HKEY)0;
    620         }
    621         else if (PyHKEY_Check(ob)) {
    622                 PyHKEYObject *pH = (PyHKEYObject *)ob;
    623                 *pHANDLE = pH->hkey;
    624         }
    625         else if (PyInt_Check(ob) || PyLong_Check(ob)) {
    626                 /* We also support integers */
    627                 PyErr_Clear();
    628                 *pHANDLE = (HKEY)PyLong_AsVoidPtr(ob);
    629                 if (PyErr_Occurred())
    630                         return FALSE;
    631         }
    632         else {
    633                 PyErr_SetString(
    634                                 PyExc_TypeError,
    635                         "The object is not a PyHKEY object");
    636                 return FALSE;
    637         }
    638         return TRUE;
     631    if (ob == Py_None) {
     632        if (!bNoneOK) {
     633            PyErr_SetString(
     634                      PyExc_TypeError,
     635                      "None is not a valid HKEY in this context");
     636            return FALSE;
     637        }
     638        *pHANDLE = (HKEY)0;
     639    }
     640    else if (PyHKEY_Check(ob)) {
     641        PyHKEYObject *pH = (PyHKEYObject *)ob;
     642        *pHANDLE = pH->hkey;
     643    }
     644    else if (PyInt_Check(ob) || PyLong_Check(ob)) {
     645        /* We also support integers */
     646        PyErr_Clear();
     647        *pHANDLE = (HKEY)PyLong_AsVoidPtr(ob);
     648        if (PyErr_Occurred())
     649            return FALSE;
     650    }
     651    else {
     652        PyErr_SetString(
     653                        PyExc_TypeError,
     654            "The object is not a PyHKEY object");
     655        return FALSE;
     656    }
     657    return TRUE;
    639658}
    640659
     
    642661PyHKEY_FromHKEY(HKEY h)
    643662{
    644         PyHKEYObject *op;
    645 
    646         /* Inline PyObject_New */
    647         op = (PyHKEYObject *) PyObject_MALLOC(sizeof(PyHKEYObject));
    648         if (op == NULL)
    649                 return PyErr_NoMemory();
    650         PyObject_INIT(op, &PyHKEY_Type);
    651         op->hkey = h;
    652         return (PyObject *)op;
     663    PyHKEYObject *op;
     664
     665    /* Inline PyObject_New */
     666    op = (PyHKEYObject *) PyObject_MALLOC(sizeof(PyHKEYObject));
     667    if (op == NULL)
     668        return PyErr_NoMemory();
     669    PyObject_INIT(op, &PyHKEY_Type);
     670    op->hkey = h;
     671    return (PyObject *)op;
    653672}
    654673
     
    660679PyWinObject_CloseHKEY(PyObject *obHandle)
    661680{
    662         BOOL ok;
    663         if (PyHKEY_Check(obHandle)) {
    664                 ok = PyHKEY_Close(obHandle);
    665         }
     681    BOOL ok;
     682    if (PyHKEY_Check(obHandle)) {
     683        ok = PyHKEY_Close(obHandle);
     684    }
    666685#if SIZEOF_LONG >= SIZEOF_HKEY
    667         else if (PyInt_Check(obHandle)) {
    668                 long rc = RegCloseKey((HKEY)PyInt_AsLong(obHandle));
    669                 ok = (rc == ERROR_SUCCESS);
    670                 if (!ok)
    671                         PyErr_SetFromWindowsErrWithFunction(rc, "RegCloseKey");
    672         }
     686    else if (PyInt_Check(obHandle)) {
     687        long rc = RegCloseKey((HKEY)PyInt_AsLong(obHandle));
     688        ok = (rc == ERROR_SUCCESS);
     689        if (!ok)
     690            PyErr_SetFromWindowsErrWithFunction(rc, "RegCloseKey");
     691    }
    673692#else
    674         else if (PyLong_Check(obHandle)) {
    675                 long rc = RegCloseKey((HKEY)PyLong_AsVoidPtr(obHandle));
    676                 ok = (rc == ERROR_SUCCESS);
    677                 if (!ok)
    678                         PyErr_SetFromWindowsErrWithFunction(rc, "RegCloseKey");
    679         }
     693    else if (PyLong_Check(obHandle)) {
     694        long rc = RegCloseKey((HKEY)PyLong_AsVoidPtr(obHandle));
     695        ok = (rc == ERROR_SUCCESS);
     696        if (!ok)
     697            PyErr_SetFromWindowsErrWithFunction(rc, "RegCloseKey");
     698    }
    680699#endif
    681         else {
    682                 PyErr_SetString(
    683                         PyExc_TypeError,
    684                         "A handle must be a HKEY object or an integer");
    685                 return FALSE;
    686         }
    687         return ok;
     700    else {
     701        PyErr_SetString(
     702            PyExc_TypeError,
     703            "A handle must be a HKEY object or an integer");
     704        return FALSE;
     705    }
     706    return ok;
    688707}
    689708
     
    702721fixupMultiSZ(char **str, char *data, int len)
    703722{
    704         char *P;
    705         int i;
    706         char *Q;
    707 
    708         Q = data + len;
    709         for (P = data, i = 0; P < Q && *P != '\0'; P++, i++) {
    710                 str[i] = P;
    711                 for(; *P != '\0'; P++)
    712                         ;
    713         }
     723    char *P;
     724    int i;
     725    char *Q;
     726
     727    Q = data + len;
     728    for (P = data, i = 0; P < Q && *P != '\0'; P++, i++) {
     729        str[i] = P;
     730        for(; *P != '\0'; P++)
     731            ;
     732    }
    714733}
    715734
     
    717736countStrings(char *data, int len)
    718737{
    719         int strings;
    720         char *P;
    721         char *Q = data + len;
    722 
    723         for (P = data, strings = 0; P < Q && *P != '\0'; P++, strings++)
    724                 for (; P < Q && *P != '\0'; P++)
    725                         ;
    726         return strings;
     738    int strings;
     739    char *P;
     740    char *Q = data + len;
     741
     742    for (P = data, strings = 0; P < Q && *P != '\0'; P++, strings++)
     743        for (; P < Q && *P != '\0'; P++)
     744            ;
     745    return strings;
    727746}
    728747
     
    732751Py2Reg(PyObject *value, DWORD typ, BYTE **retDataBuf, DWORD *retDataSize)
    733752{
    734         Py_ssize_t i,j;
    735         switch (typ) {
    736                 case REG_DWORD:
    737                         if (value != Py_None && !PyInt_Check(value))
    738                                 return FALSE;
    739                         *retDataBuf = (BYTE *)PyMem_NEW(DWORD, 1);
    740                         if (*retDataBuf==NULL){
    741                                 PyErr_NoMemory();
    742                                 return FALSE;
    743                         }
    744                         *retDataSize = sizeof(DWORD);
    745                         if (value == Py_None) {
    746                                 DWORD zero = 0;
    747                                 memcpy(*retDataBuf, &zero, sizeof(DWORD));
    748                         }
    749                         else
    750                                 memcpy(*retDataBuf,
    751                                        &PyInt_AS_LONG((PyIntObject *)value),
    752                                        sizeof(DWORD));
    753                         break;
    754                 case REG_SZ:
    755                 case REG_EXPAND_SZ:
    756                         {
    757                         int need_decref = 0;
    758                         if (value == Py_None)
    759                                 *retDataSize = 1;
    760                         else {
    761                                 if (PyUnicode_Check(value)) {
    762                                         value = PyUnicode_AsEncodedString(
    763                                                       value,
    764                                                       "mbcs",
    765                                                       NULL);
    766                                         if (value==NULL)
    767                                                 return FALSE;
    768                                         need_decref = 1;
    769                                 }
    770                                 if (!PyString_Check(value))
    771                                         return FALSE;
    772                                 *retDataSize = 1 + strlen(
    773                                         PyString_AS_STRING(
    774                                                 (PyStringObject *)value));
    775                         }
    776                         *retDataBuf = (BYTE *)PyMem_NEW(DWORD, *retDataSize);
    777                         if (*retDataBuf==NULL){
    778                                 PyErr_NoMemory();
    779                                 return FALSE;
    780                         }
    781                         if (value == Py_None)
    782                                 strcpy((char *)*retDataBuf, "");
    783                         else
    784                                 strcpy((char *)*retDataBuf,
    785                                        PyString_AS_STRING(
    786                                                 (PyStringObject *)value));
    787                         if (need_decref)
    788                                 Py_DECREF(value);
    789                         break;
    790                         }
    791                 case REG_MULTI_SZ:
    792                         {
    793                                 DWORD size = 0;
    794                                 char *P;
    795                                 PyObject **obs = NULL;
    796 
    797                                 if (value == Py_None)
    798                                         i = 0;
    799                                 else {
    800                                         if (!PyList_Check(value))
    801                                                 return FALSE;
    802                                         i = PyList_Size(value);
    803                                 }
    804                                 obs = malloc(sizeof(PyObject *) * i);
    805                                 memset(obs, 0, sizeof(PyObject *) * i);
    806                                 for (j = 0; j < i; j++)
    807                                 {
    808                                         PyObject *t;
    809                                         t = PyList_GET_ITEM(
    810                                                 (PyListObject *)value,j);
    811                                         if (PyString_Check(t)) {
    812                                                 obs[j] = t;
    813                                                 Py_INCREF(t);
    814                                         } else if (PyUnicode_Check(t)) {
    815                                                 obs[j] = PyUnicode_AsEncodedString(
    816                                                                 t,
    817                                                                 "mbcs",
    818                                                                 NULL);
    819                                                 if (obs[j]==NULL)
    820                                                         goto reg_multi_fail;
    821                                         } else
    822                                                 goto reg_multi_fail;
    823                                         size += 1 + strlen(
    824                                                 PyString_AS_STRING(
    825                                                         (PyStringObject *)obs[j]));
    826                                 }
    827 
    828                                 *retDataSize = size + 1;
    829                                 *retDataBuf = (BYTE *)PyMem_NEW(char,
    830                                                                 *retDataSize);
    831                                 if (*retDataBuf==NULL){
    832                                         PyErr_NoMemory();
    833                                         goto reg_multi_fail;
    834                                 }
    835                                 P = (char *)*retDataBuf;
    836 
    837                                 for (j = 0; j < i; j++)
    838                                 {
    839                                         PyObject *t;
    840                                         t = obs[j];
    841                                         strcpy(P,
    842                                                PyString_AS_STRING(
    843                                                         (PyStringObject *)t));
    844                                         P += 1 + strlen(
    845                                                 PyString_AS_STRING(
    846                                                         (PyStringObject *)t));
    847                                         Py_DECREF(obs[j]);
    848                                 }
    849                                 /* And doubly-terminate the list... */
    850                                 *P = '\0';
    851                                 free(obs);
    852                                 break;
    853                         reg_multi_fail:
    854                                 if (obs) {
    855                                         for (j = 0; j < i; j++)
    856                                                 Py_XDECREF(obs[j]);
    857 
    858                                         free(obs);
    859                                 }
    860                                 return FALSE;
    861                         }
    862                 case REG_BINARY:
    863                 /* ALSO handle ALL unknown data types here.  Even if we can't
    864                    support it natively, we should handle the bits. */
    865                 default:
    866                         if (value == Py_None)
    867                                 *retDataSize = 0;
    868                         else {
    869                                 void *src_buf;
    870                                 PyBufferProcs *pb = value->ob_type->tp_as_buffer;
    871                                 if (pb==NULL) {
    872                                         PyErr_Format(PyExc_TypeError,
    873                                                 "Objects of type '%s' can not "
    874                                                 "be used as binary registry values",
    875                                                 value->ob_type->tp_name);
    876                                         return FALSE;
    877                                 }
    878                                 *retDataSize = (*pb->bf_getreadbuffer)(value, 0, &src_buf);
    879                                 *retDataBuf = (BYTE *)PyMem_NEW(char,
    880                                                                 *retDataSize);
    881                                 if (*retDataBuf==NULL){
    882                                         PyErr_NoMemory();
    883                                         return FALSE;
    884                                 }
    885                                 memcpy(*retDataBuf, src_buf, *retDataSize);
    886                         }
    887                         break;
    888         }
    889         return TRUE;
     753    Py_ssize_t i,j;
     754    switch (typ) {
     755        case REG_DWORD:
     756            if (value != Py_None &&
     757                !(PyInt_Check(value) || PyLong_Check(value)))
     758                return FALSE;
     759            *retDataBuf = (BYTE *)PyMem_NEW(DWORD, 1);
     760            if (*retDataBuf==NULL){
     761                PyErr_NoMemory();
     762                return FALSE;
     763            }
     764            *retDataSize = sizeof(DWORD);
     765            if (value == Py_None) {
     766                DWORD zero = 0;
     767                memcpy(*retDataBuf, &zero, sizeof(DWORD));
     768            }
     769            else {
     770                DWORD d = PyLong_AsUnsignedLong(value);
     771                memcpy(*retDataBuf, &d, sizeof(DWORD));
     772            }
     773            break;
     774        case REG_SZ:
     775        case REG_EXPAND_SZ:
     776            {
     777            int need_decref = 0;
     778            if (value == Py_None)
     779                *retDataSize = 1;
     780            else {
     781                if (PyUnicode_Check(value)) {
     782                    value = PyUnicode_AsEncodedString(
     783                                  value,
     784                                  "mbcs",
     785                                  NULL);
     786                    if (value==NULL)
     787                        return FALSE;
     788                    need_decref = 1;
     789                }
     790                if (!PyString_Check(value))
     791                    return FALSE;
     792                *retDataSize = 1 + strlen(
     793                    PyString_AS_STRING(
     794                        (PyStringObject *)value));
     795            }
     796            *retDataBuf = (BYTE *)PyMem_NEW(DWORD, *retDataSize);
     797            if (*retDataBuf==NULL){
     798                PyErr_NoMemory();
     799                return FALSE;
     800            }
     801            if (value == Py_None)
     802                strcpy((char *)*retDataBuf, "");
     803            else
     804                strcpy((char *)*retDataBuf,
     805                       PyString_AS_STRING(
     806                                (PyStringObject *)value));
     807            if (need_decref)
     808                Py_DECREF(value);
     809            break;
     810            }
     811        case REG_MULTI_SZ:
     812            {
     813                DWORD size = 0;
     814                char *P;
     815                PyObject **obs = NULL;
     816
     817                if (value == Py_None)
     818                    i = 0;
     819                else {
     820                    if (!PyList_Check(value))
     821                        return FALSE;
     822                    i = PyList_Size(value);
     823                }
     824                obs = malloc(sizeof(PyObject *) * i);
     825                memset(obs, 0, sizeof(PyObject *) * i);
     826                for (j = 0; j < i; j++)
     827                {
     828                    PyObject *t;
     829                    t = PyList_GET_ITEM(
     830                        (PyListObject *)value,j);
     831                    if (PyString_Check(t)) {
     832                        obs[j] = t;
     833                        Py_INCREF(t);
     834                    } else if (PyUnicode_Check(t)) {
     835                        obs[j] = PyUnicode_AsEncodedString(
     836                                        t,
     837                                        "mbcs",
     838                                        NULL);
     839                        if (obs[j]==NULL)
     840                            goto reg_multi_fail;
     841                    } else
     842                        goto reg_multi_fail;
     843                    size += 1 + strlen(
     844                        PyString_AS_STRING(
     845                            (PyStringObject *)obs[j]));
     846                }
     847
     848                *retDataSize = size + 1;
     849                *retDataBuf = (BYTE *)PyMem_NEW(char,
     850                                                *retDataSize);
     851                if (*retDataBuf==NULL){
     852                    PyErr_NoMemory();
     853                    goto reg_multi_fail;
     854                }
     855                P = (char *)*retDataBuf;
     856
     857                for (j = 0; j < i; j++)
     858                {
     859                    PyObject *t;
     860                    t = obs[j];
     861                    strcpy(P,
     862                           PyString_AS_STRING(
     863                                    (PyStringObject *)t));
     864                    P += 1 + strlen(
     865                        PyString_AS_STRING(
     866                            (PyStringObject *)t));
     867                    Py_DECREF(obs[j]);
     868                }
     869                /* And doubly-terminate the list... */
     870                *P = '\0';
     871                free(obs);
     872                break;
     873            reg_multi_fail:
     874                if (obs) {
     875                    for (j = 0; j < i; j++)
     876                        Py_XDECREF(obs[j]);
     877
     878                    free(obs);
     879                }
     880                return FALSE;
     881            }
     882        case REG_BINARY:
     883        /* ALSO handle ALL unknown data types here.  Even if we can't
     884           support it natively, we should handle the bits. */
     885        default:
     886            if (value == Py_None)
     887                *retDataSize = 0;
     888            else {
     889                void *src_buf;
     890                PyBufferProcs *pb = value->ob_type->tp_as_buffer;
     891                if (pb==NULL) {
     892                    PyErr_Format(PyExc_TypeError,
     893                        "Objects of type '%s' can not "
     894                        "be used as binary registry values",
     895                        value->ob_type->tp_name);
     896                    return FALSE;
     897                }
     898                *retDataSize = (*pb->bf_getreadbuffer)(value, 0, &src_buf);
     899                *retDataBuf = (BYTE *)PyMem_NEW(char,
     900                                                *retDataSize);
     901                if (*retDataBuf==NULL){
     902                    PyErr_NoMemory();
     903                    return FALSE;
     904                }
     905                memcpy(*retDataBuf, src_buf, *retDataSize);
     906            }
     907            break;
     908    }
     909    return TRUE;
    890910}
    891911
     
    894914Reg2Py(char *retDataBuf, DWORD retDataSize, DWORD typ)
    895915{
    896         PyObject *obData;
    897 
    898         switch (typ) {
    899                 case REG_DWORD:
    900                         if (retDataSize == 0)
    901                                 obData = Py_BuildValue("i", 0);
    902                         else
    903                                 obData = Py_BuildValue("i",
    904                                                        *(int *)retDataBuf);
    905                         break;
    906                 case REG_SZ:
    907                 case REG_EXPAND_SZ:
    908                         /* retDataBuf may or may not have a trailing NULL in
    909                            the buffer. */
    910                         if (retDataSize && retDataBuf[retDataSize-1] == '\0')
    911                                 --retDataSize;
    912                         if (retDataSize ==0)
    913                                 retDataBuf = "";
    914                         obData = PyUnicode_DecodeMBCS(retDataBuf,
    915                                                       retDataSize,
    916                                                       NULL);
    917                         break;
    918                 case REG_MULTI_SZ:
    919                         if (retDataSize == 0)
    920                                 obData = PyList_New(0);
    921                         else
    922                         {
    923                                 int index = 0;
    924                                 int s = countStrings(retDataBuf, retDataSize);
    925                                 char **str = (char **)malloc(sizeof(char *)*s);
    926                                 if (str == NULL)
    927                                         return PyErr_NoMemory();
    928 
    929                                 fixupMultiSZ(str, retDataBuf, retDataSize);
    930                                 obData = PyList_New(s);
    931                                 if (obData == NULL)
    932                                         return NULL;
    933                                 for (index = 0; index < s; index++)
    934                                 {
    935                                         size_t len = _mbstrlen(str[index]);
    936                                         if (len > INT_MAX) {
    937                                                 PyErr_SetString(PyExc_OverflowError,
    938                                                         "registry string is too long for a Python string");
    939                                                 Py_DECREF(obData);
    940                                                 return NULL;
    941                                         }
    942                                         PyList_SetItem(obData,
    943                                                        index,
    944                                                        PyUnicode_DecodeMBCS(
    945                                                             (const char *)str[index],
    946                                                            (int)len,
    947                                                             NULL)
    948                                                        );
    949                                 }
    950                                 free(str);
    951 
    952                                 break;
    953                         }
    954                 case REG_BINARY:
    955                 /* ALSO handle ALL unknown data types here.  Even if we can't
    956                    support it natively, we should handle the bits. */
    957                 default:
    958                         if (retDataSize == 0) {
    959                                 Py_INCREF(Py_None);
    960                                 obData = Py_None;
    961                         }
    962                         else
    963                                 obData = Py_BuildValue("s#",
    964                                                        (char *)retDataBuf,
    965                                                        retDataSize);
    966                         break;
    967         }
    968         if (obData == NULL)
    969                 return NULL;
    970         else
    971                 return obData;
     916    PyObject *obData;
     917
     918    switch (typ) {
     919        case REG_DWORD:
     920            if (retDataSize == 0)
     921                obData = Py_BuildValue("k", 0);
     922            else
     923                obData = Py_BuildValue("k",
     924                                       *(int *)retDataBuf);
     925            break;
     926        case REG_SZ:
     927        case REG_EXPAND_SZ:
     928            /* retDataBuf may or may not have a trailing NULL in
     929               the buffer. */
     930            if (retDataSize && retDataBuf[retDataSize-1] == '\0')
     931                --retDataSize;
     932            if (retDataSize ==0)
     933                retDataBuf = "";
     934            obData = PyUnicode_DecodeMBCS(retDataBuf,
     935                                          retDataSize,
     936                                          NULL);
     937            break;
     938        case REG_MULTI_SZ:
     939            if (retDataSize == 0)
     940                obData = PyList_New(0);
     941            else
     942            {
     943                int index = 0;
     944                int s = countStrings(retDataBuf, retDataSize);
     945                char **str = (char **)malloc(sizeof(char *)*s);
     946                if (str == NULL)
     947                    return PyErr_NoMemory();
     948
     949                fixupMultiSZ(str, retDataBuf, retDataSize);
     950                obData = PyList_New(s);
     951                if (obData == NULL)
     952                    return NULL;
     953                for (index = 0; index < s; index++)
     954                {
     955                    size_t len = _mbstrlen(str[index]);
     956                    if (len > INT_MAX) {
     957                        PyErr_SetString(PyExc_OverflowError,
     958                            "registry string is too long for a Python string");
     959                        Py_DECREF(obData);
     960                        return NULL;
     961                    }
     962                    PyList_SetItem(obData,
     963                                   index,
     964                                   PyUnicode_DecodeMBCS(
     965                                        (const char *)str[index],
     966                                       (int)len,
     967                                        NULL)
     968                                   );
     969                }
     970                free(str);
     971
     972                break;
     973            }
     974        case REG_BINARY:
     975        /* ALSO handle ALL unknown data types here.  Even if we can't
     976           support it natively, we should handle the bits. */
     977        default:
     978            if (retDataSize == 0) {
     979                Py_INCREF(Py_None);
     980                obData = Py_None;
     981            }
     982            else
     983                obData = Py_BuildValue("s#",
     984                                       (char *)retDataBuf,
     985                                       retDataSize);
     986            break;
     987    }
     988    if (obData == NULL)
     989        return NULL;
     990    else
     991        return obData;
    972992}
    973993
     
    977997PyCloseKey(PyObject *self, PyObject *args)
    978998{
    979         PyObject *obKey;
    980         if (!PyArg_ParseTuple(args, "O:CloseKey", &obKey))
    981                 return NULL;
    982         if (!PyHKEY_Close(obKey))
    983                 return NULL;
    984         Py_INCREF(Py_None);
    985         return Py_None;
     999    PyObject *obKey;
     1000    if (!PyArg_ParseTuple(args, "O:CloseKey", &obKey))
     1001        return NULL;
     1002    if (!PyHKEY_Close(obKey))
     1003        return NULL;
     1004    Py_INCREF(Py_None);
     1005    return Py_None;
    9861006}
    9871007
     
    9891009PyConnectRegistry(PyObject *self, PyObject *args)
    9901010{
    991         HKEY hKey;
    992         PyObject *obKey;
    993         char *szCompName = NULL;
    994         HKEY retKey;
    995         long rc;
    996         if (!PyArg_ParseTuple(args, "zO:ConnectRegistry", &szCompName, &obKey))
    997                 return NULL;
    998         if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
    999                 return NULL;
    1000         Py_BEGIN_ALLOW_THREADS
    1001         rc = RegConnectRegistry(szCompName, hKey, &retKey);
    1002         Py_END_ALLOW_THREADS
    1003         if (rc != ERROR_SUCCESS)
    1004                 return PyErr_SetFromWindowsErrWithFunction(rc,
    1005                                                            "ConnectRegistry");
    1006         return PyHKEY_FromHKEY(retKey);
     1011    HKEY hKey;
     1012    PyObject *obKey;
     1013    char *szCompName = NULL;
     1014    HKEY retKey;
     1015    long rc;
     1016    if (!PyArg_ParseTuple(args, "zO:ConnectRegistry", &szCompName, &obKey))
     1017        return NULL;
     1018    if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
     1019        return NULL;
     1020    Py_BEGIN_ALLOW_THREADS
     1021    rc = RegConnectRegistry(szCompName, hKey, &retKey);
     1022    Py_END_ALLOW_THREADS
     1023    if (rc != ERROR_SUCCESS)
     1024        return PyErr_SetFromWindowsErrWithFunction(rc,
     1025                                                   "ConnectRegistry");
     1026    return PyHKEY_FromHKEY(retKey);
    10071027}
    10081028
     
    10101030PyCreateKey(PyObject *self, PyObject *args)
    10111031{
    1012         HKEY hKey;
    1013         PyObject *obKey;
    1014         char *subKey;
    1015         HKEY retKey;
    1016         long rc;
    1017         if (!PyArg_ParseTuple(args, "Oz:CreateKey", &obKey, &subKey))
    1018                 return NULL;
    1019         if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
    1020                 return NULL;
    1021         rc = RegCreateKey(hKey, subKey, &retKey);
    1022         if (rc != ERROR_SUCCESS)
    1023                 return PyErr_SetFromWindowsErrWithFunction(rc, "CreateKey");
    1024         return PyHKEY_FromHKEY(retKey);
     1032    HKEY hKey;
     1033    PyObject *obKey;
     1034    char *subKey;
     1035    HKEY retKey;
     1036    long rc;
     1037    if (!PyArg_ParseTuple(args, "Oz:CreateKey", &obKey, &subKey))
     1038        return NULL;
     1039    if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
     1040        return NULL;
     1041    rc = RegCreateKey(hKey, subKey, &retKey);
     1042    if (rc != ERROR_SUCCESS)
     1043        return PyErr_SetFromWindowsErrWithFunction(rc, "CreateKey");
     1044    return PyHKEY_FromHKEY(retKey);
     1045}
     1046
     1047static PyObject *
     1048PyCreateKeyEx(PyObject *self, PyObject *args)
     1049{
     1050    HKEY hKey;
     1051    PyObject *obKey;
     1052    char *subKey;
     1053    HKEY retKey;
     1054    int res = 0;
     1055    REGSAM sam = KEY_WRITE;
     1056    long rc;
     1057    if (!PyArg_ParseTuple(args, "Oz|ii:CreateKeyEx", &obKey, &subKey,
     1058                                              &res, &sam))
     1059        return NULL;
     1060    if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
     1061        return NULL;
     1062
     1063    rc = RegCreateKeyEx(hKey, subKey, res, NULL, (DWORD)NULL,
     1064                                            sam, NULL, &retKey, NULL);
     1065    if (rc != ERROR_SUCCESS)
     1066        return PyErr_SetFromWindowsErrWithFunction(rc, "CreateKeyEx");
     1067    return PyHKEY_FromHKEY(retKey);
    10251068}
    10261069
     
    10281071PyDeleteKey(PyObject *self, PyObject *args)
    10291072{
    1030         HKEY hKey;
    1031         PyObject *obKey;
    1032         char *subKey;
    1033         long rc;
    1034         if (!PyArg_ParseTuple(args, "Os:DeleteKey", &obKey, &subKey))
    1035                 return NULL;
    1036         if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
    1037                 return NULL;
    1038         rc = RegDeleteKey(hKey, subKey );
    1039         if (rc != ERROR_SUCCESS)
    1040                 return PyErr_SetFromWindowsErrWithFunction(rc, "RegDeleteKey");
    1041         Py_INCREF(Py_None);
    1042         return Py_None;
     1073    HKEY hKey;
     1074    PyObject *obKey;
     1075    char *subKey;
     1076    long rc;
     1077    if (!PyArg_ParseTuple(args, "Os:DeleteKey", &obKey, &subKey))
     1078        return NULL;
     1079    if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
     1080        return NULL;
     1081    rc = RegDeleteKey(hKey, subKey );
     1082    if (rc != ERROR_SUCCESS)
     1083        return PyErr_SetFromWindowsErrWithFunction(rc, "RegDeleteKey");
     1084    Py_INCREF(Py_None);
     1085    return Py_None;
     1086}
     1087
     1088static PyObject *
     1089PyDeleteKeyEx(PyObject *self, PyObject *args)
     1090{
     1091    HKEY hKey;
     1092    PyObject *obKey;
     1093    HMODULE hMod;
     1094    typedef LONG (WINAPI *RDKEFunc)(HKEY, const char*, REGSAM, int);
     1095    RDKEFunc pfn = NULL;
     1096    char *subKey;
     1097    long rc;
     1098    int res = 0;
     1099    REGSAM sam = KEY_WOW64_64KEY;
     1100
     1101    if (!PyArg_ParseTuple(args, "Os|ii:DeleteKeyEx",
     1102                                              &obKey, &subKey, &sam, &res))
     1103        return NULL;
     1104    if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
     1105        return NULL;
     1106
     1107    /* Only available on 64bit platforms, so we must load it
     1108       dynamically. */
     1109    hMod = GetModuleHandle("advapi32.dll");
     1110    if (hMod)
     1111        pfn = (RDKEFunc)GetProcAddress(hMod,
     1112                                                                   "RegDeleteKeyExA");
     1113    if (!pfn) {
     1114        PyErr_SetString(PyExc_NotImplementedError,
     1115                                        "not implemented on this platform");
     1116        return NULL;
     1117    }
     1118    Py_BEGIN_ALLOW_THREADS
     1119    rc = (*pfn)(hKey, subKey, sam, res);
     1120    Py_END_ALLOW_THREADS
     1121
     1122    if (rc != ERROR_SUCCESS)
     1123        return PyErr_SetFromWindowsErrWithFunction(rc, "RegDeleteKeyEx");
     1124    Py_INCREF(Py_None);
     1125    return Py_None;
    10431126}
    10441127
     
    10461129PyDeleteValue(PyObject *self, PyObject *args)
    10471130{
    1048         HKEY hKey;
    1049         PyObject *obKey;
    1050         char *subKey;
    1051         long rc;
    1052         if (!PyArg_ParseTuple(args, "Oz:DeleteValue", &obKey, &subKey))
    1053                 return NULL;
    1054         if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
    1055                 return NULL;
    1056         Py_BEGIN_ALLOW_THREADS
    1057         rc = RegDeleteValue(hKey, subKey);
    1058         Py_END_ALLOW_THREADS
    1059         if (rc !=ERROR_SUCCESS)
    1060                 return PyErr_SetFromWindowsErrWithFunction(rc,
    1061                                                            "RegDeleteValue");
    1062         Py_INCREF(Py_None);
    1063         return Py_None;
     1131    HKEY hKey;
     1132    PyObject *obKey;
     1133    char *subKey;
     1134    long rc;
     1135    if (!PyArg_ParseTuple(args, "Oz:DeleteValue", &obKey, &subKey))
     1136        return NULL;
     1137    if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
     1138        return NULL;
     1139    Py_BEGIN_ALLOW_THREADS
     1140    rc = RegDeleteValue(hKey, subKey);
     1141    Py_END_ALLOW_THREADS
     1142    if (rc !=ERROR_SUCCESS)
     1143        return PyErr_SetFromWindowsErrWithFunction(rc,
     1144                                                   "RegDeleteValue");
     1145    Py_INCREF(Py_None);
     1146    return Py_None;
    10641147}
    10651148
     
    10671150PyEnumKey(PyObject *self, PyObject *args)
    10681151{
    1069         HKEY hKey;
    1070         PyObject *obKey;
    1071         int index;
    1072         long rc;
    1073         PyObject *retStr;
    1074         char tmpbuf[256]; /* max key name length is 255 */
    1075         DWORD len = sizeof(tmpbuf); /* includes NULL terminator */
    1076 
    1077         if (!PyArg_ParseTuple(args, "Oi:EnumKey", &obKey, &index))
    1078                 return NULL;
    1079         if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
    1080                 return NULL;
    1081 
    1082         Py_BEGIN_ALLOW_THREADS
    1083         rc = RegEnumKeyEx(hKey, index, tmpbuf, &len, NULL, NULL, NULL, NULL);
    1084         Py_END_ALLOW_THREADS
    1085         if (rc != ERROR_SUCCESS)
    1086                 return PyErr_SetFromWindowsErrWithFunction(rc, "RegEnumKeyEx");
    1087 
    1088         retStr = PyString_FromStringAndSize(tmpbuf, len);
    1089         return retStr;  /* can be NULL */
     1152    HKEY hKey;
     1153    PyObject *obKey;
     1154    int index;
     1155    long rc;
     1156    PyObject *retStr;
     1157
     1158    /* The Windows docs claim that the max key name length is 255
     1159     * characters, plus a terminating nul character.  However,
     1160     * empirical testing demonstrates that it is possible to
     1161     * create a 256 character key that is missing the terminating
     1162     * nul.  RegEnumKeyEx requires a 257 character buffer to
     1163     * retrieve such a key name. */
     1164    char tmpbuf[257];
     1165    DWORD len = sizeof(tmpbuf); /* includes NULL terminator */
     1166
     1167    if (!PyArg_ParseTuple(args, "Oi:EnumKey", &obKey, &index))
     1168        return NULL;
     1169    if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
     1170        return NULL;
     1171
     1172    Py_BEGIN_ALLOW_THREADS
     1173    rc = RegEnumKeyEx(hKey, index, tmpbuf, &len, NULL, NULL, NULL, NULL);
     1174    Py_END_ALLOW_THREADS
     1175    if (rc != ERROR_SUCCESS)
     1176        return PyErr_SetFromWindowsErrWithFunction(rc, "RegEnumKeyEx");
     1177
     1178    retStr = PyString_FromStringAndSize(tmpbuf, len);
     1179    return retStr;  /* can be NULL */
    10901180}
    10911181
     
    10931183PyEnumValue(PyObject *self, PyObject *args)
    10941184{
    1095         HKEY hKey;
    1096         PyObject *obKey;
    1097         int index;
    1098         long rc;
    1099         char *retValueBuf;
    1100         char *retDataBuf;
    1101         DWORD retValueSize;
    1102         DWORD retDataSize;
    1103         DWORD typ;
    1104         PyObject *obData;
    1105         PyObject *retVal;
    1106 
    1107         if (!PyArg_ParseTuple(args, "Oi:EnumValue", &obKey, &index))
    1108                 return NULL;
    1109         if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
    1110                 return NULL;
    1111 
    1112         if ((rc = RegQueryInfoKey(hKey, NULL, NULL, NULL, NULL, NULL, NULL,
    1113                                   NULL,
    1114                                   &retValueSize, &retDataSize, NULL, NULL))
    1115             != ERROR_SUCCESS)
    1116                 return PyErr_SetFromWindowsErrWithFunction(rc,
    1117                                                            "RegQueryInfoKey");
    1118         ++retValueSize;    /* include null terminators */
    1119         ++retDataSize;
    1120         retValueBuf = (char *)PyMem_Malloc(retValueSize);
    1121         if (retValueBuf == NULL)
    1122                 return PyErr_NoMemory();
    1123         retDataBuf = (char *)PyMem_Malloc(retDataSize);
    1124         if (retDataBuf == NULL) {
    1125                 PyMem_Free(retValueBuf);
    1126                 return PyErr_NoMemory();
    1127         }
    1128 
    1129         Py_BEGIN_ALLOW_THREADS
    1130         rc = RegEnumValue(hKey,
    1131                           index,
    1132                           retValueBuf,
    1133                           &retValueSize,
    1134                           NULL,
    1135                           &typ,
    1136                           (BYTE *)retDataBuf,
    1137                           &retDataSize);
    1138         Py_END_ALLOW_THREADS
    1139 
    1140         if (rc != ERROR_SUCCESS) {
    1141                 retVal = PyErr_SetFromWindowsErrWithFunction(rc,
    1142                                                              "PyRegEnumValue");
    1143                 goto fail;
    1144         }
    1145         obData = Reg2Py(retDataBuf, retDataSize, typ);
    1146         if (obData == NULL) {
    1147                 retVal = NULL;
    1148                 goto fail;
    1149         }
    1150         retVal = Py_BuildValue("sOi", retValueBuf, obData, typ);
    1151         Py_DECREF(obData);
     1185    HKEY hKey;
     1186    PyObject *obKey;
     1187    int index;
     1188    long rc;
     1189    char *retValueBuf;
     1190    char *retDataBuf;
     1191    char *tmpBuf;
     1192    DWORD retValueSize, bufValueSize;
     1193    DWORD retDataSize, bufDataSize;
     1194    DWORD typ;
     1195    PyObject *obData;
     1196    PyObject *retVal;
     1197
     1198    if (!PyArg_ParseTuple(args, "Oi:EnumValue", &obKey, &index))
     1199        return NULL;
     1200    if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
     1201        return NULL;
     1202
     1203    if ((rc = RegQueryInfoKey(hKey, NULL, NULL, NULL, NULL, NULL, NULL,
     1204                              NULL,
     1205                              &retValueSize, &retDataSize, NULL, NULL))
     1206        != ERROR_SUCCESS)
     1207        return PyErr_SetFromWindowsErrWithFunction(rc,
     1208                                                   "RegQueryInfoKey");
     1209    ++retValueSize;    /* include null terminators */
     1210    ++retDataSize;
     1211    bufDataSize = retDataSize;
     1212    bufValueSize = retValueSize;
     1213    retValueBuf = (char *)PyMem_Malloc(retValueSize);
     1214    if (retValueBuf == NULL)
     1215        return PyErr_NoMemory();
     1216    retDataBuf = (char *)PyMem_Malloc(retDataSize);
     1217    if (retDataBuf == NULL) {
     1218        PyMem_Free(retValueBuf);
     1219        return PyErr_NoMemory();
     1220    }
     1221
     1222    while (1) {
     1223        Py_BEGIN_ALLOW_THREADS
     1224        rc = RegEnumValue(hKey,
     1225                  index,
     1226                  retValueBuf,
     1227                  &retValueSize,
     1228                  NULL,
     1229                  &typ,
     1230                  (BYTE *)retDataBuf,
     1231                  &retDataSize);
     1232        Py_END_ALLOW_THREADS
     1233
     1234        if (rc != ERROR_MORE_DATA)
     1235            break;
     1236
     1237        bufDataSize *= 2;
     1238        tmpBuf = (char *)PyMem_Realloc(retDataBuf, bufDataSize);
     1239        if (tmpBuf == NULL) {
     1240            PyErr_NoMemory();
     1241            retVal = NULL;
     1242            goto fail;
     1243        }
     1244        retDataBuf = tmpBuf;
     1245        retDataSize = bufDataSize;
     1246        retValueSize = bufValueSize;
     1247    }
     1248
     1249    if (rc != ERROR_SUCCESS) {
     1250        retVal = PyErr_SetFromWindowsErrWithFunction(rc,
     1251                                                     "PyRegEnumValue");
     1252        goto fail;
     1253    }
     1254    obData = Reg2Py(retDataBuf, retDataSize, typ);
     1255    if (obData == NULL) {
     1256        retVal = NULL;
     1257        goto fail;
     1258    }
     1259    retVal = Py_BuildValue("sOi", retValueBuf, obData, typ);
     1260    Py_DECREF(obData);
    11521261  fail:
    1153         PyMem_Free(retValueBuf);
    1154         PyMem_Free(retDataBuf);
    1155         return retVal;
     1262    PyMem_Free(retValueBuf);
     1263    PyMem_Free(retDataBuf);
     1264    return retVal;
    11561265}
    11571266
     
    11591268PyExpandEnvironmentStrings(PyObject *self, PyObject *args)
    11601269{
    1161         Py_UNICODE *retValue = NULL;
    1162         Py_UNICODE *src;
    1163         DWORD retValueSize;
    1164         DWORD rc;
    1165         PyObject *o;
    1166 
    1167         if (!PyArg_ParseTuple(args, "u:ExpandEnvironmentStrings", &src))
    1168                 return NULL;
    1169 
    1170         retValueSize = ExpandEnvironmentStringsW(src, retValue, 0);
    1171         if (retValueSize == 0) {
    1172                 return PyErr_SetFromWindowsErrWithFunction(retValueSize,
    1173                                                 "ExpandEnvironmentStrings");
    1174         }
    1175         retValue = (Py_UNICODE *)PyMem_Malloc(retValueSize * sizeof(Py_UNICODE));
    1176         if (retValue == NULL) {
    1177                 return PyErr_NoMemory();
    1178         }
    1179 
    1180         rc = ExpandEnvironmentStringsW(src, retValue, retValueSize);
    1181         if (rc == 0) {
    1182                 PyMem_Free(retValue);
    1183                 return PyErr_SetFromWindowsErrWithFunction(retValueSize,
    1184                                                 "ExpandEnvironmentStrings");
    1185         }
    1186         o = PyUnicode_FromUnicode(retValue, wcslen(retValue));
    1187         PyMem_Free(retValue);
    1188         return o;
     1270    Py_UNICODE *retValue = NULL;
     1271    Py_UNICODE *src;
     1272    DWORD retValueSize;
     1273    DWORD rc;
     1274    PyObject *o;
     1275
     1276    if (!PyArg_ParseTuple(args, "u:ExpandEnvironmentStrings", &src))
     1277        return NULL;
     1278
     1279    retValueSize = ExpandEnvironmentStringsW(src, retValue, 0);
     1280    if (retValueSize == 0) {
     1281        return PyErr_SetFromWindowsErrWithFunction(retValueSize,
     1282                                        "ExpandEnvironmentStrings");
     1283    }
     1284    retValue = (Py_UNICODE *)PyMem_Malloc(retValueSize * sizeof(Py_UNICODE));
     1285    if (retValue == NULL) {
     1286        return PyErr_NoMemory();
     1287    }
     1288
     1289    rc = ExpandEnvironmentStringsW(src, retValue, retValueSize);
     1290    if (rc == 0) {
     1291        PyMem_Free(retValue);
     1292        return PyErr_SetFromWindowsErrWithFunction(retValueSize,
     1293                                        "ExpandEnvironmentStrings");
     1294    }
     1295    o = PyUnicode_FromUnicode(retValue, wcslen(retValue));
     1296    PyMem_Free(retValue);
     1297    return o;
    11891298}
    11901299
     
    11921301PyFlushKey(PyObject *self, PyObject *args)
    11931302{
    1194         HKEY hKey;
    1195         PyObject *obKey;
    1196         long rc;
    1197         if (!PyArg_ParseTuple(args, "O:FlushKey", &obKey))
    1198                 return NULL;
    1199         if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
    1200                 return NULL;
    1201         Py_BEGIN_ALLOW_THREADS
    1202         rc = RegFlushKey(hKey);
    1203         Py_END_ALLOW_THREADS
    1204         if (rc != ERROR_SUCCESS)
    1205                 return PyErr_SetFromWindowsErrWithFunction(rc, "RegFlushKey");
    1206         Py_INCREF(Py_None);
    1207         return Py_None;
     1303    HKEY hKey;
     1304    PyObject *obKey;
     1305    long rc;
     1306    if (!PyArg_ParseTuple(args, "O:FlushKey", &obKey))
     1307        return NULL;
     1308    if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
     1309        return NULL;
     1310    Py_BEGIN_ALLOW_THREADS
     1311    rc = RegFlushKey(hKey);
     1312    Py_END_ALLOW_THREADS
     1313    if (rc != ERROR_SUCCESS)
     1314        return PyErr_SetFromWindowsErrWithFunction(rc, "RegFlushKey");
     1315    Py_INCREF(Py_None);
     1316    return Py_None;
    12081317}
    12091318static PyObject *
    12101319PyLoadKey(PyObject *self, PyObject *args)
    12111320{
    1212         HKEY hKey;
    1213         PyObject *obKey;
    1214         char *subKey;
    1215         char *fileName;
    1216 
    1217         long rc;
    1218         if (!PyArg_ParseTuple(args, "Oss:LoadKey", &obKey, &subKey, &fileName))
    1219                 return NULL;
    1220         if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
    1221                 return NULL;
    1222         Py_BEGIN_ALLOW_THREADS
    1223         rc = RegLoadKey(hKey, subKey, fileName );
    1224         Py_END_ALLOW_THREADS
    1225         if (rc != ERROR_SUCCESS)
    1226                 return PyErr_SetFromWindowsErrWithFunction(rc, "RegLoadKey");
    1227         Py_INCREF(Py_None);
    1228         return Py_None;
     1321    HKEY hKey;
     1322    PyObject *obKey;
     1323    char *subKey;
     1324    char *fileName;
     1325
     1326    long rc;
     1327    if (!PyArg_ParseTuple(args, "Oss:LoadKey", &obKey, &subKey, &fileName))
     1328        return NULL;
     1329    if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
     1330        return NULL;
     1331    Py_BEGIN_ALLOW_THREADS
     1332    rc = RegLoadKey(hKey, subKey, fileName );
     1333    Py_END_ALLOW_THREADS
     1334    if (rc != ERROR_SUCCESS)
     1335        return PyErr_SetFromWindowsErrWithFunction(rc, "RegLoadKey");
     1336    Py_INCREF(Py_None);
     1337    return Py_None;
    12291338}
    12301339
     
    12321341PyOpenKey(PyObject *self, PyObject *args)
    12331342{
    1234         HKEY hKey;
    1235         PyObject *obKey;
    1236 
    1237         char *subKey;
    1238         int res = 0;
    1239         HKEY retKey;
    1240         long rc;
    1241         REGSAM sam = KEY_READ;
    1242         if (!PyArg_ParseTuple(args, "Oz|ii:OpenKey", &obKey, &subKey,
    1243                               &res, &sam))
    1244                 return NULL;
    1245         if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
    1246                 return NULL;
    1247 
    1248         Py_BEGIN_ALLOW_THREADS
    1249         rc = RegOpenKeyEx(hKey, subKey, res, sam, &retKey);
    1250         Py_END_ALLOW_THREADS
    1251         if (rc != ERROR_SUCCESS)
    1252                 return PyErr_SetFromWindowsErrWithFunction(rc, "RegOpenKeyEx");
    1253         return PyHKEY_FromHKEY(retKey);
     1343    HKEY hKey;
     1344    PyObject *obKey;
     1345
     1346    char *subKey;
     1347    int res = 0;
     1348    HKEY retKey;
     1349    long rc;
     1350    REGSAM sam = KEY_READ;
     1351    if (!PyArg_ParseTuple(args, "Oz|ii:OpenKey", &obKey, &subKey,
     1352                          &res, &sam))
     1353        return NULL;
     1354    if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
     1355        return NULL;
     1356
     1357    Py_BEGIN_ALLOW_THREADS
     1358    rc = RegOpenKeyEx(hKey, subKey, res, sam, &retKey);
     1359    Py_END_ALLOW_THREADS
     1360    if (rc != ERROR_SUCCESS)
     1361        return PyErr_SetFromWindowsErrWithFunction(rc, "RegOpenKeyEx");
     1362    return PyHKEY_FromHKEY(retKey);
    12541363}
    12551364
     
    12671376  PyObject *ret;
    12681377  if (!PyArg_ParseTuple(args, "O:QueryInfoKey", &obKey))
    1269         return NULL;
     1378    return NULL;
    12701379  if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
    1271         return NULL;
     1380    return NULL;
    12721381  if ((rc = RegQueryInfoKey(hKey, NULL, NULL, 0, &nSubKeys, NULL, NULL,
    1273                             &nValues,  NULL,  NULL, NULL, &ft))
     1382                            &nValues,  NULL,  NULL, NULL, &ft))
    12741383      != ERROR_SUCCESS)
    1275         return PyErr_SetFromWindowsErrWithFunction(rc, "RegQueryInfoKey");
     1384    return PyErr_SetFromWindowsErrWithFunction(rc, "RegQueryInfoKey");
    12761385  li.LowPart = ft.dwLowDateTime;
    12771386  li.HighPart = ft.dwHighDateTime;
    12781387  l = PyLong_FromLongLong(li.QuadPart);
    12791388  if (l == NULL)
    1280         return NULL;
     1389    return NULL;
    12811390  ret = Py_BuildValue("iiO", nSubKeys, nValues, l);
    12821391  Py_DECREF(l);
     
    12871396PyQueryValue(PyObject *self, PyObject *args)
    12881397{
    1289         HKEY hKey;
    1290         PyObject *obKey;
    1291         char *subKey;
    1292         long rc;
    1293         PyObject *retStr;
    1294         char *retBuf;
    1295         long bufSize = 0;
    1296 
    1297         if (!PyArg_ParseTuple(args, "Oz:QueryValue", &obKey, &subKey))
    1298                 return NULL;
    1299 
    1300         if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
    1301                 return NULL;
    1302         if ((rc = RegQueryValue(hKey, subKey, NULL, &bufSize))
    1303             != ERROR_SUCCESS)
    1304                 return PyErr_SetFromWindowsErrWithFunction(rc,
    1305                                                            "RegQueryValue");
    1306         retStr = PyString_FromStringAndSize(NULL, bufSize);
    1307         if (retStr == NULL)
    1308                 return NULL;
    1309         retBuf = PyString_AS_STRING(retStr);
    1310         if ((rc = RegQueryValue(hKey, subKey, retBuf, &bufSize))
    1311             != ERROR_SUCCESS) {
    1312                 Py_DECREF(retStr);
    1313                 return PyErr_SetFromWindowsErrWithFunction(rc,
    1314                                                            "RegQueryValue");
    1315         }
    1316         _PyString_Resize(&retStr, strlen(retBuf));
    1317         return retStr;
     1398    HKEY hKey;
     1399    PyObject *obKey;
     1400    char *subKey;
     1401    long rc;
     1402    PyObject *retStr;
     1403    char *retBuf;
     1404    DWORD bufSize = 0;
     1405    DWORD retSize = 0;
     1406    char *tmp;
     1407
     1408    if (!PyArg_ParseTuple(args, "Oz:QueryValue", &obKey, &subKey))
     1409        return NULL;
     1410
     1411    if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
     1412        return NULL;
     1413 
     1414    rc = RegQueryValue(hKey, subKey, NULL, &retSize);
     1415    if (rc == ERROR_MORE_DATA)
     1416        retSize = 256;
     1417    else if (rc != ERROR_SUCCESS)
     1418        return PyErr_SetFromWindowsErrWithFunction(rc,
     1419                                                   "RegQueryValue");
     1420
     1421    bufSize = retSize;
     1422    retBuf = (char *) PyMem_Malloc(bufSize);
     1423    if (retBuf == NULL)
     1424        return PyErr_NoMemory();
     1425
     1426    while (1) {
     1427        retSize = bufSize;
     1428        rc = RegQueryValue(hKey, subKey, retBuf, &retSize);
     1429        if (rc != ERROR_MORE_DATA)
     1430            break;
     1431
     1432        bufSize *= 2;
     1433        tmp = (char *) PyMem_Realloc(retBuf, bufSize);
     1434        if (tmp == NULL) {
     1435            PyMem_Free(retBuf);
     1436            return PyErr_NoMemory();
     1437        }
     1438        retBuf = tmp;
     1439    }
     1440
     1441    if (rc != ERROR_SUCCESS) {
     1442        PyMem_Free(retBuf);
     1443        return PyErr_SetFromWindowsErrWithFunction(rc,
     1444                                                   "RegQueryValue");
     1445    }
     1446
     1447    if (retBuf[retSize-1] == '\x00')
     1448        retSize--;
     1449    retStr = PyString_FromStringAndSize(retBuf, retSize);
     1450    if (retStr == NULL) {
     1451        PyMem_Free(retBuf);
     1452        return NULL;
     1453    }
     1454    return retStr;
    13181455}
    13191456
     
    13211458PyQueryValueEx(PyObject *self, PyObject *args)
    13221459{
    1323         HKEY hKey;
    1324         PyObject *obKey;
    1325         char *valueName;
    1326 
    1327         long rc;
    1328         char *retBuf;
    1329         DWORD bufSize = 0;
    1330         DWORD typ;
    1331         PyObject *obData;
    1332         PyObject *result;
    1333 
    1334         if (!PyArg_ParseTuple(args, "Oz:QueryValueEx", &obKey, &valueName))
    1335                 return NULL;
    1336 
    1337         if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
    1338                 return NULL;
    1339         if ((rc = RegQueryValueEx(hKey, valueName,
    1340                                   NULL, NULL, NULL,
    1341                                   &bufSize))
    1342             != ERROR_SUCCESS)
    1343                 return PyErr_SetFromWindowsErrWithFunction(rc,
    1344                                                            "RegQueryValueEx");
    1345         retBuf = (char *)PyMem_Malloc(bufSize);
    1346         if (retBuf == NULL)
    1347                 return PyErr_NoMemory();
    1348         if ((rc = RegQueryValueEx(hKey, valueName, NULL,
    1349                                   &typ, (BYTE *)retBuf, &bufSize))
    1350             != ERROR_SUCCESS) {
    1351                 PyMem_Free(retBuf);
    1352                 return PyErr_SetFromWindowsErrWithFunction(rc,
    1353                                                            "RegQueryValueEx");
    1354         }
    1355         obData = Reg2Py(retBuf, bufSize, typ);
    1356         PyMem_Free((void *)retBuf);
    1357         if (obData == NULL)
    1358                 return NULL;
    1359         result = Py_BuildValue("Oi", obData, typ);
    1360         Py_DECREF(obData);
    1361         return result;
     1460    HKEY hKey;
     1461    PyObject *obKey;
     1462    char *valueName;
     1463
     1464    long rc;
     1465    char *retBuf, *tmp;
     1466    DWORD bufSize = 0, retSize;
     1467    DWORD typ;
     1468    PyObject *obData;
     1469    PyObject *result;
     1470
     1471    if (!PyArg_ParseTuple(args, "Oz:QueryValueEx", &obKey, &valueName))
     1472        return NULL;
     1473
     1474    if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
     1475        return NULL;
     1476
     1477    rc = RegQueryValueEx(hKey, valueName, NULL, NULL, NULL, &bufSize);
     1478    if (rc == ERROR_MORE_DATA)
     1479        bufSize = 256;
     1480    else if (rc != ERROR_SUCCESS)
     1481        return PyErr_SetFromWindowsErrWithFunction(rc,
     1482                                                   "RegQueryValueEx");
     1483    retBuf = (char *)PyMem_Malloc(bufSize);
     1484    if (retBuf == NULL)
     1485        return PyErr_NoMemory();
     1486
     1487    while (1) {
     1488        retSize = bufSize;
     1489        rc = RegQueryValueEx(hKey, valueName, NULL, &typ,
     1490                             (BYTE *)retBuf, &retSize);
     1491        if (rc != ERROR_MORE_DATA)
     1492            break;
     1493
     1494        bufSize *= 2;
     1495        tmp = (char *) PyMem_Realloc(retBuf, bufSize);
     1496        if (tmp == NULL) {
     1497            PyMem_Free(retBuf);
     1498            return PyErr_NoMemory();
     1499        }
     1500        retBuf = tmp;
     1501    }
     1502
     1503    if (rc != ERROR_SUCCESS) {
     1504        PyMem_Free(retBuf);
     1505        return PyErr_SetFromWindowsErrWithFunction(rc,
     1506                                                   "RegQueryValueEx");
     1507    }
     1508    obData = Reg2Py(retBuf, bufSize, typ);
     1509    PyMem_Free((void *)retBuf);
     1510    if (obData == NULL)
     1511        return NULL;
     1512    result = Py_BuildValue("Oi", obData, typ);
     1513    Py_DECREF(obData);
     1514    return result;
    13621515}
    13631516
     
    13661519PySaveKey(PyObject *self, PyObject *args)
    13671520{
    1368         HKEY hKey;
    1369         PyObject *obKey;
    1370         char *fileName;
    1371         LPSECURITY_ATTRIBUTES pSA = NULL;
    1372 
    1373         long rc;
    1374         if (!PyArg_ParseTuple(args, "Os:SaveKey", &obKey, &fileName))
    1375                 return NULL;
    1376         if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
    1377                 return NULL;
     1521    HKEY hKey;
     1522    PyObject *obKey;
     1523    char *fileName;
     1524    LPSECURITY_ATTRIBUTES pSA = NULL;
     1525
     1526    long rc;
     1527    if (!PyArg_ParseTuple(args, "Os:SaveKey", &obKey, &fileName))
     1528        return NULL;
     1529    if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
     1530        return NULL;
    13781531/*  One day we may get security into the core?
    1379         if (!PyWinObject_AsSECURITY_ATTRIBUTES(obSA, &pSA, TRUE))
    1380                 return NULL;
     1532    if (!PyWinObject_AsSECURITY_ATTRIBUTES(obSA, &pSA, TRUE))
     1533        return NULL;
    13811534*/
    1382         Py_BEGIN_ALLOW_THREADS
    1383         rc = RegSaveKey(hKey, fileName, pSA );
    1384         Py_END_ALLOW_THREADS
    1385         if (rc != ERROR_SUCCESS)
    1386                 return PyErr_SetFromWindowsErrWithFunction(rc, "RegSaveKey");
    1387         Py_INCREF(Py_None);
    1388         return Py_None;
     1535    Py_BEGIN_ALLOW_THREADS
     1536    rc = RegSaveKey(hKey, fileName, pSA );
     1537    Py_END_ALLOW_THREADS
     1538    if (rc != ERROR_SUCCESS)
     1539        return PyErr_SetFromWindowsErrWithFunction(rc, "RegSaveKey");
     1540    Py_INCREF(Py_None);
     1541    return Py_None;
    13891542}
    13901543
     
    13921545PySetValue(PyObject *self, PyObject *args)
    13931546{
    1394         HKEY hKey;
    1395         PyObject *obKey;
    1396         char *subKey;
    1397         char *str;
    1398         DWORD typ;
    1399         DWORD len;
    1400         long rc;
    1401         PyObject *obStrVal;
    1402         PyObject *obSubKey;
    1403         if (!PyArg_ParseTuple(args, "OOiO:SetValue",
    1404                               &obKey,
    1405                               &obSubKey,
    1406                               &typ,
    1407                               &obStrVal))
    1408                 return NULL;
    1409         if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
    1410                 return NULL;
    1411         if (typ != REG_SZ) {
    1412                 PyErr_SetString(PyExc_TypeError,
    1413                                 "Type must be _winreg.REG_SZ");
    1414                 return NULL;
    1415         }
    1416         /* XXX - need Unicode support */
    1417         str = PyString_AsString(obStrVal);
    1418         if (str == NULL)
    1419                 return NULL;
    1420         len = PyString_Size(obStrVal);
    1421         if (obSubKey == Py_None)
    1422                 subKey = NULL;
    1423         else {
    1424                 subKey = PyString_AsString(obSubKey);
    1425                 if (subKey == NULL)
    1426                         return NULL;
    1427         }
    1428         Py_BEGIN_ALLOW_THREADS
    1429         rc = RegSetValue(hKey, subKey, REG_SZ, str, len+1);
    1430         Py_END_ALLOW_THREADS
    1431         if (rc != ERROR_SUCCESS)
    1432                 return PyErr_SetFromWindowsErrWithFunction(rc, "RegSetValue");
    1433         Py_INCREF(Py_None);
    1434         return Py_None;
     1547    HKEY hKey;
     1548    PyObject *obKey;
     1549    char *subKey;
     1550    char *str;
     1551    DWORD typ;
     1552    DWORD len;
     1553    long rc;
     1554    PyObject *obStrVal;
     1555    PyObject *obSubKey;
     1556    if (!PyArg_ParseTuple(args, "OOiO:SetValue",
     1557                          &obKey,
     1558                          &obSubKey,
     1559                          &typ,
     1560                          &obStrVal))
     1561        return NULL;
     1562    if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
     1563        return NULL;
     1564    if (typ != REG_SZ) {
     1565        PyErr_SetString(PyExc_TypeError,
     1566                        "Type must be _winreg.REG_SZ");
     1567        return NULL;
     1568    }
     1569    /* XXX - need Unicode support */
     1570    str = PyString_AsString(obStrVal);
     1571    if (str == NULL)
     1572        return NULL;
     1573    len = PyString_Size(obStrVal);
     1574    if (obSubKey == Py_None)
     1575        subKey = NULL;
     1576    else {
     1577        subKey = PyString_AsString(obSubKey);
     1578        if (subKey == NULL)
     1579            return NULL;
     1580    }
     1581    Py_BEGIN_ALLOW_THREADS
     1582    rc = RegSetValue(hKey, subKey, REG_SZ, str, len+1);
     1583    Py_END_ALLOW_THREADS
     1584    if (rc != ERROR_SUCCESS)
     1585        return PyErr_SetFromWindowsErrWithFunction(rc, "RegSetValue");
     1586    Py_INCREF(Py_None);
     1587    return Py_None;
    14351588}
    14361589
     
    14381591PySetValueEx(PyObject *self, PyObject *args)
    14391592{
    1440         HKEY hKey;
    1441         PyObject *obKey;
    1442         char *valueName;
    1443         PyObject *obRes;
    1444         PyObject *value;
    1445         BYTE *data;
    1446         DWORD len;
    1447         DWORD typ;
    1448 
    1449         LONG rc;
    1450 
    1451         if (!PyArg_ParseTuple(args, "OzOiO:SetValueEx",
    1452                               &obKey,
    1453                               &valueName,
    1454                               &obRes,
    1455                               &typ,
    1456                               &value))
    1457                 return NULL;
    1458         if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
    1459                 return NULL;
    1460         if (!Py2Reg(value, typ, &data, &len))
    1461         {
    1462                 if (!PyErr_Occurred())
    1463                         PyErr_SetString(PyExc_ValueError,
    1464                                 "Could not convert the data to the specified type.");
    1465                 return NULL;
    1466         }
    1467         Py_BEGIN_ALLOW_THREADS
    1468         rc = RegSetValueEx(hKey, valueName, 0, typ, data, len);
    1469         Py_END_ALLOW_THREADS
    1470         PyMem_DEL(data);
    1471         if (rc != ERROR_SUCCESS)
    1472                 return PyErr_SetFromWindowsErrWithFunction(rc,
    1473                                                            "RegSetValueEx");
    1474         Py_INCREF(Py_None);
    1475         return Py_None;
     1593    HKEY hKey;
     1594    PyObject *obKey;
     1595    char *valueName;
     1596    PyObject *obRes;
     1597    PyObject *value;
     1598    BYTE *data;
     1599    DWORD len;
     1600    DWORD typ;
     1601
     1602    LONG rc;
     1603
     1604    if (!PyArg_ParseTuple(args, "OzOiO:SetValueEx",
     1605                          &obKey,
     1606                          &valueName,
     1607                          &obRes,
     1608                          &typ,
     1609                          &value))
     1610        return NULL;
     1611    if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
     1612        return NULL;
     1613    if (!Py2Reg(value, typ, &data, &len))
     1614    {
     1615        if (!PyErr_Occurred())
     1616            PyErr_SetString(PyExc_ValueError,
     1617                    "Could not convert the data to the specified type.");
     1618        return NULL;
     1619    }
     1620    Py_BEGIN_ALLOW_THREADS
     1621    rc = RegSetValueEx(hKey, valueName, 0, typ, data, len);
     1622    Py_END_ALLOW_THREADS
     1623    PyMem_DEL(data);
     1624    if (rc != ERROR_SUCCESS)
     1625        return PyErr_SetFromWindowsErrWithFunction(rc,
     1626                                                   "RegSetValueEx");
     1627    Py_INCREF(Py_None);
     1628    return Py_None;
    14761629}
    14771630
     
    14791632PyDisableReflectionKey(PyObject *self, PyObject *args)
    14801633{
    1481         HKEY hKey;
    1482         PyObject *obKey;
    1483         HMODULE hMod;
    1484         typedef LONG (WINAPI *RDRKFunc)(HKEY);
    1485         RDRKFunc pfn = NULL;
    1486         LONG rc;
    1487 
    1488         if (!PyArg_ParseTuple(args, "O:DisableReflectionKey", &obKey))
    1489                 return NULL;
    1490         if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
    1491                 return NULL;
    1492 
    1493         // Only available on 64bit platforms, so we must load it
    1494         // dynamically.
    1495         hMod = GetModuleHandle("advapi32.dll");
    1496         if (hMod)
    1497                 pfn = (RDRKFunc)GetProcAddress(hMod,
    1498                                                "RegDisableReflectionKey");
    1499         if (!pfn) {
    1500                 PyErr_SetString(PyExc_NotImplementedError,
    1501                                 "not implemented on this platform");
    1502                 return NULL;
    1503         }
    1504         Py_BEGIN_ALLOW_THREADS
    1505         rc = (*pfn)(hKey);
    1506         Py_END_ALLOW_THREADS
    1507         if (rc != ERROR_SUCCESS)
    1508                 return PyErr_SetFromWindowsErrWithFunction(rc,
    1509                                                            "RegDisableReflectionKey");
    1510         Py_INCREF(Py_None);
    1511         return Py_None;
     1634    HKEY hKey;
     1635    PyObject *obKey;
     1636    HMODULE hMod;
     1637    typedef LONG (WINAPI *RDRKFunc)(HKEY);
     1638    RDRKFunc pfn = NULL;
     1639    LONG rc;
     1640
     1641    if (!PyArg_ParseTuple(args, "O:DisableReflectionKey", &obKey))
     1642        return NULL;
     1643    if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
     1644        return NULL;
     1645
     1646    /* Only available on 64bit platforms, so we must load it
     1647       dynamically. */
     1648    hMod = GetModuleHandle("advapi32.dll");
     1649    if (hMod)
     1650        pfn = (RDRKFunc)GetProcAddress(hMod,
     1651                                       "RegDisableReflectionKey");
     1652    if (!pfn) {
     1653        PyErr_SetString(PyExc_NotImplementedError,
     1654                        "not implemented on this platform");
     1655        return NULL;
     1656    }
     1657    Py_BEGIN_ALLOW_THREADS
     1658    rc = (*pfn)(hKey);
     1659    Py_END_ALLOW_THREADS
     1660    if (rc != ERROR_SUCCESS)
     1661        return PyErr_SetFromWindowsErrWithFunction(rc,
     1662                                                   "RegDisableReflectionKey");
     1663    Py_INCREF(Py_None);
     1664    return Py_None;
    15121665}
    15131666
     
    15151668PyEnableReflectionKey(PyObject *self, PyObject *args)
    15161669{
    1517         HKEY hKey;
    1518         PyObject *obKey;
    1519         HMODULE hMod;
    1520         typedef LONG (WINAPI *RERKFunc)(HKEY);
    1521         RERKFunc pfn = NULL;
    1522         LONG rc;
    1523 
    1524         if (!PyArg_ParseTuple(args, "O:EnableReflectionKey", &obKey))
    1525                 return NULL;
    1526         if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
    1527                 return NULL;
    1528 
    1529         // Only available on 64bit platforms, so we must load it
    1530         // dynamically.
    1531         hMod = GetModuleHandle("advapi32.dll");
    1532         if (hMod)
    1533                 pfn = (RERKFunc)GetProcAddress(hMod,
    1534                                                "RegEnableReflectionKey");
    1535         if (!pfn) {
    1536                 PyErr_SetString(PyExc_NotImplementedError,
    1537                                 "not implemented on this platform");
    1538                 return NULL;
    1539         }
    1540         Py_BEGIN_ALLOW_THREADS
    1541         rc = (*pfn)(hKey);
    1542         Py_END_ALLOW_THREADS
    1543         if (rc != ERROR_SUCCESS)
    1544                 return PyErr_SetFromWindowsErrWithFunction(rc,
    1545                                                            "RegEnableReflectionKey");
    1546         Py_INCREF(Py_None);
    1547         return Py_None;
     1670    HKEY hKey;
     1671    PyObject *obKey;
     1672    HMODULE hMod;
     1673    typedef LONG (WINAPI *RERKFunc)(HKEY);
     1674    RERKFunc pfn = NULL;
     1675    LONG rc;
     1676
     1677    if (!PyArg_ParseTuple(args, "O:EnableReflectionKey", &obKey))
     1678        return NULL;
     1679    if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
     1680        return NULL;
     1681
     1682    /* Only available on 64bit platforms, so we must load it
     1683       dynamically. */
     1684    hMod = GetModuleHandle("advapi32.dll");
     1685    if (hMod)
     1686        pfn = (RERKFunc)GetProcAddress(hMod,
     1687                                       "RegEnableReflectionKey");
     1688    if (!pfn) {
     1689        PyErr_SetString(PyExc_NotImplementedError,
     1690                        "not implemented on this platform");
     1691        return NULL;
     1692    }
     1693    Py_BEGIN_ALLOW_THREADS
     1694    rc = (*pfn)(hKey);
     1695    Py_END_ALLOW_THREADS
     1696    if (rc != ERROR_SUCCESS)
     1697        return PyErr_SetFromWindowsErrWithFunction(rc,
     1698                                                   "RegEnableReflectionKey");
     1699    Py_INCREF(Py_None);
     1700    return Py_None;
    15481701}
    15491702
     
    15511704PyQueryReflectionKey(PyObject *self, PyObject *args)
    15521705{
    1553         HKEY hKey;
    1554         PyObject *obKey;
    1555         HMODULE hMod;
    1556         typedef LONG (WINAPI *RQRKFunc)(HKEY, BOOL *);
    1557         RQRKFunc pfn = NULL;
    1558         BOOL result;
    1559         LONG rc;
    1560 
    1561         if (!PyArg_ParseTuple(args, "O:QueryReflectionKey", &obKey))
    1562                 return NULL;
    1563         if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
    1564                 return NULL;
    1565 
    1566         // Only available on 64bit platforms, so we must load it
    1567         // dynamically.
    1568         hMod = GetModuleHandle("advapi32.dll");
    1569         if (hMod)
    1570                 pfn = (RQRKFunc)GetProcAddress(hMod,
    1571                                                "RegQueryReflectionKey");
    1572         if (!pfn) {
    1573                 PyErr_SetString(PyExc_NotImplementedError,
    1574                                 "not implemented on this platform");
    1575                 return NULL;
    1576         }
    1577         Py_BEGIN_ALLOW_THREADS
    1578         rc = (*pfn)(hKey, &result);
    1579         Py_END_ALLOW_THREADS
    1580         if (rc != ERROR_SUCCESS)
    1581                 return PyErr_SetFromWindowsErrWithFunction(rc,
    1582                                                            "RegQueryReflectionKey");
    1583         return PyBool_FromLong(rc);
     1706    HKEY hKey;
     1707    PyObject *obKey;
     1708    HMODULE hMod;
     1709    typedef LONG (WINAPI *RQRKFunc)(HKEY, BOOL *);
     1710    RQRKFunc pfn = NULL;
     1711    BOOL result;
     1712    LONG rc;
     1713
     1714    if (!PyArg_ParseTuple(args, "O:QueryReflectionKey", &obKey))
     1715        return NULL;
     1716    if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
     1717        return NULL;
     1718
     1719    /* Only available on 64bit platforms, so we must load it
     1720       dynamically. */
     1721    hMod = GetModuleHandle("advapi32.dll");
     1722    if (hMod)
     1723        pfn = (RQRKFunc)GetProcAddress(hMod,
     1724                                       "RegQueryReflectionKey");
     1725    if (!pfn) {
     1726        PyErr_SetString(PyExc_NotImplementedError,
     1727                        "not implemented on this platform");
     1728        return NULL;
     1729    }
     1730    Py_BEGIN_ALLOW_THREADS
     1731    rc = (*pfn)(hKey, &result);
     1732    Py_END_ALLOW_THREADS
     1733    if (rc != ERROR_SUCCESS)
     1734        return PyErr_SetFromWindowsErrWithFunction(rc,
     1735                                                   "RegQueryReflectionKey");
     1736    return PyBool_FromLong(result);
    15841737}
    15851738
    15861739static struct PyMethodDef winreg_methods[] = {
    1587         {"CloseKey",         PyCloseKey,        METH_VARARGS, CloseKey_doc},
    1588         {"ConnectRegistry",  PyConnectRegistry, METH_VARARGS, ConnectRegistry_doc},
    1589         {"CreateKey",        PyCreateKey,       METH_VARARGS, CreateKey_doc},
    1590         {"DeleteKey",        PyDeleteKey,       METH_VARARGS, DeleteKey_doc},
    1591         {"DeleteValue",      PyDeleteValue,     METH_VARARGS, DeleteValue_doc},
    1592         {"DisableReflectionKey", PyDisableReflectionKey, METH_VARARGS, DisableReflectionKey_doc},
    1593         {"EnableReflectionKey",  PyEnableReflectionKey,  METH_VARARGS, EnableReflectionKey_doc},
    1594         {"EnumKey",          PyEnumKey,         METH_VARARGS, EnumKey_doc},
    1595         {"EnumValue",        PyEnumValue,       METH_VARARGS, EnumValue_doc},
    1596         {"ExpandEnvironmentStrings", PyExpandEnvironmentStrings, METH_VARARGS,
    1597                 ExpandEnvironmentStrings_doc },
    1598         {"FlushKey",         PyFlushKey,        METH_VARARGS, FlushKey_doc},
    1599         {"LoadKey",          PyLoadKey,         METH_VARARGS, LoadKey_doc},
    1600         {"OpenKey",          PyOpenKey,         METH_VARARGS, OpenKey_doc},
    1601         {"OpenKeyEx",        PyOpenKey,         METH_VARARGS, OpenKeyEx_doc},
    1602         {"QueryValue",       PyQueryValue,      METH_VARARGS, QueryValue_doc},
    1603         {"QueryValueEx",     PyQueryValueEx,    METH_VARARGS, QueryValueEx_doc},
    1604         {"QueryInfoKey",     PyQueryInfoKey,    METH_VARARGS, QueryInfoKey_doc},
    1605         {"QueryReflectionKey",PyQueryReflectionKey,METH_VARARGS, QueryReflectionKey_doc},
    1606         {"SaveKey",          PySaveKey,         METH_VARARGS, SaveKey_doc},
    1607         {"SetValue",         PySetValue,        METH_VARARGS, SetValue_doc},
    1608         {"SetValueEx",       PySetValueEx,      METH_VARARGS, SetValueEx_doc},
    1609         NULL,
     1740    {"CloseKey",         PyCloseKey,        METH_VARARGS, CloseKey_doc},
     1741    {"ConnectRegistry",  PyConnectRegistry, METH_VARARGS, ConnectRegistry_doc},
     1742    {"CreateKey",        PyCreateKey,       METH_VARARGS, CreateKey_doc},
     1743    {"CreateKeyEx",      PyCreateKeyEx,     METH_VARARGS, CreateKeyEx_doc},
     1744    {"DeleteKey",        PyDeleteKey,       METH_VARARGS, DeleteKey_doc},
     1745    {"DeleteKeyEx",      PyDeleteKeyEx,     METH_VARARGS, DeleteKeyEx_doc},
     1746    {"DeleteValue",      PyDeleteValue,     METH_VARARGS, DeleteValue_doc},
     1747    {"DisableReflectionKey", PyDisableReflectionKey, METH_VARARGS, DisableReflectionKey_doc},
     1748    {"EnableReflectionKey",  PyEnableReflectionKey,  METH_VARARGS, EnableReflectionKey_doc},
     1749    {"EnumKey",          PyEnumKey,         METH_VARARGS, EnumKey_doc},
     1750    {"EnumValue",        PyEnumValue,       METH_VARARGS, EnumValue_doc},
     1751    {"ExpandEnvironmentStrings", PyExpandEnvironmentStrings, METH_VARARGS,
     1752        ExpandEnvironmentStrings_doc },
     1753    {"FlushKey",         PyFlushKey,        METH_VARARGS, FlushKey_doc},
     1754    {"LoadKey",          PyLoadKey,         METH_VARARGS, LoadKey_doc},
     1755    {"OpenKey",          PyOpenKey,         METH_VARARGS, OpenKey_doc},
     1756    {"OpenKeyEx",        PyOpenKey,         METH_VARARGS, OpenKeyEx_doc},
     1757    {"QueryValue",       PyQueryValue,      METH_VARARGS, QueryValue_doc},
     1758    {"QueryValueEx",     PyQueryValueEx,    METH_VARARGS, QueryValueEx_doc},
     1759    {"QueryInfoKey",     PyQueryInfoKey,    METH_VARARGS, QueryInfoKey_doc},
     1760    {"QueryReflectionKey",PyQueryReflectionKey,METH_VARARGS, QueryReflectionKey_doc},
     1761    {"SaveKey",          PySaveKey,         METH_VARARGS, SaveKey_doc},
     1762    {"SetValue",         PySetValue,        METH_VARARGS, SetValue_doc},
     1763    {"SetValueEx",       PySetValueEx,      METH_VARARGS, SetValueEx_doc},
     1764    NULL,
    16101765};
    16111766
     
    16131768insint(PyObject * d, char * name, long value)
    16141769{
    1615         PyObject *v = PyInt_FromLong(value);
    1616         if (!v || PyDict_SetItemString(d, name, v))
    1617                 PyErr_Clear();
    1618         Py_XDECREF(v);
     1770    PyObject *v = PyInt_FromLong(value);
     1771    if (!v || PyDict_SetItemString(d, name, v))
     1772        PyErr_Clear();
     1773    Py_XDECREF(v);
    16191774}
    16201775
     
    16241779inskey(PyObject * d, char * name, HKEY key)
    16251780{
    1626         PyObject *v = PyLong_FromVoidPtr(key);
    1627         if (!v || PyDict_SetItemString(d, name, v))
    1628                 PyErr_Clear();
    1629         Py_XDECREF(v);
     1781    PyObject *v = PyLong_FromVoidPtr(key);
     1782    if (!v || PyDict_SetItemString(d, name, v))
     1783        PyErr_Clear();
     1784    Py_XDECREF(v);
    16301785}
    16311786
     
    16341789PyMODINIT_FUNC init_winreg(void)
    16351790{
    1636         PyObject *m, *d;
    1637         m = Py_InitModule3("_winreg", winreg_methods, module_doc);
    1638         if (m == NULL)
    1639                 return;
    1640         d = PyModule_GetDict(m);
    1641         PyHKEY_Type.ob_type = &PyType_Type;
    1642         PyHKEY_Type.tp_doc = PyHKEY_doc;
    1643         Py_INCREF(&PyHKEY_Type);
    1644         if (PyDict_SetItemString(d, "HKEYType",
    1645                                  (PyObject *)&PyHKEY_Type) != 0)
    1646                 return;
    1647         Py_INCREF(PyExc_WindowsError);
    1648         if (PyDict_SetItemString(d, "error",
    1649                                  PyExc_WindowsError) != 0)
    1650                 return;
    1651 
    1652         /* Add the relevant constants */
    1653         ADD_KEY(HKEY_CLASSES_ROOT);
    1654         ADD_KEY(HKEY_CURRENT_USER);
    1655         ADD_KEY(HKEY_LOCAL_MACHINE);
    1656         ADD_KEY(HKEY_USERS);
    1657         ADD_KEY(HKEY_PERFORMANCE_DATA);
     1791    PyObject *m, *d;
     1792    m = Py_InitModule3("_winreg", winreg_methods, module_doc);
     1793    if (m == NULL)
     1794        return;
     1795    d = PyModule_GetDict(m);
     1796    if (PyType_Ready(&PyHKEY_Type) < 0)
     1797        return;
     1798    PyHKEY_Type.tp_doc = PyHKEY_doc;
     1799    Py_INCREF(&PyHKEY_Type);
     1800    if (PyDict_SetItemString(d, "HKEYType",
     1801                             (PyObject *)&PyHKEY_Type) != 0)
     1802        return;
     1803    Py_INCREF(PyExc_WindowsError);
     1804    if (PyDict_SetItemString(d, "error",
     1805                             PyExc_WindowsError) != 0)
     1806        return;
     1807
     1808    /* Add the relevant constants */
     1809    ADD_KEY(HKEY_CLASSES_ROOT);
     1810    ADD_KEY(HKEY_CURRENT_USER);
     1811    ADD_KEY(HKEY_LOCAL_MACHINE);
     1812    ADD_KEY(HKEY_USERS);
     1813    ADD_KEY(HKEY_PERFORMANCE_DATA);
    16581814#ifdef HKEY_CURRENT_CONFIG
    1659         ADD_KEY(HKEY_CURRENT_CONFIG);
     1815    ADD_KEY(HKEY_CURRENT_CONFIG);
    16601816#endif
    16611817#ifdef HKEY_DYN_DATA
    1662         ADD_KEY(HKEY_DYN_DATA);
     1818    ADD_KEY(HKEY_DYN_DATA);
    16631819#endif
    1664         ADD_INT(KEY_QUERY_VALUE);
    1665         ADD_INT(KEY_SET_VALUE);
    1666         ADD_INT(KEY_CREATE_SUB_KEY);
    1667         ADD_INT(KEY_ENUMERATE_SUB_KEYS);
    1668         ADD_INT(KEY_NOTIFY);
    1669         ADD_INT(KEY_CREATE_LINK);
    1670         ADD_INT(KEY_READ);
    1671         ADD_INT(KEY_WRITE);
    1672         ADD_INT(KEY_EXECUTE);
    1673         ADD_INT(KEY_ALL_ACCESS);
     1820    ADD_INT(KEY_QUERY_VALUE);
     1821    ADD_INT(KEY_SET_VALUE);
     1822    ADD_INT(KEY_CREATE_SUB_KEY);
     1823    ADD_INT(KEY_ENUMERATE_SUB_KEYS);
     1824    ADD_INT(KEY_NOTIFY);
     1825    ADD_INT(KEY_CREATE_LINK);
     1826    ADD_INT(KEY_READ);
     1827    ADD_INT(KEY_WRITE);
     1828    ADD_INT(KEY_EXECUTE);
     1829    ADD_INT(KEY_ALL_ACCESS);
    16741830#ifdef KEY_WOW64_64KEY
    1675         ADD_INT(KEY_WOW64_64KEY);
     1831    ADD_INT(KEY_WOW64_64KEY);
    16761832#endif
    16771833#ifdef KEY_WOW64_32KEY
    1678         ADD_INT(KEY_WOW64_32KEY);
     1834    ADD_INT(KEY_WOW64_32KEY);
    16791835#endif
    1680         ADD_INT(REG_OPTION_RESERVED);
    1681         ADD_INT(REG_OPTION_NON_VOLATILE);
    1682         ADD_INT(REG_OPTION_VOLATILE);
    1683         ADD_INT(REG_OPTION_CREATE_LINK);
    1684         ADD_INT(REG_OPTION_BACKUP_RESTORE);
    1685         ADD_INT(REG_OPTION_OPEN_LINK);
    1686         ADD_INT(REG_LEGAL_OPTION);
    1687         ADD_INT(REG_CREATED_NEW_KEY);
    1688         ADD_INT(REG_OPENED_EXISTING_KEY);
    1689         ADD_INT(REG_WHOLE_HIVE_VOLATILE);
    1690         ADD_INT(REG_REFRESH_HIVE);
    1691         ADD_INT(REG_NO_LAZY_FLUSH);
    1692         ADD_INT(REG_NOTIFY_CHANGE_NAME);
    1693         ADD_INT(REG_NOTIFY_CHANGE_ATTRIBUTES);
    1694         ADD_INT(REG_NOTIFY_CHANGE_LAST_SET);
    1695         ADD_INT(REG_NOTIFY_CHANGE_SECURITY);
    1696         ADD_INT(REG_LEGAL_CHANGE_FILTER);
    1697         ADD_INT(REG_NONE);
    1698         ADD_INT(REG_SZ);
    1699         ADD_INT(REG_EXPAND_SZ);
    1700         ADD_INT(REG_BINARY);
    1701         ADD_INT(REG_DWORD);
    1702         ADD_INT(REG_DWORD_LITTLE_ENDIAN);
    1703         ADD_INT(REG_DWORD_BIG_ENDIAN);
    1704         ADD_INT(REG_LINK);
    1705         ADD_INT(REG_MULTI_SZ);
    1706         ADD_INT(REG_RESOURCE_LIST);
    1707         ADD_INT(REG_FULL_RESOURCE_DESCRIPTOR);
    1708         ADD_INT(REG_RESOURCE_REQUIREMENTS_LIST);
    1709 }
    1710 
     1836    ADD_INT(REG_OPTION_RESERVED);
     1837    ADD_INT(REG_OPTION_NON_VOLATILE);
     1838    ADD_INT(REG_OPTION_VOLATILE);
     1839    ADD_INT(REG_OPTION_CREATE_LINK);
     1840    ADD_INT(REG_OPTION_BACKUP_RESTORE);
     1841    ADD_INT(REG_OPTION_OPEN_LINK);
     1842    ADD_INT(REG_LEGAL_OPTION);
     1843    ADD_INT(REG_CREATED_NEW_KEY);
     1844    ADD_INT(REG_OPENED_EXISTING_KEY);
     1845    ADD_INT(REG_WHOLE_HIVE_VOLATILE);
     1846    ADD_INT(REG_REFRESH_HIVE);
     1847    ADD_INT(REG_NO_LAZY_FLUSH);
     1848    ADD_INT(REG_NOTIFY_CHANGE_NAME);
     1849    ADD_INT(REG_NOTIFY_CHANGE_ATTRIBUTES);
     1850    ADD_INT(REG_NOTIFY_CHANGE_LAST_SET);
     1851    ADD_INT(REG_NOTIFY_CHANGE_SECURITY);
     1852    ADD_INT(REG_LEGAL_CHANGE_FILTER);
     1853    ADD_INT(REG_NONE);
     1854    ADD_INT(REG_SZ);
     1855    ADD_INT(REG_EXPAND_SZ);
     1856    ADD_INT(REG_BINARY);
     1857    ADD_INT(REG_DWORD);
     1858    ADD_INT(REG_DWORD_LITTLE_ENDIAN);
     1859    ADD_INT(REG_DWORD_BIG_ENDIAN);
     1860    ADD_INT(REG_LINK);
     1861    ADD_INT(REG_MULTI_SZ);
     1862    ADD_INT(REG_RESOURCE_LIST);
     1863    ADD_INT(REG_FULL_RESOURCE_DESCRIPTOR);
     1864    ADD_INT(REG_RESOURCE_REQUIREMENTS_LIST);
     1865}
     1866
Note: See TracChangeset for help on using the changeset viewer.