~ubuntu-branches/ubuntu/precise/python3.2/precise-proposed

« back to all changes in this revision

Viewing changes to Python/bltinmodule.c

  • Committer: Package Import Robot
  • Author(s): Matthias Klose
  • Date: 2012-03-09 18:40:39 UTC
  • mfrom: (30.1.2 sid)
  • Revision ID: package-import@ubuntu.com-20120309184039-j3yk2emxr1plyo21
Tags: 3.2.3~rc1-1
* Python 3.2.3 release candidate 1.
* Update to 20120309 from the 3.2 branch.
* Fix libpython.a symlink. Closes: #660146.
* Build-depend on xauth.
* Run the gdb tests for the debug build only.

Show diffs side-by-side

added added

removed removed

Lines of Context:
35
35
static PyObject *
36
36
builtin___build_class__(PyObject *self, PyObject *args, PyObject *kwds)
37
37
{
38
 
    PyObject *func, *name, *bases, *mkw, *meta, *prep, *ns, *cell;
 
38
    PyObject *func, *name, *bases, *mkw, *meta, *winner, *prep, *ns, *cell;
39
39
    PyObject *cls = NULL;
40
 
    Py_ssize_t nargs, nbases;
 
40
    Py_ssize_t nargs;
 
41
    int isclass;
41
42
 
42
43
    assert(args != NULL);
43
44
    if (!PyTuple_Check(args)) {
61
62
    bases = PyTuple_GetSlice(args, 2, nargs);
62
63
    if (bases == NULL)
63
64
        return NULL;
64
 
    nbases = nargs - 2;
65
65
 
66
66
    if (kwds == NULL) {
67
67
        meta = NULL;
82
82
                Py_DECREF(bases);
83
83
                return NULL;
84
84
            }
 
85
            /* metaclass is explicitly given, check if it's indeed a class */
 
86
            isclass = PyType_Check(meta);
85
87
        }
86
88
    }
87
89
    if (meta == NULL) {
88
 
        if (PyTuple_GET_SIZE(bases) == 0)
 
90
        /* if there are no bases, use type: */
 
91
        if (PyTuple_GET_SIZE(bases) == 0) {
89
92
            meta = (PyObject *) (&PyType_Type);
 
93
        }
 
94
        /* else get the type of the first base */
90
95
        else {
91
96
            PyObject *base0 = PyTuple_GET_ITEM(bases, 0);
92
97
            meta = (PyObject *) (base0->ob_type);
93
98
        }
94
99
        Py_INCREF(meta);
95
 
    }
 
100
        isclass = 1;  /* meta is really a class */
 
101
    }
 
102
    if (isclass) {
 
103
        /* meta is really a class, so check for a more derived
 
104
           metaclass, or possible metaclass conflicts: */
 
105
        winner = (PyObject *)_PyType_CalculateMetaclass((PyTypeObject *)meta,
 
106
                                                        bases);
 
107
        if (winner == NULL) {
 
108
            Py_DECREF(meta);
 
109
            Py_XDECREF(mkw);
 
110
            Py_DECREF(bases);
 
111
            return NULL;
 
112
        }
 
113
        if (winner != meta) {
 
114
            Py_DECREF(meta);
 
115
            meta = winner;
 
116
            Py_INCREF(meta);
 
117
        }
 
118
    }
 
119
    /* else: meta is not a class, so we cannot do the metaclass
 
120
       calculation, so we will use the explicitly given object as it is */
96
121
    prep = PyObject_GetAttrString(meta, "__prepare__");
97
122
    if (prep == NULL) {
98
123
        if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
469
494
    PyObject *format_spec = NULL;
470
495
 
471
496
    if (!PyArg_ParseTuple(args, "O|U:format", &value, &format_spec))
472
 
    return NULL;
 
497
        return NULL;
473
498
 
474
499
    return PyObject_Format(value, format_spec);
475
500
}
1477
1502
    PyObject *sep = NULL, *end = NULL, *file = NULL;
1478
1503
    int i, err;
1479
1504
 
1480
 
    if (dummy_args == NULL) {
1481
 
        if (!(dummy_args = PyTuple_New(0)))
 
1505
    if (dummy_args == NULL && !(dummy_args = PyTuple_New(0)))
1482
1506
            return NULL;
1483
 
    }
1484
1507
    if (!PyArg_ParseTupleAndKeywords(dummy_args, kwds, "|OOO:print",
1485
1508
                                     kwlist, &sep, &end, &file))
1486
1509
        return NULL;
1615
1638
 
1616
1639
    /* If we're interactive, use (GNU) readline */
