Tests about a simple python3 fastcgi runner using libfcgi and the Python-C API.
python
c
wsgi
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

python_ioin.c 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719
  1. #include "python_ioin.h"
  2. /**@file python_ioin.c
  3. * @ingroup lib_ioin
  4. */
  5. /**@brief If given object io_stream is NULL set error indicator
  6. * @return 0 if no error else -1 or -2
  7. */
  8. static int _check_nullin(PyObject *self);
  9. /**@brief Request a new buffer size.
  10. *
  11. * @param PyObject* self The IoIn instance
  12. * @param int nreq number of bytes required in buffer
  13. * @return 0 on error else buffer size
  14. */
  15. static int pyfcgi_io__reqbuf(PyObject *self, int nreq);
  16. /**@brief Concat two bytes/unicode given IoIn configuration flag
  17. */
  18. static PyObject* IoIn__Concat(PyObject *self, PyObject *left, PyObject *right);
  19. PyObject* pyfcgi_io_true(PyObject *self, PyObject **argv, Py_ssize_t argc);
  20. PyObject* pyfcgi_io_false(PyObject *self, PyObject **argv, Py_ssize_t argc);
  21. PyMethodDef IoIn_methods[] = {
  22. {"close", (PyCFunction)pyfcgi_io_close, METH_FASTCALL, NULL},
  23. {"fileno", (PyCFunction)pyfcgi_io_fileno, METH_FASTCALL, NULL},
  24. {"flush", (PyCFunction)pyfcgi_io_flush, METH_FASTCALL, NULL},
  25. {"isatty", (PyCFunction)pyfcgi_io_isatty, METH_FASTCALL, NULL},
  26. {"readable", (PyCFunction)pyfcgi_ioin_readable, METH_FASTCALL, NULL},
  27. {"readline", (PyCFunction)pyfcgi_ioin_readline, METH_FASTCALL, NULL},
  28. {"readlines", (PyCFunction)pyfcgi_ioin_readlines, METH_FASTCALL, NULL},
  29. {"seek", (PyCFunction)pyfcgi_io_seek, METH_FASTCALL, NULL},
  30. {"seekable", (PyCFunction)pyfcgi_io_seekable, METH_FASTCALL, NULL},
  31. {"tell", (PyCFunction)pyfcgi_io_tell, METH_FASTCALL, NULL},
  32. {"truncate", (PyCFunction)pyfcgi_io_truncate, METH_FASTCALL, NULL},
  33. {"writable", (PyCFunction)pyfcgi_ioin_writable, METH_FASTCALL, NULL},
  34. {"writelines", (PyCFunction)pyfcgi_ioin_writelines, METH_FASTCALL, NULL},
  35. {"read", (PyCFunction)pyfcgi_ioin_read, METH_FASTCALL, NULL},
  36. {"readall", (PyCFunction)pyfcgi_ioin_readall, METH_FASTCALL, NULL},
  37. {"readinto", (PyCFunction)pyfcgi_ioin_readinto, METH_FASTCALL, NULL},
  38. {"write", (PyCFunction)pyfcgi_ioin_write, METH_FASTCALL, NULL},
  39. {NULL} //Sentinel
  40. };
  41. PyMemberDef IoIn_members[] = {
  42. {"closed", T_OBJECT, offsetof(PyIO_t, closed), READONLY, "True if the stream is closed"},
  43. {NULL}
  44. };
  45. PyTypeObject IoInType = {
  46. PyVarObject_HEAD_INIT(NULL, 0)
  47. "libpyfcgi.IoIn", /* tp_name */
  48. sizeof(PyIO_t), /* tp_basicsize */
  49. 0, /* tp_itemsize */
  50. (destructor)pyfcgi_io_del, /* tp_dealloc */
  51. 0, /* tp_print */
  52. 0, /* tp_getattr */
  53. 0, /* tp_setattr */
  54. 0, /* tp_reserved */
  55. 0, /* tp_repr */
  56. 0, /* tp_as_number */
  57. 0, /* tp_as_sequence */
  58. 0, /* tp_as_mapping */
  59. 0, /* tp_hash */
  60. 0, /* tp_call */
  61. 0, /* tp_str */
  62. 0, /* tp_getattro */
  63. 0, /* tp_setattro */
  64. 0, /* tp_as_buffer */
  65. Py_TPFLAGS_DEFAULT |
  66. Py_TPFLAGS_BASETYPE, /* tp_flags */
  67. "RawIo interface to FCGI input stream", /* tp_doc */
  68. 0, /* tp_traverse */
  69. 0, /* tp_clear */
  70. 0, /* tp_richcompare */
  71. 0, /* tp_weaklistoffset */
  72. 0, /* tp_iter */
  73. 0, /* tp_iternext */
  74. IoIn_methods, /* tp_methods */
  75. IoIn_members, /* tp_members */
  76. 0, /* tp_getset */
  77. 0, /* tp_base */
  78. 0, /* tp_dict */
  79. 0, /* tp_descr_get */
  80. 0, /* tp_descr_set */
  81. 0, /* tp_dictoffset */
  82. pyfcgi_ioin_init, /* tp_init */
  83. 0, /* tp_alloc */
  84. 0, /* tp_new */
  85. };
  86. PyMethodDef IoOut_methods[] = {
  87. {"close", (PyCFunction)pyfcgi_io_close, METH_FASTCALL, NULL},
  88. {"fileno", (PyCFunction)pyfcgi_io_fileno, METH_FASTCALL, NULL},
  89. {"flush", (PyCFunction)pyfcgi_io_flush, METH_FASTCALL, NULL},
  90. {"isatty", (PyCFunction)pyfcgi_io_isatty, METH_FASTCALL, NULL},
  91. {"readable", (PyCFunction)pyfcgi_ioout_readable, METH_FASTCALL, NULL},
  92. {"readline", (PyCFunction)pyfcgi_ioout_readline, METH_FASTCALL, NULL},
  93. {"readlines", (PyCFunction)pyfcgi_ioout_readlines, METH_FASTCALL, NULL},
  94. {"seek", (PyCFunction)pyfcgi_io_seek, METH_FASTCALL, NULL},
  95. {"seekable", (PyCFunction)pyfcgi_io_seekable, METH_FASTCALL, NULL},
  96. {"tell", (PyCFunction)pyfcgi_io_tell, METH_FASTCALL, NULL},
  97. {"truncate", (PyCFunction)pyfcgi_io_truncate, METH_FASTCALL, NULL},
  98. {"writable", (PyCFunction)pyfcgi_ioout_writable, METH_FASTCALL, NULL},
  99. {"writelines", (PyCFunction)pyfcgi_ioout_writelines, METH_FASTCALL, NULL},
  100. {"read", (PyCFunction)pyfcgi_ioout_read, METH_FASTCALL, NULL},
  101. {"readall", (PyCFunction)pyfcgi_ioout_readall, METH_FASTCALL, NULL},
  102. {"readinto", (PyCFunction)pyfcgi_ioout_readinto, METH_FASTCALL, NULL},
  103. {"write", (PyCFunction)pyfcgi_ioout_write, METH_FASTCALL, NULL},
  104. {NULL} //Sentinel
  105. };
  106. PyMemberDef IoOut_members[] = {
  107. {"closed", T_OBJECT, offsetof(PyIO_t, closed), READONLY, "True if the stream is closed"},
  108. {NULL}
  109. };
  110. PyTypeObject IoOutType = {
  111. PyVarObject_HEAD_INIT(NULL, 0)
  112. "libpyfcgi.IoOut", /* tp_name */
  113. sizeof(PyIO_t), /* tp_basicsize */
  114. 0, /* tp_itemsize */
  115. (destructor)pyfcgi_io_del, /* tp_dealloc */
  116. 0, /* tp_print */
  117. 0, /* tp_getattr */
  118. 0, /* tp_setattr */
  119. 0, /* tp_reserved */
  120. 0, /* tp_repr */
  121. 0, /* tp_as_number */
  122. 0, /* tp_as_sequence */
  123. 0, /* tp_as_mapping */
  124. 0, /* tp_hash */
  125. 0, /* tp_call */
  126. 0, /* tp_str */
  127. 0, /* tp_getattro */
  128. 0, /* tp_setattro */
  129. 0, /* tp_as_buffer */
  130. Py_TPFLAGS_DEFAULT |
  131. Py_TPFLAGS_BASETYPE, /* tp_flags */
  132. "RawIo interface to FCGI input stream", /* tp_doc */
  133. 0, /* tp_traverse */
  134. 0, /* tp_clear */
  135. 0, /* tp_richcompare */
  136. 0, /* tp_weaklistoffset */
  137. 0, /* tp_iter */
  138. 0, /* tp_iternext */
  139. IoOut_methods, /* tp_methods */
  140. IoOut_members, /* tp_members */
  141. 0, /* tp_getset */
  142. 0, /* tp_base */
  143. 0, /* tp_dict */
  144. 0, /* tp_descr_get */
  145. 0, /* tp_descr_set */
  146. 0, /* tp_dictoffset */
  147. pyfcgi_ioout_init, /* tp_init */
  148. 0, /* tp_alloc */
  149. 0, /* tp_new */
  150. };
  151. int pyfcgi_io_init(PyObject *self)
  152. {
  153. PyIO_t *ioin = (void*)self;
  154. ioin->io_stream = NULL;
  155. ioin->buff = NULL;
  156. ioin->buff_sz = 0;
  157. ioin->eof=0;
  158. ioin->bin=1;
  159. ioin->write = NULL;
  160. return 0;
  161. }
  162. int pyfcgi_ioin_init(PyObject *self, PyObject *args, PyObject *kwds)
  163. {
  164. pyfcgi_io_init(self);
  165. return 0;
  166. }
  167. int pyfcgi_ioout_init(PyObject *self, PyObject *args, PyObject *kwds)
  168. {
  169. pyfcgi_io_init(self);
  170. return 0;
  171. }
  172. void pyfcgi_io_del(PyIO_t *self)
  173. {
  174. PyIO_t *ioin = self;
  175. if(ioin->buff) { free(ioin->buff); }
  176. Py_TYPE(self)->tp_free((PyObject*)self);
  177. }
  178. PyObject* pyfcgi_io_close(PyObject *self, PyObject **argv, Py_ssize_t argc)
  179. {
  180. if(_check_nullin(self)) { Py_RETURN_NONE; }
  181. if(argc)
  182. {
  183. PyErr_Format(PyExc_ValueError,
  184. "libpyfcgi.IoIn.close() not expecting any argument but %zd given",
  185. argc);
  186. Py_RETURN_NONE;
  187. }
  188. if(FCGX_FClose(*((PyIO_t*)self)->io_stream) < 0)
  189. {
  190. PyErr_Format(PyExc_OSError,
  191. "%A unable to close", self);
  192. }
  193. ((PyIO_t*)self)->closed = Py_True;
  194. Py_RETURN_NONE;
  195. }
  196. PyObject* pyfcgi_io_fileno(PyObject *self, PyObject **argv, Py_ssize_t argc)
  197. {
  198. PyErr_SetString(PyExc_OSError, "libpyfcgi.IoIn has no fileno");
  199. Py_RETURN_NONE;
  200. }
  201. PyObject* pyfcgi_io_flush(PyObject *self, PyObject **argv, Py_ssize_t argc)
  202. {
  203. if(_check_nullin(self)) { Py_RETURN_NONE; }
  204. if(argc)
  205. {
  206. PyErr_Format(PyExc_ValueError,
  207. "libpyfcgi.IoIn.close() not expecting any argument but %zd given",
  208. argc);
  209. Py_RETURN_NONE;
  210. }
  211. if(FCGX_FFlush(*((PyIO_t*)self)->io_stream) < 0)
  212. {
  213. PyErr_Format(PyExc_OSError,
  214. "%A unable to flush", self);
  215. }
  216. Py_RETURN_NONE;
  217. }
  218. PyObject* pyfcgi_ioin_readline(PyObject *self, PyObject **argv, Py_ssize_t argc)
  219. {
  220. int read_n;
  221. long arg;
  222. char *ret;
  223. if(_check_nullin(self)) { Py_RETURN_NONE; }
  224. if(argc > 1)
  225. {
  226. PyErr_Format(PyExc_ValueError,
  227. "libpyfcgi.IoIn.close() expecting 0 or 1 argument but %zd given",
  228. argc);
  229. Py_RETURN_NONE;
  230. }
  231. if(!argc || !argv[0])
  232. {
  233. read_n = pyfcgi_io__reqbuf(self, 0);
  234. }
  235. else
  236. {
  237. arg = PyLong_AsLong(argv[0]);
  238. if(PyErr_Occurred())
  239. {
  240. Py_RETURN_NONE;
  241. }
  242. read_n = pyfcgi_io__reqbuf(self, (arg>INT_MAX)?INT_MAX:arg);
  243. }
  244. ret = ((PyIO_t*)self)->buff;
  245. if(!FCGX_GetLine(ret, read_n, *((PyIO_t*)self)->io_stream))
  246. {
  247. ((PyIO_t*)self)->eof = 1;
  248. ret = "";
  249. }
  250. //dprintf(2, "readline : '%s'\n", ret);
  251. return IoIn__FromString(self, ret);
  252. }
  253. PyObject* pyfcgi_ioin_readlines(PyObject *self, PyObject **argv, Py_ssize_t argc)
  254. {
  255. size_t hint, left;
  256. int read_n, toread;
  257. size_t sz;
  258. PyObject *res, *cur_str, *read_str, *tmp;
  259. char *buff;
  260. if(_check_nullin(self)) { Py_RETURN_NONE; }
  261. if(argc > 1)
  262. {
  263. PyErr_Format(PyExc_ValueError,
  264. "libpyfcgi.IoIn.close() expecting 0 or 1 argument but %zd given",
  265. argc);
  266. Py_RETURN_NONE;
  267. }
  268. if(!argc || !argv[0])
  269. {
  270. hint = 0;
  271. left = 0;
  272. }
  273. else
  274. {
  275. hint = PyLong_AsSize_t(argv[0]);
  276. if(PyErr_Occurred())
  277. {
  278. Py_RETURN_NONE;
  279. }
  280. left = hint;
  281. }
  282. res = PyList_New(0);
  283. if(!res)
  284. {
  285. Py_RETURN_NONE;
  286. }
  287. Py_INCREF(res);
  288. read_n = pyfcgi_io__reqbuf(self, 0);
  289. buff = ((PyIO_t*)self)->buff;
  290. cur_str = NULL;
  291. while((hint && left) || !hint)
  292. {
  293. toread = (hint&&((size_t)read_n > left))?(int)left:read_n;
  294. if(!FCGX_GetLine(buff, toread,
  295. *((PyIO_t*)self)->io_stream))
  296. {
  297. ((PyIO_t*)self)->eof = 1;
  298. break;
  299. }
  300. sz = strlen(buff);
  301. left -= sz;
  302. read_str = IoIn__FromBuff(self);
  303. if(!read_str)
  304. {
  305. Py_RETURN_NONE;
  306. }
  307. Py_INCREF(read_str);
  308. if(cur_str)
  309. {
  310. tmp = IoIn__Concat(self, cur_str, read_str);
  311. Py_INCREF(tmp);
  312. cur_str = tmp;
  313. }
  314. else
  315. {
  316. cur_str = read_str;
  317. }
  318. if(buff[sz-1] == '\n')
  319. {
  320. //dprintf(2, "readlines : '%s'\n", PyUnicode_AsUTF8(cur_str));
  321. if(PyList_Append(res, cur_str))
  322. {
  323. Py_DECREF(cur_str);
  324. Py_RETURN_NONE;
  325. }
  326. Py_DECREF(cur_str);
  327. cur_str = NULL;
  328. }
  329. }
  330. if(cur_str)
  331. {
  332. //dprintf(2, "readlines(post) : '%s'\n", PyUnicode_AsUTF8(cur_str));
  333. if(PyList_Append(res, cur_str))
  334. {
  335. Py_DECREF(cur_str);
  336. Py_RETURN_NONE;
  337. }
  338. Py_DECREF(cur_str);
  339. }
  340. Py_DECREF(res);
  341. return res;
  342. }
  343. PyObject* pyfcgi_ioin_read(PyObject *self, PyObject **argv, Py_ssize_t argc)
  344. {
  345. size_t left, max;
  346. long l;
  347. int read_n, toread, sz;
  348. PyObject *res, *read_str;
  349. char *buff;
  350. if(_check_nullin(self)) { Py_RETURN_NONE; }
  351. if(argc > 1)
  352. {
  353. PyErr_Format(PyExc_ValueError,
  354. "libpyfcgi.IoIn.read() expecting at most 1 argument but %zd given",
  355. argc);
  356. Py_RETURN_NONE;
  357. }
  358. if(((PyIO_t*)self)->eof)
  359. {
  360. return IoIn__FromString(self, "");
  361. }
  362. max = 0;
  363. if(argc)
  364. {
  365. l = PyLong_AsLong(argv[0]);
  366. if(PyErr_Occurred() || l > 0)
  367. {
  368. PyErr_Clear();
  369. max = PyLong_AsSize_t(argv[0]);
  370. if(PyErr_Occurred())
  371. {
  372. Py_RETURN_NONE;
  373. }
  374. }
  375. }
  376. left = max;
  377. read_n = pyfcgi_io__reqbuf(self, 0);
  378. buff = ((PyIO_t*)self)->buff;
  379. if(!(res = IoIn__FromString(self, "")))
  380. {
  381. Py_RETURN_NONE;
  382. }
  383. Py_INCREF(res);
  384. while((max && left) || !max)
  385. {
  386. toread = (max&&((size_t)read_n > left))?(int)left:read_n;
  387. sz = FCGX_GetStr(buff, toread, *((PyIO_t*)self)->io_stream);
  388. if(sz)
  389. {
  390. if(sz == toread) { buff[sz] = '\0'; }
  391. left -= sz;
  392. if(!(read_str = IoIn__FromBuff(self)))
  393. {
  394. Py_RETURN_NONE;
  395. }
  396. Py_INCREF(read_str);
  397. if(!(res = IoIn__Concat(self, res, read_str)))
  398. {
  399. Py_RETURN_NONE;
  400. }
  401. Py_INCREF(res);
  402. }
  403. if( sz < toread)
  404. {
  405. ((PyIO_t*)self)->eof = 1;
  406. break;
  407. }
  408. }
  409. //dprintf(2, "read : %s\n", PyBytes_AsString(res));
  410. Py_DECREF(res);
  411. return res;
  412. }
  413. PyObject* pyfcgi_ioin_readall(PyObject *self, PyObject **argv, Py_ssize_t argc)
  414. {
  415. if(_check_nullin(self)) { Py_RETURN_NONE; }
  416. if(argc)
  417. {
  418. PyErr_Format(PyExc_ValueError,
  419. "libpyfcgi.IoIn.readall() not expecting any argument but %zd given",
  420. argc);
  421. Py_RETURN_NONE;
  422. }
  423. dprintf(2, "readall calling read : ");
  424. return pyfcgi_ioin_read(self, NULL, 0);
  425. }
  426. PyObject* pyfcgi_ioin_readinto(PyObject *self, PyObject **argv, Py_ssize_t argc)
  427. {
  428. PyObject *b;
  429. Py_ssize_t max, left, read_n;
  430. char *buff, *buff_ptr;
  431. int toread, ret;
  432. if(_check_nullin(self)) { Py_RETURN_NONE; }
  433. if(argc != 1)
  434. {
  435. PyErr_Format(PyExc_ValueError,
  436. "libpyfcgi.IoIn.readinto() expecting 1 argument but %zd given",
  437. argc);
  438. Py_RETURN_NONE;
  439. }
  440. b = argv[0];
  441. if(!PyByteArray_Check(b))
  442. {
  443. PyErr_Format(PyExc_ValueError,
  444. "libpyfcgi.IoIn.readinto() expected bytearray as argument but %A given",
  445. b);
  446. Py_RETURN_NONE;
  447. }
  448. left = max = PyByteArray_Size(b);
  449. buff = PyByteArray_AsString(b);
  450. buff_ptr = buff;
  451. read_n = pyfcgi_io__reqbuf(self, 0);
  452. while(left)
  453. {
  454. toread = left>read_n?read_n:left;
  455. if((ret = FCGX_GetStr(buff_ptr, toread,
  456. *((PyIO_t*)self)->io_stream)) < toread)
  457. {
  458. ((PyIO_t*)self)->eof = 1;
  459. break;
  460. }
  461. buff_ptr += ret;
  462. left -= ret;
  463. }
  464. //dprintf(2, "readinto chr buff : '%s'\n", buff);
  465. //dprintf(2, "readinto bytes repr : '%s'\n", PyByteArray_AsString(b));
  466. return b;
  467. }
  468. PyObject* pyfcgi_ioout_writelines(PyObject *self, PyObject **argv, Py_ssize_t argc)
  469. {
  470. PyObject *lines, *iter, *line;
  471. const char *bytes;
  472. Py_ssize_t bytes_len, lineno;
  473. if( ! *((PyIO_t*)self)->write)
  474. {
  475. PyErr_Format(PyExc_RuntimeError, "%A write function not set",
  476. self);
  477. Py_RETURN_NONE;
  478. }
  479. if(argc != 1)
  480. {
  481. PyErr_Format(PyExc_ValueError,
  482. "libpyfcgi.IoOut.writelines() expected 1 argument but %zd given",
  483. argc);
  484. Py_RETURN_NONE;
  485. }
  486. lines = argv[0];
  487. iter = PyObject_GetIter(lines);
  488. if(!iter)
  489. {
  490. // should drop exception raised by GetIter ??
  491. PyErr_Format(PyExc_ValueError,
  492. "libpyfcgi.IoOut.writelines() first argument does not support iterator protocol : %A",
  493. lines);
  494. Py_RETURN_NONE;
  495. }
  496. lineno = 0;
  497. Py_INCREF(iter);
  498. while( (line = PyIter_Next(lines)) )
  499. {
  500. lineno++;
  501. Py_INCREF(line);
  502. if(PyUnicode_Check(line))
  503. {
  504. bytes = PyUnicode_AsUTF8AndSize(line, &bytes_len);
  505. if(!bytes) { Py_RETURN_NONE; } //forward error
  506. }
  507. else if(PyBytes_Check(line))
  508. {
  509. if(PyBytes_AsStringAndSize(line, (char**)&bytes, &bytes_len) == -1)
  510. {
  511. Py_RETURN_NONE; //forward error
  512. }
  513. }
  514. else
  515. {
  516. PyErr_Format(PyExc_TypeError,
  517. "libpyfcgi.IoOut.writelines() expected argument to be\
  518. str or bytes but %A given on line %zd",
  519. line, lineno);
  520. Py_RETURN_NONE;
  521. }
  522. if( ((PyIO_t*)self)->write(bytes, bytes_len) == -1)
  523. {
  524. PyErr_Format(PyExc_EOFError,
  525. "libpyfcgi.IoOut.writelines() EOF error when writing line %zd",
  526. lineno);
  527. Py_RETURN_NONE;
  528. }
  529. Py_DECREF(line);
  530. }
  531. Py_DECREF(iter);
  532. Py_RETURN_NONE;
  533. }
  534. PyObject* pyfcgi_ioout_write(PyObject *self, PyObject **argv, Py_ssize_t argc)
  535. {
  536. PyObject *b;
  537. const char *bytes;
  538. Py_ssize_t bytes_len;
  539. if( ! *((PyIO_t*)self)->write)
  540. {
  541. PyErr_Format(PyExc_RuntimeError, "%A write function not set",
  542. self);
  543. Py_RETURN_NONE;
  544. }
  545. if(argc != 1)
  546. {
  547. PyErr_Format(PyExc_ValueError,
  548. "libpyfcgi.IoOut.writelines() expected 1 argument but %zd given",
  549. argc);
  550. Py_RETURN_NONE;
  551. }
  552. b = argv[0];
  553. if(PyBytes_Check(b))
  554. {
  555. if(PyBytes_AsStringAndSize(b, (char**)&bytes, &bytes_len) == -1)
  556. {
  557. Py_RETURN_NONE; //forward error
  558. }
  559. }
  560. else if(PyUnicode_Check(b))
  561. {
  562. bytes = PyUnicode_AsUTF8AndSize(b, &bytes_len);
  563. if(!bytes) { Py_RETURN_NONE; } //forward error
  564. }
  565. else
  566. {
  567. PyErr_Format(PyExc_TypeError,
  568. "libpyfcgi.IoOut.write() expected argument to be bytes\
  569. or str but %A given on line %zd",
  570. b);
  571. Py_RETURN_NONE;
  572. }
  573. if( ((PyIO_t*)self)->write(bytes, bytes_len) == -1)
  574. {
  575. PyErr_Format(PyExc_EOFError,
  576. "libpyfcgi.IoOut.writelines() EOF error when calling write");
  577. Py_RETURN_NONE;
  578. }
  579. Py_RETURN_NONE;
  580. }
  581. PyObject* pyfcgi_io_false(PyObject *self, PyObject **argv, Py_ssize_t argc)
  582. {
  583. return Py_False;
  584. }
  585. PyObject* pyfcgi_io_true(PyObject *self, PyObject **argv, Py_ssize_t argc)
  586. {
  587. return Py_True;
  588. }
  589. PyObject* pyfcgi_io_truncate(PyObject *self, PyObject **argv, Py_ssize_t argc)
  590. {
  591. PyErr_SetString(PyExc_OSError, "libpyfcgi.Io cannot be truncated");
  592. Py_RETURN_NONE;
  593. }
  594. PyObject* pyfcgi_io_SeekError(PyObject *self, PyObject **argv, Py_ssize_t argc)
  595. {
  596. PyErr_SetString(PyExc_OSError, "libpyfcgi.Io is not seekable");
  597. Py_RETURN_NONE;
  598. }
  599. PyObject* pyfcgi_io_WriteError(PyObject *self, PyObject **argv, Py_ssize_t argc)
  600. {
  601. PyErr_SetString(PyExc_OSError, "libpyfcgi.IoIn is not writable");
  602. Py_RETURN_NONE;
  603. }
  604. PyObject* pyfcgi_io_ReadError(PyObject *self, PyObject **argv, Py_ssize_t argc)
  605. {
  606. PyErr_SetString(PyExc_OSError, "libpyfcgi.IoOut is not readable");
  607. Py_RETURN_NONE;
  608. }
  609. static int _check_nullin(PyObject *self)
  610. {
  611. if(!((PyIO_t*)self)->io_stream)
  612. {
  613. PyErr_SetString(PyExc_RuntimeError,
  614. "pyfcgi.IoIn called in wrong context : FGCI input not set");
  615. return -1;
  616. }
  617. else if(!(*(((PyIO_t*)self)->io_stream)))
  618. {
  619. PyErr_SetString(PyExc_RuntimeError,
  620. "pyfcgi.IoIn called in wrong context : FGCI input is NULL");
  621. return -2;
  622. }
  623. return 0;
  624. }
  625. static int pyfcgi_io__reqbuf(PyObject *self, int nreq)
  626. {
  627. PyIO_t *ioin;
  628. void *tmp;
  629. int err;
  630. ioin = (PyIO_t*)self;
  631. if(ioin->buff_sz > nreq && ioin->buff)
  632. {
  633. return ioin->buff_sz;
  634. }
  635. ioin->buff_sz = ((nreq>>12)+1)<<12;
  636. if(ioin->buff_sz < nreq) { ioin->buff_sz = nreq; }
  637. if(!(tmp = realloc(ioin->buff, ioin->buff_sz)))
  638. {
  639. err = errno;
  640. PyErr_Format(PyExc_OSError,
  641. "%A error reallocating internal buffer : %s",
  642. self, strerror(err));
  643. errno = err;
  644. return 0;
  645. }
  646. ioin->buff = tmp;
  647. return ioin->buff_sz;
  648. }
  649. /**@brief Concat two bytes/unicode given IoIn configuration flag
  650. * @warn Py_DECREF both arguments
  651. */
  652. static PyObject* IoIn__Concat(PyObject *self, PyObject *left, PyObject *right)
  653. {
  654. if(((PyIO_t*)self)->bin)
  655. {
  656. PyBytes_Concat(&left, right);
  657. Py_DECREF(right);
  658. return left;
  659. }
  660. PyObject *res;
  661. res = PyUnicode_Concat(left, right);
  662. Py_DECREF(left);
  663. Py_DECREF(right);
  664. return res;
  665. }