~ubuntu-branches/ubuntu/wily/sip4-qt3/wily

« back to all changes in this revision

Viewing changes to siplib/siplib.c

  • Committer: Bazaar Package Importer
  • Author(s): Alessandro Ghersi
  • Date: 2010-10-19 23:44:39 UTC
  • mfrom: (1.3.5 upstream)
  • Revision ID: james.westby@ubuntu.com-20101019234439-1pzo6va0h8yx3bwn
Tags: 4.11.1-0ubuntu1
* New upstream release
* Add python 3 support
  - Add python3-all-dev, python3-all-dbg to build-dependencies
  - Add sipconfig_py3.py
  - Add rules to install sipconfig_py3.py

Show diffs side-by-side

added added

removed removed

Lines of Context:
362
362
    sip_api_deprecated,
363
363
    sip_api_keep_reference,
364
364
    sip_api_parse_kwd_args,
365
 
    sip_api_add_exception
 
365
    sip_api_add_exception,
 
366
    sip_api_get_address
366
367
};
367
368
 
368
369
 
648
649
static int objectify(const char *s, PyObject **objp);
649
650
static void add_failure(PyObject **parseErrp, sipParseFailure *failure);
650
651
static PyObject *bad_type_str(int arg_nr, PyObject *arg);
 
652
static void *explicit_access_func(sipSimpleWrapper *sw, AccessFuncOp op);
 
653
static void *indirect_access_func(sipSimpleWrapper *sw, AccessFuncOp op);
 
654
static void clear_access_func(sipSimpleWrapper *sw);
 
655
static int check_encoded_string(PyObject *obj);
651
656
 
652
657
 
653
658
/*
899
904
#else
900
905
        printf("    Reference count: %d\n", Py_REFCNT(sw));
901
906
#endif
902
 
        printf("    Address of wrapped object: %p\n", sipGetAddress(sw));
 
907
        printf("    Address of wrapped object: %p\n", sip_api_get_address(sw));
903
908
        printf("    To be destroyed by: %s\n", (sipIsPyOwned(sw) ? "Python" : "C/C++"));
904
909
        printf("    Derived class?: %s\n", (sipIsDerived(sw) ? "yes" : "no"));
905
910
 
1069
1074
    if (!PyArg_ParseTuple(args, "O!:isdeleted", &sipSimpleWrapper_Type, &sw))
1070
1075
        return NULL;
1071
1076
 
1072
 
    res = (sipGetAddress(sw) == NULL ? Py_True : Py_False);
 
1077
    res = (sip_api_get_address(sw) == NULL ? Py_True : Py_False);
1073
1078
 
1074
1079
    Py_INCREF(res);
1075
1080
    return res;
1114
1119
        sipResetPyOwned(sw);
1115
1120
    }
1116
1121
 
1117
 
    sw->u.cppPtr = NULL;
 
1122
    clear_access_func(sw);
1118
1123
 
1119
1124
    Py_INCREF(Py_None);
1120
1125
    return Py_None;
3442
3447
            {
3443
3448
                /* String from a Python string or None. */
3444
3449
 
3445
 
                PyObject **keep = va_arg(va, PyObject **);
3446
 
                const char **p = va_arg(va, const char **);
3447
 
                char sub_fmt = *fmt++;
3448
 
 
3449
 
                if (arg != NULL)
3450
 
                {
3451
 
                    PyObject *s;
3452
 
 
3453
 
                    switch (sub_fmt)
3454
 
                    {
3455
 
                    case 'A':
3456
 
                        s = parseString_AsASCIIString(arg, p);
3457
 
                        break;
3458
 
 
3459
 
                    case 'L':
3460
 
                        s = parseString_AsLatin1String(arg, p);
3461
 
                        break;
3462
 
 
3463
 
                    case '8':
3464
 
                        s = parseString_AsUTF8String(arg, p);
3465
 
                        break;
3466
 
                    }
3467
 
 
3468
 
                    if (s == NULL)
3469
 
                    {
3470
 
                        failure.reason = WrongType;
3471
 
                        failure.detail_obj = arg;
3472
 
                        Py_INCREF(arg);
3473
 
                    }
3474
 
                    else
3475
 
                    {
3476
 
                        *keep = s;
3477
 
                    }
 
3450
                va_arg(va, PyObject **);
 
3451
                va_arg(va, const char **);
 
3452
                fmt++;
 
3453
 
 
3454
                if (arg != NULL && check_encoded_string(arg) < 0)
 
3455
                {
 
3456
                    failure.reason = WrongType;
 
3457
                    failure.detail_obj = arg;
 
3458
                    Py_INCREF(arg);
 
3459
                }
 
3460
 
 
3461
                break;
 
3462
            }
 
3463
 
 
3464
        case 'a':
 
3465
            {
 
3466
                /* Character from a Python string. */
 
3467
 
 
3468
                va_arg(va, char *);
 
3469
                fmt++;
 
3470
 
 
3471
                if (arg != NULL && check_encoded_string(arg) < 0)
 
3472
                {
 
3473
                    failure.reason = WrongType;
 
3474
                    failure.detail_obj = arg;
 
3475
                    Py_INCREF(arg);
3478
3476
                }
3479
3477
 
3480
3478
                break;
3923
3921
                break;
3924
3922
            }
3925
3923
 
3926
 
        case 'a':
3927
 
            {
3928
 
                /* Character from a Python string. */
3929
 
 
3930
 
                char *p = va_arg(va, char *);
3931
 
                char sub_fmt = *fmt++;
3932
 
 
3933
 
                if (arg != NULL)
3934
 
                {
3935
 
                    int enc;
3936
 
 
3937
 
                    switch (sub_fmt)
3938
 
                    {
3939
 
                    case 'A':
3940
 
                        enc = parseString_AsASCIIChar(arg, p);
3941
 
                        break;
3942
 
 
3943
 
                    case 'L':
3944
 
                        enc = parseString_AsLatin1Char(arg, p);
3945
 
                        break;
3946
 
 
3947
 
                    case '8':
3948
 
                        enc = parseString_AsUTF8Char(arg, p);
3949
 
                        break;
3950
 
                    }
3951
 
 
3952
 
                    if (enc < 0)
3953
 
                    {
3954
 
                        failure.reason = WrongType;
3955
 
                        failure.detail_obj = arg;
3956
 
                        Py_INCREF(arg);
3957
 
                    }
3958
 
                }
3959
 
 
3960
 
                break;
3961
 
            }
3962
 
 
3963
3924
        case 'w':
3964
3925
#if defined(HAVE_WCHAR_H)
3965
3926
            {
4788
4749
                break;
4789
4750
            }
4790
4751
 
4791
 
        /*
4792
 
         * These need special handling because they have a sub-format
4793
 
         * character.
4794
 
         */
4795
4752
        case 'A':
4796
 
            va_arg(va, void *);
4797
 
 
4798
 
            /* Drop through. */
 
4753
            {
 
4754
                /* String from a Python string or None. */
 
4755
 
 
4756
                PyObject **keep = va_arg(va, PyObject **);
 
4757
                const char **p = va_arg(va, const char **);
 
4758
                char sub_fmt = *fmt++;
 
4759
 
 
4760
                if (arg != NULL)
 
4761
                {
 
4762
                    PyObject *s;
 
4763
 
 
4764
                    switch (sub_fmt)
 
4765
                    {
 
4766
                    case 'A':
 
4767
                        s = parseString_AsASCIIString(arg, p);
 
4768
                        break;
 
4769
 
 
4770
                    case 'L':
 
4771
                        s = parseString_AsLatin1String(arg, p);
 
4772
                        break;
 
4773
 
 
4774
                    case '8':
 
4775
                        s = parseString_AsUTF8String(arg, p);
 
4776
                        break;
 
4777
                    }
 
4778
 
 
4779
                    if (s == NULL)
 
4780
                        return FALSE;
 
4781
 
 
4782
                    *keep = s;
 
4783
                }
 
4784
 
 
4785
                break;
 
4786
            }
4799
4787
 
4800
4788
        case 'a':
4801
 
            va_arg(va, void *);
4802
 
            fmt++;
4803
 
            break;
 
