[pypy-svn] r18837 - in pypy/dist/pypy: module/_socket/rpython module/_socket/rpython/test translator/c translator/c/src translator/c/test
afa at codespeak.net
afa at codespeak.net
Sat Oct 22 21:11:19 CEST 2005
Author: afa
Date: Sat Oct 22 21:11:07 2005
New Revision: 18837
Modified:
pypy/dist/pypy/module/_socket/rpython/exttable.py
pypy/dist/pypy/module/_socket/rpython/ll__socket.py
pypy/dist/pypy/module/_socket/rpython/test/test_ll__socket.py
pypy/dist/pypy/translator/c/extfunc.py
pypy/dist/pypy/translator/c/src/ll__socket.h
pypy/dist/pypy/translator/c/test/test_extfunc.py
Log:
Translation of socket.gethostname()
The exception raised is wrong - but it is difficult to make
gethostname return an error.
Will test with another function.
Modified: pypy/dist/pypy/module/_socket/rpython/exttable.py
==============================================================================
--- pypy/dist/pypy/module/_socket/rpython/exttable.py (original)
+++ pypy/dist/pypy/module/_socket/rpython/exttable.py Sat Oct 22 21:11:07 2005
@@ -10,6 +10,8 @@
# ____________________________________________________________
# Built-in functions needed in the rtyper
+declare(_socket.gethostname, str, '%s/gethostname' % module)
+
declare(_socket.ntohs, int, '%s/ntohs' % module)
declare(_socket.htons, int, '%s/ntohs' % module)
declare(_socket.ntohl, int, '%s/ntohl' % module)
Modified: pypy/dist/pypy/module/_socket/rpython/ll__socket.py
==============================================================================
--- pypy/dist/pypy/module/_socket/rpython/ll__socket.py (original)
+++ pypy/dist/pypy/module/_socket/rpython/ll__socket.py Sat Oct 22 21:11:07 2005
@@ -1,5 +1,11 @@
import _socket
+from pypy.rpython.module.support import to_rstr
+
+def ll__socket_gethostname():
+ return to_rstr(_socket.gethostname())
+ll__socket_gethostname.suggested_primitive = True
+
def ll__socket_ntohs(htons):
return _socket.ntohs(htons)
ll__socket_ntohs.suggested_primitive = True
@@ -15,3 +21,4 @@
def ll__socket_ntohl(htonl):
return _socket.ntohl(htonl)
ll__socket_ntohl.suggested_primitive = True
+
Modified: pypy/dist/pypy/module/_socket/rpython/test/test_ll__socket.py
==============================================================================
--- pypy/dist/pypy/module/_socket/rpython/test/test_ll__socket.py (original)
+++ pypy/dist/pypy/module/_socket/rpython/test/test_ll__socket.py Sat Oct 22 21:11:07 2005
@@ -4,6 +4,7 @@
from pypy.module._socket.rpython.ll__socket import *
from pypy.translator.annrpython import RPythonAnnotator
from pypy.rpython.test.test_llinterp import interpret
+from pypy.rpython.module.support import from_rstr
def test_ntohs():
def fn():
@@ -11,3 +12,10 @@
a = RPythonAnnotator()
res = interpret(fn, [])
assert res == _socket.ntohs(1)
+
+def test_gethostname():
+ def fn():
+ return _socket.gethostname()
+ a = RPythonAnnotator()
+ res = interpret(fn, [])
+ assert from_rstr(res) == _socket.gethostname()
Modified: pypy/dist/pypy/translator/c/extfunc.py
==============================================================================
--- pypy/dist/pypy/translator/c/extfunc.py (original)
+++ pypy/dist/pypy/translator/c/extfunc.py Sat Oct 22 21:11:07 2005
@@ -57,6 +57,7 @@
ll_stackless.ll_stackless_stack_frames_depth: 'LL_stackless_stack_frames_depth',
ll_stack.ll_stack_unwind: 'LL_stack_unwind',
ll_stack.ll_stack_too_big: 'LL_stack_too_big',
+ ll__socket.ll__socket_gethostname: 'LL__socket_gethostname',
ll__socket.ll__socket_ntohs: 'LL__socket_ntohs',
ll__socket.ll__socket_htons: 'LL__socket_htons',
ll__socket.ll__socket_htonl: 'LL__socket_htonl',
Modified: pypy/dist/pypy/translator/c/src/ll__socket.h
==============================================================================
--- pypy/dist/pypy/translator/c/src/ll__socket.h (original)
+++ pypy/dist/pypy/translator/c/src/ll__socket.h Sat Oct 22 21:11:07 2005
@@ -40,4 +40,20 @@
}
+RPyString *LL__socket_gethostname()
+{
+ char buf[1024];
+ char *res;
+ res = gethostname(buf, sizeof buf - 1);
+ if (res < 0) {
+ //XXX
+ //RPYTHON_RAISE_OSERROR(errno);
+ RPyRaiseSimpleException(PyExc_ValueError,
+ "gethostname() error");
+ return NULL;
+ }
+ buf[sizeof buf - 1] = '\0';
+ return RPyString_FromString(buf);
+}
+
#endif
Modified: pypy/dist/pypy/translator/c/test/test_extfunc.py
==============================================================================
--- pypy/dist/pypy/translator/c/test/test_extfunc.py (original)
+++ pypy/dist/pypy/translator/c/test/test_extfunc.py Sat Oct 22 21:11:07 2005
@@ -1,6 +1,6 @@
import autopath
import py
-import os, time, sys
+import os, time, sys, _socket
from pypy.tool.udir import udir
from pypy.translator.c.test.test_genc import compile
from pypy.translator.c.extfunc import EXTERNALS
@@ -532,8 +532,7 @@
compared_with.sort()
assert result == compared_with
-def test_socket():
- import _socket
+def test_htonl():
import pypy.module._socket.rpython.exttable # for declare()/declaretype()
# This just checks that htons etc. are their own inverse,
# when looking at the lower 16 or 32 bits.
@@ -552,4 +551,14 @@
# Don't try with 'long' values: type conversion is done
# at the interp level, not at the C level
for i in (0, 1, 0xffff, 2, 0x01234567, 0x76543210):
+ print func, hex(i&mask)
assert i & mask == func(func(i&mask)) & mask
+
+def test_gethostname():
+ import pypy.module._socket.rpython.exttable # for declare()/declaretype()
+ def does_stuff():
+ return _socket.gethostname()
+ f1 = compile(does_stuff, [])
+ res = f1()
+ assert res == _socket.gethostname()
+
More information about the Pypy-commit
mailing list