当前位置: 首页>>代码示例>>Python>>正文


Python ModuleType.__new__方法代码示例

本文整理汇总了Python中types.ModuleType.__new__方法的典型用法代码示例。如果您正苦于以下问题:Python ModuleType.__new__方法的具体用法?Python ModuleType.__new__怎么用?Python ModuleType.__new__使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在types.ModuleType的用法示例。


在下文中一共展示了ModuleType.__new__方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: _hacky_make_metamodule

# 需要导入模块: from types import ModuleType [as 别名]
# 或者: from types.ModuleType import __new__ [as 别名]
def _hacky_make_metamodule(orig_module, class_):
    # Construct the new module instance by hand, calling only ModuleType
    # methods, so as to simulate what happens in the __class__ assignment
    # path.
    new_module = ModuleType.__new__(class_)
    ModuleType.__init__(new_module, orig_module.__name__, orig_module.__doc__)

    # Now we jump through hoops to get at the module object guts...

    import ctypes
    # These are the only fields in the module object in CPython 1.0
    # through 2.7.
    fields = [
        ("PyObject_HEAD", ctypes.c_byte * object.__basicsize__),
        ("md_dict", ctypes.c_void_p),
    ]
    data_fields = ["md_dict"]
    # 3.0 adds PEP 3121 stuff:
    if (3,) <= sys.version_info:
        fields += [("md_def", ctypes.c_void_p),
                   ("md_state", ctypes.c_void_p),
               ]
        data_fields += ["md_def", "md_state"]
    # 3.4 adds md_weaklist and md_name
    if (3, 4) <= sys.version_info:
        fields += [("md_weaklist", ctypes.c_void_p),
                   ("md_name", ctypes.c_void_p),
                   ]
        # don't try to mess with md_weaklist, that seems unlikely to end
        # well.
        data_fields += ["md_name"]
    if (3, 5) <= sys.version_info:
        raise RuntimeError("Sorry, I can't read the future!")

    class CModule(ctypes.Structure):
        _fields_ = fields

    corig_module = ctypes.cast(id(orig_module), ctypes.POINTER(CModule))
    cnew_module = ctypes.cast(id(new_module), ctypes.POINTER(CModule))

    # And now we swap the two module's internal data fields. This makes
    # reference counting easier, plus prevents the destruction of orig_module
    # from cleaning up the objects we are still using.
    for data_field in data_fields:
        _swap_attr(corig_module.contents, cnew_module.contents, data_field)

    return new_module
开发者ID:njsmith,项目名称:metamodule,代码行数:49,代码来源:metamodule.py

示例2: __new__

# 需要导入模块: from types import ModuleType [as 别名]
# 或者: from types.ModuleType import __new__ [as 别名]
    def __new__(cls, name):
        """Returns a namespace with a given name, creating it if needed.

        Adds standard imports to a module without clojure.core.
        clojure.core is special-cased: being the first created module, some
        specific Vars must be added "by hand" (currently: *ns*,
        *command-line-args*).
        """
        if isinstance(name, Symbol):
            name = name.name
        if not sys.modules.get(name):
            mod = ModuleType.__new__(cls)
            ModuleType.__init__(mod, name)
            mod.__file__ = "<interactive namespace>"
            for i in dir(stdimps):
                if i.startswith("_"):
                    continue
                setattr(mod, i, getattr(stdimps, i))
            if mod.__name__ == "clojure.core":
                setattr(mod, "*ns*", Var(mod, "*ns*", mod).setDynamic())
                setattr(mod, "*command-line-args*",
                        Var(mod, "*command-line-args*", None).setDynamic())
            sys.modules[name] = mod
        return sys.modules[name]
开发者ID:khinsen,项目名称:clojure-py,代码行数:26,代码来源:namespace.py

示例3: __new__

# 需要导入模块: from types import ModuleType [as 别名]
# 或者: from types.ModuleType import __new__ [as 别名]
 def __new__(cls, name):
     obj = ModuleType.__new__(cls)
     obj.mdict = obj.__dict__
     print "========> id of mdict is %r, type %r" % (id(obj.mdict), type(obj.mdict))
     return obj
开发者ID:xanalogica,项目名称:talk-Magic-of-Metaprogramming,代码行数:7,代码来源:module_ro.py

示例4: repr

# 需要导入模块: from types import ModuleType [as 别名]
# 或者: from types.ModuleType import __new__ [as 别名]
# Test the module type

from test_support import verify, vereq, verbose, TestFailed
from types import ModuleType as module

# An uninitialized module has no __dict__ or __name__, and __doc__ is None
foo = module.__new__(module)
verify(foo.__dict__ is None)
try:
    s = foo.__name__
except AttributeError:
    pass
else:
    raise TestFailed, "__name__ = %s" % repr(s)
# __doc__ is None by default in CPython but not in Jython. 
# We're not worrying about that now.
#vereq(foo.__doc__, module.__doc__)

try:
    foo_dir = dir(foo)
except TypeError:
    pass
else:
    raise TestFailed, "__dict__ = %s" % repr(foo_dir)

try:
    del foo.somename
except AttributeError:
    pass
else:
    raise TestFailed, "del foo.somename"
开发者ID:denis-vilyuzhanin,项目名称:OpenModelSphereMirror,代码行数:33,代码来源:test_module.py


注:本文中的types.ModuleType.__new__方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。