Commit Graph

215 Commits

Author SHA1 Message Date
Guido van Rossum 036f999669 Implementing the salient parts of __reduce_ex__ in C.
This still falls back to helpers in copy_reg for:
   - pickle protocols < 2
   - calculating the list of slot names (done only once per class)
   - the __newobj__ function (which is used as a token but never called)
2003-02-21 22:02:54 +00:00
Guido van Rossum c53f009f94 Introducing __reduce_ex__, which is called with a protocol number argument
if it exists in preference over __reduce__.  Now Tim can go implement this
in cPickle.c.
2003-02-18 22:05:12 +00:00
Tim Peters 97e5ff555e Removed unreferenced label. 2003-02-18 19:32:50 +00:00
Guido van Rossum 8e80a72be4 The recent changes to super(), in particular supercheck(), broke when
using super() for an instance in a metaclass situation.  Because the
class was a metaclass, the instance was a class, and hence the
PyType_Check() branch was taken.  But this branch didn't apply.  Make
it so that if this branch doesn't apply, the other branch is still
tried.  All tests pass.
2003-02-18 19:22:22 +00:00
Guido van Rossum 298e421453 SF patch #685738 by Michael Stone.
This changes the default __new__ to refuse arguments iff tp_init is the
default __init__ implementation -- thus making it a TypeError when you
try to pass arguments to a constructor if the class doesn't override at
least __init__ or __new__.
2003-02-13 16:30:16 +00:00
Guido van Rossum a89d10edc9 Implement another useful feature for proxies: in super(X, x), x may
now be a proxy for an X instance, as long as issubclass(x.__class__, X).
2003-02-12 03:58:38 +00:00
Guido van Rossum eea4718e81 Fix from SF #681367: inherit tp_as_buffer. This only applies to C
types -- Python types already inherited this.
2003-02-11 20:39:59 +00:00
Guido van Rossum 9af48ff44e Inline create_specialmethod() -- since METH_CLASS is done differently
now, it was only called once, and its existence merely obfuscates the
control flow.
2003-02-11 17:12:46 +00:00
Guido van Rossum 82ed25c15a Add basic arg sanity checking to wrap_descr_get(). This is called
when Python code calls a descriptor's __get__ method.  It should
translate None to NULL in both argument positions, and insist that at
least one of the argument positions is not NULL after this
transformation.
2003-02-11 16:25:43 +00:00
Guido van Rossum 3f50cdc05e Get rid of the "bozo" __getstate__ that was inserted when __slots__
was used.  This simplifies some logic in copy_reg.py (used by
pickling).  It also broke a test, but this was rewritten to test the
new feature. :-)
2003-02-10 21:31:27 +00:00
Andrew M. Kuchling c9172d3832 Comment typo fix 2003-02-06 15:22:49 +00:00
Guido van Rossum ce8bcd8405 Fix for SF #668433. I'm not explaining it here; ample comments are in
the code.
2003-02-05 22:39:45 +00:00
Guido van Rossum 373c7412f2 Fix for SF bug #642358: only provide a new with a __dict__ or
__weaklist__ descriptor if we added __dict__ or __weaklist__,
respectively.  With unit test.
2003-01-07 13:41:37 +00:00
Guido van Rossum cd118803b5 Add a refinement to SLOT1BINFULL() that fixes the problem reported in
SF bug #623669: only try (e.g.) __rdiv__ before __div__ if the right
class actually overrides it.
2003-01-06 22:57:47 +00:00
Guido van Rossum 768158c11b Fix an out-of-bound index in pmerge() discovered by Zooko (SF bug
645404).  I'm not 100% sure this is the right fix, so I'll keep the
bug report open for Samuele, but this fixes the index error and passes
the test suite (and I can't see why it *shouldn't* be the right fix
:-).
2002-12-31 16:33:01 +00:00
Raymond Hettinger a828586c3a Punctuation fix. 2002-12-14 17:17:56 +00:00
Guido van Rossum 3bbc0eea10 Tighten the tests for assignment to __bases__: disallow empty tuple. 2002-12-13 17:49:38 +00:00
Tim Peters bca1cbc6f8 SF 548651: Fix the METH_CLASS implementation.
Most of these patches are from Thomas Heller, with long lines folded
by Tim.  The change to test_descr.py is from Guido.  See the bug report.

Not a bugfix candidate -- METH_CLASS is new in 2.3.
2002-12-09 22:56:13 +00:00
Tim Peters ea7f75d423 slot_nb_nonzero(): Another leak uncovered by the sandbox datetime
tests.  I found the logic too confusing to follow here, so rewrote more
than was likely absolutely necessary.

Bugfix candidate.
2002-12-07 21:39:16 +00:00
Tim Peters 61ce0a9bae slot_tp_hash(): In the normal path, this leaked a reference to the
integer hash object returned by __hash__().  This accounts for some of
the "mystery leaks" in the sandbox datetime tests, but probably not
all of them.
2002-12-06 23:38:02 +00:00
Michael W. Hudson ade8c8b2c3 Nudge getting __module__ and __name__ for new-style classes so that
the results of *setting* __name__ are not so surprising.

