Skip to content

Memory-Importer is an experimental tool demonstrates that how to import python libraries from MEMORY under PEP 451

Notifications You must be signed in to change notification settings

shabbywu/python-memory-importer

Repository files navigation

💾⤵

Memory-Importer

Memory-Importer is an experimental tool demonstrates that how to import python libraries from MEMORY under PEP 451



github-stars

CI status
Linux/macOS Travis Travis-CI
MSVC 2019 AppVeyor
pip builds Pip Actions Status
cibuildwheel Wheels Actions Status

Installation

  • pip install memory-importer

Requirements

CMake for building, and pybind11 and physfs.py

Features

memory-importer implement a class called PhysfsImporter follow the finder and loader protocol which are proposed by PEP-451.

After append PhysfsImporter into sys.meta_path, You can import any pure python code from an archive stored in memory( or dicks).

PhysfsImporter use physfs.py to easily visit entry in achieve.

Test call

Case 1: import from an archive stored in disk.

import sys
from memory_importer import PhysfsImporter

# init importer and mount tests/assets/py.zip into memory
i = PhysfsImporter("tests/assets/py.zip")

# append it into sys.meta_path to notice python use it to import library
sys.meta_path.insert(0, i)

# test import and code
import single
assert str(single) == "<module 'single' (physfs://single.py)>"
assert single.flag() == "single"

Case 2: import from archive stored in memory.

import sys
from memory_importer import PhysfsImporter, physfs

# init physfs
physfs.init()
# load archive into memory
with open("tests/assets/py.zip", mode="rb") as fh:
    archive = fh.read()

# mount archive into physfs
physfs.mount_memory(archive, "py.zip", "/")

# init importer
i = PhysfsImporter()

# append it into sys.meta_path to notice python use it to import library
sys.meta_path.insert(0, i)

# test import and code
import single
assert str(single) == "<module 'single' (physfs://single.py)>"
assert single.flag() == "single"

What can it be used for?

  1. Provide a better way to demonstrate how PEP-451 work.
  2. You can embed your pure python code with your cpp python library.
  3. Waiting for you to discover...

About

Memory-Importer is an experimental tool demonstrates that how to import python libraries from MEMORY under PEP 451

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •