| 1 |
import os |
| 2 |
import sys |
| 3 |
from rpython.annotator import model as annmodel |
| 4 |
from rpython.rlib._os_support import _WIN32, string_traits, utf8_traits |
| 5 |
from rpython.rlib.objectmodel import enforceargs |
| 6 |
|
| 7 |
from rpython.rtyper.controllerentry import Controller |
| 8 |
from rpython.rtyper.extfunc import register_external |
| 9 |
from rpython.rtyper.lltypesystem import rffi, lltype |
| 10 |
from rpython.translator.tool.cbuild import ExternalCompilationInfo |
| 11 |
|
| 12 |
str0 = annmodel.s_Str0 |
| 13 |
|
| 14 |
def llexternal(name, args, result, **kwds): |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
return rffi.llexternal(name, args, result, releasegil=False, **kwds) |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
class OsEnvironController(Controller): |
| 27 |
knowntype = os.environ.__class__ |
| 28 |
|
| 29 |
def convert(self, obj): |
| 30 |
|
| 31 |
return None |
| 32 |
|
| 33 |
def getitem(self, obj, key): |
| 34 |
|
| 35 |
|
| 36 |
result = r_getenv(key) |
| 37 |
if result is None: |
| 38 |
raise KeyError |
| 39 |
return result |
| 40 |
|
| 41 |
@enforceargs(None, None, str0, None) |
| 42 |
def setitem(self, obj, key, value): |
| 43 |
|
| 44 |
|
| 45 |
r_putenv(key, value) |
| 46 |
|
| 47 |
def delitem(self, obj, key): |
| 48 |
|
| 49 |
|
| 50 |
absent = r_getenv(key) is None |
| 51 |
|
| 52 |
r_unsetenv(key) |
| 53 |
if absent: |
| 54 |
raise KeyError |
| 55 |
|
| 56 |
def get_keys(self, obj): |
| 57 |
|
| 58 |
|
| 59 |
return r_envkeys |
| 60 |
|
| 61 |
def get_items(self, obj): |
| 62 |
|
| 63 |
|
| 64 |
return r_envitems |
| 65 |
|
| 66 |
def get_get(self, obj): |
| 67 |
|
| 68 |
|
| 69 |
return r_getenv |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
prefix = '' |
| 74 |
if sys.platform.startswith('darwin'): |
| 75 |
CCHARPPP = rffi.CArrayPtr(rffi.CCHARPP) |
| 76 |
_os_NSGetEnviron = llexternal( |
| 77 |
'_NSGetEnviron', [], CCHARPPP, |
| 78 |
compilation_info=ExternalCompilationInfo(includes=['crt_externs.h']) |
| 79 |
) |
| 80 |
def os_get_environ(): |
| 81 |
return _os_NSGetEnviron()[0] |
| 82 |
elif _WIN32: |
| 83 |
eci = ExternalCompilationInfo(includes=['stdlib.h']) |
| 84 |
CWCHARPP = lltype.Ptr(lltype.Array(rffi.CWCHARP, hints={'nolength': True})) |
| 85 |
|
| 86 |
os_get_environ, _os_set_environ = rffi.CExternVariable( |
| 87 |
rffi.CCHARPP, '_environ', eci) |
| 88 |
get__wenviron, _set__wenviron = rffi.CExternVariable( |
| 89 |
CWCHARPP, '_wenviron', eci, c_type='wchar_t **') |
| 90 |
prefix = '_' |
| 91 |
else: |
| 92 |
os_get_environ, _os_set_environ = rffi.CExternVariable( |
| 93 |
rffi.CCHARPP, 'environ', ExternalCompilationInfo()) |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
def r_envkeys(): |
| 100 |
just_a_placeholder |
| 101 |
|
| 102 |
def envkeys_llimpl(): |
| 103 |
environ = os_get_environ() |
| 104 |
result = [] |
| 105 |
i = 0 |
| 106 |
while environ[i]: |
| 107 |
name_value = rffi.charp2str(environ[i]) |
| 108 |
p = name_value.find('=') |
| 109 |
if p >= 0: |
| 110 |
result.append(name_value[:p]) |
| 111 |
i += 1 |
| 112 |
return result |
| 113 |
|
| 114 |
register_external(r_envkeys, [], [str0], |
| 115 |
export_name='ll_os.ll_os_envkeys', |
| 116 |
llimpl=envkeys_llimpl) |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
def r_envitems(): |
| 121 |
just_a_placeholder |
| 122 |
|
| 123 |
def r_getenv(name): |
| 124 |
just_a_placeholder |
| 125 |
|
| 126 |
def r_putenv(name, value): |
| 127 |
just_a_placeholder |
| 128 |
|
| 129 |
os_getenv = llexternal('getenv', [rffi.CCHARP], rffi.CCHARP) |
| 130 |
os_putenv = llexternal(prefix + 'putenv', [rffi.CCHARP], rffi.INT, |
| 131 |
save_err=rffi.RFFI_SAVE_ERRNO) |
| 132 |
if _WIN32: |
| 133 |
_wgetenv = llexternal('_wgetenv', [rffi.CWCHARP], rffi.CWCHARP, |
| 134 |
compilation_info=eci) |
| 135 |
_wputenv = llexternal('_wputenv', [rffi.CWCHARP], rffi.INT, |
| 136 |
compilation_info=eci, |
| 137 |
save_err=rffi.RFFI_SAVE_LASTERROR) |
| 138 |
|
| 139 |
class EnvKeepalive: |
| 140 |
pass |
| 141 |
envkeepalive = EnvKeepalive() |
| 142 |
envkeepalive.byname = {} |
| 143 |
envkeepalive.bywname = {} |
| 144 |
|
| 145 |
def make_env_impls(win32=False): |
| 146 |
if not win32: |
| 147 |
traits = string_traits |
| 148 |
get_environ, getenv, putenv = os_get_environ, os_getenv, os_putenv |
| 149 |
byname, eq = envkeepalive.byname, '=' |
| 150 |
def last_error(msg): |
| 151 |
from rpython.rlib import rposix |
| 152 |
raise OSError(rposix.get_saved_errno(), msg) |
| 153 |
else: |
| 154 |
traits = utf8_traits |
| 155 |
get_environ, getenv, putenv = get__wenviron, _wgetenv, _wputenv |
| 156 |
byname, eq = envkeepalive.bywname, '=' |
| 157 |
from rpython.rlib.rwin32 import lastSavedWindowsError as last_error |
| 158 |
|
| 159 |
def envitems_llimpl(): |
| 160 |
environ = get_environ() |
| 161 |
result = [] |
| 162 |
if not environ: |
| 163 |
return result |
| 164 |
i = 0 |
| 165 |
while environ[i]: |
| 166 |
name_value = traits.charp2str(environ[i]) |
| 167 |
p = name_value.find(eq) |
| 168 |
if p >= 0: |
| 169 |
result.append((name_value[:p], name_value[p+1:])) |
| 170 |
i += 1 |
| 171 |
return result |
| 172 |
|
| 173 |
def getenv_llimpl(name): |
| 174 |
with traits.scoped_str2charp(name) as l_name: |
| 175 |
l_result = getenv(l_name) |
| 176 |
return traits.charp2str(l_result) if l_result else None |
| 177 |
|
| 178 |
def putenv_llimpl(name, value): |
| 179 |
l_string = traits.str2charp(name + eq + value) |
| 180 |
error = rffi.cast(lltype.Signed, putenv(l_string)) |
| 181 |
if error: |
| 182 |
traits.free_charp(l_string) |
| 183 |
last_error("putenv failed") |
| 184 |
|
| 185 |
|
| 186 |
l_oldstring = byname.get(name, lltype.nullptr(traits.CCHARP.TO)) |
| 187 |
byname[name] = l_string |
| 188 |
if l_oldstring: |
| 189 |
traits.free_charp(l_oldstring) |
| 190 |
|
| 191 |
return envitems_llimpl, getenv_llimpl, putenv_llimpl |
| 192 |
|
| 193 |
envitems_llimpl, getenv_llimpl, putenv_llimpl = make_env_impls() |
| 194 |
|
| 195 |
register_external(r_envitems, [], [(str0, str0)], |
| 196 |
export_name='ll_os.ll_os_envitems', |
| 197 |
llimpl=envitems_llimpl) |
| 198 |
register_external(r_getenv, [str0], |
| 199 |
annmodel.SomeString(can_be_None=True, no_nul=True), |
| 200 |
export_name='ll_os.ll_os_getenv', |
| 201 |
llimpl=getenv_llimpl) |
| 202 |
register_external(r_putenv, [str0, str0], annmodel.s_None, |
| 203 |
export_name='ll_os.ll_os_putenv', |
| 204 |
llimpl=putenv_llimpl) |
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
def r_unsetenv(name): |
| 209 |
|
| 210 |
r_putenv(name, '') |
| 211 |
|
| 212 |
REAL_UNSETENV = False |
| 213 |
|
| 214 |
if hasattr(__import__(os.name), 'unsetenv'): |
| 215 |
os_unsetenv = llexternal('unsetenv', [rffi.CCHARP], rffi.INT, |
| 216 |
save_err=rffi.RFFI_SAVE_ERRNO) |
| 217 |
|
| 218 |
def unsetenv_llimpl(name): |
| 219 |
with rffi.scoped_str2charp(name) as l_name: |
| 220 |
error = rffi.cast(lltype.Signed, os_unsetenv(l_name)) |
| 221 |
if error: |
| 222 |
from rpython.rlib import rposix |
| 223 |
raise OSError(rposix.get_saved_errno(), "os_unsetenv failed") |
| 224 |
try: |
| 225 |
l_oldstring = envkeepalive.byname[name] |
| 226 |
except KeyError: |
| 227 |
pass |
| 228 |
else: |
| 229 |
del envkeepalive.byname[name] |
| 230 |
rffi.free_charp(l_oldstring) |
| 231 |
|
| 232 |
register_external(r_unsetenv, [str0], annmodel.s_None, |
| 233 |
export_name='ll_os.ll_os_unsetenv', |
| 234 |
llimpl=unsetenv_llimpl) |
| 235 |
REAL_UNSETENV = True |