From 7a1cccbc4e098762fb0a6303cf5193b94a503985 Mon Sep 17 00:00:00 2001 From: "James R. Barlow" Date: Sun, 3 Jan 2021 01:51:57 -0800 Subject: [PATCH] Fallback to LeptonicaErrorTrap_Redirect if ffi.callback fails Might fix issue #709, Apple silicon support. --- src/ocrmypdf/leptonica.py | 33 ++++++++++++++++++--------------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/src/ocrmypdf/leptonica.py b/src/ocrmypdf/leptonica.py index af69129f..69f6e444 100644 --- a/src/ocrmypdf/leptonica.py +++ b/src/ocrmypdf/leptonica.py @@ -13,6 +13,7 @@ import argparse import logging import os +import platform import sys import threading import warnings @@ -170,20 +171,6 @@ tls = threading.local() tls.trap = None -@ffi.callback("void(char *)") -def _stderr_handler(cstr): - msg = ffi.string(cstr).decode(errors='replace') - if msg.startswith("Error"): - logger.error(msg) - elif msg.startswith("Warning"): - logger.warning(msg) - else: - logger.debug(msg) - if tls.trap is not None: - tls.trap.append(msg) - return - - class _LeptonicaErrorTrap_Queue: def __init__(self): self.queue = deque() @@ -213,9 +200,25 @@ class _LeptonicaErrorTrap_Queue: try: + + @ffi.callback("void(char *)") + def _stderr_handler(cstr): + msg = ffi.string(cstr).decode(errors='replace') + if msg.startswith("Error"): + logger.error(msg) + elif msg.startswith("Warning"): + logger.warning(msg) + else: + logger.debug(msg) + if tls.trap is not None: + tls.trap.append(msg) + return + lept.leptSetStderrHandler(_stderr_handler) -except ffi.error: +except (ffi.error, MemoryError): # Pre-1.79 Leptonica does not have leptSetStderrHandler + # And some platforms, notably Apple ARM 64, do not allow the write+execute + # memory needed to set up the callback function. _LeptonicaErrorTrap = _LeptonicaErrorTrap_Redirect else: # 1.79 have this new symbol