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
This commit is contained in:
Cosimo Lupo 2023-03-02 17:23:49 +00:00
parent e7c6d2aa8b
commit 0df4997661
No known key found for this signature in database
GPG Key ID: DF65A8A5A119C9A8
6 changed files with 24 additions and 44 deletions

View File

@ -17,10 +17,14 @@
try: try:
import cython import cython
except ImportError:
COMPILED = cython.compiled
except (AttributeError, ImportError):
# if cython not installed, use mock module with no-op decorators and types # if cython not installed, use mock module with no-op decorators and types
from fontTools.misc import cython from fontTools.misc import cython
COMPILED = False
import math import math
from .errors import Error as Cu2QuError, ApproxNotFoundError from .errors import Error as Cu2QuError, ApproxNotFoundError
@ -33,14 +37,6 @@ MAX_N = 100
NAN = float("NaN") NAN = float("NaN")
if cython.compiled:
# Yep, I'm compiled.
COMPILED = True
else:
# Just a lowly interpreted script.
COMPILED = False
@cython.cfunc @cython.cfunc
@cython.inline @cython.inline
@cython.returns(cython.double) @cython.returns(cython.double)

View File

@ -9,10 +9,14 @@ from collections import namedtuple
try: try:
import cython import cython
except ImportError:
COMPILED = cython.compiled
except (AttributeError, ImportError):
# if cython not installed, use mock module with no-op decorators and types # if cython not installed, use mock module with no-op decorators and types
from fontTools.misc import cython from fontTools.misc import cython
COMPILED = False
Intersection = namedtuple("Intersection", ["pt", "t1", "t2"]) Intersection = namedtuple("Intersection", ["pt", "t1", "t2"])
@ -48,13 +52,6 @@ __all__ = [
"segmentSegmentIntersections", "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): def calcCubicArcLength(pt1, pt2, pt3, pt4, tolerance=0.005):
"""Calculates the arc length for a cubic Bezier segment. """Calculates the arc length for a cubic Bezier segment.

View File

@ -123,15 +123,12 @@ def printGreenPen(penName, funcs, file=sys.stdout, docstring=None):
"""from fontTools.pens.basePen import BasePen, OpenContourError """from fontTools.pens.basePen import BasePen, OpenContourError
try: try:
import cython import cython
except ImportError:
COMPILED = cython.compiled
except (AttributeError, ImportError):
# if cython not installed, use mock module with no-op decorators and types # if cython not installed, use mock module with no-op decorators and types
from fontTools.misc import cython from fontTools.misc import cython
if cython.compiled:
# Yep, I'm compiled.
COMPILED = True
else:
# Just a lowly interpreted script.
COMPILED = False COMPILED = False

View File

@ -2,15 +2,12 @@ from fontTools.pens.basePen import BasePen, OpenContourError
try: try:
import cython import cython
except ImportError:
COMPILED = cython.compiled
except (AttributeError, ImportError):
# if cython not installed, use mock module with no-op decorators and types # if cython not installed, use mock module with no-op decorators and types
from fontTools.misc import cython from fontTools.misc import cython
if cython.compiled:
# Yep, I'm compiled.
COMPILED = True
else:
# Just a lowly interpreted script.
COMPILED = False COMPILED = False

View File

@ -18,10 +18,14 @@
try: try:
import cython import cython
except ImportError:
COMPILED = cython.compiled
except (AttributeError, ImportError):
# if cython not installed, use mock module with no-op decorators and types # if cython not installed, use mock module with no-op decorators and types
from fontTools.misc import cython from fontTools.misc import cython
COMPILED = False
from fontTools.misc.bezierTools import splitCubicAtTC from fontTools.misc.bezierTools import splitCubicAtTC
from collections import namedtuple from collections import namedtuple
import math import math
@ -35,14 +39,6 @@ from typing import (
__all__ = ["quadratic_to_curves"] __all__ = ["quadratic_to_curves"]
if cython.compiled:
# Yep, I'm compiled.
COMPILED = True
else:
# Just a lowly interpreted script.
COMPILED = False
# Copied from cu2qu # Copied from cu2qu
@cython.cfunc @cython.cfunc
@cython.returns(cython.int) @cython.returns(cython.int)

View File

@ -7,15 +7,12 @@ from numbers import Integral, Real
try: try:
import cython import cython
except ImportError:
COMPILED = cython.compiled
except (AttributeError, ImportError):
# if cython not installed, use mock module with no-op decorators and types # if cython not installed, use mock module with no-op decorators and types
from fontTools.misc import cython from fontTools.misc import cython
if cython.compiled:
# Yep, I'm compiled.
COMPILED = True
else:
# Just a lowly interpreted script.
COMPILED = False COMPILED = False