1

プロジェクトにNakedMUDコード ベースを使用しています。モジュールのインポートで問題が発生しています。

*.py (Python ファイル) では、次の構文でモジュールをインポートします。

import mudsys, mud, socket, char, hooks

そして C で Python を埋め込むために、彼らは以下を使用します:

mudmod = PyImport_ImportModule("char");

これらの方法はどちらも、Python で検索可能なパスのどこかにあるいくつかの mudsys.py、mud.py ... ファイルであることを示しているようです。私はそれらを見つけることができません。ファイル名以外のモジュールの名前を変更する方法を見つけるためにどこを見ればよいのでしょうか。これを見つけるために他に何が必要かはわかりません。

問題は、1 つのケースで 2 番目のインポート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 も返します。

4

2 に答える 2

0

Python 2.5では、デフォルト値が__import__()のパラメータが追加されました。のドキュメントは次のとおりです。level-1PyImport_ImportModule()

これは以下の PyImport_ImportModuleEx() への簡略化されたインターフェースであり、globals および locals 引数を NULL に設定し、level を 0 に設定したままにします...

質問に示されているように、引数をフィルタリングしてに渡す関数である で__import__()オーバーライドされました。したがって、余分な引数が、4 つの引数しか受け入れない関数に渡されていました。r_import()__import()__level = 1

于 2013-11-14T21:37:37.873 に答える