Commit Graph

118 Commits

Author SHA1 Message Date
Tim Peters a427a2b8d0 Rename "dictionary" (type and constructor) to "dict". 2001-10-29 22:25:45 +00:00
Guido van Rossum 7ad2d1eb8e Add __del__ callbacks. They are too useful to leave out.
XXX Remaining problems:

- The GC module doesn't know about these; I think it has its reasons
  to disallow calling __del__, but for now, __del__ on new-style
  objects is called when the GC module discards an object, for better
  or for worse.

- The code to call a __del__ handler is really ridiculously
  complicated, due to all the different debug #ifdefs.  I've copied
  this from the similar code in classobject.c, so I'm pretty sure I
  did it right, but it's not pretty. :-(

- No tests yet.
2001-10-29 22:11:00 +00:00
Guido van Rossum afe7a94089 When overriding __str__ or __repr__, set the tp_print slot to NULL. 2001-10-29 14:33:44 +00:00
Tim Peters 3abca127fe SF bug #475327: type() produces incorrect error msg
object.h:  Added PyType_CheckExact macro.

typeobject.c, type_new():

+ Use the new macro.
+ Assert that the arguments have the right types rather than do incomplete
  runtime checks "sometimes".
+ If this isn't the 1-argument flavor() of type, and there aren't 3 args
  total, produce a "types() takes 1 or 3 args" msg before
  PyArg_ParseTupleAndKeywords produces a "takes exactly 3" msg.
2001-10-27 19:37:48 +00:00
Guido van Rossum 6661be3bed Allow assignment to newinstance.__dict__. 2001-10-26 04:26:12 +00:00
Guido van Rossum c8e5645f15 Methods of built-in types now properly check for keyword arguments
(formerly these were silently ignored).  The only built-in methods
that take keyword arguments are __call__, __init__ and __new__.
2001-10-22 00:43:43 +00:00
Neil Schemenauer f23473f008 Add missing "static" declarations (found by "make smelly"). 2001-10-21 22:28:58 +00:00
Guido van Rossum 6d204074cb Big internal change that should have no external effects: unify the
'slotdef' structure typedef and 'struct wrapperbase'.  By adding the
wrapper docstrings to the slotdef structure, the slotdefs array can
serve as the data structure that drives add_operators(); the wrapper
descriptor contains a pointer to slotdef structure.  This replaces
lots of custom code from add_operators() by a loop over the slotdefs
array, and does away with all the tab_xxx tables.
2001-10-21 00:44:31 +00:00
Guido van Rossum f76de62f7d Fix SF bug #472234: type(obj) calls type->tp_init (Roeland Rengelink)
The fix is a band-aid: type_call() now makes the same exception for a
single-argument call to type() as type_new() was already making.
2001-10-18 15:49:21 +00:00
Guido van Rossum 14a6f8378e Remove a bunch of stuff that's no longer needed now that update_slot()
and fixup_slot_dispatchers() always select the proper slot dispatcher.
This affects slot_sq_item(), slot_tp_getattro(), and
slot_tp_getattr_hook().
2001-10-17 13:59:09 +00:00
Guido van Rossum caf59043d1 slot_sq_item(): ensure that self is an instance of the wrapper's
d_type before calling the wrapped function.

fixup_slot_dispatchers(): fix indentation.
2001-10-17 07:15:43 +00:00
Guido van Rossum b85a8b7bc7 Refactored the update_slot() code a bit to be hopefully slightly more
efficient:

- recurse down subclasses only once rather than for each affected
  slot;

- short-circuit recursing down subclasses when a subclass has its own
  definition of the name that caused the update_slot() calls in the
  first place;

