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: 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} \begin{funcdesc}{instancemethod}{function, instance, class}
This function will return a method object, bound to \var{instance}, or This function will return a method object, bound to \var{instance}, or
unbound if \var{instance} is \code{None}. \var{function} must be 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. The type of user-defined classes.
\end{datadesc} \end{datadesc}
\begin{datadesc}{InstanceType}
The type of instances of user-defined classes.
\end{datadesc}
\begin{datadesc}{MethodType} \begin{datadesc}{MethodType}
The type of methods of user-defined class instances. The type of methods of user-defined class instances.
\end{datadesc} \end{datadesc}

View File

@ -119,26 +119,6 @@ def _copy_with_copy_method(x):
if PyStringMap is not None: if PyStringMap is not None:
d[PyStringMap] = _copy_with_copy_method 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 del d
def deepcopy(x, memo=None, _nil=[]): def deepcopy(x, memo=None, _nil=[]):
@ -273,29 +253,6 @@ def _keep_alive(x, memo):
# aha, this is the first one :-) # aha, this is the first one :-)
memo[id(memo)]=[x] 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): def _reconstruct(x, info, deep, memo=None):
if isinstance(info, str): if isinstance(info, str):
return x return x

View File

@ -18,8 +18,6 @@ def dis(x=None):
if x is None: if x is None:
distb() distb()
return return
if type(x) is types.InstanceType:
x = x.__class__
if hasattr(x, 'im_func'): if hasattr(x, 'im_func'):
x = x.im_func x = x.im_func
if hasattr(x, 'func_code'): 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 ClassType as classobj
from types import FunctionType as function from types import FunctionType as function
from types import InstanceType as instance
from types import MethodType as instancemethod from types import MethodType as instancemethod
from types import ModuleType as module from types import ModuleType as module

View File

@ -687,46 +687,6 @@ class Pickler:
write(SETITEM) write(SETITEM)
# else tmp is empty, and we're done # 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): def save_global(self, obj, name=None, pack=struct.pack):
write = self.write write = self.write
memo = self.memo memo = self.memo

View File

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

View File

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

View File

@ -21,22 +21,12 @@ print 'new.classobj()'
C = new.classobj('Spam', (Spam.Eggs,), {'get_more_yolks': get_more_yolks}) C = new.classobj('Spam', (Spam.Eggs,), {'get_more_yolks': get_more_yolks})
if verbose: if verbose:
print C 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): def break_yolks(self):
self.yolks = self.yolks - 2 self.yolks = self.yolks - 2
print 'new.instancemethod()' print 'new.instancemethod()'
c = C()
c.yolks = 3
im = new.instancemethod(break_yolks, c, C) im = new.instancemethod(break_yolks, c, C)
if verbose: if verbose:
print im print im

View File

@ -56,9 +56,7 @@ class _C:
def _m(self): pass def _m(self): pass
ClassType = type(_C) ClassType = type(_C)
UnboundMethodType = type(_C._m) # Same as MethodType UnboundMethodType = type(_C._m) # Same as MethodType
_x = _C() MethodType = type(_C()._m)
InstanceType = type(_x)
MethodType = type(_x._m)
BuiltinFunctionType = type(len) BuiltinFunctionType = type(len)
BuiltinMethodType = type([].append) # Same as BuiltinFunctionType BuiltinMethodType = type([].append) # Same as BuiltinFunctionType
@ -86,4 +84,4 @@ EllipsisType = type(Ellipsis)
DictProxyType = type(TypeType.__dict__) DictProxyType = type(TypeType.__dict__)
NotImplementedType = type(NotImplemented) 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: else:
# store instance attributes as a struct (really?) # store instance attributes as a struct (really?)
self.dump_struct(value.__dict__, write) self.dump_struct(value.__dict__, write)
dispatch[InstanceType] = dump_instance
dispatch[DateTime] = dump_instance dispatch[DateTime] = dump_instance
dispatch[Binary] = dump_instance dispatch[Binary] = dump_instance