4789
            {
 
4790
                /* Character from a Python string. */
 
4791
 
 
4792
                char *p = va_arg(va, char *);
 
4793
                char sub_fmt = *fmt++;
 
4794
 
 
4795
                if (arg != NULL)
 
4796
                {
 
4797
                    int enc;
 
4798
 
 
4799
                    switch (sub_fmt)
 
4800
                    {
 
4801
                    case 'A':
 
4802
                        enc = parseString_AsASCIIChar(arg, p);
 
4803
                        break;
 
4804
 
 
4805
                    case 'L':
 
4806
                        enc = parseString_AsLatin1Char(arg, p);
 
4807
                        break;
 
4808
 
 
4809
                    case '8':
 
4810
                        enc = parseString_AsUTF8Char(arg, p);
 
4811
                        break;
 
4812
                    }
 
4813
 
 
4814
                    if (enc < 0)
 
4815
                        return FALSE;
 
4816
                }
 
4817
 
 
4818
                break;
 
4819
            }
4804
4820
 
4805
4821
        /*
4806
4822
         * Every other argument is a pointer and only differ in how many there
5004
5020
        callPyDtor(sipSelf);
5005
5021
        PyErr_Restore(xtype, xvalue, xtb);
5006
5022
 
5007
 
        if (!sipNotInMap(sipSelf))
5008
 
            sipOMRemoveObject(&cppPyMap, sipSelf);
 
5023
        sipOMRemoveObject(&cppPyMap, sipSelf);
5009
5024
 
5010
5025
        /* This no longer points to anything useful. */
5011
 
        sipSelf->u.cppPtr = NULL;
 
5026
        clear_access_func(sipSelf);
5012
5027
 
5013
5028
        /*
5014
5029
         * If C/C++ has a reference (and therefore no parent) then remove it.
5028
5043
 
5029
5044
 
5030
5045
/*
 
5046
 * Clear any access function so that sip_api_get_address() will always return a
 
5047
 * NULL pointer.
 
5048
 */
 
5049
static void clear_access_func(sipSimpleWrapper *sw)
 
5050
{
 
5051
    if (sw->access_func != NULL)
 
5052
    {
 
5053
        sw->access_func(sw, ReleaseGuard);
 
5054
        sw->access_func = NULL;
 
5055
    }
 
5056
 
 
5057
    sw->data = NULL;
 
5058
}
 
5059
 
 
5060
 
 
5061
/*
5031
5062
 * Call self.__dtor__() if it is implemented.
5032
5063
 */
5033
5064
static void callPyDtor(sipSimpleWrapper *self)
5226
5257
        goto relname;
5227
5258
 
5228
5259
    /* Pass the type via the back door. */
 
5260
    assert(currentType == NULL);
5229
5261
    currentType = td;
 
5262
    py_type = PyObject_Call(metatype, args, NULL);
 
5263
    currentType = NULL;
5230
5264
 
5231
 
    if ((py_type = PyObject_Call(metatype, args, NULL)) == NULL)
 
5265
    if (py_type == NULL)
5232
5266
        goto relargs;
5233
5267
 
5234
5268
    /* Add the type to the "parent" dictionary. */
5256
5290
    Py_DECREF(typedict);
5257
5291
 
5258
5292
reterr:
5259
 
    currentType = NULL;
5260
5293
    return NULL;
5261
5294
}
5262
5295
 
5697
5730
        goto relname;
5698
5731
 
5699
5732
    /* Pass the type via the back door. */
 
5733
    assert(currentType == NULL);
5700
5734
    currentType = &etd->etd_base;
5701
 
 
5702
 
    py_type = (PyTypeObject *)PyObject_Call((PyObject *)&sipEnumType_Type, args, NULL);
 
5735
    py_type = (PyTypeObject *)PyObject_Call((PyObject *)&sipEnumType_Type,
 
5736
            args, NULL);
 
5737
    currentType = NULL;
5703
5738
 
5704
5739
    Py_DECREF(args);
5705
5740
 
7347
7382
 
7348
7383
 
7349
7384
/*
7350
 
 * Return the C/C++ pointer from a wrapper without any checks.
7351
 
 */
7352
 
void *sipGetAddress(sipSimpleWrapper *sw)
7353
 
