quantmind / pulsar

Event driven concurrent framework for Python
BSD 3-Clause "New" or "Revised" License
1.87k stars 162 forks source link

passing a TemporaryFile to client.HttpRequest fails #311

Open DavHau opened 6 years ago

DavHau commented 6 years ago

The problem here seems to be that python's tempfile.TemporaryFile's filename has type int and not string. A look at the documentation of io.FileIO:

The name can be one of two things:

a character string or bytes object representing the path to the file which will be opened. In this case closefd must be True (the default) otherwise an error will be raised.
an integer representing the number of an existing OS-level file descriptor to which the resulting FileIO object will give access. When the FileIO object is closed this fd will be closed as well, unless closefd is set to False.

As a user of pulsar the issue can be prevented by using NamedTemporaryFile instead of TemporaryFile.

Expected behaviour

A TemporaryFile (which has no name) is a file like object and therefore it should be possible to pass it via the 'file' parameter to a request. (It is possible with with the 'requests' lib)

Actual behaviour

guess_filename fails during encoding the file because the filename is an int

Steps to reproduce

from pulsar.apps import http
from tempfile import TemporaryFile
import asyncio

async def request():
  with TemporaryFile() as tmpFile:
    tmpFile.write('hello world'.encode())
    tmpFile.seek(0)
    sessions = http.HttpClient()
    await sessions.post(
            'http://google.com',
            files={'file.txt': tmpFile}
    )

asyncio.get_event_loop().run_until_complete(request())