Skip to content

关于5.1以后版本修改的一些发现及问题 #6

Description

@xinghui233

在阅读了https://github.com/kovidgoyal/calibre 中的几个关键提交

  • fe478a2ceedb8b7111517c80d39d064c0d438717 Use the new bypy freezing code for windows build
  • 2938e2c2037e1a47a4536bf373f9e72e7e5a8037 New bypy freezing framework for Linux builds
    https://github.com/kovidgoyal/bypy中的
  • cbe8e76783bcadc1df1d49a8c9b99c249a8a56bc Get the freeze module building on Windows
  • 63bef79da4d045c52c5c748e48d95247b535c3d8 Basic freezing support
    后 在 bypy/freeze/init.py中发现了关键函数
def freeze_python(base, dest_dir, include_dir):
    files = collect_files_for_internment(base)
    frozen_file = os.path.join(dest_dir, 'python-lib.bypy.frozen')
    index_data = {}
    with open(frozen_file, 'wb') as frozen_file:
        for name, path in files.items():
            raw = open(path, 'rb').read()
            index_data[name] = frozen_file.tell(), len(raw)
            frozen_file.write(raw)
    if len(index_data) > 9999:
        raise ValueError(
            'Too many files in python-lib have to switch'
            ' hash function to IntSaltHash and change C'
            ' template accordingly.')
    perfect_hash, code_to_get_index = get_c_code(index_data)
    values = []
    for k in sorted(index_data.keys(), key=perfect_hash):
        v = index_data[k]
        values.append(f'{{ {v[0]}u, {v[1]}u }}')
    vals = ','.join(values)
    tree = '\n'.join(bin_to_c(as_tree(index_data.keys())))
    header = code_to_get_index + f'''
static void
get_value_for_hash_index(int index, unsigned long *offset, unsigned long *size)
{{
    typedef struct {{ unsigned long offset, size; }} Item;
    static const Item values[{len(values)}] = {{ {vals} }};
    if (index >= 0 && index < {len(values)}) {{
       *offset = values[index].offset; *size = values[index].size;
    }} else {{
    *offset = 0; *size = 0;
    }}
}}
static const char filesystem_tree[] = {{ {tree} }};
'''
    with open(os.path.join(include_dir, 'bypy-data-index.h'), 'w') as f:
        f.write(header)

它看起来是抛弃了旧版本将pyc压缩成压缩包的方法 而是直接将pyc文件已二进制依次存入 \bin\python-lib.bypy.frozen 中
在我以16进制打开该文件后 也找到了 对应的方法名
但现在的问题是 我不知道如何寻找并修改frozen文件以取消核心函数(通过python3.9编译的原代码和修改后的代码均和存档存在细微差异 以至于我很难寻找到二进制文件准确修改位置(或许是python版本的问题

想问问作者有什么办法来解决

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions