[Python-checkins] r46653 - sandbox/trunk/pdb/README.txt sandbox/trunk/pdb/mconnection.py sandbox/trunk/pdb/mpdb.py

matt.fleming python-checkins at python.org
Mon Jun 5 00:38:54 CEST 2006


Author: matt.fleming
Date: Mon Jun  5 00:38:22 2006
New Revision: 46653

Added:
   sandbox/trunk/pdb/mconnection.py
Modified:
   sandbox/trunk/pdb/README.txt
   sandbox/trunk/pdb/mpdb.py
Log:
Moved the connection classes into a separate file. Plan on writing unittests. Work some code into MPdb so that a user can specify the absolute path of a class to instantiate for a debugger's connection.

Modified: sandbox/trunk/pdb/README.txt
==============================================================================
--- sandbox/trunk/pdb/README.txt	(original)
+++ sandbox/trunk/pdb/README.txt	Mon Jun  5 00:38:22 2006
@@ -9,7 +9,7 @@
 aims to correct this wish.
 
 -=[TODO]=-
-* make help command work
+* Write unittests
 * sort out the namespace corruption - 
 	 """
          c:\soc\pdb\test.py(3)x()

Added: sandbox/trunk/pdb/mconnection.py
==============================================================================
--- (empty file)
+++ sandbox/trunk/pdb/mconnection.py	Mon Jun  5 00:38:22 2006
@@ -0,0 +1,154 @@
+""" This file contains all connections that a debugger can
+create.
+"""
+import socket
+
+NotImplementedMessage = "This method must be overriden in a subclass"
+
+class MServerConnectionInterface(object):
+    """ This is an abstract class that specifies the interface a server
+    connection class must implement.
+    """
+    def accept(self, console, addr):
+	""" This method is called when a connection from a debugger
+	is accepted by this server.
+	"""
+	raise NotImplementedError, NotImplementedMessage
+
+    def disconnect(self):
+	""" This method is called to disconnect	any debuggers that
+        are connected and to stop accepting any more connections from
+        debuggers.
+	"""
+	raise NotImplementedError, NotImplementedMessage
+
+    def listen(self):
+	""" This method is called when a server is initially
+	configured to listen for connections from debuggers. 
+	"""
+	raise NotImplementedError, NotImplementedMessage
+
+    def setup(self):
+        """ This is the only method that is called by the debugger.
+        It must handle setting up a connection to listen on and accept
+        a debugger connection.
+        """
+        raise NotImplementedError, NotImplementedMessage
+
+class MClientConnectionInterface(object):
+    """ This is the interface that a client connection should implement.
+    """
+    def setup(self):
+        """ This method is called by a debugger to connect to a server. """
+        raise NotImplementedError, NotImplementedMessage
+
+    def disconnect(self):
+        """ This method is called by a debugger to disconnect from a
+        server.
+        """
+        raise NotImplementedError, NotImplementedMessage
+    
+class MServerConnectionSerial(MServerConnectionInterface):
+    """ This is a server connection class that allows a debugger
+    to connect via a serial line. A serial device must be specified
+    (e.g. /dev/ttyS0, COM1, etc.).
+    """
+    def __init__(self, device):
+        self._dev = device
+        MServerConnectionInterface.__init__(self)
+
+    def setup(self):
+        self.output = open(self._dev, 'w')
+        self.input = open(self._dev, 'r')
+
+    def listen(self):
+        pass
+
+    def accept(self, debugger, addr):
+        pass
+
+    def disconnect(self):
+        self.output.close()
+        self.input.close()
+
+class MServerConnectionTCP(MServerConnectionInterface):
+    """ This is an implementation of a server class that uses the TCP
+    protocol as its means of communication. Debuggers wishing to connect
+    to this target must use this syntax for the server command,
+
+    `target tcp hostname:port
+
+    """
+    def __init__(self, addr):
+        """ 'addr' specifies the hostname and port combination of
+        the server.
+        """
+	MServerConnectionInterface.__init__(self)
+        h,p = addr.split(':')
+        self.host = h
+        self.port = int(p)
+        
+    def setup(self):
+        self._sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+        self._sock.bind((self.host, self.port))
+        self.listen()
+        
+    def listen(self):
+        self._sock.listen(5)
+        debugger, addr = self._sock.accept()
+        self.accept(debugger, addr)
+
+    def accept(self, debugger, addr):
+        self.output = debugger.makefile('w')
+
+    def disconnect(self):
+        self.output.flush()
+        self.output.close()
+        self._sock.close()
+
+class MClientConnectionSerial(MClientConnectionInterface):
+    """ A class that allows a connection to be made from a debugger
+    to a server via a serial line. Specify the serial device it is
+    connected to (e.g. /dev/ttya).
+    """
+    def __init__(self, device):
+        """ Specify the serial device. """
+        MClientConnectionInterface.__init__(self)
+        self._dev = device
+        self.input = None
+        self.output = None
+        self.setup()
+
+    def setup(self):
+        """ Create our fileobject by opening the serial device for
+        this connection.
+        """
+        self.output = open(self._dev, "w")
+
+    def disconnect(self):
+        """ Close the serial device. """
+        self.output.close()
+
+class MClientConnectionTCP(MClientConnectionInterface):
+    """ A class that allows a connection to be made from a debugger
+    to a server via TCP. Specify the address of the server
+    (e.g. host:2020).
+    """
+    def __init__(self, addr):
+        """ Specify the address to connection to. """
+        MClientConnectionInterface.__init__(self)
+        h, p = addr.split(':')
+        self.host = h
+        self.port = int(p)
+        self.setup()
+
+    def setup(self):
+        """ Connect to the server. """
+        self._sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+        self._sock.connect((self.host, self.port))
+        self.output = self._sock.makefile('w')
+
+    def disconnect(self):
+        """ Close the socket to the server. """
+        self.output.close()
+        self._sock.close()

Modified: sandbox/trunk/pdb/mpdb.py
==============================================================================
--- sandbox/trunk/pdb/mpdb.py	(original)
+++ sandbox/trunk/pdb/mpdb.py	Mon Jun  5 00:38:22 2006
@@ -50,7 +50,6 @@
 	by 'new_input'.
 	"""
         self.raw_input = 1
-        print >> sys.stderr, "From %s to %s" % (self.stdin, new_input)
         self.stdin.flush()
 	self.stdin = new_input
         self.stdin.flush()
@@ -94,153 +93,43 @@
 target xml -- Use a remote computer via the xmlrpc lib
 """
         cls, addr = args.split(' ')
-        self.connection = eval(cls+'(addr)')
+        if '.' in cls:
+            base = cls[:cls.rfind('.')]
+            cls = cls[cls.rfind('.')+1:]
+            exec 'from ' + base + ' import ' + cls
+        else:
+            __import__(cls)
+        self.connection = eval(mod+'(addr)')
         self.connection.setup()
         # XXX currently this method doesn't do anything
 
-    def do_serve(self, args):
+    def do_pdbserver(self, args):
         """ Allow a debugger to connect to this session.
 The first argument is the type or protocol that is used for this connection
 (which can be the name of a class that is avaible either in the current
 working directory or in Python's PYTHONPATH environtment variable).
-Remaining arguments are interpreted by the protocol. For more
-information on the arguments for a particular protocol, type
-`help target ' followed by the protocol name.
+The next argument is protocol specific arguments (e.g. hostname and
+port number for a TCP connection, or a serial device for a serial line
+connection). The next argument is the filename of the script that is
+being debugged. The rest of the arguments are passed to the script file
+and are optional. For more information on the arguments for a
+particular protocol, type `help pdbserver ' followed by the protocol name.
+The syntax for this command is,
+
+`pdbserver ConnectionClass comm scriptfile [args ...]'
+
 """
-        cls, addr = args.split(' ')
-        self.connection = eval(cls+'(addr)')
+        cls, comm, scriptfile_and_args = args.split(' ')
+        if '.' in cls:
+            base = cls[:cls.rfind('.')]
+            cls = cls[cls.rfind('.')+1:]
+            exec 'from ' + base + ' import ' + cls
+        else:
+            __import__(cls)
+        self.connection = eval(cls+'(comm)')
         self.connection.setup()
         self._rebind_output(self.connection.output)
 
-NotImplementedMessage = "This method must be overriden in a subclass"
-
-class MTargetInterface(object):
-    """ This is an abstract class that specifies the interface a debugging
-    target class must implement.
-    """
-    def accept(self, console, addr):
-	""" This method is called when a connection from a debugger
-	is accepted by this target.
-	"""
-	raise NotImplementedError, NotImplementedMessage
-
-    def disconnect(self):
-	""" This method is called to disconnect	any debuggers that
-        are connected and to stop accepting any more connections from
-        debuggers.
-	"""
-	raise NotImplementedError, NotImplementedMessage
-
-    def listen(self):
-	""" This method is called when a target is initially
-	configured to listen for connections from debuggers. 
-	"""
-	raise NotImplementedError, NotImplementedMessage
-
-class MTargetConnectionTCP(MTargetInterface):
-    """ This is an implementation of a target class that uses the TCP
-    protocol as its means of communication. Debuggers wishing to connect
-    to this target must use this syntax for the target command,
-
-    `target tcp hostname:port
-
-    """
-    def __init__(self, addr):
-        """ 'addr' specifies the hostname and port combination of
-        the target.
-        """
-	MTargetInterface.__init__(self)
-        h,p = addr.split(':')
-        self.host = h
-        self.port = int(p)
-        
-
-    def setup(self):
-        self._sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
-        self._sock.bind((self.host, self.port))
-        self.listen()
-        
-    def listen(self):
-        self._sock.listen(5)
-        debugger, addr = self._sock.accept()
-        self.accept(debugger, addr)
-
-    def accept(self, debugger, addr):
-        self.output = debugger.makefile('w')
-
-    def disconnect(self):
-        self.output.flush()
-        self.output.close()
-        self._sock.close()
-
-class MTargetConnectionSerial(MTargetInterface):
-    """ This is a target connection class that allows a debugger
-    to connect via a serial line. A serial device must be specified
-    (e.g. /dev/ttyS0, COM1, etc.).
-    """
-    def __init__(self, device):
-        self._dev
-        MTargetInterface.__init__(self)
-
-    def setup(self):
-        self.output = open(self._dev, 'w')
-        self.input = open(self._dev, 'r')
-
-    def listen(self):
-        pass
-
-    def accept(self, debugger, addr):
-        pass
-
-    def disconnect(self):
-        self.output.close()
-        self.input.close()
-        
-class MDebuggerConnectionTCP(object):
-    """ A class that allows a connection to be made from a debugger
-    to a target via TCP. Specify the address of the target
-    (e.g. host:2020).
-    """
-    def __init__(self, addr):
-        """ Specify the address to connection to. """
-        h, p = addr.split(':')
-        self.host = h
-        self.port = int(p)
-        self.setup()
-
-    def setup(self):
-        """ Connect to the target. """
-        self._sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
-        self._sock.connect((self.host, self.port))
-
-class MDebuggerConnectionSerial(object):
-    """ A class that allows a connection to be made from a debugger
-    to a target via a serial line. Specify the serial device it is
-    connected to (e.g. /dev/ttya).
-    """
-    def __init__(self, device):
-        """ Specify the serial device. """
-        self._dev = device
-        self.input = None
-        self.output = None
-        self.setup()
-
-    def setup(self):
-        """ Create our fileobject by opening the serial device for
-        this connection.
-        """
-        self.output = open(self._dev, "w")
-
-# These are the default classes that are instantiated when connecting
-# to a target,
-#
-# `target tcp localhost:8080 '
-#
-# These can be changed at runtime.
-
-serial = MDebuggerConnectionSerial
-tcp = MDebuggerConnectionTCP
-
 def main(options):
     opts = options[0]
     args = options[1]
@@ -271,7 +160,8 @@
                 print "Post mortem debugger finished. The " + \
                       mainpyfile + " will be restarted"
 
-# A utility function to parse the options from the command line
+# Utility functions
+# Parse arguments
 def parse_opts():
     parser = OptionParser()
     parser.add_option("-s", "--script", dest="scriptname",
@@ -290,7 +180,7 @@
     (options, args) = parser.parse_args()
     # We don't currently support any arguments
     return (options,args)
-    
+
 if __name__ == '__main__':
     main(parse_opts())
 


More information about the Python-checkins mailing list