Printing.c 5.0 KB

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