如何导入名称不是文件名的python模块

时间:2013-11-12 21:46:02

标签: python c import

我正在为项目使用NakedMUD代码库。我在导入模块时遇到了一个问题。

在* .py(Python文件)中,他们使用以下语法导入模块:

import mudsys, mud, socket, char, hooks

并在C中嵌入Python使用:

mudmod = PyImport_ImportModule("char");

这两种方法似乎都向我表明,在Python可以找到的路径中某处有一些mudsys.py,mud.py ...文件。我无法找到它们。我想知道我在哪里可以找到他们如何重命名模块以外的文件名。我不确定找到这个还需要什么。

问题是,在一种情况下,第二个导入PyImport_ImportModule()没有找到模块,它们引用了由此返回的null指针。

Python文档提到“Changed in version 2.6: Always uses absolute imports.”,我怀疑这是问题的一部分。

值得注意的是,它们会覆盖Python的一些内置函数,这些函数也可能会影响__restricted_builtin__.py__restricted_builtin_funcs__.py

################################################################################
#
# __restricted_builtin_funcs__.py
#
# This contains functions used by __restricted_builtin__ to do certain
# potentially dangerous actions in a safe mode
#
################################################################################
import __builtin__

def r_import(name, globals = {}, locals = {}, fromlist = []):
    '''Restricted __import__ only allows importing of specific modules'''

    ok_modules = ("mud", "obj", "char", "room", "exit", "account", "mudsock",
                  "event", "action", "random", "traceback", "utils",
                  "__restricted_builtin__")
    if name not in ok_modules:
        raise ImportError, "Untrusted module, %s" % name
    return __builtin__.__import__(name, globals, locals, fromlist)

def r_open(file, mode = "r", buf = -1):
    if mode not in ('r', 'rb'):
        raise IOError, "can't open files for writing in restricted mode"
    return open(file, mode, buf)

def r_exec(code):
    """exec is disabled in restricted mode"""
    raise NotImplementedError,"execution of code is disabled"

def r_eval(code):
    """eval is disabled in restricted mode"""
    raise NotImplementedError,"evaluating code is disabled"

def r_execfile(file):
    """executing files is disabled in restricted mode"""
    raise NotImplementedError,"executing files is disabled"

def r_reload(module):
    """reloading modules is disabled in restricted mode"""
    raise NotImplementedError, "reloading modules is disabled"

def r_unload(module):
    """unloading modules is disabled in restricted mode"""
    raise NotImplementedError, "unloading modules is disabled"

################################################################################
#
# __restricted_builtin__.py
#
# This module is designed to replace the __builtin__, but overwrite many of the
# functions that would allow an unscrupulous scripter to take malicious actions
#
################################################################################
from __builtin__ import *
from __restricted_builtin_funcs__ import r_import, r_open, r_execfile, r_eval, \
     r_reload, r_exec, r_unload

# override some dangerous functions with their safer versions
__import__   = r_import
execfile     = r_execfile
open         = r_open
eval         = r_eval
reload       = r_reload

编辑: PyObject *sys = PyImport_ImportModule("sys");也返回NULL。

2 个答案:

答案 0 :(得分:0)

在Python 2.5 __import__()中添加了level参数,其默认值为-1PyImport_ImportModule()的文档内容如下:

  

这是下面PyImport_ImportModuleEx()的简化接口,将globals和locals参数设置为NULL并将level设置为0 ......

如问题所示,__import__()r_import()覆盖,__import()__是一个过滤参数并将其传递给level = 1的函数。因此,额外的参数{{1}}被传递给只接受四个参数的函数。

答案 1 :(得分:0)

这些mudchar和其他模块已编译为C-code extensions,因此您应该在mud.pyd中查找获胜,或者mud.sosite-packages中查找unix 1}}

刚看了一下NakedMud包,它不是带有setup.py的标准python模块,它是用make编译的,所以你必须在baremood src / {中查找它们{1}}文件夹。