[pypy-svn] r79826 - in pypy/trunk/pypy/module/cpyext: . test

afa at codespeak.net afa at codespeak.net
Sat Dec 4 22:02:03 CET 2010


Author: afa
Date: Sat Dec  4 22:02:02 2010
New Revision: 79826

Modified:
   pypy/trunk/pypy/module/cpyext/pythonrun.py
   pypy/trunk/pypy/module/cpyext/state.py
   pypy/trunk/pypy/module/cpyext/stubs.py
   pypy/trunk/pypy/module/cpyext/test/test_cpyext.py
Log:
Add Py_GetProgramName


Modified: pypy/trunk/pypy/module/cpyext/pythonrun.py
==============================================================================
--- pypy/trunk/pypy/module/cpyext/pythonrun.py	(original)
+++ pypy/trunk/pypy/module/cpyext/pythonrun.py	Sat Dec  4 22:02:02 2010
@@ -1,6 +1,16 @@
 from pypy.rpython.lltypesystem import rffi, lltype
 from pypy.module.cpyext.api import cpython_api, CANNOT_FAIL
+from pypy.module.cpyext.state import State
 
 @cpython_api([], rffi.INT_real, error=CANNOT_FAIL)
 def Py_IsInitialized(space):
     return 1
+
+ at cpython_api([], rffi.CCHARP, error=CANNOT_FAIL)
+def Py_GetProgramName(space):
+    """
+    Return the program name set with Py_SetProgramName(), or the default.
+    The returned string points into static storage; the caller should not modify its
+    value."""
+    return space.fromcache(State).get_programname()
+

Modified: pypy/trunk/pypy/module/cpyext/state.py
==============================================================================
--- pypy/trunk/pypy/module/cpyext/state.py	(original)
+++ pypy/trunk/pypy/module/cpyext/state.py	Sat Dec  4 22:02:02 2010
@@ -8,6 +8,7 @@
     def __init__(self, space):
         self.space = space
         self.reset()
+        self.programname = lltype.nullptr(rffi.CCHARP.TO)
 
     def reset(self):
         from pypy.module.cpyext.modsupport import PyMethodDef
@@ -41,3 +42,16 @@
         if always:
             raise OperationError(self.space.w_SystemError, self.space.wrap(
                 "Function returned an error result without setting an exception"))
+
+    def get_programname(self):
+        if not self.programname:
+            space = self.space
+            argv = space.sys.get('argv')
+            if space.int_w(space.len(argv)):
+                argv0 = space.getitem(argv, space.wrap(0))
+                progname = space.str_w(argv0)
+            else:
+                progname = "pypy"
+            self.programname = rffi.str2charp(progname)
+            lltype.render_immortal(self.programname)
+        return self.programname

Modified: pypy/trunk/pypy/module/cpyext/stubs.py
==============================================================================
--- pypy/trunk/pypy/module/cpyext/stubs.py	(original)
+++ pypy/trunk/pypy/module/cpyext/stubs.py	Sat Dec  4 22:02:02 2010
@@ -1422,17 +1422,6 @@
     raise NotImplementedError
 
 @cpython_api([], rffi.CCHARP, error=CANNOT_FAIL)
-def Py_GetProgramName(space, ):
-    """
-    
-    
-    
-    Return the program name set with Py_SetProgramName(), or the default.
-    The returned string points into static storage; the caller should not modify its
-    value."""
-    raise NotImplementedError
-
- at cpython_api([], rffi.CCHARP, error=CANNOT_FAIL)
 def Py_GetPrefix(space, ):
     """Return the prefix for installed platform-independent files. This is derived
     through a number of complicated rules from the program name set with

Modified: pypy/trunk/pypy/module/cpyext/test/test_cpyext.py
==============================================================================
--- pypy/trunk/pypy/module/cpyext/test/test_cpyext.py	(original)
+++ pypy/trunk/pypy/module/cpyext/test/test_cpyext.py	Sat Dec  4 22:02:02 2010
@@ -682,3 +682,19 @@
         assert mod.get_names() == ('cell', 'module', 'property',
                                    'staticmethod',
                                    'builtin_function_or_method')
+
+    def test_get_programname(self):
+        mod = self.import_extension('foo', [
+            ('get_programname', 'METH_NOARGS',
+             '''
+             char* name1 = Py_GetProgramName();
+             char* name2 = Py_GetProgramName();
+             if (name1 != name2)
+                 Py_RETURN_FALSE;
+             return PyString_FromString(name1);
+             '''
+             ),
+            ])
+        p = mod.get_programname()
+        print p
+        assert 'py' in p



More information about the Pypy-commit mailing list