{
7354
 
    if (sipIsAccessFunc(sw))
7355
 
        return (*sw->u.afPtr)();
7356
 
 
7357
 
    if (sipIsIndirect(sw))
7358
 
        return *((void **)sw->u.cppPtr);
7359
 
 
7360
 
    return sw->u.cppPtr;
 
7385
 * The default access function.
 
7386
 */
 
7387
void *sip_api_get_address(sipSimpleWrapper *w)
 
7388
{
 
7389
    return (w->access_func != NULL) ? w->access_func(w, GuardedPointer) : w->data;
 
7390
}
 
7391
 
 
7392
 
 
7393
/*
 
7394
 * The access function for handwritten access functions.
 
7395
 */
 
7396
static void *explicit_access_func(sipSimpleWrapper *sw, AccessFuncOp op)
 
7397
{
 
7398
    typedef void *(*explicitAccessFunc)(void);
 
7399
 
 
7400
    if (op == ReleaseGuard)
 
7401
        return NULL;
 
7402
 
 
7403
    return ((explicitAccessFunc)(sw->data))();
 
7404
}
 
7405
 
 
7406
 
 
7407
/*
 
7408
 * The access function for indirect access.
 
7409
 */
 
7410
static void *indirect_access_func(sipSimpleWrapper *sw, AccessFuncOp op)
 
7411
{
 
7412
    if (op == ReleaseGuard)
 
7413
        return NULL;
 
7414
 
 
7415
    return *((void **)sw->data);
7361
7416
}
7362
7417
 
7363
7418
 
7396
7451
 */
7397
7452
void *sip_api_get_cpp_ptr(sipSimpleWrapper *sw, const sipTypeDef *td)
7398
7453
{
7399
 
    void *ptr = sipGetAddress(sw);
 
7454
    void *ptr = sip_api_get_address(sw);
7400
7455
 
7401
7456
    if (checkPointer(ptr) < 0)
7402
7457
        return NULL;
8246
8301
{
8247
8302
    *ctd = (const sipClassTypeDef *)((sipWrapperType *)Py_TYPE(self))->type;
8248
8303
 
8249
 
    return (sipNotInMap(self) ? NULL : self->u.cppPtr);
 
8304
    return (sipNotInMap(self) ? NULL : sip_api_get_address(self));
8250
8305
}
8251
8306
 
8252
8307
 
8369
8424
     */
8370
8425
    if (currentType != NULL)
8371
8426
    {
 
8427
        assert(!sipTypeIsEnum(currentType));
 
8428
 
8372
8429
        ((sipWrapperType *)o)->type = currentType;
8373
8430
 
8374
8431
        if (sipTypeIsClass(currentType))
8386
8443
 
8387
8444
            addClassSlots((sipWrapperType *)o, (sipClassTypeDef *)currentType);
8388
8445
        }
8389
 
 
8390
 
        currentType = NULL;
8391
8446
    }
8392
8447
 
8393
8448
    return o;
8680
8735
        addToParent((sipWrapper *)self, (sipWrapper *)owner);
8681
8736
    }
8682
8737
 
8683
 
    self->u.cppPtr = sipNew;
 
8738
    self->data = sipNew;
8684
8739
    self->flags = sipFlags;
8685
8740
 
 
8741
    /* Set the access function. */
 
8742
    if (sipIsAccessFunc(self))
 
8743
        self->access_func = explicit_access_func;
 
8744
    else if (sipIsIndirect(self))
 
8745
        self->access_func = indirect_access_func;
 
8746
    else
 
8747
        self->access_func = NULL;
 
8748
 
8686
8749
    if (!sipNotInMap(self))
8687
8750
        sipOMAddObject(&cppPyMap, self);
8688
8751
 
8924
8987
 
8925
8988
 
8926
8989
/*
8927
 
 * The type call slot.  Note that keyword arguments aren't supported.
 
8990
 * The type call slot.
8928
8991
 */
8929
 
static PyObject *slot_call(PyObject *self,PyObject *args,PyObject *kw)
 
8992
static PyObject *slot_call(PyObject *self, PyObject *args, PyObject *kw)
8930
8993
{
8931
 
    PyObject *(*f)(PyObject *,PyObject *);
 
8994
    PyObject *(*f)(PyObject *, PyObject *, PyObject *);
8932
8995
 
8933
 
    f = (PyObject *(*)(PyObject *,PyObject *))findSlot(self, call_slot);
 
8996
    f = (PyObject *(*)(PyObject *, PyObject *, PyObject *))findSlot(self, call_slot);
8934
8997
 
8935
8998
    assert(f != NULL);
8936
8999
 
8937
 
    return f(self,args);
 
9000
    return f(self, args, kw);
8938
9001
}
8939
9002
 
8940
9003
 
9191
9254
    /* Remove any slots connected via a proxy. */
