[pypy-commit] cffi default: pycparser is not thread-safe. Fix mostly by Sarvi.

arigo noreply at buildbot.pypy.org
Wed Nov 7 14:36:32 CET 2012


Author: Armin Rigo <arigo at tunes.org>
Branch: 
Changeset: r1032:501b14aca6cd
Date: 2012-11-07 14:36 +0100
http://bitbucket.org/cffi/cffi/changeset/501b14aca6cd/

Log:	pycparser is not thread-safe. Fix mostly by Sarvi.

diff --git a/cffi/cparser.py b/cffi/cparser.py
--- a/cffi/cparser.py
+++ b/cffi/cparser.py
@@ -1,6 +1,15 @@
 
 from . import api, model
-import pycparser.c_parser, weakref, re
+import pycparser.c_parser, weakref, re, sys
+
+try:
+    if sys.version_info < (3,):
+        import thread as _thread
+    else:
+        import _thread
+    lock = _thread.allocate_lock()
+except ImportError:
+    lock = None
 
 _r_comment = re.compile(r"/\*.*?\*/|//.*?$", re.DOTALL | re.MULTILINE)
 _r_define  = re.compile(r"^\s*#\s*define\s+([A-Za-z_][A-Za-z_0-9]*)\s+(.*?)$",
@@ -69,10 +78,15 @@
         csource, macros = _preprocess(csource)
         csourcelines.append(csource)
         csource = '\n'.join(csourcelines)
+        if lock is not None:
+            lock.acquire()     # pycparser is not thread-safe...
         try:
             ast = _get_parser().parse(csource)
         except pycparser.c_parser.ParseError as e:
             self.convert_pycparser_error(e, csource)
+        finally:
+            if lock is not None:
+                lock.release()
         return ast, macros
 
     def convert_pycparser_error(self, e, csource):


More information about the pypy-commit mailing list