[pypy-commit] pypy default: Kill the ctypes (and ctypes_config_cache) version of syslog,

arigo noreply at buildbot.pypy.org
Fri Jun 28 20:55:25 CEST 2013


Author: Armin Rigo <arigo at tunes.org>
Branch: 
Changeset: r65081:f379c0cd7d76
Date: 2013-06-28 20:51 +0200
http://bitbucket.org/pypy/pypy/changeset/f379c0cd7d76/

Log:	Kill the ctypes (and ctypes_config_cache) version of syslog, replace
	it with a cffi version.

diff --git a/lib_pypy/ctypes_config_cache/syslog.ctc.py b/lib_pypy/ctypes_config_cache/syslog.ctc.py
deleted file mode 100644
--- a/lib_pypy/ctypes_config_cache/syslog.ctc.py
+++ /dev/null
@@ -1,75 +0,0 @@
-"""
-'ctypes_configure' source for syslog.py.
-Run this to rebuild _syslog_cache.py.
-"""
-
-from ctypes_configure.configure import (configure,
-    ExternalCompilationInfo, ConstantInteger, DefinedConstantInteger)
-import dumpcache
-
-
-_CONSTANTS = (
-    'LOG_EMERG',
-    'LOG_ALERT',
-    'LOG_CRIT',
-    'LOG_ERR',
-    'LOG_WARNING',
-    'LOG_NOTICE',
-    'LOG_INFO',
-    'LOG_DEBUG',
-
-    'LOG_PID',
-    'LOG_CONS',
-    'LOG_NDELAY',
-
-    'LOG_KERN',
-    'LOG_USER',
-    'LOG_MAIL',
-    'LOG_DAEMON',
-    'LOG_AUTH',
-    'LOG_LPR',
-    'LOG_LOCAL0',
-    'LOG_LOCAL1',
-    'LOG_LOCAL2',
-    'LOG_LOCAL3',
-    'LOG_LOCAL4',
-    'LOG_LOCAL5',
-    'LOG_LOCAL6',
-    'LOG_LOCAL7',
-)
-_OPTIONAL_CONSTANTS = (
-    'LOG_NOWAIT',
-    'LOG_PERROR',
-
-    'LOG_SYSLOG',
-    'LOG_CRON',
-    'LOG_UUCP',
-    'LOG_NEWS',
-)
-
-# Constant aliases if there are not defined
-_ALIAS = (
-    ('LOG_SYSLOG', 'LOG_DAEMON'),
-    ('LOG_CRON', 'LOG_DAEMON'),
-    ('LOG_NEWS', 'LOG_MAIL'),
-    ('LOG_UUCP', 'LOG_MAIL'),
-)
-
-class SyslogConfigure:
-    _compilation_info_ = ExternalCompilationInfo(includes=['sys/syslog.h'])
-for key in _CONSTANTS:
-    setattr(SyslogConfigure, key, ConstantInteger(key))
-for key in _OPTIONAL_CONSTANTS:
-    setattr(SyslogConfigure, key, DefinedConstantInteger(key))
-
-config = configure(SyslogConfigure)
-for key in _OPTIONAL_CONSTANTS:
-    if config[key] is None:
-        del config[key]
-for alias, key in _ALIAS:
-    config.setdefault(alias, config[key])
-
-all_constants = config.keys()
-all_constants.sort()
-config['ALL_CONSTANTS'] = tuple(all_constants)
-dumpcache.dumpcache2('syslog', config)
diff --git a/lib_pypy/syslog.py b/lib_pypy/syslog.py
--- a/lib_pypy/syslog.py
+++ b/lib_pypy/syslog.py
@@ -1,3 +1,4 @@
+# this cffi version was rewritten based on the
 # ctypes implementation: Victor Stinner, 2008-05-08
 """
 This module provides an interface to the Unix syslog library routines.
@@ -9,34 +10,84 @@
 if sys.platform == 'win32':
     raise ImportError("No syslog on Windows")
 
-# load the platform-specific cache made by running syslog.ctc.py
-from ctypes_config_cache._syslog_cache import *
-
-from ctypes_support import standard_c_lib as libc
-from ctypes import c_int, c_char_p
+from cffi import FFI
 
 try: from __pypy__ import builtinify
 except ImportError: builtinify = lambda f: f
 
+ffi = FFI()
 
-# Real prototype is:
-# void syslog(int priority, const char *format, ...);
-# But we also need format ("%s") and one format argument (message)
-_syslog = libc.syslog
-_syslog.argtypes = (c_int, c_char_p, c_char_p)
-_syslog.restype = None
+ffi.cdef("""
+/* mandatory constants */
+#define LOG_EMERG ...
+#define LOG_ALERT ...
+#define LOG_CRIT ...
+#define LOG_ERR ...
+#define LOG_WARNING ...
+#define LOG_NOTICE ...
+#define LOG_INFO ...
+#define LOG_DEBUG ...
 
-_openlog = libc.openlog
-_openlog.argtypes = (c_char_p, c_int, c_int)
-_openlog.restype = None
+#define LOG_PID ...
+#define LOG_CONS ...
+#define LOG_NDELAY ...
 
-_closelog = libc.closelog
-_closelog.argtypes = None
-_closelog.restype = None
+#define LOG_KERN ...
+#define LOG_USER ...
+#define LOG_MAIL ...
+#define LOG_DAEMON ...
+#define LOG_AUTH ...
+#define LOG_LPR ...
+#define LOG_LOCAL0 ...
+#define LOG_LOCAL1 ...
+#define LOG_LOCAL2 ...
+#define LOG_LOCAL3 ...
+#define LOG_LOCAL4 ...
+#define LOG_LOCAL5 ...
+#define LOG_LOCAL6 ...
+#define LOG_LOCAL7 ...
 
-_setlogmask = libc.setlogmask
-_setlogmask.argtypes = (c_int,)
-_setlogmask.restype = c_int
+/* optional constants, gets defined to -919919 if missing */
+#define LOG_NOWAIT ...
+#define LOG_PERROR ...
+
+/* aliased constants, gets defined as some other constant if missing */
+#define LOG_SYSLOG ...
+#define LOG_CRON ...
+#define LOG_UUCP ...
+#define LOG_NEWS ...
+
+/* functions */
+void openlog(const char *ident, int option, int facility);
+void syslog(int priority, const char *format, const char *string);
+// NB. the signature of syslog() is specialized to the only case we use
+void closelog(void);
+int setlogmask(int mask);
+""")
+
+lib = ffi.verify("""
+#include <syslog.h>
+
+#ifndef LOG_NOWAIT
+#define LOG_NOWAIT -919919
+#endif
+#ifndef LOG_PERROR
+#define LOG_PERROR -919919
+#endif
+#ifndef LOG_SYSLOG
+#define LOG_SYSLOG LOG_DAEMON
+#endif
+#ifndef LOG_CRON
+#define LOG_CRON LOG_DAEMON
+#endif
+#ifndef LOG_UUCP
+#define LOG_UUCP LOG_MAIL
+#endif
+#ifndef LOG_NEWS
+#define LOG_NEWS LOG_MAIL
+#endif
+""")
+
 
 _S_log_open = False
 _S_ident_o = None
@@ -52,12 +103,17 @@
     return None
 
 @builtinify
-def openlog(ident=None, logoption=0, facility=LOG_USER):
+def openlog(ident=None, logoption=0, facility=lib.LOG_USER):
     global _S_ident_o, _S_log_open
     if ident is None:
         ident = _get_argv()
-    _S_ident_o = c_char_p(ident)    # keepalive
-    _openlog(_S_ident_o, logoption, facility)
+    if ident is None:
+        _S_ident_o = ffi.NULL
+    elif isinstance(ident, str):
+        _S_ident_o = ffi.new("char[]", ident)    # keepalive
+    else:
+        raise TypeError("'ident' must be a string or None")
+    lib.openlog(_S_ident_o, logoption, facility)
     _S_log_open = True
 
 @builtinify
@@ -69,19 +125,19 @@
     # if log is not opened, open it now
     if not _S_log_open:
         openlog()
-    _syslog(priority, "%s", message)
+    lib.syslog(priority, "%s", message)
 
 @builtinify
 def closelog():
     global _S_log_open, S_ident_o
     if _S_log_open:
-        _closelog()
+        lib.closelog()
         _S_log_open = False
         _S_ident_o = None
 
 @builtinify
 def setlogmask(mask):
-    return _setlogmask(mask)
+    return lib.setlogmask(mask)
 
 @builtinify
 def LOG_MASK(pri):
@@ -91,8 +147,15 @@
 def LOG_UPTO(pri):
     return (1 << (pri + 1)) - 1
 
-__all__ = ALL_CONSTANTS + (
+__all__ = []
+
+for name in sorted(lib.__dict__):
+    if name.startswith('LOG_'):
+        value = getattr(lib, name)
+        if value != -919919:
+            globals()[name] = value
+            __all__.append(name)
+
+__all__ = tuple(__all__) + (
     'openlog', 'syslog', 'closelog', 'setlogmask',
     'LOG_MASK', 'LOG_UPTO')
-
-del ALL_CONSTANTS
diff --git a/pypy/module/test_lib_pypy/test_syslog.py b/pypy/module/test_lib_pypy/test_syslog.py
--- a/pypy/module/test_lib_pypy/test_syslog.py
+++ b/pypy/module/test_lib_pypy/test_syslog.py
@@ -7,9 +7,5 @@
 
 # XXX very minimal test
 
-from lib_pypy.ctypes_config_cache import rebuild
-rebuild.rebuild_one('syslog.ctc.py')
-
-
 def test_syslog():
     assert hasattr(syslog, 'LOG_ALERT')


More information about the pypy-commit mailing list