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