-
Notifications
You must be signed in to change notification settings - Fork 189
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Python 3 support, step 1: Get rid of cStringIO.
- Loading branch information
Showing
9 changed files
with
160 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,81 @@ | |||
#include "stream.h" | |||
|
|||
static PyObject* | |||
_Stream_read(Stream* stream, size_t howmuch) | |||
{ | |||
size_t bytesleft; | |||
PyObject* chunk; | |||
if(stream->data.data == NULL) | |||
return NULL; | |||
bytesleft = stream->data.len - stream->pos; | |||
if(howmuch > bytesleft) | |||
howmuch = bytesleft; | |||
if(howmuch == 0) | |||
return NULL; | |||
chunk = PyString_FromStringAndSize(stream->data.data + stream->pos, howmuch); | |||
stream->pos += howmuch; | |||
return chunk; | |||
} | |||
|
|||
static PyObject* | |||
Stream_read(PyObject* self, PyObject* args) | |||
{ | |||
size_t howmuch; | |||
if(!PyArg_ParseTuple(args, "I", &howmuch)) | |||
return NULL; | |||
PyObject* chunk = _Stream_read((Stream*)self, howmuch); | |||
if(chunk == NULL) { | |||
Py_INCREF(_empty_string); | |||
chunk = _empty_string; | |||
} | |||
return chunk; | |||
} | |||
|
|||
static PyObject* | |||
Stream_notimplemented(PyObject* self, PyObject* args) | |||
{ | |||
PyErr_SetString(PyExc_NotImplementedError, | |||
"Nobody would ever use readline(s) on a stream of random bytes, right?"); | |||
return NULL; | |||
} | |||
|
|||
static PyObject* | |||
Stream_iternew(PyObject* self) { | |||
Py_INCREF(self); | |||
return self; | |||
} | |||
|
|||
static PyObject* | |||
Stream_iternext(PyObject* self) | |||
{ | |||
return _Stream_read((Stream*)self, STREAM_CHUNK_SIZE); | |||
} | |||
|
|||
static void | |||
Stream_dealloc(PyObject* self) | |||
{ | |||
free(((Stream*)self)->data.data); | |||
self->ob_type->tp_free(self); | |||
} | |||
|
|||
static PyMethodDef Stream_methods[] = { | |||
{"read", Stream_read, METH_VARARGS, NULL}, | |||
{"readline", Stream_notimplemented, METH_NOARGS, NULL}, | |||
{"readlines", Stream_notimplemented, METH_VARARGS, NULL}, | |||
{NULL, NULL, 0, NULL} | |||
}; | |||
|
|||
PyTypeObject StreamType = { | |||
PyObject_HEAD_INIT(NULL) | |||
0, | |||
"StringStream", | |||
sizeof(Stream), | |||
0, | |||
Stream_dealloc, | |||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |||
Py_TPFLAGS_HAVE_CLASS | Py_TPFLAGS_HAVE_ITER, | |||
0, 0, 0, 0, 0, | |||
Stream_iternew, | |||
Stream_iternext, | |||
Stream_methods | |||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,11 @@ | |||
#include "common.h" | |||
|
|||
#define STREAM_CHUNK_SIZE 4*1024 /* XXX this is kind of random */ | |||
|
|||
PyTypeObject StreamType; | |||
|
|||
typedef struct { | |||
PyObject_HEAD | |||
string data; | |||
size_t pos; | |||
} Stream; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,23 @@ | |||
from __future__ import print_function | |||
import pprint | |||
import bjoern | |||
import sys | |||
|
|||
if 'silenceillkillyou' in sys.argv: | |||
def print(*args): pass | |||
|
|||
def app(env, start_response): | |||
print('--') | |||
stream = env['wsgi.input'] | |||
print(stream) | |||
print(dir(stream)) | |||
print(repr(stream.read(2))) | |||
print(repr(stream.read(10))) | |||
print(repr(stream.read(10))) | |||
print(repr(stream.read(10))) | |||
print(repr(stream.read(10))) | |||
print(repr(stream.read(10))) | |||
start_response('200 yo', []) | |||
return [] | |||
|
|||
bjoern.run(app, '0.0.0.0', 8080) |