pypy /pypy/rpython/module/ll_os_environ.py

Language Python Lines 202
MD5 Hash de3415cb1e2cedef2e57a5343ab70b14 Estimated Cost $3,454 (why?)
Repository https://bitbucket.org/pypy/pypy/ View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import os, sys
from pypy.annotation import model as annmodel
from pypy.rpython.controllerentry import Controller
from pypy.rpython.extfunc import register_external
from pypy.rpython.lltypesystem import rffi, lltype
from pypy.rpython.module import ll_os
from pypy.rlib import rposix

str0 = ll_os.str0

# ____________________________________________________________
#
# Annotation support to control access to 'os.environ' in the RPython program

class OsEnvironController(Controller):
    knowntype = os.environ.__class__

    def convert(self, obj):
        return None     # 'None' is good enough, there is only one os.environ

    def getitem(self, obj, key):
        # in the RPython program reads of 'os.environ[key]' are redirected here
        result = r_getenv(key)
        if result is None:
            raise KeyError
        return result

    def setitem(self, obj, key, value):
        # in the RPython program, 'os.environ[key] = value' is redirected here
        r_putenv(key, value)

    def delitem(self, obj, key):
        # in the RPython program, 'del os.environ[key]' is redirected here
        if r_getenv(key) is None:
            raise KeyError
        r_unsetenv(key)

    def get_keys(self, obj):
        # 'os.environ.keys' is redirected here - note that it's the getattr
        # that arrives here, not the actual method call!
        return r_envkeys

    def get_items(self, obj):
        # 'os.environ.items' is redirected here (not the actual method call!)
        return r_envitems

    def get_get(self, obj):
        # 'os.environ.get' is redirected here (not the actual method call!)
        return r_getenv

# ____________________________________________________________
#
# Lower-level interface: dummy placeholders and external registations

def r_getenv(name):
    just_a_placeholder     # should return None if name not found

os_getenv = rffi.llexternal('getenv', [rffi.CCHARP], rffi.CCHARP)

def getenv_llimpl(name):
    l_name = rffi.str2charp(name)
    l_result = os_getenv(l_name)
    if l_result:
        result = rffi.charp2str(l_result)
    else:
        result = None
    rffi.free_charp(l_name)
    return result

register_external(r_getenv, [str0], annmodel.SomeString(can_be_None=True),
                  export_name='ll_os.ll_os_getenv',
                  llimpl=getenv_llimpl)

# ____________________________________________________________

def r_putenv(name, value):
    just_a_placeholder

class EnvKeepalive:
    pass
envkeepalive = EnvKeepalive()
envkeepalive.byname = {}

os_putenv = rffi.llexternal('putenv', [rffi.CCHARP], rffi.INT)

def putenv_llimpl(name, value):
    l_string = rffi.str2charp('%s=%s' % (name, value))
    error = rffi.cast(lltype.Signed, os_putenv(l_string))
    if error:
        rffi.free_charp(l_string)
        raise OSError(rposix.get_errno(), "os_putenv failed")
    # keep 'l_string' alive - we know that the C library needs it
    # until the next call to putenv() with the same 'name'.
    l_oldstring = envkeepalive.byname.get(name, lltype.nullptr(rffi.CCHARP.TO))
    envkeepalive.byname[name] = l_string
    if l_oldstring:
        rffi.free_charp(l_oldstring)

register_external(r_putenv, [str0, str0], annmodel.s_None,
                  export_name='ll_os.ll_os_putenv',
                  llimpl=putenv_llimpl)

# ____________________________________________________________

def r_unsetenv(name):
    # default implementation for platforms without a real unsetenv()
    r_putenv(name, '')

if hasattr(__import__(os.name), 'unsetenv'):

    if sys.platform.startswith('darwin'):
        RETTYPE = lltype.Void
        os_unsetenv = rffi.llexternal('unsetenv', [rffi.CCHARP], RETTYPE)
    else:
        RETTYPE = rffi.INT
        _os_unsetenv = rffi.llexternal('unsetenv', [rffi.CCHARP], RETTYPE)
        def os_unsetenv(l_name):
            return rffi.cast(lltype.Signed, _os_unsetenv(l_name))

    def unsetenv_llimpl(name):
        l_name = rffi.str2charp(name)
        error = os_unsetenv(l_name)     # 'error' is None on OS/X
        rffi.free_charp(l_name)
        if error:
            raise OSError(rposix.get_errno(), "os_unsetenv failed")
        try:
            l_oldstring = envkeepalive.byname[name]
        except KeyError:
            pass
        else:
            del envkeepalive.byname[name]
            rffi.free_charp(l_oldstring)

    register_external(r_unsetenv, [str0], annmodel.s_None,
                      export_name='ll_os.ll_os_unsetenv',
                      llimpl=unsetenv_llimpl)

# ____________________________________________________________
# Access to the 'environ' external variable

from pypy.translator.tool.cbuild import ExternalCompilationInfo

if sys.platform.startswith('darwin'):
    CCHARPPP = rffi.CArrayPtr(rffi.CCHARPP)
    _os_NSGetEnviron = rffi.llexternal(
        '_NSGetEnviron', [], CCHARPPP,
        compilation_info=ExternalCompilationInfo(includes=['crt_externs.h'])
        )
    def os_get_environ():
        return _os_NSGetEnviron()[0]
elif sys.platform.startswith('win'):
    os_get_environ, _os_set_environ = rffi.CExternVariable(
        rffi.CCHARPP,
        '_environ',
        ExternalCompilationInfo(includes=['stdlib.h']))
else:
    os_get_environ, _os_set_environ = rffi.CExternVariable(rffi.CCHARPP,
                                                           'environ',
                                                           ExternalCompilationInfo())

# ____________________________________________________________

def r_envkeys():
    just_a_placeholder

def envkeys_llimpl():
    environ = os_get_environ()
    result = []
    i = 0
    while environ[i]:
        name_value = rffi.charp2str(environ[i])
        p = name_value.find('=')
        if p >= 0:
            result.append(name_value[:p])
        i += 1
    return result

register_external(r_envkeys, [], [str0],   # returns a list of strings
                  export_name='ll_os.ll_os_envkeys',
                  llimpl=envkeys_llimpl)

# ____________________________________________________________

def r_envitems():
    just_a_placeholder

def envitems_llimpl():
    environ = os_get_environ()
    result = []
    i = 0
    while environ[i]:
        name_value = rffi.charp2str(environ[i])
        p = name_value.find('=')
        if p >= 0:
            result.append((name_value[:p], name_value[p+1:]))
        i += 1
    return result

register_external(r_envitems, [], [(str0, str0)],
                  export_name='ll_os.ll_os_envitems',
                  llimpl=envitems_llimpl)
Back to Top