SF patch 1495675: Remove types.InstanceType and new.instance

(Collin Winter)
This commit is contained in:
Guido van Rossum 2006-05-26 19:12:38 +00:00
parent 2018831b2b
commit 65810fee5e
11 changed files with 50 additions and 146 deletions

View File

@ -16,14 +16,6 @@ interpreter when the object is used.
The \module{new} module defines the following functions:
\begin{funcdesc}{instance}{class\optional{, dict}}
This function creates an instance of \var{class} with dictionary
\var{dict} without calling the \method{__init__()} constructor. If
\var{dict} is omitted or \code{None}, a new, empty dictionary is
created for the new instance. Note that there are no guarantees that
the object will be in a consistent state.
\end{funcdesc}
\begin{funcdesc}{instancemethod}{function, instance, class}
This function will return a method object, bound to \var{instance}, or
unbound if \var{instance} is \code{None}. \var{function} must be

View File

@ -122,10 +122,6 @@ The type for code objects such as returned by
The type of user-defined classes.
\end{datadesc}
\begin{datadesc}{InstanceType}
The type of instances of user-defined classes.
\end{datadesc}
\begin{datadesc}{MethodType}
The type of methods of user-defined class instances.
\end{datadesc}

View File

@ -119,26 +119,6 @@ def _copy_with_copy_method(x):
if PyStringMap is not None:
d[PyStringMap] = _copy_with_copy_method
def _copy_inst(x):
if hasattr(x, '__copy__'):
return x.__copy__()
if hasattr(x, '__getinitargs__'):
args = x.__getinitargs__()
y = x.__class__(*args)
else:
y = _EmptyClass()
y.__class__ = x.__class__
if hasattr(x, '__getstate__'):
state = x.__getstate__()
else:
state = x.__dict__
if hasattr(y, '__setstate__'):
y.__setstate__(state)
else:
y.__dict__.update(state)
return y
d[types.InstanceType] = _copy_inst
del d
def deepcopy(x, memo=None, _nil=[]):
@ -273,29 +253,6 @@ def _keep_alive(x, memo):
# aha, this is the first one :-)
memo[id(memo)]=[x]
def _deepcopy_inst(x, memo):
if hasattr(x, '__deepcopy__'):
return x.__deepcopy__(memo)
if hasattr(x, '__getinitargs__'):
args = x.__getinitargs__()
args = deepcopy(args, memo)
y = x.__class__(*args)
else:
y = _EmptyClass()
y.__class__ = x.__class__
memo[id(x)] = y
if hasattr(x, '__getstate__'):
state = x.__getstate__()
else:
state = x.__dict__
state = deepcopy(state, memo)
if hasattr(y, '__setstate__'):
y.__setstate__(state)
else:
y.__dict__.update(state)
return y
d[types.InstanceType] = _deepcopy_inst
def _reconstruct(x, info, deep, memo=None):
if isinstance(info, str):
return x

View File

@ -18,8 +18,6 @@ def dis(x=None):
if x is None:
distb()
return
if type(x) is types.InstanceType:
x = x.__class__
if hasattr(x, 'im_func'):
x = x.im_func
if hasattr(x, 'func_code'):

View File

@ -6,7 +6,6 @@ Objects of most types can now be created by calling the type object.
from types import ClassType as classobj
from types import FunctionType as function
from types import InstanceType as instance
from types import MethodType as instancemethod
from types import ModuleType as module

View File

@ -687,46 +687,6 @@ class Pickler:
write(SETITEM)
# else tmp is empty, and we're done
def save_inst(self, obj):
cls = obj.__class__
memo = self.memo
write = self.write
save = self.save
if hasattr(obj, '__getinitargs__'):
args = obj.__getinitargs__()
len(args) # XXX Assert it's a sequence
_keep_alive(args, memo)
else:
args = ()
write(MARK)
if self.bin:
save(cls)
for arg in args:
save(arg)
write(OBJ)
else:
for arg in args:
save(arg)
write(INST + cls.__module__ + '\n' + cls.__name__ + '\n')
self.memoize(obj)
try:
getstate = obj.__getstate__
except AttributeError:
stuff = obj.__dict__
else:
stuff = getstate()
_keep_alive(stuff, memo)
save(stuff)
write(BUILD)
dispatch[InstanceType] = save_inst
def save_global(self, obj, name=None, pack=struct.pack):
write = self.write
memo = self.memo

