[pypy-svn] r25979 - in pypy/dist/pypy: annotation rpython/rctypes rpython/rctypes/test
arigo at codespeak.net
arigo at codespeak.net
Wed Apr 19 13:22:24 CEST 2006
Author: arigo
Date: Wed Apr 19 13:22:22 2006
New Revision: 25979
Modified:
pypy/dist/pypy/annotation/unaryop.py
pypy/dist/pypy/rpython/rctypes/avoid_p.py
pypy/dist/pypy/rpython/rctypes/rmodel.py
pypy/dist/pypy/rpython/rctypes/rpointer.py
pypy/dist/pypy/rpython/rctypes/test/test_ctypes.py
pypy/dist/pypy/rpython/rctypes/test/test_rchar_p.py
pypy/dist/pypy/rpython/rctypes/test/test_rfunc.py
pypy/dist/pypy/rpython/rctypes/test/test_rpointer.py
pypy/dist/pypy/rpython/rctypes/test/test_rprimitive.py
pypy/dist/pypy/rpython/rctypes/test/test_rvoid_p.py
Log:
Truth value of various ctypes objects.
Using None in calls to functions expecting pointers.
Modified: pypy/dist/pypy/annotation/unaryop.py
==============================================================================
--- pypy/dist/pypy/annotation/unaryop.py (original)
+++ pypy/dist/pypy/annotation/unaryop.py Wed Apr 19 13:22:22 2006
@@ -645,6 +645,9 @@
else:
return SomeObject()
+ def is_true(cto):
+ return SomeBool()
+
#_________________________________________
# memory addresses
Modified: pypy/dist/pypy/rpython/rctypes/avoid_p.py
==============================================================================
--- pypy/dist/pypy/rpython/rctypes/avoid_p.py (original)
+++ pypy/dist/pypy/rpython/rctypes/avoid_p.py Wed Apr 19 13:22:22 2006
@@ -7,11 +7,18 @@
# c_void_p() as a function
-def c_void_p_compute_result_annotation(s_arg=None):
- raise NotImplementedError("XXX calling c_void_p()")
+def c_void_p_compute_result_annotation():
+ return annmodel.SomeCTypesObject(c_void_p,
+ annmodel.SomeCTypesObject.OWNSMEMORY)
+
+def c_void_specialize_call(hop):
+ r_void_p = hop.r_result
+ v_result = r_void_p.allocate_instance(hop.llops)
+ return v_result
extregistry.register_value(c_void_p,
compute_result_annotation=c_void_p_compute_result_annotation,
+ specialize_call=c_void_specialize_call,
)
# c_void_p instances
Modified: pypy/dist/pypy/rpython/rctypes/rmodel.py
==============================================================================
--- pypy/dist/pypy/rpython/rctypes/rmodel.py (original)
+++ pypy/dist/pypy/rpython/rctypes/rmodel.py Wed Apr 19 13:22:22 2006
@@ -1,6 +1,6 @@
from pypy.rpython.rmodel import Repr, inputconst
from pypy.rpython.error import TyperError
-from pypy.rpython.lltypesystem import lltype
+from pypy.rpython.lltypesystem import lltype, llmemory
from pypy.annotation.model import SomeCTypesObject
from pypy.annotation.pairtype import pairtype
@@ -237,8 +237,28 @@
r_temp.setvalue(llops, v_owned_box, v_value)
return llops.convertvar(v_owned_box, r_temp, self)
+ def rtype_is_true(self, hop):
+ [v_box] = hop.inputargs(self)
+ v_value = self.getvalue(hop.llops, v_box)
+ if v_value.concretetype in (lltype.Char, lltype.UniChar):
+ llfn = ll_c_char_is_true
+ elif v_value.concretetype == llmemory.Address:
+ llfn = ll_address_is_true
+ else:
+ llfn = ll_is_true
+ return hop.gendirectcall(llfn, v_value)
+
# ____________________________________________________________
+def ll_is_true(x):
+ return bool(x)
+
+def ll_c_char_is_true(x):
+ return bool(ord(x))
+
+def ll_address_is_true(x):
+ return x != llmemory.NULL
+
C_ZERO = inputconst(lltype.Signed, 0)
def reccopy(source, dest):
Modified: pypy/dist/pypy/rpython/rctypes/rpointer.py
==============================================================================
--- pypy/dist/pypy/rpython/rctypes/rpointer.py (original)
+++ pypy/dist/pypy/rpython/rctypes/rpointer.py Wed Apr 19 13:22:22 2006
@@ -30,6 +30,8 @@
llops.genop('setfield', inputargs)
def initialize_const(self, p, ptr):
+ if ptr is None: # passed as argument to functions expecting pointers
+ return
llcontents = self.r_contents.convert_const(ptr.contents)
p.c_data[0] = llcontents.c_data
# the following line is probably pointless, as 'llcontents' will be
Modified: pypy/dist/pypy/rpython/rctypes/test/test_ctypes.py
==============================================================================
--- pypy/dist/pypy/rpython/rctypes/test/test_ctypes.py (original)
+++ pypy/dist/pypy/rpython/rctypes/test/test_ctypes.py Wed Apr 19 13:22:22 2006
@@ -132,3 +132,26 @@
s.p = a
s.p.contents.value = 42
assert a[0] == 42
+
+def test_truth_value():
+ p = POINTER(c_int)()
+ assert not p
+ p.contents = c_int(12)
+ assert p
+ # I can't figure out how to reset p to NULL...
+
+ assert c_int(12)
+ assert not c_int(0) # a bit strange, if you ask me
+ assert c_int(-1)
+ assert not c_byte(0)
+ assert not c_char('\x00') # hum
+ assert not c_float(0.0)
+ assert not c_double(0.0)
+ assert not c_ulonglong(0)
+ assert c_ulonglong(2L**42)
+
+ assert c_char_p("hello")
+ assert c_char_p("")
+ assert not c_char_p(None)
+
+ assert not c_void_p()
Modified: pypy/dist/pypy/rpython/rctypes/test/test_rchar_p.py
==============================================================================
--- pypy/dist/pypy/rpython/rctypes/test/test_rchar_p.py (original)
+++ pypy/dist/pypy/rpython/rctypes/test/test_rchar_p.py Wed Apr 19 13:22:22 2006
@@ -104,6 +104,12 @@
res = interpret(func, [])
assert ''.join(res.chars) == "hello"
+ def test_truth_value(self):
+ def func():
+ assert c_char_p("hello")
+ assert c_char_p("")
+ assert not c_char_p(None)
+ interpret(func, [])
class Test_compilation:
def test_compile_c_char_p(self):
Modified: pypy/dist/pypy/rpython/rctypes/test/test_rfunc.py
==============================================================================
--- pypy/dist/pypy/rpython/rctypes/test/test_rfunc.py (original)
+++ pypy/dist/pypy/rpython/rctypes/test/test_rfunc.py Wed Apr 19 13:22:22 2006
@@ -14,6 +14,7 @@
from ctypes import cdll
from ctypes import c_int, c_long, c_char_p, c_char, create_string_buffer
+from ctypes import POINTER
# __________ the standard C library __________
@@ -53,6 +54,10 @@
return result
atoi.llinterp_friendly_version = ll_atoi
+time_ = mylib.time
+time_.restype = c_long # should rather use ctypes_platform.getsimpletype()
+time_.argtypes = [POINTER(c_long)]
+
def test_labs(n=6):
assert labs(n) == abs(n)
@@ -82,6 +87,13 @@
assert ll_atoi(str2subarray("blah")) == 0
assert ll_atoi(str2subarray("18238")) == 18238
+def test_time():
+ import time
+ t1 = time.time()
+ t2 = time_(None)
+ t3 = time.time()
+ assert int(t1) <= t2 <= int(t3 + 1.0)
+
class Test_annotation:
def test_annotate_labs(self):
a = RPythonAnnotator()
@@ -141,3 +153,13 @@
fn = compile(test_labs, [int])
res = fn(-11)
assert res == 11
+
+ def test_compile_time(self):
+ import time
+ def fn1():
+ return time_(None)
+ fn = compile(fn1, [])
+ t1 = time.time()
+ t2 = fn()
+ t3 = time.time()
+ assert int(t1) <= t2 <= int(t3 + 1.0)
Modified: pypy/dist/pypy/rpython/rctypes/test/test_rpointer.py
==============================================================================
--- pypy/dist/pypy/rpython/rctypes/test/test_rpointer.py (original)
+++ pypy/dist/pypy/rpython/rctypes/test/test_rpointer.py Wed Apr 19 13:22:22 2006
@@ -267,6 +267,14 @@
res = interpret(fn, [])
assert res == 5
+ def test_specialize_null_pointer(self):
+ def fn():
+ p = POINTER(c_int)()
+ assert not p
+ p.contents = c_int(12)
+ assert p
+ interpret(fn, [])
+
class Test_compilation:
def test_compile_getitem_nonzero_index(self):
A = c_int * 10
Modified: pypy/dist/pypy/rpython/rctypes/test/test_rprimitive.py
==============================================================================
--- pypy/dist/pypy/rpython/rctypes/test/test_rprimitive.py (original)
+++ pypy/dist/pypy/rpython/rctypes/test/test_rprimitive.py Wed Apr 19 13:22:22 2006
@@ -253,6 +253,21 @@
assert x.value == u'B'
interpret(func, [])
+ def test_truth_value(self):
+ bigzero = r_ulonglong(0)
+ big = r_ulonglong(2L**42)
+ def func(n, z):
+ assert c_int(n)
+ assert not c_int(z)
+ assert c_int(-1)
+ assert not c_byte(z)
+ assert not c_char(chr(z))
+ assert not c_float(z)
+ assert not c_double(z)
+ assert not c_ulonglong(bigzero)
+ assert c_ulonglong(big)
+ interpret(func, [17, 0])
+
class Test_compilation:
def test_compile_c_int(self):
def create_c_int():
Modified: pypy/dist/pypy/rpython/rctypes/test/test_rvoid_p.py
==============================================================================
--- pypy/dist/pypy/rpython/rctypes/test/test_rvoid_p.py (original)
+++ pypy/dist/pypy/rpython/rctypes/test/test_rvoid_p.py Wed Apr 19 13:22:22 2006
@@ -43,6 +43,11 @@
assert lltype.typeOf(res.item0.c_data[0]) == llmemory.Address
assert res.item1 == 12
+ def test_truth_value(self):
+ def func():
+ assert not c_void_p()
+ interpret(func, [])
+
class Test_compilation:
def test_compile_c_char_p(self):
def func():
More information about the Pypy-commit
mailing list