aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/tools/cython/Cython/Utility/Printing.c
blob: 40768e6aff4994ef5e11feb956a24a72b0d658e1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
////////////////////// Print.proto ////////////////////// 
//@substitute: naming 
 
static int __Pyx_Print(PyObject*, PyObject *, int); /*proto*/ 
#if CYTHON_COMPILING_IN_PYPY || PY_MAJOR_VERSION >= 3 
static PyObject* $print_function = 0; 
static PyObject* $print_function_kwargs = 0; 
#endif 
 
////////////////////// Print.cleanup ////////////////////// 
//@substitute: naming 
 
#if CYTHON_COMPILING_IN_PYPY || PY_MAJOR_VERSION >= 3 
Py_CLEAR($print_function); 
Py_CLEAR($print_function_kwargs); 
#endif 
 
////////////////////// Print ////////////////////// 
//@substitute: naming 
 
#if !CYTHON_COMPILING_IN_PYPY && PY_MAJOR_VERSION < 3 
static PyObject *__Pyx_GetStdout(void) { 
    PyObject *f = PySys_GetObject((char *)"stdout"); 
    if (!f) { 
        PyErr_SetString(PyExc_RuntimeError, "lost sys.stdout"); 
    } 
    return f; 
} 
 
static int __Pyx_Print(PyObject* f, PyObject *arg_tuple, int newline) { 
    int i; 
 
    if (!f) { 
        if (!(f = __Pyx_GetStdout())) 
            return -1; 
    } 
    Py_INCREF(f); 
    for (i=0; i < PyTuple_GET_SIZE(arg_tuple); i++) { 
        PyObject* v; 
        if (PyFile_SoftSpace(f, 1)) { 
            if (PyFile_WriteString(" ", f) < 0) 
                goto error; 
        } 
        v = PyTuple_GET_ITEM(arg_tuple, i); 
        if (PyFile_WriteObject(v, f, Py_PRINT_RAW) < 0) 
            goto error; 
        if (PyString_Check(v)) { 
            char *s = PyString_AsString(v); 
            Py_ssize_t len = PyString_Size(v); 
            if (len > 0) { 
                // append soft-space if necessary (not using isspace() due to C/C++ problem on MacOS-X) 
                switch (s[len-1]) { 
                    case ' ': break; 
                    case '\f': case '\r': case '\n': case '\t': case '\v': 
                        PyFile_SoftSpace(f, 0); 
                        break; 
                    default:  break; 
                } 
            } 
        } 
    } 
    if (newline) { 
        if (PyFile_WriteString("\n", f) < 0) 
            goto error; 
        PyFile_SoftSpace(f, 0); 
    } 
    Py_DECREF(f); 
    return 0; 
error: 
    Py_DECREF(f); 
    return -1; 
} 
 
#else /* Python 3 has a print function */ 
 
static int __Pyx_Print(PyObject* stream, PyObject *arg_tuple, int newline) { 
    PyObject* kwargs = 0; 
    PyObject* result = 0; 
    PyObject* end_string; 
    if (unlikely(!$print_function)) { 
        $print_function = PyObject_GetAttr($builtins_cname, PYIDENT("print")); 
        if (!$print_function) 
            return -1; 
    } 
    if (stream) { 
        kwargs = PyDict_New(); 
        if (unlikely(!kwargs)) 
            return -1; 
        if (unlikely(PyDict_SetItem(kwargs, PYIDENT("file"), stream) < 0)) 
            goto bad; 
        if (!newline) { 
            end_string = PyUnicode_FromStringAndSize(" ", 1); 
            if (unlikely(!end_string)) 
                goto bad; 
            if (PyDict_SetItem(kwargs, PYIDENT("end"), end_string) < 0) { 
                Py_DECREF(end_string); 
                goto bad; 
            } 
            Py_DECREF(end_string); 
        } 
    } else if (!newline) { 
        if (unlikely(!$print_function_kwargs)) { 
            $print_function_kwargs = PyDict_New(); 
            if (unlikely(!$print_function_kwargs)) 
                return -1; 
            end_string = PyUnicode_FromStringAndSize(" ", 1); 
            if (unlikely(!end_string)) 
                return -1; 
            if (PyDict_SetItem($print_function_kwargs, PYIDENT("end"), end_string) < 0) { 
                Py_DECREF(end_string); 
                return -1; 
            } 
            Py_DECREF(end_string); 
        } 
        kwargs = $print_function_kwargs; 
    } 
    result = PyObject_Call($print_function, arg_tuple, kwargs); 
    if (unlikely(kwargs) && (kwargs != $print_function_kwargs)) 
        Py_DECREF(kwargs); 
    if (!result) 
        return -1; 
    Py_DECREF(result); 
    return 0; 
bad: 
    if (kwargs != $print_function_kwargs) 
        Py_XDECREF(kwargs); 
    return -1; 
} 
#endif 
 
////////////////////// PrintOne.proto ////////////////////// 
//@requires: Print 
 
static int __Pyx_PrintOne(PyObject* stream, PyObject *o); /*proto*/ 
 
////////////////////// PrintOne ////////////////////// 
 
#if !CYTHON_COMPILING_IN_PYPY && PY_MAJOR_VERSION < 3 
 
static int __Pyx_PrintOne(PyObject* f, PyObject *o) { 
    if (!f) { 
        if (!(f = __Pyx_GetStdout())) 
            return -1; 
    } 
    Py_INCREF(f); 
    if (PyFile_SoftSpace(f, 0)) { 
        if (PyFile_WriteString(" ", f) < 0) 
            goto error; 
    } 
    if (PyFile_WriteObject(o, f, Py_PRINT_RAW) < 0) 
        goto error; 
    if (PyFile_WriteString("\n", f) < 0) 
        goto error; 
    Py_DECREF(f); 
    return 0; 
error: 
    Py_DECREF(f); 
    return -1; 
    /* the line below is just to avoid C compiler 
     * warnings about unused functions */ 
    return __Pyx_Print(f, NULL, 0); 
} 
 
#else /* Python 3 has a print function */ 
 
static int __Pyx_PrintOne(PyObject* stream, PyObject *o) { 
    int res; 
    PyObject* arg_tuple = PyTuple_Pack(1, o); 
    if (unlikely(!arg_tuple)) 
        return -1; 
    res = __Pyx_Print(stream, arg_tuple, 1); 
    Py_DECREF(arg_tuple); 
    return res; 
} 
 
#endif