- inline collect_ptrs().
2001-10-16 17:00:48 +00:00
Guido van Rossum 687ae00460 Get rid of __defined__ and tp_defined -- there's no need to
distinguish __dict__ and __defined__ any more.  In the C structure,
tp_cache takes its place -- but this hasn't been implemented yet.
2001-10-15 22:03:32 +00:00
Guido van Rossum 2f3ca6eeb6 Completely get rid of __dynamic__ and the corresponding
Py_TPFLAGS_DYNAMICTYPE bit.  There is no longer a performance benefit,
and I don't really see the use case any more.
2001-10-15 21:05:10 +00:00
Guido van Rossum 825d875371 Add (void *) casts to solve some problems on HP-UX 11.0, as discussed
on SF bug #467145.
2001-10-15 19:44:24 +00:00
Guido van Rossum d396b9c9c3 Redid the slot computation. The initial slot assignments are now done
using the same algorithm as the slot updates.  The slotdefs array is
now sorted by slot offset and has an interned string object corresponding
to the name added to each item.  More can be done but I need to commit
this first as a working intermediate stage.
2001-10-13 20:02:41 +00:00
Guido van Rossum 5af588b7f0 Now that COPYBUF is a new local macro, add #undef COPYBUF. 2001-10-12 14:13:21 +00:00
Tim Peters fc57ccb982 SF bug [#470040] ParseTuple t# vs subclasses.
inherit_slots():  tp_as_buffer was getting inherited as if it were a
method pointer, rather than a pointer to a vector of method pointers.  As
a result, inheriting from a type that implemented buffer methods was
ineffective, leaving all the tp_as_buffer slots NULL in the subclass.
2001-10-12 02:38:24 +00:00
Guido van Rossum 875eeaa193 Another step in the right direction: when a new class's attribute
corresponding to a dispatch slot (e.g. __getitem__ or __add__) is set,
calculate the proper dispatch slot and propagate the change to all
subclasses.  Because of multiple inheritance, there's no easy way to
avoid always recursing down the tree of subclasses.  Who cares?

(There's more to do, but this works.  There's also a test for this now.)
2001-10-11 18:33:53 +00:00
Guido van Rossum fd38f8e638 The slot definition table entry for mp_getitem had a bogus wrapper
function, which caused test_minidom to fail.  Fixed this.
2001-10-09 20:17:57 +00:00
Guido van Rossum 7b9144b2ee Halfway checkin. This is still messy, but it's beginning to address
the problem that slots weren't inherited properly.  override_slots()
no longer exists; in its place comes fixup_slot_dispatchers() which
does more and different work and is table-based.  (Eventually I want
this table also to replace all the little tab_foo tables.)

Also add a wrapper for __delslice__; this required a change in
test_descrtut.py.
2001-10-09 19:39:46 +00:00
Guido van Rossum 0eb2a6e974 It turned out not so difficult to support old-style numbers (those
without the Py_TPFLAGS_CHECKTYPES flag) in the wrappers.  This
required a few changes in test_descr.py to cope with the fact that the
complex type has __int__, __long__ and __float__ methods that always
raise an exception.
2001-10-09 11:07:24 +00:00
Tim Peters 44383384b3 type_subclasses(): debug build was broken due to typo in new assert(). 2001-10-08 16:49:26 +00:00
Guido van Rossum 1c45073aba Keep track of a type's subclasses (subtypes), in tp_subclasses, which
is a list of weak references to types (new-style classes).  Make this
accessible to Python as the function __subclasses__ which returns a
list of types -- we don't want Python programmers to be able to
manipulate the raw list.

In order to make this possible, I also had to add weak reference
support to type objects.

This will eventually be used together with a trap on attribute
assignment for dynamic classes for a major speed-up without losing the
dynamic properties of types: when a __foo__ method is added to a
class, the class and all its subclasses will get an appropriate tp_foo
slot function.
2001-10-08 15:18:27 +00:00
Tim Peters f2a67daca2 Guido suggests, and I agree, to insist that SIZEOF_VOID_P be a power of 2.
This simplifies the rounding in _PyObject_VAR_SIZE, allows to restore the
pre-rounding calling sequence, and allows some nice little simplifications
in its callers.  I'm still making it return a size_t, though.
2001-10-07 03:54:51 +00:00
Tim Peters 6d483d3477 _PyObject_VAR_SIZE: always round up to a multiple-of-pointer-size value.
As Guido suggested, this makes the new subclassing code substantially
simpler.  But the mechanics of doing it w/ C macro semantics are a mess,
and _PyObject_VAR_SIZE has a new calling sequence now.

Question:  The PyObject_NEW_VAR macro appears to be part of the public API.
Regardless of what it expands to, the notion that it has to round up the
memory it allocates is new, and extensions containing the old
PyObject_NEW_VAR macro expansion (which was embedded in the
PyObject_NEW_VAR expansion) won't do this rounding.  But the rounding
isn't actually *needed* except for new-style instances with dict pointers
after a variable-length blob of embedded data.  So my guess is that we do
not need to bump the API version for this (as the rounding isn't needed
for anything an extension can do unless it's recompiled anyway).  What's
your guess?
2001-10-06 21:27:34 +00:00
Tim Peters 406fe3b1c0 Repaired the debug Windows deaths in test_descr, by allocating enough
pad memory to properly align the __dict__ pointer in all cases.

gcmodule.c/objimpl.h, _PyObject_GC_Malloc:
+ Added a "padding" argument so that this flavor of malloc can allocate
  enough bytes for alignment padding (it can't know this is needed, but
  its callers do).

typeobject.c, PyType_GenericAlloc:
+ Allocated enough bytes to align the __dict__ pointer.
+ Sped and simplified the round-up-to-PTRSIZE logic.
+ Added blank lines so I could parse the if/else blocks <0.7 wink>.
2001-10-06 19:04:01 +00:00
Guido van Rossum 9475a2310d Enable GC for new-style instances. This touches lots of files, since
many types were subclassable but had a xxx_dealloc function that
called PyObject_DEL(self) directly instead of deferring to
self->ob_type->tp_free(self).  It is permissible to set tp_free in the
type object directly to _PyObject_Del, for non-GC types, or to
_PyObject_GC_Del, for GC types.  Still, PyObject_DEL was a tad faster,
so I'm fearing that our pystone rating is going down again.  I'm not
sure if doing something like

void xxx_dealloc(PyObject *self)
{
	if (PyXxxCheckExact(self))
		PyObject_DEL(self);
	else
		self->ob_type->tp_free(self);
}

is any faster than always calling the else branch, so I haven't
attempted that -- however those types whose own dealloc is fancier
(int, float, unicode) do use this pattern.
2001-10-05 20:51:39 +00:00
Guido van Rossum 50fda3ba26 Make new classes dynamic by default. 2001-10-04 19:46:06 +00:00
Tim Peters 59f809d3bc type_new(): cast PyObject_MALLOC's result to char*, for clarity. 2001-10-04 05:43:02 +00:00
Tim Peters 2f93e28a19 SF bug [#467331] ClassType.__doc__ always None.
For a dynamically constructed type object, fill in the tp_doc slot with
a copy of the argument dict's "__doc__" value, provided the latter exists
and is a string.
NOTE:  I don't know what to do if it's a Unicode string, so in that case
tp_doc is left NULL (which shows up as Py_None if you do Class.__doc__).
Note that tp_doc holds a char*, not a general PyObject*.
2001-10-04 05:27:00 +00:00
Guido van Rossum 1e1de1cf35 typeobject.c, slot_tp_gettattr_hook(): fix the speedup hack -- the
test for getattribute==NULL was bogus because it always found
object.__getattribute__.  Pick it apart using the trick we learned
from slot_sq_item, and if it's just a wrapper around
PyObject_GenericGetAttr, zap it.  Also added a long XXX comment
explaining the consequences.
2001-10-03 13:58:35 +00:00
Guido van Rossum f4593e0b65 *EXPERIMENTAL* speedup of slot_sq_item. This sped up the following
test dramatically:

    class T(tuple): __dynamic__ = 1
    t = T(range(1000))
    for i in range(1000): tt = tuple(t)

The speedup was about 5x compared to the previous state of CVS (1.7
vs. 8.8, in arbitrary time units).  But it's still more than twice as
slow as as the same test with __dynamic__ = 0 (0.8).

I'm not sure that I really want to go through the trouble of this kind
of speedup for every slot.  Even doing it just for the most popular
slots will be a major effort (the new slot_sq_item is 40+ lines, while
the old one was one line with a powerful macro -- unfortunately the
speedup comes from expanding the macro and doing things in a way
specific to the slot signature).

An alternative that I'm currently considering is sketched in PLAN.txt:
trap setattr on type objects.  But this will require keeping track of
all derived types using weak references.
2001-10-03 12:09:30 +00:00
Guido van Rossum da21c0110b call_method(), call_maybe(): fix a performance bug: the argument
pointing to a static variable to hold the object form of the string
was never used, causing endless calls to PyString_InternFromString().
One particular test (with lots of __getitem__ calls) became a third
faster with this!
2001-10-03 00:50:18 +00:00
Guido van Rossum 048eb75c2d Add Garbage Collection support to new-style classes (not yet to their
instances).

Also added GC support to various auxiliary types: super, property,
descriptors, wrappers, dictproxy.  (Only type objects have a tp_clear
field; the other types are.)

One change was necessary to the GC infrastructure.  We have statically
allocated type objects that don't have a GC header (and can't easily
be given one) and heap-allocated type objects that do have a GC
header.  Giving these different metatypes would be really ugly: I
tried, and I had to modify pickle.py, cPickle.c, copy.py, add a new
invent a new name for the new metatype and make it a built-in, change
affected tests...  In short, a mess.  So instead, we add a new type
slot tp_is_gc, which is a simple Boolean function that determines
whether a particular instance has GC headers or not.  This slot is
only relevant for types that have the (new) GC flag bit set.  If the
tp_is_gc slot is NULL (by far the most common case), all instances of
the type are deemed to have GC headers.  This slot is called by the
PyObject_IS_GC() macro (which is only used twice, both times in
gcmodule.c).

I also changed the extern declarations for a bunch of GC-related
functions (_PyObject_GC_Del etc.): these always exist but objimpl.h
only declared them when WITH_CYCLE_GC was defined, but I needed to be
able to reference them without #ifdefs.  (When WITH_CYCLE_GC is not
defined, they do the same as their non-GC counterparts anyway.)
2001-10-02 21:24:57 +00:00
Guido van Rossum 55f2099b2f Miscellaneous code fiddling:
- SLOT1BINFULL() macro: changed this to check for __rop__ overriding
  __op__, like binary_op1() in abstract.c -- the latter only calls the
  slot function once if both types use the same slot function, so the
  slot function must make both calls -- which it already did for the
  __op__, __rop__ order, but not yet for the __rop__, __op__ order
  when B.__class__ is a subclass of A.__class__.

- slot_sq_contains(), slot_nb_nonzero(): use lookup_maybe() rather
  than lookup_method() which sets an exception which we then clear.

- slot_nb_coerce(): don't give up when left argument's __coerce__
returns NotImplemented, but give the right argument a chance.
2001-10-01 17:18:22 +00:00
Guido van Rossum 2611162345 slot_sq_length(): squash a leak. 2001-10-01 16:42:49 +00:00
Guido van Rossum 25d1807d23 slot_tp_new(): newargs was leaking. 2001-10-01 15:55:28 +00:00
Guido van Rossum d016e45fdb Fix typo found by doerwalter. 2001-10-01 13:17:24 +00:00
Tim Peters 8b13b3ede2 SF bug [#466173] unpack TypeError unclear
Replaced 3 instances of "iter() of non-sequence" with
"iteration over non-sequence".
Restored "unpack non-sequence" for stuff like "a, b = 1".
2001-09-30 05:58:42 +00:00
Guido van Rossum 9bea3abf0d Ouch. The wrapper for __rpow__ was the same as for __pow__, resulting
in bizarre outcomes.  Test forthcoming.
2001-09-28 22:58:52 +00:00
Guido van Rossum 2ed6bf87c9 Merge branch changes (coercion, rich comparisons) into trunk. 2001-09-27 20:30:07 +00:00
Guido van Rossum 874f15aa28 add_operators(): the __floordiv__ and __truediv__ descriptors (and
their 'i' and 'r' variants) were not being generated if the
corresponding nb_ slots were present in the type object.  I bet this
is because floor and true division were introduced after I last
looked at that part of the code.
2001-09-25 21:16:33 +00:00
Guido van Rossum 3926a63d05 - Provisional support for pickling new-style objects. (*)
- Made cls.__module__ writable.

- Ensure that obj.__dict__ is returned as {}, not None, even upon first
  reference; it simply springs into life when you ask for it.

(*) The pickling support is provisional for the following reasons:

- It doesn't support classes with __slots__.

- It relies on additional support in copy_reg.py: the C method
  __reduce__, defined in the object class, really calls calling
  copy_reg._reduce(obj).  Eventually the Python code in copy_reg.py
  needs to be migrated to C, but I'd like to experiment with the
  Python implementation first.  The _reduce() code also relies on an
  additional helper function, _reconstructor(), defined in
  copy_reg.py; this should also be reimplemented in C.
2001-09-25 16:25:58 +00:00
Guido van Rossum a4cb78874c Change repr() of a new-style class to say <class 'ClassName'> rather
than <type 'ClassName'>.  Exception: if it's a built-in type or an
extension type, continue to call it <type 'ClassName>.  Call me a
wimp, but I don't want to break more user code than necessary.
2001-09-25 03:56:29 +00:00
Guido van Rossum 5c294fb0e6 Make __class__ assignment possible, when the object structures are the
same.  I hope the test for structural equivalence is stringent enough.
It only allows the assignment if the old and new types:

- have the same basic size
- have the same item size
- have the same dict offset
- have the same weaklist offset
- have the same GC flag bit
- have a common base that is the same except for maybe the dict and
  weaklist (which may have been added separately at the same offsets
  in both types)
2001-09-25 03:43:42 +00:00
Guido van Rossum 3d45d8f12e Another comparison patch-up: comparing a type with a dynamic metatype
to one with a static metatype raised an obscure error.
2001-09-24 18:47:40 +00:00
Guido van Rossum ff0e6d6ef5 Fix the baffler that Tim reported: sometimes the repr() of an object
looks like <X object at ...>, sometimes it says <X instance at ...>.
Make this uniformly say <X object at ...>.
2001-09-24 16:03:59 +00:00
Guido van Rossum 19c1cd5b35 Add the __getattr__ hook back. The rules are now:
- if __getattribute__ exists, it is called first;
  if it doesn't exists, PyObject_GenericGetAttr is called first.
- if the above raises AttributeError, and __getattr__ exists,
  it is called.
2001-09-21 21:24:49 +00:00
Guido van Rossum 867a8d2e26 Change the name of the __getattr__ special method for new-style
classes to __getattribute__, to make it crystal-clear that it doesn't
have the same semantics as overriding __getattr__ on classic classes.

This is a halfway checkin -- I'll proceed to add a __getattr__ hook
that works the way it works in classic classes.
2001-09-21 19:29:08 +00:00