[Python-Dev] [python 2.7] a question about exporting a new method to socket object

Ani Sinha ani at aristanetworks.com
Fri Jun 21 07:18:27 CEST 2013


Hi python devs :

As a part of exploring an idea, I am trying to add a new method to the
python socket object. Here's what I have been trying to do (the patch may
be whitespace damaged so please bear with me) :


Index: Python-2.7/Modules/socketmodule.c
===================================================================
--- Python-2.7.orig/Modules/socketmodule.c
+++ Python-2.7/Modules/socketmodule.c
@@ -126,6 +126,7 @@ makefile([mode, [bufsize]]) -- return a
 recv(buflen[, flags]) -- receive data\n\
 recv_into(buffer[, nbytes[, flags]]) -- receive data (into a buffer)\n\
 recvfrom(buflen[, flags]) -- receive data and sender\'s address\n\
+arecvfrom(buflen[, flags]) -- same as recvfrom \n\
 recvfrom_into(buffer[, nbytes, [, flags])\n\
   -- receive data and sender\'s address (into a buffer)\n\
 sendall(data[, flags]) -- send all data\n\
@@ -468,6 +469,13 @@ static PyTypeObject sock_type;
 #define IS_SELECTABLE(s) ((s)->sock_fd < FD_SETSIZE || s->sock_timeout <=
0.0)
 #endif

@@ -2620,6 +2802,58 @@ PyDoc_STRVAR(recvfrom_doc,
 \n\
 Like recv(buffersize, flags) but also return the sender's address info.");

+static PyObject *
+sock_arecvfrom(PySocketSockObject *s, PyObject *args)
+{
+    return sock_recvfrom(s,args);
+}
+
+PyDoc_STRVAR(arecvfrom_doc,
+"arecvfrom(buffersize[, flags]) -> (data, address info)\n\
+\n\
+ experimental stuff");

 /* s.recvfrom_into(buffer[, nbytes [,flags]]) method */

@@ -2963,6 +3197,8 @@ static PyMethodDef sock_methods[] = {
                       recv_into_doc},
     {"recvfrom",          (PyCFunction)sock_recvfrom, METH_VARARGS,
                       recvfrom_doc},
+    {"arecvfrom",          (PyCFunction)sock_arecvfrom, METH_VARARGS,
+                      arecvfrom_doc},
     {"recvfrom_into",  (PyCFunction)sock_recvfrom_into, METH_VARARGS |
METH_KEYWORDS,
                       recvfrom_into_doc},
     {"send",              (PyCFunction)sock_send, METH_VARARGS,


When I compile this and run a simple script like the following that uses
arecvfrom() :

sock = socket.socket( socket.PF_PACKET, socket.SOCK_RAW )
sock.bind( ( intf, ETH_P_ALL ) )

(pkt,dontcare) = dst.arecvfrom( 500, socket.MSG_DONTWAIT )

I get this exception :

AttributeError: '_socketobject' object has no attribute 'arecvfrom'

So what am I doing wrong? How do I export this new socket method?

any help/pointer will be greatly appreciated.

cheers,
ani

-- 
Ani
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20130620/f2ed2bc3/attachment.html>


More information about the Python-Dev mailing list