1617
1640
    if (tty) {
1618
 
        PyObject *po;
 
1641
        PyObject *po = NULL;
1619
1642
        char *prompt;
1620
 
        char *s;
1621
 
        PyObject *stdin_encoding;
1622
 
        char *stdin_encoding_str;
 
1643
        char *s = NULL;
 
1644
        PyObject *stdin_encoding = NULL, *stdin_errors = NULL;
 
1645
        PyObject *stdout_encoding = NULL, *stdout_errors = NULL;
 
1646
        char *stdin_encoding_str, *stdin_errors_str;
1623
1647
        PyObject *result;
1624
1648
        size_t len;
1625
1649
 
1626
1650
        stdin_encoding = PyObject_GetAttrString(fin, "encoding");
1627
 
        if (!stdin_encoding)
 
1651
        stdin_errors = PyObject_GetAttrString(fin, "errors");
 
1652
        if (!stdin_encoding || !stdin_errors)
1628
1653
            /* stdin is a text stream, so it must have an
1629
1654
               encoding. */
1630
 
            return NULL;
 
1655
            goto _readline_errors;
1631
1656
        stdin_encoding_str = _PyUnicode_AsString(stdin_encoding);
1632
 
        if (stdin_encoding_str  == NULL) {
1633
 
            Py_DECREF(stdin_encoding);
1634
 
            return NULL;
1635
 
        }
 
1657
        stdin_errors_str = _PyUnicode_AsString(stdin_errors);
 
1658
        if (!stdin_encoding_str || !stdin_errors_str)
 
1659
            goto _readline_errors;
1636
1660
        tmp = PyObject_CallMethod(fout, "flush", "");
1637
1661
        if (tmp == NULL)
1638
1662
            PyErr_Clear();
1639
1663
        else
1640
1664
            Py_DECREF(tmp);
1641
1665
        if (promptarg != NULL) {
 
1666
            /* We have a prompt, encode it as stdout would */
 
1667
            char *stdout_encoding_str, *stdout_errors_str;
1642
1668
            PyObject *stringpo;
1643
 
            PyObject *stdout_encoding;
1644
 
            char *stdout_encoding_str;
1645
1669
            stdout_encoding = PyObject_GetAttrString(fout, "encoding");
1646
 
            if (stdout_encoding == NULL) {
1647
 
                Py_DECREF(stdin_encoding);
1648
 
                return NULL;
1649
 
            }
 
1670
            stdout_errors = PyObject_GetAttrString(fout, "errors");
 
1671
            if (!stdout_encoding || !stdout_errors)
 
1672
                goto _readline_errors;
1650
1673
            stdout_encoding_str = _PyUnicode_AsString(stdout_encoding);
1651
 
            if (stdout_encoding_str == NULL) {
1652
 
                Py_DECREF(stdin_encoding);
1653
 
                Py_DECREF(stdout_encoding);
1654
 
                return NULL;
1655
 
            }
 
1674
            stdout_errors_str = _PyUnicode_AsString(stdout_errors);
 
1675
            if (!stdout_encoding_str || !stdout_errors_str)
 
1676
                goto _readline_errors;
1656
1677
            stringpo = PyObject_Str(promptarg);
1657
 
            if (stringpo == NULL) {
1658
 
                Py_DECREF(stdin_encoding);
1659
 
                Py_DECREF(stdout_encoding);
1660
 
                return NULL;
1661
 
            }
 
1678
            if (stringpo == NULL)
 
1679
                goto _readline_errors;
1662
1680
            po = PyUnicode_AsEncodedString(stringpo,
1663
 
                stdout_encoding_str, NULL);
1664
 
            Py_DECREF(stdout_encoding);
1665
 
            Py_DECREF(stringpo);
1666
 
            if (po == NULL) {
1667
 
                Py_DECREF(stdin_encoding);
1668
 
                return NULL;
1669
 
            }
 
1681
                stdout_encoding_str, stdout_errors_str);
 
1682
            Py_CLEAR(stdout_encoding);
 
1683
            Py_CLEAR(stdout_errors);
 
1684
            Py_CLEAR(stringpo);
 
1685
            if (po == NULL)
 
1686
                goto _readline_errors;
1670
1687
            prompt = PyBytes_AsString(po);
1671
 
            if (prompt == NULL) {
1672
 
                Py_DECREF(stdin_encoding);
1673
 
                Py_DECREF(po);
1674
 
                return NULL;
1675
 
            }
 
1688
            if (prompt == NULL)
 
1689
                goto _readline_errors;
1676
1690
        }
1677
1691
        else {
1678
1692
            po = NULL;
1679
1693
            prompt = "";
1680
1694
        }
1681
1695
        s = PyOS_Readline(stdin, stdout, prompt);
1682
 
        Py_XDECREF(po);
1683
1696
        if (s == NULL) {
1684
1697
            if (!PyErr_Occurred())
1685
1698
                PyErr_SetNone(PyExc_KeyboardInterrupt);
1686
 
            Py_DECREF(stdin_encoding);
1687
 
            return NULL;
 
1699
            goto _readline_errors;
1688
1700
        }
1689
1701
 
1690
1702
        len = strlen(s);
1702
1714
                len--;   /* strip trailing '\n' */
1703
1715
                if (len != 0 && s[len-1] == '\r')
1704
1716
                    len--;   /* strip trailing '\r' */
1705
 
                result = PyUnicode_Decode(s, len, stdin_encoding_str, NULL);
 
1717
                result = PyUnicode_Decode(s, len, stdin_encoding_str,
 
1718
                                                  stdin_errors_str);
1706
1719
            }
1707
1720
        }
1708
1721
        Py_DECREF(stdin_encoding);
 
1722
        Py_DECREF(stdin_errors);
 
1723
        Py_XDECREF(po);
1709
1724
        PyMem_FREE(s);
1710
1725
        return result;
 
1726
    _readline_errors:
 
1727
        Py_XDECREF(stdin_encoding);
 
1728
        Py_XDECREF(stdout_encoding);
 
1729
        Py_XDECREF(stdin_errors);
 
1730
        Py_XDECREF(stdout_errors);
 
1731
        Py_XDECREF(po);
 
1732
        return NULL;
1711
1733
    }
1712
1734
 
1713
1735
    /* Fallback if we're not interactive */