Python copy_reg 模块,__newobj__() 实例源码

我们从Python开源项目中,提取了以下6个代码示例,用于说明如何使用copy_reg.__newobj__()

项目:oil    作者:oilshell    | 项目源码 | 文件源码
def test_issue24097(self):
        # Slot name is freed inside __getattr__ and is later used.
        class S(str):  # Not interned
            pass
        class A(object):
            __slotnames__ = [S('spam')]
            def __getattr__(self, attr):
                if attr == 'spam':
                    A.__slotnames__[:] = [S('spam')]
                    return 42
                else:
                    raise AttributeError

        import copy_reg
        expected = (copy_reg.__newobj__, (A,), ({}, {'spam': 42}), None, None)
        self.assertEqual(A().__reduce__(2), expected)
项目:python2-tracer    作者:extremecoders-re    | 项目源码 | 文件源码
def test_issue24097(self):
        # Slot name is freed inside __getattr__ and is later used.
        class S(str):  # Not interned
            pass
        class A(object):
            __slotnames__ = [S('spam')]
            def __getattr__(self, attr):
                if attr == 'spam':
                    A.__slotnames__[:] = [S('spam')]
                    return 42
                else:
                    raise AttributeError

        import copy_reg
        expected = (copy_reg.__newobj__, (A,), ({}, {'spam': 42}), None, None)
        self.assertEqual(A().__reduce__(2), expected)
项目:pytorch    作者:tylergenter    | 项目源码 | 文件源码
def __reduce_ex__(self, proto):
        state = (self.requires_grad, self.volatile, self._backward_hooks)
        if proto > 1:
            return type(self), (self.data,), state
        if sys.version_info[0] == 2:
            from copy_reg import __newobj__
        else:
            from copyreg import __newobj__
        return __newobj__, (type(self), self.data), state
项目:pytorch-coriander    作者:hughperkins    | 项目源码 | 文件源码
def __reduce_ex__(self, proto):
        state = (self.requires_grad, self.volatile, self._backward_hooks)
        if proto > 1:
            return type(self), (self.data,), state
        if sys.version_info[0] == 2:
            from copy_reg import __newobj__
        else:
            from copyreg import __newobj__
        return __newobj__, (type(self), self.data), state
项目:pytorch    作者:ezyang    | 项目源码 | 文件源码
def __reduce_ex__(self, proto):
        state = (self.requires_grad, self.volatile, self._backward_hooks)
        if proto > 1:
            return type(self), (self.data,), state
        if sys.version_info[0] == 2:
            from copy_reg import __newobj__
        else:
            from copyreg import __newobj__
        return __newobj__, (type(self), self.data), state
项目:pytorch    作者:pytorch    | 项目源码 | 文件源码
def __reduce_ex__(self, proto):
        state = (self.requires_grad, False, self._backward_hooks)
        if proto > 1:
            return type(self), (self.data,), state
        if sys.version_info[0] == 2:
            from copy_reg import __newobj__
        else:
            from copyreg import __newobj__
        return __newobj__, (type(self), self.data), state