Skip to content

Support loading of .pyc files #563

@dhruvkb

Description

@dhruvkb

Currently the process for loading the file run the file through compile and then exec.

compiled_code = compile( # noqa: WPS421
to_compile.read(), included_file, 'exec',
)
exec(compiled_code, scope) # noqa: S102, WPS421

This works perfectly for .py files (thanks for the project, by the way, it is really awesome).

I have a project where the files are precompiled into .pyc during building the production version, and all .py files are deleted from the codebase.

This causes two issues:

  1. The names in the include call are no longer present. They've been deleted in a previous build step.
  2. Even if I use a workaround for issue 1, the .pyc files do not work with the compile + exec approach.

I would like to request the ability to load .pyc files by addressing both of these points.

  1. Allowing scripts to have a fallback, that is only loaded if the file is not found. Maybe that can be built into the optional implementation.

    include(
        optional("abc.py", "abc.pyc"),
        # ...
    )
  2. Allowing .pyc files to be loaded. I've done it locally using this.

    import marshal
    import types
    
    with open(included_file, 'rb') as to_compile:
        to_compile.seek(16)
        compiled_code = marshal.load(to_compile)
        if isinstance(compiled_code, types.CodeType):
            exec(compiled_code, scope)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions