/usr/share/pyshared/spyderlib/utils/bsdsocket.py is in python-spyderlib 2.1.9-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 | # -*- coding: utf-8 -*-
#
# Copyright © 2011 Pierre Raybaut
# Licensed under the terms of the MIT License
# (see spyderlib/__init__.py for details)
"""BSD socket interface communication utilities"""
import socket
import struct
import cPickle as pickle
import threading
SZ = struct.calcsize("l")
def write_packet(sock, data, already_pickled=False):
"""Write *data* to socket *sock*"""
if already_pickled:
sent_data = data
else:
sent_data = pickle.dumps(data, pickle.HIGHEST_PROTOCOL)
sock.send(struct.pack("l", len(sent_data)) + sent_data)
def read_packet(sock, timeout=None):
"""
Read data from socket *sock*
Returns None if something went wrong
"""
sock.settimeout(timeout)
dlen, data = None, None
try:
datalen = sock.recv(SZ)
dlen, = struct.unpack("l", datalen)
data = ''
while len(data) < dlen:
data += sock.recv(dlen)
except socket.timeout:
raise
except socket.error:
data = None
finally:
sock.settimeout(None)
if data is not None:
try:
return pickle.loads(data)
except (EOFError, pickle.UnpicklingError):
return
# Using a lock object to avoid communication issues described in Issue 857:
# http://code.google.com/p/spyderlib/issues/detail?id=857
COMMUNICATE_LOCK = threading.Lock()
# * Old com implementation *
# See solution (1) in Issue 434:
# http://code.google.com/p/spyderlib/issues/detail?id=434#c13
def communicate(sock, command, settings=[]):
"""Communicate with monitor"""
try:
COMMUNICATE_LOCK.acquire()
write_packet(sock, command)
for option in settings:
write_packet(sock, option)
return read_packet(sock)
finally:
COMMUNICATE_LOCK.release()
## new com implementation:
## See solution (2) in Issue 434:
## http://code.google.com/p/spyderlib/issues/detail?id=434#c13
#def communicate(sock, command, settings=[], timeout=None):
# """Communicate with monitor"""
# write_packet(sock, command)
# for option in settings:
# write_packet(sock, option)
# if timeout == 0.:
# # non blocking socket is not really supported:
# # setting timeout to 0. here is equivalent (in current monitor's
# # implementation) to say 'I don't need to receive anything in return'
# return
# while True:
# output = read_packet(sock, timeout=timeout)
# if output is None:
# return
# output_command, output_data = output
# if command == output_command:
# return output_data
# elif DEBUG:
# logging.debug("###### communicate/warning /Begin ######")
# logging.debug("was expecting '%s', received '%s'" \
# % (command, output_command))
# logging.debug("###### communicate/warning /End ######")
class PacketNotReceived(object):
pass
PACKET_NOT_RECEIVED = PacketNotReceived()
|