[pypy-svn] r23766 - in pypy/dist/pypy/rpython: . rctypes rctypes/test
goden at codespeak.net
goden at codespeak.net
Tue Feb 28 18:48:11 CET 2006
Author: goden
Date: Tue Feb 28 18:48:06 2006
New Revision: 23766
Modified:
pypy/dist/pypy/rpython/extregistry.py
pypy/dist/pypy/rpython/rbuiltin.py
pypy/dist/pypy/rpython/rctypes/implementation.py
pypy/dist/pypy/rpython/rctypes/test/test_rctypes.py
Log:
- (micktwomey, arigo, goden) - support for specializing instances of ctypes
function pointer metatypes
Modified: pypy/dist/pypy/rpython/extregistry.py
==============================================================================
--- pypy/dist/pypy/rpython/extregistry.py (original)
+++ pypy/dist/pypy/rpython/extregistry.py Tue Feb 28 18:48:06 2006
@@ -1,9 +1,9 @@
import weakref
class ExtRegistryFunc(object):
- def __init__(self, compute_result_annotation, specialize):
+ def __init__(self, compute_result_annotation, specialize_call):
self.compute_result_annotation = compute_result_annotation
- self.specialize = specialize
+ self.specialize_call = specialize_call
def get_annotation(self, type, func=None):
assert func is not None
@@ -28,7 +28,7 @@
EXT_REGISTRY_BY_TYPE = weakref.WeakKeyDictionary()
EXT_REGISTRY_BY_METATYPE = weakref.WeakKeyDictionary()
-def register_func(func, compute_result_annotation, specialize=None):
+def register_func(func, compute_result_annotation, specialize_call=None):
from pypy.annotation import model as annmodel
if isinstance(compute_result_annotation, annmodel.SomeObject):
s_result = compute_result_annotation
@@ -38,7 +38,8 @@
compute_result_annotation = annotation
EXT_REGISTRY_BY_VALUE[func] = ExtRegistryFunc(compute_result_annotation,
- specialize)
+ specialize_call)
+ return EXT_REGISTRY_BY_VALUE[func]
def register_type(t, compute_annotation):
from pypy.annotation import model as annmodel
@@ -51,9 +52,13 @@
EXT_REGISTRY_BY_TYPE[t] = ExtRegistryType(compute_annotation)
+ return EXT_REGISTRY_BY_TYPE[t]
+
def register_metatype(t, compute_annotation):
EXT_REGISTRY_BY_METATYPE[t] = ExtRegistryMetaType(compute_annotation)
+ return EXT_REGISTRY_BY_METATYPE[t]
+
def lookup_type(tp):
try:
return EXT_REGISTRY_BY_TYPE[tp]
Modified: pypy/dist/pypy/rpython/rbuiltin.py
==============================================================================
--- pypy/dist/pypy/rpython/rbuiltin.py (original)
+++ pypy/dist/pypy/rpython/rbuiltin.py Tue Feb 28 18:48:06 2006
@@ -31,7 +31,13 @@
def rtyper_makekey(self):
if self.s_self is None:
# built-in function case
- return self.__class__, getattr(self, 'const', None)
+
+ const = getattr(self, 'const', None)
+
+ if extregistry.is_registered(const):
+ const = extregistry.lookup(const)
+
+ return self.__class__, const
else:
# built-in method case
# NOTE: we hash by id of self.s_self here. This appears to be
@@ -54,17 +60,17 @@
def rtype_simple_call(self, hop):
try:
bltintyper = BUILTIN_TYPER[self.builtinfunc]
- except KeyError:
+ except (KeyError, TypeError):
try:
rtyper = hop.rtyper
bltintyper = rtyper.type_system.rbuiltin.\
BUILTIN_TYPER[self.builtinfunc]
- except KeyError:
+ except (KeyError, TypeError):
if hasattr(self.builtinfunc,"specialize"):
bltintyper = self.builtinfunc.specialize
elif extregistry.is_registered(self.builtinfunc):
entry = extregistry.lookup(self.builtinfunc)
- bltintyper = entry.specialize
+ bltintyper = entry.specialize_call
else:
raise TyperError("don't know about built-in function %r" % (
self.builtinfunc,))
Modified: pypy/dist/pypy/rpython/rctypes/implementation.py
==============================================================================
--- pypy/dist/pypy/rpython/rctypes/implementation.py (original)
+++ pypy/dist/pypy/rpython/rctypes/implementation.py Tue Feb 28 18:48:06 2006
@@ -101,7 +101,32 @@
return SomeBuiltin(compute_result_annotation,
methodname=instance.__name__)
-register_metatype(CFuncPtrType, cfuncptrtype_compute_annotation)
+def specialize_call(hop):
+ # this is necessary to get the original function pointer when specializing
+ # the metatype
+ cfuncptr = hop.spaceop.args[0].value
+
+ def convert_params(backend, param_info_list):
+ assert "c" == backend.lower()
+ assert cfuncptr.argtypes is not None
+ answer = []
+ for ctype_type, (ll_type, arg_name) in zip(cfuncptr.argtypes,
+ param_info_list):
+ if ll_type == ctype_type.ll_type:
+ answer.append(arg_name)
+ else:
+ answer.append(ctype_type.wrap_arg(ll_type, arg_name))
+ return answer
+
+ return hop.llops.gencapicall(
+ cfuncptr.__name__,
+ hop.args_v,
+ resulttype = cfuncptr.restype.ll_type,
+ _callable=None,
+ convert_params = convert_params )
+
+entry = register_metatype(CFuncPtrType, cfuncptrtype_compute_annotation)
+entry.specialize_call = specialize_call
class FunctionPointerTranslation(object):
Modified: pypy/dist/pypy/rpython/rctypes/test/test_rctypes.py
==============================================================================
--- pypy/dist/pypy/rpython/rctypes/test/test_rctypes.py (original)
+++ pypy/dist/pypy/rpython/rctypes/test/test_rctypes.py Tue Feb 28 18:48:06 2006
@@ -255,7 +255,7 @@
# result should be an integer
assert s.knowntype == int
- def failing_test_specialize_simple(self):
+ def test_specialize_simple(self):
t = TranslationContext()
a = t.buildannotator()
s = a.build_types(o_atoi, [str])
@@ -264,7 +264,7 @@
t.buildrtyper().specialize()
#d#t.view()
- def failing_test_compile_simple(self):
+ def test_compile_simple(self):
fn = compile(o_atoi, [str])
res = fn("42")
assert res == 42
More information about the Pypy-commit
mailing list