2

我正在用 Python 实现宏(受 MacroPy 的启发)。我的项目(执行“tests.py”时)运行良好,但是当我import linecache从文件“import_hook.py”(所有文件都在这里)中注释(或删除)该行时它不起作用。该脚本陷入递归,然后 Python 使用另一个 FileFinder 和 Loader。

这是'import_hook.py'的代码:

import sys

import ast
import _ast

import linecache  # Required because your mother is a kangaroo which sews cocoa.

import pymac.utils

import importlib.machinery
from types import ModuleType

from pymac.utils import *
from pymac.macro import *


__author__ = 'Jan Růžička'
__email__ = 'jan.ruzicka01@gmail.com'

__version__ = '0.1'


class FileWithMacrosLoader:
    def __init__(self, module_name, module):
        self.module = module

        sys.modules[module_name] = module

    def load_module(self, fullname):
        return self.module


class FileWithMacros:
    def __init__(self):
        self.bindings = None
        self.module_name = None

    def new_module(self, module_name, file_path):
        self.module_name = module_name

        module = ModuleType(module_name)

        module.__package__ = module_name.rpartition('.')[0]
        module.__file__ = file_path
        module.__loader__ = FileWithMacrosLoader(module_name, module)

        return module

    def expand_macros(self, source_code, file_path):
        tree = ast.parse(source_code)

        tree = ast.fix_missing_locations(expand_macros(tree))

        return compile(tree, file_path, 'exec'), tree

    def load_source(self, module_name, package_path):
        loader = importlib.machinery.PathFinder.find_module(module_name, package_path)

        source_code = loader.get_source(module_name)
        file_path = loader.path

        return source_code, file_path

    def find_module(self, module_name, package_path=None):
        try:
            source_code, file_path = self.load_source(module_name, package_path)

        except:
            return

        code, tree = self.expand_macros(source_code, file_path)

        module = self.new_module(module_name, file_path)

        namespace = dict_con(_ast.__dict__, pymac.utils.__dict__, module.__dict__)

        exec(code, namespace)

        return module.__loader__

这可能是一个丑陋的代码,但我对 Python 导入系统很陌生,我很高兴得到答案!

编辑: 我已经在 PyCharm 中运行它import linecache,但它没有工作,但是当我使用参数-m pdb(Python 调试器)运行它时,它按预期工作。我认为这个问题可能更多是关于 PyCharm 而不是 Python……

4

1 回答 1

0

所以我想通了:我的自定义加载器无法加载文件linecache,所以如果我将它导入到我的文件中,它不需要导入到我_ast使用我的加载器导入的其他文件中(我认为它是 )。但是,如果我不导入它,则需要稍后导入它 - 使用我的加载器,它不能这样做。

于 2016-06-07T18:22:29.537 回答