Andriy Kornatskyy avatar Andriy Kornatskyy committed c6ecaeb

Conditionally compile boost extension.

Comments (0)

Files changed (2)

 #!/usr/bin/env python
 
 import os
+import platform
+import sys
+
+extra = {}
+can_build_ext = getattr(platform, 'python_implementation', lambda: None
+                   )() != 'PyPy' and 'java' not in sys.platform
+sources = [os.path.join('src', 'wheezy', 'html', 'boost.c')]
 
 try:
-    from setuptools import setup
-except:
-    from distutils.core import setup
+    from setuptools import setup, Extension, Feature
+    from setuptools.command.build_ext import build_ext
+except ImportError:
+    from distutils.core import setup, Extension
+    from distutils.command.build_ext import build_ext
+    if can_build_ext:
+        extra['ext_modules'] = [Extension('wheezy.html.boost', sources)]
+else:
+    if can_build_ext:
+        extra['features'] = {
+            'boost': Feature(
+                'code optimizations',
+                standard=True,
+                ext_modules=[Extension('wheezy.html.boost', sources)])
+        }
+
+
+if can_build_ext:
+
+    class build_ext_optional(build_ext):
+
+        def run(self):
+            from distutils.errors import DistutilsPlatformError
+            try:
+                build_ext.run(self)
+            except DistutilsPlatformError, e:
+                self.warn(e)
+
+        def build_extension(self, ext):
+            from distutils.errors import CCompilerError
+            from distutils.errors import DistutilsExecError
+            try:
+                build_ext.build_extension(self, ext)
+            except (CCompilerError, DistutilsExecError), e:
+                self.warn(e)
+
+        def warn(self, e):
+            print(' WARNING '.center(44, '*'))
+            print('An optional extension could not be compiled.')
+            print(e)
+
+    extra['cmdclass'] = {'build_ext': build_ext_optional}
+
 
 README = open(os.path.join(os.path.dirname(__file__), 'README')).read()
 
         ]
     },
 
-    platforms='any'
+    platforms='any',
+    **extra
 )

src/wheezy/html/utils.py

 from wheezy.html.comp import str_type
 
 
-def html_escape(s):
-    """ Escapes a string so it is valid within HTML.
+try:
+    from wheezy.html.boost import escape_html
+    html_escape = escape_html
+except ImportError:
+    def escape_html(s):
+        """ Escapes a string so it is valid within HTML.
 
-        >>> html_escape('abc')
-        'abc'
-        >>> html_escape('&<>"\\'')
-        "&amp;&lt;&gt;&quot;\'"
-    """
-    return s.replace('&', '&amp;').replace('<', '&lt;').replace(
-        '>', '&gt;').replace('"', '&quot;')
+            >>> html_escape('abc')
+            'abc'
+            >>> escape_html('&<>"\\'')
+            "&amp;&lt;&gt;&quot;\'"
+        """
+        return s.replace('&', '&amp;').replace('<', '&lt;').replace(
+            '>', '&gt;').replace('"', '&quot;')
+    html_escape = escape_html
 
 
 html_id = lambda name: name.replace('_', '-')
             def my_formatter(value, format_spec):
                 return value_formatted
 
-        >>> format_value(date(2012, 2, 6), '%m-%d-%y')
+        >>> str(format_value(date(2012, 2, 6), '%m-%d-%y'))
         '02-06-12'
         >>> format_value(date(2012, 2, 6),
         ...         format_provider=lambda value, ignore:
 
 
 str_format_provider = lambda value, format_spec: str_type(value)
+EMPTY_STR = str_type('')
 
 
 def date_format_provider(value, format_spec=None):
     """ Default format provider for ``datetime.date``.
 
-        >>> date_format_provider(date.min)
+        >>> str(date_format_provider(date.min))
         ''
-        >>> date_format_provider(date(2012, 2, 6))
+        >>> str(date_format_provider(date(2012, 2, 6)))
         '2012/02/06'
     """
     if date.min == value:
-        return ''
-    return value.strftime(str(format_spec or '%Y/%m/%d'))
+        return EMPTY_STR
+    return str_type(value.strftime(format_spec or '%Y/%m/%d'))
 
 
 def datetime_format_provider(value, format_spec=None):
     """ Default format provider for ``datetime.datetime``.
 
-        >>> datetime_format_provider(datetime.min)
+        >>> str(datetime_format_provider(datetime.min))
         ''
-        >>> datetime_format_provider(datetime(2012, 2, 6, 15, 17))
+        >>> str(datetime_format_provider(datetime(2012, 2, 6, 15, 17)))
         '2012/02/06 15:17'
     """
     if datetime.min == value:
-        return ''
-    return value.strftime(str(format_spec or '%Y/%m/%d %H:%M'))
+        return EMPTY_STR
+    return str_type(value.strftime(format_spec or '%Y/%m/%d %H:%M'))
 
 
 format_providers = {
     'bool': str_format_provider,
     'float': str_format_provider,
     'date': date_format_provider,
-    'time': lambda value, format_spec: value.strftime(
-        str(format_spec or '%H:%M')),
+    'time': lambda value, format_spec: value.strftime(format_spec or '%H:%M'),
     'datetime': datetime_format_provider,
-    'NoneType': lambda value, format_spec: ''
+    'NoneType': lambda value, format_spec: EMPTY_STR
 }
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.