Skip to content

Commit

Permalink
Move library load/destory to seprate file
Browse files Browse the repository at this point in the history
& improve autoclose debug setting
  • Loading branch information
mara004 committed Jun 12, 2023
1 parent ee14e56 commit 12d2327
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 19 deletions.
11 changes: 1 addition & 10 deletions src/pypdfium2/__init__.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,7 @@
# SPDX-FileCopyrightText: 2023 geisserml <geisserml@gmail.com>
# SPDX-License-Identifier: Apache-2.0 OR BSD-3-Clause

import atexit
import logging
import pypdfium2._library_scope
from pypdfium2.version import *
from pypdfium2._helpers import *
from pypdfium2 import raw, internal

logger = logging.getLogger(__name__) # FIXME perhaps unnecessary?

# Note: PDFium developers plan changes to the initialisation API (see https://crbug.com/pdfium/1446)
raw.FPDF_InitLibrary()
atexit.register(raw.FPDF_DestroyLibrary)

del atexit, logging
3 changes: 1 addition & 2 deletions src/pypdfium2/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,7 @@ def get_parser():

def setup_logging():

debug_autoclose = bool(int( os.environ.get("DEBUG_AUTOCLOSE", 0) ))
pdfium_i.set_autoclose_debug(debug_autoclose)
pdfium_i.DEBUG_AUTOCLOSE.value = bool(int( os.environ.get("DEBUG_AUTOCLOSE", 0) ))

lib_logger = logging.getLogger("pypdfium2")
lib_logger.addHandler(logging.StreamHandler())
Expand Down
21 changes: 21 additions & 0 deletions src/pypdfium2/_library_scope.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# SPDX-FileCopyrightText: 2023 geisserml <geisserml@gmail.com>
# SPDX-License-Identifier: Apache-2.0 OR BSD-3-Clause

import atexit
import pypdfium2.raw as pdfium_c
import pypdfium2.internal as pdfium_i


def destroy_library():
if pdfium_i.DEBUG_AUTOCLOSE:
import os, sys
os.write(sys.stderr.fileno(), b"Destroy PDFium (auto)\n")
pdfium_c.FPDF_DestroyLibrary()

# Load pdfium
# Note: PDFium developers plan changes to the initialisation API (see https://crbug.com/pdfium/1446)
pdfium_c.FPDF_InitLibrary()

# Register an exit handler that will free pdfium
# Trust in Python to call exit handlers only after all objects have been finalized
atexit.register(destroy_library)
10 changes: 3 additions & 7 deletions src/pypdfium2/internal/bases.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# SPDX-FileCopyrightText: 2023 geisserml <geisserml@gmail.com>
# SPDX-License-Identifier: Apache-2.0 OR BSD-3-Clause

__all__ = ("AutoCastable", "AutoCloseable", "set_autoclose_debug")
__all__ = ("AutoCastable", "AutoCloseable", "DEBUG_AUTOCLOSE")

import os
import sys
Expand All @@ -12,19 +12,15 @@

logger = logging.getLogger(__name__)

DEBUG_AUTOCLOSE = False

DEBUG_AUTOCLOSE = ctypes.c_bool(False) # mutable bool

STATE_INVALID = -1
STATE_AUTO = 0
STATE_EXPLICIT = 1
STATE_BYPARENT = 2


def set_autoclose_debug(value=True):
global DEBUG_AUTOCLOSE
DEBUG_AUTOCLOSE = value


class AutoCastable:

@property
Expand Down

0 comments on commit 12d2327

Please sign in to comment.