2011-11-22 16:33:38 -08:00
|
|
|
# -*- coding: utf-8 -*-
|
2010-03-26 14:32:16 -07:00
|
|
|
"""
|
2011-11-22 16:33:38 -08:00
|
|
|
sleekxmpp.xmlstream.filesocket
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
2010-03-26 14:32:16 -07:00
|
|
|
|
2011-11-22 16:33:38 -08:00
|
|
|
This module is a shim for correcting deficiencies in the file
|
|
|
|
socket implementation of Python2.6.
|
|
|
|
|
|
|
|
Part of SleekXMPP: The Sleek XMPP Library
|
|
|
|
|
|
|
|
:copyright: (c) 2011 Nathanael C. Fritz
|
|
|
|
:license: MIT, see LICENSE for more details
|
2010-03-26 14:32:16 -07:00
|
|
|
"""
|
2010-08-27 11:29:48 -04:00
|
|
|
|
2010-01-08 07:45:26 -08:00
|
|
|
from socket import _fileobject
|
2010-01-25 10:40:44 -08:00
|
|
|
import socket
|
2010-01-08 07:45:26 -08:00
|
|
|
|
2010-09-01 14:25:30 -04:00
|
|
|
|
2010-08-27 11:29:48 -04:00
|
|
|
class FileSocket(_fileobject):
|
|
|
|
|
2011-11-22 16:33:38 -08:00
|
|
|
"""Create a file object wrapper for a socket to work around
|
2010-08-27 11:29:48 -04:00
|
|
|
issues present in Python 2.6 when using sockets as file objects.
|
2010-01-08 07:45:26 -08:00
|
|
|
|
2011-11-22 16:33:38 -08:00
|
|
|
The parser for :class:`~xml.etree.cElementTree` requires a file, but
|
|
|
|
we will be reading from the XMPP connection socket instead.
|
2010-08-27 11:29:48 -04:00
|
|
|
"""
|
|
|
|
|
|
|
|
def read(self, size=4096):
|
|
|
|
"""Read data from the socket as if it were a file."""
|
2011-05-31 10:23:05 -07:00
|
|
|
if self._sock is None:
|
|
|
|
return None
|
2010-08-27 11:29:48 -04:00
|
|
|
data = self._sock.recv(size)
|
|
|
|
if data is not None:
|
|
|
|
return data
|
2010-01-25 10:40:44 -08:00
|
|
|
|
2010-09-01 14:25:30 -04:00
|
|
|
|
2010-01-25 10:40:44 -08:00
|
|
|
class Socket26(socket._socketobject):
|
|
|
|
|
2011-11-22 16:33:38 -08:00
|
|
|
"""A custom socket implementation that uses our own FileSocket class
|
2010-08-27 11:29:48 -04:00
|
|
|
to work around issues in Python 2.6 when using sockets as files.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def makefile(self, mode='r', bufsize=-1):
|
|
|
|
"""makefile([mode[, bufsize]]) -> file object
|
|
|
|
Return a regular file object corresponding to the socket. The mode
|
|
|
|
and bufsize arguments are as for the built-in open() function."""
|
|
|
|
return FileSocket(self._sock, mode, bufsize)
|