[Python-checkins] python/dist/src/Modules socketmodule.c,1.235,1.236

jhylton@users.sourceforge.net jhylton@users.sourceforge.net
Thu, 25 Jul 2002 09:01:15 -0700


Update of /cvsroot/python/python/dist/src/Modules
In directory usw-pr-cvs1:/tmp/cvs-serv29556/Modules

Modified Files:
	socketmodule.c 
Log Message:
Extended socket.htonl and ntohl to accept longs.

Fixes SF bug #568322.

The code should raise an OverflowError if the long is > 32 bits, even
on platforms where sizeof(long) > 4.





Index: socketmodule.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Modules/socketmodule.c,v
retrieving revision 1.235
retrieving revision 1.236
diff -C2 -d -r1.235 -r1.236
*** socketmodule.c	23 Jul 2002 06:31:13 -0000	1.235
--- socketmodule.c	25 Jul 2002 16:01:12 -0000	1.236
***************
*** 2470,2482 ****
  
  static PyObject *
! socket_ntohl(PyObject *self, PyObject *args)
  {
! 	int x1, x2;
  
! 	if (!PyArg_ParseTuple(args, "i:ntohl", &x1)) {
! 		return NULL;
  	}
! 	x2 = ntohl(x1);
! 	return PyInt_FromLong(x2);
  }
  
--- 2470,2501 ----
  
  static PyObject *
! socket_ntohl(PyObject *self, PyObject *arg)
  {
! 	unsigned long x;
  
! 	if (PyInt_Check(arg)) {
! 		x = PyInt_AS_LONG(arg);
  	}
! 	else if (PyLong_Check(arg)) {
! 		x = PyLong_AsUnsignedLong(arg);
! #if SIZEOF_LONG > 4
! 		{
! 			unsigned long y;
! 			/* only want the trailing 32 bits */
! 			y = x & 0xFFFFFFFFUL;
! 			if (y ^ x)
! 				return PyErr_Format(PyExc_OverflowError,
! 					    "long int larger than 32 bits");
! 			x = y;
! 		}			
! #endif
! 	}
! 	else
! 		return PyErr_Format(PyExc_TypeError, 
! 				    "expected int/long, %s found",
! 				    arg->ob_type->tp_name);
! 	if (x == (unsigned long) -1 && PyErr_Occurred())
! 		return NULL;
! 	return PyInt_FromLong(ntohl(x));
  }
  
***************
*** 2490,2494 ****
  socket_htons(PyObject *self, PyObject *args)
  {
! 	int x1, x2;
  
  	if (!PyArg_ParseTuple(args, "i:htons", &x1)) {
--- 2509,2513 ----
  socket_htons(PyObject *self, PyObject *args)
  {
! 	unsigned long x1, x2;
  
  	if (!PyArg_ParseTuple(args, "i:htons", &x1)) {
***************
*** 2506,2518 ****
  
  static PyObject *
! socket_htonl(PyObject *self, PyObject *args)
  {
! 	int x1, x2;
  
! 	if (!PyArg_ParseTuple(args, "i:htonl", &x1)) {
! 		return NULL;
  	}
! 	x2 = htonl(x1);
! 	return PyInt_FromLong(x2);
  }
  
--- 2525,2556 ----
  
  static PyObject *
! socket_htonl(PyObject *self, PyObject *arg)
  {
! 	unsigned long x;
  
! 	if (PyInt_Check(arg)) {
! 		x = PyInt_AS_LONG(arg);
  	}
! 	else if (PyLong_Check(arg)) {
! 		x = PyLong_AsUnsignedLong(arg);
! #if SIZEOF_LONG > 4
! 		{
! 			unsigned long y;
! 			/* only want the trailing 32 bits */
! 			y = x & 0xFFFFFFFFUL;
! 			if (y ^ x)
! 				return PyErr_Format(PyExc_OverflowError,
! 					    "long int larger than 32 bits");
! 			x = y;
! 		}			
! #endif
! 	}
! 	else
! 		return PyErr_Format(PyExc_TypeError, 
! 				    "expected int/long, %s found",
! 				    arg->ob_type->tp_name);
! 	if (x == (unsigned long) -1 && PyErr_Occurred())
! 		return NULL;
! 	return PyInt_FromLong(htonl(x));
  }
  
***************
*** 2813,2821 ****
  	 METH_VARARGS, ntohs_doc},
  	{"ntohl",		socket_ntohl,
! 	 METH_VARARGS, ntohl_doc},
  	{"htons",		socket_htons,
  	 METH_VARARGS, htons_doc},
  	{"htonl",		socket_htonl,
! 	 METH_VARARGS, htonl_doc},
  	{"inet_aton",		socket_inet_aton,
  	 METH_VARARGS, inet_aton_doc},
--- 2851,2859 ----
  	 METH_VARARGS, ntohs_doc},
  	{"ntohl",		socket_ntohl,
! 	 METH_O, ntohl_doc},
  	{"htons",		socket_htons,
  	 METH_VARARGS, htons_doc},
  	{"htonl",		socket_htonl,
! 	 METH_O, htonl_doc},
  	{"inet_aton",		socket_inet_aton,
  	 METH_VARARGS, inet_aton_doc},