Skip to content

Commit

Permalink
Fixed jonashaag#57: File wrapper should accept optional 'chunksize' a…
Browse files Browse the repository at this point in the history
…rgument
  • Loading branch information
jonashaag committed Apr 28, 2013
1 parent 2bc5e32 commit 99fc09d
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 9 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

Copyright (c) 2010-2011 Jonas Haag <[email protected]> and contributors.
Copyright (c) 2010-2013 Jonas Haag <[email protected]> and contributors.
All rights reserved.
License: 2-clause-BSD (Berkley Software Distribution) license

Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ debug:
CFLAGS='-D DEBUG' make again

$(BUILD_DIR)/%.o: $(SOURCE_DIR)/%.c
@echo ' -> ' $(CC) -c $< -o $@
@echo ' -> ' $(CC) $(CPPFLAGS) $(CFLAGS) -c $< -o $@
@$(CC) $(CPPFLAGS) $(CFLAGS) -c $< -o $@

# foo.o: shortcut to $(BUILD_DIR)/foo.o
Expand Down
10 changes: 8 additions & 2 deletions bjoern/filewrapper.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,22 @@ static PyObject*
FileWrapper_New(PyTypeObject* cls, PyObject* args, PyObject* kwargs)
{
PyObject* file;
if(!PyArg_ParseTuple(args, "O:FileWrapper", &file))
unsigned int ignored_blocksize;

if(!PyArg_ParseTuple(args, "O|I:FileWrapper", &file, &ignored_blocksize))
return NULL;

if(!PyFile_Check(file)) {
TYPE_ERROR("FileWrapper argument", "file", file);
return NULL;
}

Py_INCREF(file);
FileWrapper* wrapper = PyObject_NEW(FileWrapper, &FileWrapper_Type);
PyFile_IncUseCount((PyFileObject*)file);

FileWrapper* wrapper = PyObject_NEW(FileWrapper, cls);
wrapper->file = file;

return (PyObject*)wrapper;
}

Expand Down
6 changes: 4 additions & 2 deletions bjoern/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -234,13 +234,15 @@ ev_io_on_write(struct ev_loop* mainloop, ev_io* watcher, const int events)
/* current_chunk contains the HTTP headers */
if(send_chunk(request))
goto out;
assert(!request->current_chunk_p);
assert(request->current_chunk_p == 0);
/* abuse current_chunk_p to store the file fd */
request->current_chunk_p = PyObject_AsFileDescriptor(request->iterable);
goto out;
}

if(do_sendfile(request))
goto out;

} else {
/* iterable */
if(send_chunk(request))
Expand Down Expand Up @@ -307,7 +309,7 @@ send_chunk(Request* request)

assert(request->current_chunk != NULL);
assert(!(request->current_chunk_p == PyString_GET_SIZE(request->current_chunk)
&& PyString_GET_SIZE(request->current_chunk) != 0));
&& PyString_GET_SIZE(request->current_chunk) != 0));

bytes_sent = write(
request->client_fd,
Expand Down
7 changes: 4 additions & 3 deletions tests/filewrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
import bjoern

W = {
'callable-iterator' : lambda f, e: iter(lambda: f.read(64*1024), ''),
'xreadlines' : lambda f, e: f,
'filewrapper' : lambda f, env: env['wsgi.file_wrapper'](f)
'callable-iterator': lambda f, e: iter(lambda: f.read(64*1024), ''),
'xreadlines': lambda f, e: f,
'filewrapper': lambda f, env: env['wsgi.file_wrapper'](f),
'filewrapper2': lambda f, env: env['wsgi.file_wrapper'](f, 1)
}

F = len(sys.argv) > 1 and sys.argv[1] or 'README.rst'
Expand Down

0 comments on commit 99fc09d

Please sign in to comment.