我正在为一个项目使用NakedMUD代码库。我在导入模块时遇到了问题。
在 *.py(Python 文件)中,它们使用以下语法导入模块:
import mudsys, mud, socket, char, hooks
并在 C 中嵌入 Python,他们使用:
mudmod = PyImport_ImportModule("char");
这两种方法似乎都向我表明,在 Python 可找到的路径中某处有一些 mudsys.py、mud.py ... 文件。我找不到他们。我想知道在哪里可以找到他们如何重命名模块而不是文件名。我不确定要找到它还需要什么。
问题在于,PyImport_ImportModule()
在一种情况下,第二次导入没有找到模块,它们引用了null
this 返回的指针。
Python 文档提到“在 2.6 版中更改:始终使用绝对导入。 ”我怀疑这是问题的一部分。
值得注意的是,它们覆盖了 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。