From 0df4997661746ed673952dadde268fa448a81271 Mon Sep 17 00:00:00 2001 From: Cosimo Lupo Date: Thu, 2 Mar 2023 17:23:49 +0000 Subject: [PATCH] prevent cython.compiled raise AttributeError if cython not properly installed It's possible sometimes that 'import cython' does not fail but then 'cython.compiled' raises AttributeError. It actually happened in our internal production environment... Similar issue to https://github.com/pydantic/pydantic/pull/573 and https://github.com/ipython/ipython/issues/13294 --- Lib/fontTools/cu2qu/cu2qu.py | 14 +++++--------- Lib/fontTools/misc/bezierTools.py | 13 +++++-------- Lib/fontTools/misc/symfont.py | 9 +++------ Lib/fontTools/pens/momentsPen.py | 9 +++------ Lib/fontTools/qu2cu/qu2cu.py | 14 +++++--------- Lib/fontTools/varLib/iup.py | 9 +++------ 6 files changed, 24 insertions(+), 44 deletions(-) diff --git a/Lib/fontTools/cu2qu/cu2qu.py b/Lib/fontTools/cu2qu/cu2qu.py index f99053b26..0aaaf653b 100644 --- a/Lib/fontTools/cu2qu/cu2qu.py +++ b/Lib/fontTools/cu2qu/cu2qu.py @@ -17,10 +17,14 @@ try: import cython -except ImportError: + + COMPILED = cython.compiled +except (AttributeError, ImportError): # if cython not installed, use mock module with no-op decorators and types from fontTools.misc import cython + COMPILED = False + import math from .errors import Error as Cu2QuError, ApproxNotFoundError @@ -33,14 +37,6 @@ MAX_N = 100 NAN = float("NaN") -if cython.compiled: - # Yep, I'm compiled. - COMPILED = True -else: - # Just a lowly interpreted script. - COMPILED = False - - @cython.cfunc @cython.inline @cython.returns(cython.double) diff --git a/Lib/fontTools/misc/bezierTools.py b/Lib/fontTools/misc/bezierTools.py index bdc3005c5..141279255 100644 --- a/Lib/fontTools/misc/bezierTools.py +++ b/Lib/fontTools/misc/bezierTools.py @@ -9,10 +9,14 @@ from collections import namedtuple try: import cython -except ImportError: + + COMPILED = cython.compiled +except (AttributeError, ImportError): # if cython not installed, use mock module with no-op decorators and types from fontTools.misc import cython + COMPILED = False + Intersection = namedtuple("Intersection", ["pt", "t1", "t2"]) @@ -48,13 +52,6 @@ __all__ = [ "segmentSegmentIntersections", ] -if cython.compiled: - # Yep, I'm compiled. - COMPILED = True -else: - # Just a lowly interpreted script. - COMPILED = False - def calcCubicArcLength(pt1, pt2, pt3, pt4, tolerance=0.005): """Calculates the arc length for a cubic Bezier segment. diff --git a/Lib/fontTools/misc/symfont.py b/Lib/fontTools/misc/symfont.py index c9e0a10c0..0bd69a386 100644 --- a/Lib/fontTools/misc/symfont.py +++ b/Lib/fontTools/misc/symfont.py @@ -123,15 +123,12 @@ def printGreenPen(penName, funcs, file=sys.stdout, docstring=None): """from fontTools.pens.basePen import BasePen, OpenContourError try: import cython -except ImportError: + + COMPILED = cython.compiled +except (AttributeError, ImportError): # if cython not installed, use mock module with no-op decorators and types from fontTools.misc import cython -if cython.compiled: - # Yep, I'm compiled. - COMPILED = True -else: - # Just a lowly interpreted script. COMPILED = False diff --git a/Lib/fontTools/pens/momentsPen.py b/Lib/fontTools/pens/momentsPen.py index df17032dd..dab0d10e2 100644 --- a/Lib/fontTools/pens/momentsPen.py +++ b/Lib/fontTools/pens/momentsPen.py @@ -2,15 +2,12 @@ from fontTools.pens.basePen import BasePen, OpenContourError try: import cython -except ImportError: + + COMPILED = cython.compiled +except (AttributeError, ImportError): # if cython not installed, use mock module with no-op decorators and types from fontTools.misc import cython -if cython.compiled: - # Yep, I'm compiled. - COMPILED = True -else: - # Just a lowly interpreted script. COMPILED = False diff --git a/Lib/fontTools/qu2cu/qu2cu.py b/Lib/fontTools/qu2cu/qu2cu.py index 141b00bd6..23f07c76c 100644 --- a/Lib/fontTools/qu2cu/qu2cu.py +++ b/Lib/fontTools/qu2cu/qu2cu.py @@ -18,10 +18,14 @@ try: import cython -except ImportError: + + COMPILED = cython.compiled +except (AttributeError, ImportError): # if cython not installed, use mock module with no-op decorators and types from fontTools.misc import cython + COMPILED = False + from fontTools.misc.bezierTools import splitCubicAtTC from collections import namedtuple import math @@ -35,14 +39,6 @@ from typing import ( __all__ = ["quadratic_to_curves"] -if cython.compiled: - # Yep, I'm compiled. - COMPILED = True -else: - # Just a lowly interpreted script. - COMPILED = False - - # Copied from cu2qu @cython.cfunc @cython.returns(cython.int) diff --git a/Lib/fontTools/varLib/iup.py b/Lib/fontTools/varLib/iup.py index 27f0ee110..84077eb1e 100644 --- a/Lib/fontTools/varLib/iup.py +++ b/Lib/fontTools/varLib/iup.py @@ -7,15 +7,12 @@ from numbers import Integral, Real try: import cython -except ImportError: + + COMPILED = cython.compiled +except (AttributeError, ImportError): # if cython not installed, use mock module with no-op decorators and types from fontTools.misc import cython -if cython.compiled: - # Yep, I'm compiled. - COMPILED = True -else: - # Just a lowly interpreted script. COMPILED = False