[pypy-svn] r45660 - pypy/branch/pypy-more-rtti-inprogress/rpython/module

arigo at codespeak.net arigo at codespeak.net
Tue Aug 14 19:23:33 CEST 2007


Author: arigo
Date: Tue Aug 14 19:23:32 2007
New Revision: 45660

Added:
   pypy/branch/pypy-more-rtti-inprogress/rpython/module/ll_os_environ.py   (contents, props changed)
Log:
Forgot to add this to the previously-created branch.


Added: pypy/branch/pypy-more-rtti-inprogress/rpython/module/ll_os_environ.py
==============================================================================
--- (empty file)
+++ pypy/branch/pypy-more-rtti-inprogress/rpython/module/ll_os_environ.py	Tue Aug 14 19:23:32 2007
@@ -0,0 +1,68 @@
+import os
+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
+
+# ____________________________________________________________
+#
+# 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
+        return r_getenv(key)
+
+    def setitem(self, obj, key, value):
+        # in the RPython program, 'os.environ[key] = value' is redirected here
+        r_putenv(key, value)
+
+
+# ____________________________________________________________
+#
+# Lower-level interface: dummy placeholders and external registations
+
+def r_getenv(name):
+    just_a_placeholder
+
+os_getenv = rffi.llexternal('getenv', [rffi.CCHARP], rffi.CCHARP)
+
+def getenv_lltypeimpl(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, [str], annmodel.SomeString(can_be_None=True),
+                   export_name='ll_os.ll_os_getenv',
+                   llimpl=getenv_lltypeimpl)
+
+# ____________________________________________________________
+
+def r_putenv(name, value):
+    just_a_placeholder
+
+os_putenv = rffi.llexternal('putenv', [rffi.CCHARP], rffi.INT)
+
+def putenv_lltypeimpl(name, value):
+    l_string = rffi.str2charp('%s=%s' % (name, value))
+    l_result = os_putenv(l_name)
+    if l_result:
+        result = rffi.charp2str(l_result)
+    else:
+        result = None
+    rffi.free_charp(l_string)     <-------!
+    return result
+
+_register_external(r_putenv, [str, str], annmodel.s_None,
+                   export_name='ll_os.ll_os_putenv',
+                   llimpl=putenv_lltypeimpl)



More information about the Pypy-commit mailing list