View File

@ -2071,42 +2071,58 @@ highest protocol among opcodes = 0
0: ( MARK
1: l LIST (MARK at 0)
2: p PUT 0
5: ( MARK
6: i INST 'pickletools _Example' (MARK at 5)
28: p PUT 1
31: ( MARK
32: d DICT (MARK at 31)
33: p PUT 2
36: S STRING 'value'
45: p PUT 3
48: I INT 42
52: s SETITEM
53: b BUILD
54: a APPEND
55: g GET 1
58: a APPEND
59: . STOP
5: c GLOBAL 'copy_reg _reconstructor'
30: p PUT 1
33: ( MARK
34: c GLOBAL 'pickletools _Example'
56: p PUT 2
59: c GLOBAL '__builtin__ object'
79: p PUT 3
82: N NONE
83: t TUPLE (MARK at 33)
84: p PUT 4
87: R REDUCE
88: p PUT 5
91: ( MARK
92: d DICT (MARK at 91)
93: p PUT 6
96: S STRING 'value'
105: p PUT 7
108: I INT 42
112: s SETITEM
113: b BUILD
114: a APPEND
115: g GET 5
118: a APPEND
119: . STOP
highest protocol among opcodes = 0
>>> dis(pickle.dumps(x, 1))
0: ] EMPTY_LIST
1: q BINPUT 0
3: ( MARK
4: ( MARK
5: c GLOBAL 'pickletools _Example'
27: q BINPUT 1
29: o OBJ (MARK at 4)
30: q BINPUT 2
32: } EMPTY_DICT
33: q BINPUT 3
35: U SHORT_BINSTRING 'value'
42: q BINPUT 4
44: K BININT1 42
46: s SETITEM
47: b BUILD
48: h BINGET 2
50: e APPENDS (MARK at 3)
51: . STOP
4: c GLOBAL 'copy_reg _reconstructor'
29: q BINPUT 1
31: ( MARK
32: c GLOBAL 'pickletools _Example'
54: q BINPUT 2
56: c GLOBAL '__builtin__ object'
76: q BINPUT 3
78: N NONE
79: t TUPLE (MARK at 31)
80: q BINPUT 4
82: R REDUCE
83: q BINPUT 5
85: } EMPTY_DICT
86: q BINPUT 6
88: U SHORT_BINSTRING 'value'
95: q BINPUT 7
97: K BININT1 42
99: s SETITEM
100: b BUILD
101: h BINGET 5
103: e APPENDS (MARK at 3)
104: . STOP
highest protocol among opcodes = 1
Try "the canonical" recursive-object test.

View File

@ -1,7 +1,6 @@
test_new
new.module()
new.classobj()
new.instance()
new.instancemethod()
new.function()
new.code()

View File

@ -21,22 +21,12 @@ print 'new.classobj()'
C = new.classobj('Spam', (Spam.Eggs,), {'get_more_yolks': get_more_yolks})
if verbose:
print C
print 'new.instance()'
c = new.instance(C, {'yolks': 3})
if verbose:
print c
o = new.instance(C)
verify(o.__dict__ == {},
"new __dict__ should be empty")
del o
o = new.instance(C, None)
verify(o.__dict__ == {},
"new __dict__ should be empty")
del o
def break_yolks(self):
self.yolks = self.yolks - 2
print 'new.instancemethod()'
c = C()
c.yolks = 3
im = new.instancemethod(break_yolks, c, C)
if verbose:
print im

View File

@ -56,9 +56,7 @@ class _C:
def _m(self): pass
ClassType = type(_C)
UnboundMethodType = type(_C._m) # Same as MethodType
_x = _C()
InstanceType = type(_x)
MethodType = type(_x._m)
MethodType = type(_C()._m)
BuiltinFunctionType = type(len)
BuiltinMethodType = type([].append) # Same as BuiltinFunctionType
@ -86,4 +84,4 @@ EllipsisType = type(Ellipsis)
DictProxyType = type(TypeType.__dict__)
NotImplementedType = type(NotImplemented)
del sys, _f, _g, _C, _x # Not for export
del sys, _f, _g, _C # Not for export

View File

@ -748,7 +748,6 @@ class Marshaller:
else:
# store instance attributes as a struct (really?)
self.dump_struct(value.__dict__, write)
dispatch[InstanceType] = dump_instance
dispatch[DateTime] = dump_instance
dispatch[Binary] = dump_instance