Skip to content

Commit

Permalink
early load of static extension finder v2
Browse files Browse the repository at this point in the history
Summary:
if static extension is on, we load it as early as possible

NOTE: if this end up breaking a cogwheel, or a untested binary, just disable native python on it!

Reviewed By: zsol

Differential Revision: D52860272

fbshipit-source-id: 994e48ad60fc33e80c08badf5a0ff620a25f6c99
  • Loading branch information
Alvaro Leiva Geisse authored and facebook-github-bot committed Jan 23, 2024
1 parent f5894f3 commit a646384
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions prelude/python/tools/static_extension_finder.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
# License, Version 2.0 found in the LICENSE-APACHE file in the root directory
# of this source tree.

import sys
from importlib.machinery import ModuleSpec

# Add a try except to force eager importing
try:
Expand All @@ -17,6 +15,9 @@


class StaticExtensionFinder:
# pyre-fixme
ModuleSpec = None

@classmethod
# pyre-fixme[3]: Return type must be annotated.
# pyre-fixme[2]: Parameter must be annotated.
Expand All @@ -25,16 +26,22 @@ def find_spec(cls, fullname, path, target=None):
Use fullname to look up the PyInit function in the main binary. Returns None if not present.
This allows importing CExtensions that have been statically linked in.
"""

if not fullname:
return None
if not _check_module(fullname):
return None
spec = ModuleSpec(
spec = cls.ModuleSpec(
fullname, StaticExtensionLoader, origin="static-extension", is_package=False
)
return spec


# pyre-fixme[3]: Return type must be annotated.
def _initialize():
def _initialize() -> None:
# This imports are here to avoid tricking circular dependencies. see S389486
import sys
from importlib.machinery import ModuleSpec

StaticExtensionFinder.ModuleSpec = ModuleSpec

sys.meta_path.insert(0, StaticExtensionFinder)

0 comments on commit a646384

Please sign in to comment.