[pypy-commit] pypy missing-os-functions: Use platform.configure() to detect the presence of most posix functions.

amauryfa noreply at buildbot.pypy.org
Wed Mar 6 22:29:00 CET 2013


Author: Amaury Forgeot d'Arc <amauryfa at gmail.com>
Branch: missing-os-functions
Changeset: r62141:873c7be6cbfe
Date: 2013-03-06 22:28 +0100
http://bitbucket.org/pypy/pypy/changeset/873c7be6cbfe/

Log:	Use platform.configure() to detect the presence of most posix
	functions.

	The posix module now relies on these HAVE_XXX instead of the host
	Python running the translation.

diff --git a/pypy/module/posix/__init__.py b/pypy/module/posix/__init__.py
--- a/pypy/module/posix/__init__.py
+++ b/pypy/module/posix/__init__.py
@@ -73,6 +73,7 @@
         'access'    : 'interp_posix.access',
         'times'     : 'interp_posix.times',
         'system'    : 'interp_posix.system',
+        'getpid'    : 'interp_posix.getpid',
         'unlink'    : 'interp_posix.unlink',
         'remove'    : 'interp_posix.remove',
         'getcwd'    : 'interp_posix.getcwd',
@@ -84,7 +85,6 @@
         'listdir'   : 'interp_posix.listdir',
         'strerror'  : 'interp_posix.strerror',
         'pipe'      : 'interp_posix.pipe',
-        'chmod'     : 'interp_posix.chmod',
         'rename'    : 'interp_posix.rename',
         'umask'     : 'interp_posix.umask',
         '_exit'     : 'interp_posix._exit',
@@ -95,25 +95,33 @@
         'urandom'   : 'interp_posix.urandom',
         }
 
+    # XXX Missing functions: chflags lchmod lchflags ctermid fork1
+    #                        plock setgroups initgroups tcgetpgrp
+    #                        tcsetpgrp confstr pathconf
+
     for name in '''
-            wait wait3 wait4 chown lchown ftruncate
-            fsync fdatasync fchdir putenv unsetenv killpg getpid
-            link symlink readlink
-            fork openpty forkpty waitpid execv execve uname sysconf fpathconf
-            ttyname getloadavg makedev major minor mkfifo mknod nice getlogin
+            ttyname chmod fchmod chown lchown fchown chroot link symlink readlink
+            ftruncate getloadavg nice uname execv execve fork spawnv spawnve
+            putenv unsetenv fchdir fsync fdatasync mknod
+            openpty forkpty mkfifo getlogin sysconf fpathconf
             getsid getuid geteuid getgid getegid getpgrp getpgid
             setsid setuid seteuid setgid setegid setpgrp setpgid
-            getppid getgroups setreuid setregid chroot
-            _getfullpathname
+            getppid getgroups setreuid setregid
+            wait wait3 wait4 killpg waitpid
             '''.split():
-        if hasattr(posix, name):
+        symbol = 'HAVE_' + name.upper()
+        if getattr(rposix, symbol):
             interpleveldefs[name] = 'interp_posix.%s' % (name,)
 
-    for name in '''fchmod fchown
-                '''.split():
-        symbol = 'HAS_' + name.upper()
-        if getattr(rposix, symbol):
-            interpleveldefs[name] = 'interp_posix.%s' % (name,)
+    if rposix.HAVE_DEVICE_MACROS:
+        interpleveldefs['major']   = 'interp_posix.major'
+        interpleveldefs['minor']   = 'interp_posix.minor'
+        interpleveldefs['makedev'] = 'interp_posix.makedev'
+
+    if os.name == 'nt':
+        interpleveldefs['_getfullpathname'] = 'interp_posix._getfullpathname'
+        # On Windows, _cwait() can be used to emulate waitpid
+        interpleveldefs['waitpid'] = 'interp_posix.waitpid'
 
     for constant in '''
             F_OK R_OK W_OK X_OK NGROUPS_MAX TMP_MAX
@@ -138,6 +146,7 @@
         interpleveldefs['pathconf_names'] = 'space.wrap(os.pathconf_names)'
 
     # Macros for process exit statuses: WIFEXITED &co
+    # XXX HAVE_SYS_WAIT_H
     for name in RegisterOs.w_star:
         if hasattr(posix, name):
             interpleveldefs[name] = 'interp_posix.' + name
diff --git a/pypy/module/posix/test/test_posix2.py b/pypy/module/posix/test/test_posix2.py
--- a/pypy/module/posix/test/test_posix2.py
+++ b/pypy/module/posix/test/test_posix2.py
@@ -63,25 +63,26 @@
         except UnicodeDecodeError:
             # filesystem encoding is not good enough
             cls.w_unicode_dir = space.w_None
-        if hasattr(os, 'getuid'):
+        if rposix.HAVE_GETUID:
             cls.w_getuid = space.wrap(os.getuid())
+        if rposix.HAVE_GETEUID:
             cls.w_geteuid = space.wrap(os.geteuid())
-        if hasattr(os, 'getgid'):
+        if rposix.HAVE_GETGID:
             cls.w_getgid = space.wrap(os.getgid())
-        if hasattr(os, 'getgroups'):
+        if rposix.HAVE_GETGROUPS:
             cls.w_getgroups = space.newlist([space.wrap(e) for e in os.getgroups()])
-        if hasattr(os, 'getpgid'):
+        if rposix.HAVE_GETPGID:
             cls.w_getpgid = space.wrap(os.getpgid(os.getpid()))
-        if hasattr(os, 'getsid'):
+        if rposix.HAVE_GETSID:
             cls.w_getsid0 = space.wrap(os.getsid(0))
-        if hasattr(os, 'sysconf'):
+        if rposix.HAVE_SYSCONF:
             sysconf_name = os.sysconf_names.keys()[0]
             cls.w_sysconf_name = space.wrap(sysconf_name)
             cls.w_sysconf_value = space.wrap(os.sysconf_names[sysconf_name])
             cls.w_sysconf_result = space.wrap(os.sysconf(sysconf_name))
         cls.w_SIGABRT = space.wrap(signal.SIGABRT)
         cls.w_python = space.wrap(sys.executable)
-        if hasattr(os, 'major'):
+        if rposix.HAVE_DEVICE_MACROS:
             cls.w_expected_major_12345 = space.wrap(os.major(12345))
             cls.w_expected_minor_12345 = space.wrap(os.minor(12345))
         cls.w_udir = space.wrap(str(udir))
@@ -101,6 +102,7 @@
         if sys.platform.startswith('win32'):
             for name in '''_getfullpathname O_TEXT O_BINARY'''.split():
                 assert name in dir(self.posix)
+        assert 'kill' in dir(self.posix)
 
     def test_some_posix_basic_operation(self):
         path = self.path
@@ -583,7 +585,7 @@
             assert os.WIFSIGNALED(0) == False
             assert os.WIFSIGNALED(1) == True
 
-    if hasattr(os, 'uname'):
+    if rposix.HAVE_UNAME:
         def test_os_uname(self):
             os = self.posix
             res = os.uname()
@@ -592,47 +594,47 @@
                 assert isinstance(i, str)
             assert isinstance(res, tuple)
 
-    if hasattr(os, 'getuid'):
+    if rposix.HAVE_GETUID:
         def test_os_getuid(self):
             os = self.posix
             assert os.getuid() == self.getuid
             assert os.geteuid() == self.geteuid
 
-    if hasattr(os, 'setuid'):
+    if rposix.HAVE_SETUID:
         def test_os_setuid_error(self):
             os = self.posix
             raises(OverflowError, os.setuid, -2**31-1)
             raises(OverflowError, os.setuid, 2**32)
 
-    if hasattr(os, 'getgid'):
+    if rposix.HAVE_GETGID:
         def test_os_getgid(self):
             os = self.posix
             assert os.getgid() == self.getgid
 
-    if hasattr(os, 'getgroups'):
+    if rposix.HAVE_GETGROUPS:
         def test_os_getgroups(self):
             os = self.posix
             assert os.getgroups() == self.getgroups
 
-    if hasattr(os, 'getpgid'):
+    if rposix.HAVE_GETPGID:
         def test_os_getpgid(self):
             os = self.posix
             assert os.getpgid(os.getpid()) == self.getpgid
             raises(OSError, os.getpgid, 1234567)
 
-    if hasattr(os, 'setgid'):
+    if rposix.HAVE_SETGID:
         def test_os_setgid_error(self):
             os = self.posix
             raises(OverflowError, os.setgid, -2**31-1)
             raises(OverflowError, os.setgid, 2**32)
 
-    if hasattr(os, 'getsid'):
+    if rposix.HAVE_GETSID:
         def test_os_getsid(self):
             os = self.posix
             assert os.getsid(0) == self.getsid0
             raises(OSError, os.getsid, -100000)
 
-    if hasattr(os, 'sysconf'):
+    if rposix.HAVE_SYSCONF:
         def test_os_sysconf(self):
             os = self.posix
             assert os.sysconf(self.sysconf_value) == self.sysconf_result
@@ -643,14 +645,14 @@
             os = self.posix
             raises(ValueError, os.sysconf, "!@#$%!#$!@#")
 
-    if hasattr(os, 'fpathconf'):
+    if rposix.HAVE_FPATHCONF:
         def test_os_fpathconf(self):
             os = self.posix
             assert os.fpathconf(1, "PC_PIPE_BUF") >= 128
             raises(OSError, os.fpathconf, -1, "PC_PIPE_BUF")
             raises(ValueError, os.fpathconf, 1, "##")
 
-    if hasattr(os, 'wait'):
+    if rposix.HAVE_WAIT:
         def test_os_wait(self):
             os = self.posix
             exit_status = 0x33
@@ -672,7 +674,7 @@
                 assert os.WIFEXITED(status)
                 assert os.WEXITSTATUS(status) == exit_status
 
-    if hasattr(os, 'getloadavg'):
+    if rposix.HAVE_GETLOADAVG:
         def test_os_getloadavg(self):
             os = self.posix
             l0, l1, l2 = os.getloadavg()
@@ -680,7 +682,7 @@
             assert type(l1) is float and l0 >= 0.0
             assert type(l2) is float and l0 >= 0.0
 
-    if hasattr(os, 'major'):
+    if rposix.HAVE_DEVICE_MACROS:
         def test_major_minor(self):
             os = self.posix
             assert os.major(12345) == self.expected_major_12345
@@ -688,7 +690,7 @@
             assert os.makedev(self.expected_major_12345,
                               self.expected_minor_12345) == 12345
 
-    if hasattr(os, 'fsync'):
+    if rposix.HAVE_FSYNC:
         def test_fsync(self):
             os = self.posix
             f = open(self.path2, "w")
@@ -706,7 +708,7 @@
                 pass
             raises(ValueError, os.fsync, -1)
 
-    if hasattr(os, 'fdatasync'):
+    if rposix.HAVE_FDATASYNC:
         def test_fdatasync(self):
             os = self.posix
             f = open(self.path2, "w")
@@ -722,7 +724,7 @@
                 pass
             raises(ValueError, os.fdatasync, -1)
 
-    if hasattr(os, 'fchdir'):
+    if rposix.HAVE_FCHDIR:
         def test_fchdir(self):
             os = self.posix
             localdir = os.getcwd()
@@ -801,8 +803,6 @@
 
     def test_closerange(self):
         os = self.posix
-        if not hasattr(os, 'closerange'):
-            skip("missing os.closerange()")
         fds = [os.open(self.path + str(i), os.O_CREAT|os.O_WRONLY, 0777)
                for i in range(15)]
         fds.sort()
@@ -816,7 +816,7 @@
         for fd in range(start, stop):
             raises(OSError, os.fstat, fd)   # should have been closed
 
-    if hasattr(os, 'chown'):
+    if rposix.HAVE_CHOWN:
         def test_chown(self):
             os = self.posix
             os.unlink(self.path)
@@ -826,7 +826,7 @@
             f.close()
             os.chown(self.path, os.getuid(), os.getgid())
 
-    if hasattr(os, 'lchown'):
+    if rposix.HAVE_LCHOWN:
         def test_lchown(self):
             os = self.posix
             os.unlink(self.path)
@@ -834,14 +834,14 @@
             os.symlink('foobar', self.path)
             os.lchown(self.path, os.getuid(), os.getgid())
 
-    if rposix.HAS_FCHOWN:
+    if rposix.HAVE_FCHOWN:
         def test_fchown(self):
             os = self.posix
             f = open(self.path, "w")
             os.fchown(f.fileno(), os.getuid(), os.getgid())
             f.close()
 
-    if hasattr(os, 'chmod'):
+    if rposix.HAVE_CHMOD:
         def test_chmod(self):
             import sys
             os = self.posix
@@ -857,7 +857,7 @@
                 os.chmod(self.path, 0200)
                 assert (os.stat(self.path).st_mode & 0777) == 0200
 
-    if rposix.HAS_FCHMOD:
+    if rposix.HAVE_FCHMOD:
         def test_fchmod(self):
             os = self.posix
             f = open(self.path, "w")
@@ -866,7 +866,7 @@
             f.close()
             assert (os.stat(self.path).st_mode & 0777) == 0200
 
-    if hasattr(os, 'mkfifo'):
+    if rposix.HAVE_MKFIFO:
         def test_mkfifo(self):
             os = self.posix
             os.mkfifo(self.path2 + 'test_mkfifo', 0666)
@@ -874,7 +874,7 @@
             import stat
             assert stat.S_ISFIFO(st.st_mode)
 
-    if hasattr(os, 'mknod'):
+    if rposix.HAVE_MKNOD:
         def test_mknod(self):
             import stat
             os = self.posix
@@ -907,7 +907,7 @@
                     assert stat.S_ISCHR(st.st_mode)
                     assert st.st_rdev == 0x105
 
-    if hasattr(os, 'nice') and hasattr(os, 'fork') and hasattr(os, 'waitpid'):
+    if rposix.HAVE_NICE and rposix.HAVE_FORK and rposix.HAVE_WAITPID:
         def test_nice(self):
             os = self.posix
             myprio = os.nice(0)
@@ -922,7 +922,7 @@
             assert os.WIFEXITED(status1)
             assert os.WEXITSTATUS(status1) == myprio + 3
 
-    if hasattr(os, 'symlink'):
+    if rposix.HAVE_SYMLINK:
         def test_symlink(self):
             posix = self.posix
             unicode_dir = self.unicode_dir
@@ -1005,10 +1005,6 @@
             # not to some code inside app_posix.py
             assert w[-1].lineno == f_tmpnam_warning.func_code.co_firstlineno
 
-    def test_has_kill(self):
-        import os
-        assert hasattr(os, 'kill')
-
     def test_pipe_flush(self):
         os = self.posix
         ffd, gfd = os.pipe()
diff --git a/rpython/rlib/rposix.py b/rpython/rlib/rposix.py
--- a/rpython/rlib/rposix.py
+++ b/rpython/rlib/rposix.py
@@ -84,7 +84,8 @@
 else:
     separate_module_sources = []
     export_symbols = []
-    includes=['errno.h', 'stdio.h', 'unistd.h', 'sys/stat.h']
+    includes=['errno.h', 'stdio.h', 'stdlib.h', 'unistd.h', 'sys/stat.h',
+              'signal.h', 'pty.h', 'sys/utsname.h', 'sys/wait.h']
 rposix_eci = ExternalCompilationInfo(
     includes=includes,
     separate_module_sources=separate_module_sources,
@@ -94,8 +95,20 @@
 class CConfig:
     _compilation_info_ = rposix_eci
 
-    HAS_FCHMOD = rffi_platform.Has("fchmod")
-    HAS_FCHOWN = rffi_platform.Has("fchown")
+    HAVE_DEVICE_MACROS = rffi_platform.Has("makedev(major(0),minor(0))")
+
+for name in '''
+        ttyname chmod fchmod chown lchown fchown chroot link symlink readlink
+        ftruncate getloadavg nice uname execv execve fork spawnv spawnve
+        putenv unsetenv fchdir fsync fdatasync mknod
+        openpty forkpty mkfifo getlogin sysconf fpathconf
+        getsid getuid geteuid getgid getegid getpgrp getpgid
+        setsid setuid seteuid setgid setegid setpgrp setpgid
+        getppid getgroups setreuid setregid
+        wait wait3 wait4 killpg waitpid
+        '''.split():
+    symbol = 'HAVE_' + name.upper()
+    setattr(CConfig, symbol, rffi_platform.Has(name))
 
 globals().update(rffi_platform.configure(CConfig))
 


More information about the pypy-commit mailing list