If people can suggest more tests, that'd be grand, or is what's there
sufficient?
2002-11-27 16:29:26 +00:00
Michael W. Hudson 7e7c00db0c I don't know why staring at the email to python-checkins made me
see problems with my code that I didn't see before the checkin, but:

When a subtype .mro() fails, we need to reset the type whose __bases__
are being changed, too.  Fix + test.
2002-11-27 15:40:09 +00:00
Michael W. Hudson 586da8fddd Readjustments to the way we cope with exceptions from subclasses'
mro() methods.  Now any exception aborts the whole __bases__ change.

And more tests.
2002-11-27 15:20:19 +00:00
Michael W. Hudson caf17be1b7 I had the inheritance cycle stuff backwards. Oops! 2002-11-27 10:24:44 +00:00
Michael W. Hudson ac74f5d44b Initialize a variable. Hope this makes things work for Guido.
It's odd that gcc on my ibook didn't complain about this.
2002-11-26 17:49:11 +00:00
Michael W. Hudson 98bbc49c54 This is my patch:
[ 635933 ] make some type attrs writable

Plus a couple of extra tests beyond what's up there.

It hasn't been as carefully reviewed as it perhaps should, so all readers
are encouraged, nay exhorted, to give this a close reading.

There are still a couple of oddities related to assigning to __name__,
but I intend to solicit python-dev's opinions on these.
2002-11-26 14:47:27 +00:00
Guido van Rossum 98f3373a8c A tweaked version of Jeremy's patch #642489, to produce better error
messages about MRO conflicts.  (The tweaks include correcting spelling
errors, some refactoring to get the name of classic classes, and a
style nit or two.)
2002-11-25 21:36:54 +00:00
Tim Peters 9a6b8d8cf8 Repaired illegal syntax most compilers probably let slide (but MSVC
treats as a fatal error).
2002-11-14 23:22:33 +00:00
Guido van Rossum 1f1213120e Use the new C3 MRO algorithm, implemented by Samuele Pedroni (SF patch
619475; also closing SF bug 618704).  I tweaked his code a bit for
style.

This raises TypeError for MRO order disagreements, which is an
improvement (previously these went undetected) but also a degradation:
what if the order disagreement doesn't affect any method lookups?
I don't think I care.
2002-11-14 19:49:16 +00:00
Neal Norwitz 673cd824ba Fix SF # 624982, Potential AV in slot_sq_item, by Greg Chapman
Don't crash when getting value of a property raises an exception
2002-10-18 16:33:13 +00:00
Guido van Rossum 3930bc35d0 Sigh. That wasn't a memory leak, that was Guido committing before
running tests.  Withdraw 2.183 and its backport.
2002-10-18 13:51:49 +00:00
Guido van Rossum 72297bb71e Fix memory leak in add_subclass() found by NealN with valgrind.
Will backport.
2002-10-18 13:41:47 +00:00
Guido van Rossum 6e5680fc83 For some reason (probably cut and paste), __ipow__ for new-style
classes was called with three arguments.  This makes no sense, there's
no way to pass in the "modulo" 3rd argument as for __pow__, and
classic classes don't do this.  [SF bug 620179]

I don't want to backport this to 2.2.2, because it could break
existing code that has developed a work-around.  Code in 2.2.2 that
wants to use __ipow__ and wants to be forward compatible with 2.3
should be written like this:

  def __ipow__(self, exponent, modulo=None):
      ...
2002-10-15 01:01:53 +00:00
Martin v. Löwis 13b1a5cc99 Don't drop old slots if _unicode_to_string did not change anything. 2002-10-14 21:11:34 +00:00
Martin v. Löwis d919a59ab5 Allow Unicode strings in __slots__, converting them to byte strings. 2002-10-14 21:07:28 +00:00
Guido van Rossum 84b2bed435 Squash a few calls to the hideously expensive PyObject_CallObject(o,a)
-- replace then with slightly faster PyObject_Call(o,a,NULL).  (The
difference is that the latter requires a to be a tuple; the former
allows other values and wraps them in a tuple if necessary; it
involves two more levels of C function calls to accomplish all that.)
2002-08-16 17:01:09 +00:00
Guido van Rossum 8e829200b1 Fix SF bug 595838 -- buffer in type_new() should not be static. Moved
to inner scope, too.
2002-08-16 03:47:49 +00:00
Guido van Rossum 323a9cfc83 PyType_Ready(): initialize the base class a bit earlier, so that if we
copy the metatype from the base, the base actually has one!
2002-08-14 17:26:30 +00:00
Neal Norwitz 5dc2a37f0f Allow more docstrings to be removed during compilation 2002-08-13 22:19:13 +00:00
Neal Norwitz d47714a727 Allow docstrings to be removed during compilation for *SLOT macro and friends 2002-08-13 19:01:38 +00:00
Neal Norwitz 858e34f649 Allow docstrings to be removed during compilation 2002-08-13 17:18:45 +00:00
Guido van Rossum 3747a0f04c Fix MSVC warnings. 2002-08-12 19:25:08 +00:00
Guido van Rossum ad47da072a Refactor how __dict__ and __weakref__ interact with __slots__.
1. You can now have __dict__ and/or __weakref__ in your __slots__
   (before only __weakref__ was supported).  This is treated
   differently than before: it merely sets a flag that the object
   should support the corresponding magic.