9192
9255
    if (sipQtSupport != NULL && sipPossibleProxy(sw))
9193
9256
    {
9194
 
        void *tx = sipGetAddress(sw);
 
9257
        void *tx = sip_api_get_address(sw);
9195
9258
 
9196
9259
        if (tx != NULL)
9197
9260
        {
9259
9322
    /* This should be handwritten code in PyQt. */
9260
9323
    if (sipQtSupport != NULL)
9261
9324
    {
9262
 
        void *tx = sipGetAddress(sw);
 
9325
        void *tx = sip_api_get_address(sw);
9263
9326
 
9264
9327
        if (tx != NULL)
9265
9328
        {
9664
9727
     */
9665
9728
    PyObject_GC_UnTrack((PyObject *)sw);
9666
9729
 
 
9730
    /*
 
9731
     * Remove the object from the map before calling the class specific dealloc
 
9732
     * code.  This code calls the C++ dtor and may result in further calls that
 
9733
     * pass the instance as an argument.  If this is still in the map then it's
 
9734
     * reference count would be increased (to one) and bad things happen when
 
9735
     * it drops back to zero again.  (An example is PyQt events generated
 
9736
     * during the dtor call being passed to an event filter implemented in
 
9737
     * Python.)  By removing it from the map first we ensure that a new Python
 
9738
     * object is created.
 
9739
     */
 
9740
    sipOMRemoveObject(&cppPyMap, sw);
 
9741
 
9667
9742
    if (getPtrTypeDef(sw, &ctd) != NULL)
9668
9743
    {
9669
 
        /*
9670
 
         * Remove the object from the map before calling the class specific
9671
 
         * dealloc code.  This code calls the C++ dtor and may result in
9672
 
         * further calls that pass the instance as an argument.  If this is
9673
 
         * still in the map then it's reference count would be increased (to
9674
 
         * one) and bad things happen when it drops back to zero again.  (An
9675
 
         * example is PyQt events generated during the dtor call being passed
9676
 
         * to an event filter implemented in Python.)  By removing it from the
9677
 
         * map first we ensure that a new Python object is created.
9678
 
         */
9679
 
        sipOMRemoveObject(&cppPyMap, sw);
9680
 
 
9681
9744
        /* Call the C++ dtor if there is one. */
9682
9745
        if (ctd->ctd_dealloc != NULL)
9683
9746
            ctd->ctd_dealloc(sw);
9684
9747
    }
 
9748
 
 
9749
    clear_access_func(sw);
9685
9750
}
9686
9751
 
9687
9752
 
10141
10206
        return bytes;
10142
10207
    }
10143
10208
 
 
10209
    /* Don't try anything else if there was an encoding error. */
 
10210
    if (PyUnicode_Check(obj))
 
10211
        return NULL;
 
10212
 
10144
10213
    PyErr_Clear();
10145
10214
 
10146
10215
    if (parseBytes_AsString(obj, ap) < 0)
10476
10545
    sipPySlotDef *psd;
10477
10546
 
10478
10547
    assert(currentType != NULL);
 
10548
    assert(sipTypeIsEnum(currentType));
10479
10549
 
10480
10550
    /* Call the standard super-metatype alloc. */
10481
10551
    if ((py_type = (sipEnumTypeObject *)PyType_Type.tp_alloc(self, nitems)) == NULL)
10495
10565
    if ((psd = ((sipEnumTypeDef *)currentType)->etd_pyslots) != NULL)
10496
10566
        addTypeSlots(&py_type->super, psd);
10497
10567
 
10498
 
    currentType = NULL;
10499
 
 
10500
10568
    return (PyObject *)py_type;
10501
10569
}
 
10570
 
 
10571
 
 
10572
/*
 
10573
 * Check if an object is of the right type to convert to an encoded string.
 
10574
 */
 
10575
static int check_encoded_string(PyObject *obj)
 
10576
{
 
10577
    if (obj == Py_None)
 
10578
        return 0;
 
10579
 
 
10580
    if (PyUnicode_Check(obj))
 
10581
        return 0;
 
10582
 
 
10583
    if (SIPBytes_Check(obj))
 
10584
        return 0;
 
10585
 
 
10586
    if (PyObject_CheckReadBuffer(obj))
 
10587
        return 0;
 
10588
 
 
10589
    return -1;
 
10590
}