36
36
builtin___build_class__(PyObject *self, PyObject *args, PyObject *kwds)
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;
42
43
assert(args != NULL);
43
44
if (!PyTuple_Check(args)) {
85
/* metaclass is explicitly given, check if it's indeed a class */
86
isclass = PyType_Check(meta);
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);
94
/* else get the type of the first base */
91
96
PyObject *base0 = PyTuple_GET_ITEM(bases, 0);
92
97
meta = (PyObject *) (base0->ob_type);
100
isclass = 1; /* meta is really a class */
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,
107
if (winner == NULL) {
113
if (winner != meta) {
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)) {
1477
1502
PyObject *sep = NULL, *end = NULL, *file = NULL;
1480
if (dummy_args == NULL) {
1481
if (!(dummy_args = PyTuple_New(0)))
1505
if (dummy_args == NULL && !(dummy_args = PyTuple_New(0)))
1484
1507
if (!PyArg_ParseTupleAndKeywords(dummy_args, kwds, "|OOO:print",
1485
1508
kwlist, &sep, &end, &file))
1616
1639
/* If we're interactive, use (GNU) readline */
1641
PyObject *po = NULL;
1621
PyObject *stdin_encoding;
1622
char *stdin_encoding_str;
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;
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
1655
goto _readline_errors;
1631
1656
stdin_encoding_str = _PyUnicode_AsString(stdin_encoding);
1632
if (stdin_encoding_str == NULL) {
1633
Py_DECREF(stdin_encoding);
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)
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);
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);
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);
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);
1667
Py_DECREF(stdin_encoding);
1681
stdout_encoding_str, stdout_errors_str);
1682
Py_CLEAR(stdout_encoding);
1683
Py_CLEAR(stdout_errors);
1686
goto _readline_errors;
1670
1687
prompt = PyBytes_AsString(po);
1671
if (prompt == NULL) {
1672
Py_DECREF(stdin_encoding);
1689
goto _readline_errors;
1681
1695
s = PyOS_Readline(stdin, stdout, prompt);
1683
1696
if (s == NULL) {
1684
1697
if (!PyErr_Occurred())
1685
1698
PyErr_SetNone(PyExc_KeyboardInterrupt);
1686
Py_DECREF(stdin_encoding);
1699
goto _readline_errors;
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,
1708
1721
Py_DECREF(stdin_encoding);
1722
Py_DECREF(stdin_errors);
1727
Py_XDECREF(stdin_encoding);
1728
Py_XDECREF(stdout_encoding);
1729
Py_XDECREF(stdin_errors);
1730
Py_XDECREF(stdout_errors);
1713
1735
/* Fallback if we're not interactive */