2. Dynamic types now always have descriptors __dict__ and __weakref__
   thrust upon them.  If the type in fact does not support one or the
   other, that descriptor's __get__ method will raise AttributeError.

3. (This is the reason for all this; it fixes SF bug 575229, reported
   by Cesar Douady.)  Given this code:
      class A(object): __slots__ = []
      class B(object): pass
      class C(A, B): __slots__ = []
   the class object for C was broken; its size was less than that of
   B, and some descriptors on B could cause a segfault.  C now
   correctly inherits __weakrefs__ and __dict__ from B, even though A
   is the "primary" base (C.__base__ is A).

4. Some code cleanup, and a few comments added.
2002-08-12 19:05:44 +00:00
Guido van Rossum 40af889081 Disallow class assignment completely unless both old and new are heap
types.  This prevents nonsense like 2.__class__ = bool or
True.__class__ = int.
2002-08-10 05:42:07 +00:00
Guido van Rossum 721f62e200 Major speedup for new-style class creation. Turns out there was some
trampolining going on with the tp_new descriptor, where the inherited
PyType_GenericNew was overwritten with the much slower slot_tp_new
which would end up calling tp_new_wrapper which would eventually call
PyType_GenericNew.  Add a special case for this to update_one_slot().

XXX Hope there isn't a loophole in this.  I'll buy the first person to
point out a bug in the reasoning a beer.

Backport candidate (but I won't do it).
2002-08-09 02:14:34 +00:00
Guido van Rossum 7bed213224 Significant speedup in new-style object creation: in slot_tp_new(),
intern the string "__new__" so we can call PyObject_GetAttr() rather
than PyObject_GetAttrString().  (Though it's a mystery why slot_tp_new
is being called when a class doesn't define __new__.  I'll look into
that tomorrow.)

2.2 backport candidate (but I won't do it).
2002-08-08 21:57:53 +00:00
Guido van Rossum febd61dc02 A modest speedup of object deallocation. call_finalizer() did rather
a lot of work: it had to save and restore the current exception around
a call to lookup_maybe(), because that could fail in rare cases, and
most objects don't have a __del__ method, so the whole exercise was
usually a waste of time.  Changed this to cache the __del__ method in
the type object just like all other special methods, in a new slot
tp_del.  So now subtype_dealloc() can test whether tp_del is NULL and
skip the whole exercise if it is.  The new slot doesn't need a new
flag bit: subtype_dealloc() is only called if the type was dynamically
allocated by type_new(), so it's guaranteed to have all current slots.
Types defined in C cannot fill in tp_del with a function of their own,
so there's no corresponding "wrapper".  (That functionality is already
available through tp_dealloc.)
2002-08-08 20:55:20 +00:00
Guido van Rossum 0906e07442 Fix a subtle bug in the trashcan code I added yesterday to
subtype_dealloc().

When call_finalizer() failed, it would return without going through
the trashcan end macro, thereby unbalancing the trashcan nesting level
counter, and thereby defeating the test case (slottrash() in
test_descr.py).  This in turn meant that the assert in the GC_UNTRACK
macro wasn't triggered by the slottrash() test despite a bug in the
code: _PyTrash_destroy_chain() calls the dealloc routine with an
object that's untracked, and the assert in the GC_UNTRACK macro would
fail on this; but because of an earlier test that resurrects an
object, causing call_finalizer() to fail and the trashcan nesting
level to be unbalanced, so _PyTrash_destroy_chain() was never called.
Calling the slottrash() test in isolation *did* trigger the assert,
however.

So the fix is twofold: (1) call the GC_UnTrack() function instead of
the GC_UNTRACK macro, because the function is safe when the object is
already untracked; (2) when call_finalizer() fails, jump to a label
that exits through the trashcan end macro, keeping the trashcan
nesting balanced.
2002-08-07 20:42:09 +00:00
Guido van Rossum 22b1387c51 Fix SF bug 574207 (chained __slots__ dealloc segfault).
This is inspired by SF patch 581742 (by Jonathan Hogg, who also
submitted the bug report, and two other suggested patches), but
separates the non-GC case from the GC case to avoid testing for GC
several times.

Had to fix an assert() from call_finalizer() that asserted that the
object wasn't untracked, because it's possible that the object isn't
GC'ed!
2002-08-06 21:41:44 +00:00
Guido van Rossum 0dbab4c560 SF patch 588728 (Nathan Srebro).
The __delete__ method wrapper for descriptors was not supported

(I added a test, too.)

2.2 bugfix candidate.
2002-08-01 14:39:25 +00:00