177 lines
5.0 KiB
C
177 lines
5.0 KiB
C
////////////////////// 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
|