~mwhudson/pypy/imported-merging

« back to all changes in this revision

Viewing changes to rpython/module/support.py

  • Committer: arigo
  • Date: 2007-09-05 10:35:18 UTC
  • Revision ID: svn-v4:fd0d7bf2-dfb6-0310-8d31-b7ecfe96aada:pypy/branch/merging:46335
(arigo, antocuni around)

Finish merging the rffi branch into the "merging" branch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
from pypy.rpython.lltypesystem import lltype
 
2
from pypy.rpython.ootypesystem import ootype
 
3
from pypy.rpython import extfunctable
 
4
from pypy.rpython.lltypesystem.lltype import \
 
5
     GcStruct, Signed, Array, Char, Ptr, malloc, GcArray
 
6
from pypy.rpython.rlist import ll_append
 
7
from pypy.rpython.lltypesystem.rlist import ll_newlist, ListRepr,\
 
8
    ll_getitem_fast
 
9
from pypy.rpython.lltypesystem.rstr import string_repr
 
10
from pypy.rpython.lltypesystem.rdict import ll_newdict, DictRepr, dum_items,\
 
11
    ll_kvi, dum_keys, ll_dict_getitem, ll_dict_setitem
 
12
from pypy.rpython.lltypesystem.rstr import StringRepr
 
13
from pypy.rpython.lltypesystem.rtuple import TupleRepr
 
14
from pypy.annotation.dictdef import DictKey, DictValue
 
15
from pypy.annotation.model import SomeString
 
16
import os
 
17
 
 
18
# utility conversion functions
 
19
class LLSupport:
 
20
    _mixin_ = True
 
21
    
 
22
    def to_rstr(s):
 
23
        from pypy.rpython.lltypesystem.rstr import STR, mallocstr
 
24
        if s is None:
 
25
            return lltype.nullptr(STR)
 
26
        p = mallocstr(len(s))
 
27
        for i in range(len(s)):
 
28
            p.chars[i] = s[i]
 
29
        return p
 
30
    to_rstr = staticmethod(to_rstr)
 
31
 
 
32
    def from_rstr(rs):
 
33
        if not rs:   # null pointer
 
34
            return None
 
35
        else:
 
36
            return ''.join([rs.chars[i] for i in range(len(rs.chars))])
 
37
    from_rstr = staticmethod(from_rstr)
 
38
 
 
39
    def from_rstr_nonnull(rs):
 
40
        assert rs
 
41
        return ''.join([rs.chars[i] for i in range(len(rs.chars))])
 
42
    from_rstr_nonnull = staticmethod(from_rstr_nonnull)
 
43
 
 
44
class OOSupport:
 
45
    _mixin_ = True
 
46
 
 
47
    def to_rstr(s):
 
48
        return ootype.oostring(s, -1)
 
49
    to_rstr = staticmethod(to_rstr)
 
50
    
 
51
    def from_rstr(rs):
 
52
        if not rs:   # null pointer
 
53
            return None
 
54
        else:
 
55
            return "".join([rs.ll_stritem_nonneg(i) for i in range(rs.ll_strlen())])
 
56
    from_rstr = staticmethod(from_rstr)        
 
57
 
 
58
    def from_rstr_nonnull(rs):
 
59
        assert rs
 
60
        return "".join([rs.ll_stritem_nonneg(i) for i in range(rs.ll_strlen())])
 
61
    from_rstr_nonnull = staticmethod(from_rstr_nonnull)
 
62
 
 
63
 
 
64
def ll_strcpy(dst_s, src_s, n):
 
65
    dstchars = dst_s.chars
 
66
    srcchars = src_s.chars
 
67
    i = 0
 
68
    while i < n:
 
69
        dstchars[i] = srcchars[i]
 
70
        i += 1
 
71
 
 
72
def _ll_strfill(dst_s, srcchars, n):
 
73
    dstchars = dst_s.chars
 
74
    i = 0
 
75
    while i < n:
 
76
        dstchars[i] = srcchars[i]
 
77
        i += 1
 
78
 
 
79
def init_opaque_object(opaqueptr, value):
 
80
    "NOT_RPYTHON"
 
81
    opaqueptr._obj.externalobj = value
 
82
init_opaque_object._annspecialcase_ = "override:init_opaque_object"
 
83
 
 
84
def from_opaque_object(opaqueptr):
 
85
    "NOT_RPYTHON"
 
86
    return opaqueptr._obj.externalobj
 
87
from_opaque_object._annspecialcase_ = "override:from_opaque_object"
 
88
 
 
89
def to_opaque_object(value):
 
90
    "NOT_RPYTHON"
 
91
    exttypeinfo = extfunctable.typetable[value.__class__]
 
92
    return lltype.opaqueptr(exttypeinfo.get_lltype(), 'opaque',
 
93
                            externalobj=value)
 
94
to_opaque_object._annspecialcase_ = "override:to_opaque_object"