Commit Graph

132 Commits

Author SHA1 Message Date
Tim Peters afb2c80b29 ceval.c/do_raise(): Tighten the test to disallow raising an instance of
a str subclass.

test_descr.py/string_exceptions():  New sub-test.

For 2.3 only.  Guido doesn't want this backported.
2002-04-18 18:06:20 +00:00
Guido van Rossum d99b3e78dd SF bug 542984.
Change type_get_doc (the get function for __doc__) to look in tp_dict
more often, and if it finds a descriptor in tp_dict, to call it (with
a NULL instance).  This means you can add a __doc__ descriptor to a
new-style class that returns instance docs when called on an instance,
and class docs when called on a class -- or the same docs in either
case, but lazily computed.

I'll also check this into the 2.2 maintenance branch.
2002-04-18 00:27:33 +00:00
Guido van Rossum e8fc640349 SF bug 544647.
PyNumber_InPlaceMultiply insisted on calling sq_inplace_repeat if it
existed, even if nb_inplace_multiply also existed and the arguments
weren't right for sq_inplace_repeat.  Change this to only use
sq_inplace_repeat if nb_inplace_multiply isn't defined.

Bugfix candidate.
2002-04-16 16:44:51 +00:00
Tim Peters 4d9b466ea5 Fewer deprecation warnings. 2002-04-16 01:59:17 +00:00
Guido van Rossum b6b8942f53 SF bug #541883 (Vincent Fiack).
A stupid bug in object_set_class(): didn't check for value==NULL
before checking its type.

Bugfix candidate.
2002-04-15 01:03:30 +00:00
Guido van Rossum 8ace1ab53a - Changed new-style class instantiation so that when C's __new__
method returns something that's not a C instance, its __init__ is
  not called.  [SF bug #537450]
2002-04-06 01:05:01 +00:00
Guido van Rossum 77f6a65eb0 Add the 'bool' type and its values 'False' and 'True', as described in
PEP 285.  Everything described in the PEP is here, and there is even
some documentation.  I had to fix 12 unit tests; all but one of these
were printing Boolean outcomes that changed from 0/1 to False/True.
(The exception is test_unicode.py, which did a type(x) == type(y)
style comparison.  I could've fixed that with a single line using
issubtype(x, type(y)), but instead chose to be explicit about those
places where a bool is expected.

Still to do: perhaps more documentation; change standard library
modules to return False/True from predicates.
2002-04-03 22:41:51 +00:00
Guido van Rossum 155db9aa22 SF patch 537536 by Phillip J. Eby, fix for SF bug 535444, super()
broken w/ classmethods.

Bugfix candidate.
2002-04-02 17:53:47 +00:00
Neal Norwitz b1295da59e There is no TestError, use TestFailed appropriately 2002-04-01 18:59:20 +00:00
Fred Drake f841aa6fc0 Add a simple test of the METH_CLASS and METH_STATIC flags for type methods. 2002-03-28 15:49:54 +00:00
Walter Dörwald dbd2d2560c Add tests for the iterkeys, itervalues and iteritems
methods in dict-proxy objects.
2002-03-25 18:36:32 +00:00
Guido van Rossum 7e30548285 Fix for SF bug 528132 (Armin Rigo): classmethod().__get__() segfault
The proper fix is not quite what was submitted; it's really better to
take the class of the object passed rather than calling PyMethod_New
with NULL pointer args, because that can then cause other core dumps
later.

I also added a testcase for the fix to classmethods() in test_descr.py.

I've already applied this to the 2.2 branch.
2002-03-18 03:09:06 +00:00
Guido van Rossum 8c84255316 "Fix" for SF bug #520644: __slots__ are not pickled.
As promised in my response to the bug report, I'm not really fixing
it; in fact, one could argule over what the proper fix should do.
Instead, I'm adding a little magic that raises TypeError if you try to
pickle an instance of a class that has __slots__ but doesn't define or
override __getstate__.  This is done by adding a bozo __getstate__
that always raises TypeError.

Bugfix candidate (also the checkin to typeobject.c, of course).
2002-03-14 23:05:54 +00:00
Guido van Rossum d70356729f Test for the fix I just checked in to moduleobject.c.
Bugfix candidate.
2002-03-12 20:43:31 +00:00
Neal Norwitz 1af5e35a98 Fix typo 2002-03-11 14:44:12 +00:00
Guido van Rossum c9e9e40b7b Add a check that SF bug 516727 is really fixed. 2002-03-11 13:21:25 +00:00
Guido van Rossum 15d529aec5 Bugfix candidate.
Adapter from SF patch 528038; fixes SF bug 527816.

The wrapper for __nonzero__ should be wrap_inquiry rather than
wrap_unaryfunc, since the slot returns an int, not a PyObject *.
2002-03-11 00:07:13 +00:00
Tim Peters 5329cdb3ce _PyLong_Copy(): was creating a copy of the absolute value, but should
copy the sign too.  Added a test to test_descr to ensure that it does.

Bugfix candidate.
2002-03-02 04:18:04 +00:00
Guido van Rossum 2eb0b87d14 SF patch 514641 (Naofumi Honda) - Negative ob_size of LongObjects
Due to the bizarre definition of _PyLong_Copy(), creating an instance
of a subclass of long with a negative value could cause core dumps
later on.  Unfortunately it looks like the behavior of _PyLong_Copy()
is quite intentional, so the fix is more work than feels comfortable.

This fix is almost, but not quite, the code that Naofumi Honda added;
in addition, I added a test case.
2002-03-01 22:24:49 +00:00
Guido van Rossum 2764a3a50e Fix for SF bug ##497426: can't deepcopy recursive new objects
deepcopy(), _reconstruct(): pass the memo to the other function, so
that recursive data structures built out of new-style objects may be
deeply copied correctly.

2.2.1 bugfix!
2001-12-28 21:39:03 +00:00
Guido van Rossum e54616cb6f (Merge into trunk.)
Fix for SF bug #492345.  (I could've sworn I checked this in, but
apparently I didn't!)

This code:

    class Classic:
        pass

    class New(Classic):
        __metaclass__ = type

attempts to create a new-style class with only classic bases -- but it
doesn't work right.  Attempts to fix it so it works caused problems
elsewhere, so I'm now raising a TypeError in this case.
2001-12-14 04:19:56 +00:00
Guido van Rossum 29d260670e Additional coverage tests by Neil Norwitz.
(SF patch #491418, #491420, #491421.)
2001-12-11 04:37:34 +00:00
Tim Peters 3caca2326e SF bug #488514: -Qnew needs work
Big Hammer to implement -Qnew as PEP 238 says it should work (a global
option affecting all instances of "/").

pydebug.h, main.c, pythonrun.c:  define a private _Py_QnewFlag flag, true
iff -Qnew is passed on the command line.  This should go away (as the
comments say) when true division becomes The Rule.  This is
deliberately not exposed to runtime inspection or modification:  it's
a one-way one-shot switch to pretend you're using Python 3.

ceval.c:  when _Py_QnewFlag is set, treat BINARY_DIVIDE as
BINARY_TRUE_DIVIDE.

test_{descr, generators, zipfile}.py:  fiddle so these pass under
-Qnew too.  This was just a matter of s!/!//! in test_generators and
test_zipfile.  test_descr was trickier, as testbinop() is passed
assumptions that "/" is the same as calling a "__div__" method; put
a temporary hack there to call "__truediv__" instead when the method
name is "__div__" and 1/2 evaluates to 0.5.

Three standard tests still fail under -Qnew (on Windows; somebody
please try the Linux tests with -Qnew too!  Linux runs a whole bunch
of tests Windows doesn't):
    test_augassign
    test_class
    test_coercion
I can't stay awake longer to stare at this (be my guest).  Offhand
cures weren't obvious, nor was it even obvious that cures are possible
without major hackery.

Question:  when -Qnew is in effect, should calls to __div__ magically
change into calls to __truediv__?  See "major hackery" at tail end of
last paragraph <wink>.
2001-12-06 06:23:26 +00:00
Guido van Rossum 33bab01da6 Fix SF bug #489581: __slots__ leak.
It was easier than I thought, assuming that no other things contribute
to the instance size besides slots -- a pretty good bet.  With a test
suite, no less!
2001-12-05 22:45:48 +00:00
Guido van Rossum d331cb5502 At the PythonLabs meeting someone mentioned it would make Jim really
happy if one could delete the __dict__ attribute of an instance.  I
love to make Jim happy, so here goes...

- New-style objects now support deleting their __dict__.  This is for
  all intents and purposes equivalent to assigning a brand new empty
  dictionary, but saves space if the object is not used further.
2001-12-05 19:46:42 +00:00
Guido van Rossum 6b70599450 Fix SF bug #486144: Uninitialized __slot__ vrbl is None.
There's now a new structmember code, T_OBJECT_EX, which is used for
all __slot__ variables (except __weakref__, which has special behavior
anyway).  This new code raises AttributeError when the variable is
NULL rather than converting NULL to None.
2001-12-04 16:23:42 +00:00
Guido van Rossum dbb53d9918 Fix of SF bug #475877 (Mutable subtype instances are hashable).
Rather than tweaking the inheritance of type object slots (which turns
out to be too messy to try), this fix adds a __hash__ to the list and
dict types (the only mutable types I'm aware of) that explicitly
raises an error.  This has the advantage that list.__hash__([]) also
raises an error (previously, this would invoke object.__hash__([]),
returning the argument's address); ditto for dict.__hash__.

The disadvantage for this fix is that 3rd party mutable types aren't
automatically fixed.  This should be added to the rules for creating
subclassable extension types: if you don't want your object to be
hashable, add a tp_hash function that raises an exception.

Also, it's possible that I've forgotten about other mutable types for
which this should be done.
2001-12-03 16:32:18 +00:00
Guido van Rossum 5b443c6282 Address SF patch #480716 as well as related issues.
SF patch #480716 by Greg Chapman fixes the problem that super's
__get__ method always returns an instance of super, even when the
instance whose __get__ method is called is an instance of a subclass
of super.

Other issues fixed:

- super(C, C()).__class__ would return the __class__ attribute of C()
  rather than the __class__ attribute of the super object.  This is
  confusing.  To fix this, I decided to change the semantics of super
  so that it only applies to code attributes, not to data attributes.
  After all, overriding data attributes is not supported anyway.

- While super(C, x) carefully checked that x is an instance of C,
  super(C).__get__(x) made no such check, allowing for a loophole.
  This is now fixed.
2001-12-03 15:38:28 +00:00
Guido van Rossum 90c45142d7 - Change all remaining assertions into verify() and vereq() calls.
- Add tests for the recent fixes to copy_reg.py:
  __getstate__/__setstate__ and mixed inheritance from new+classic
  classes.
2001-11-24 21:07:01 +00:00
Tim Peters 144b98dab8 More simple test cases for mixed classic+new multiple inheritance. 2001-11-14 23:56:45 +00:00
Tim Peters a91e9646e0 Changing diapers reminded Guido that he wanted to allow for some measure
of multiple inheritance from a mix of new- and classic-style classes.
This is his patch, plus a start at some test cases from me.  Will check
in more, plus a NEWS blurb, later tonight.
2001-11-14 23:32:33 +00:00
Tim Peters c2fe618575 Fix bad bug in structseq slicing (NULL pointers in result). Reported by
Jack Jansen on python-dev.
Add simple test case.
Move vereq() from test_descr to test_support (it's handy!).
2001-10-30 23:20:46 +00:00
Tim Peters 9e6a399b14 To cover a recent checkin, added a test to ensure dir(None) == dir(Ellipsis). 2001-10-30 05:45:26 +00:00
Tim Peters fe677e2012 Just changed some continued-line indentation to read better, due to
the earlier s/dictionary/dict/ change.
2001-10-30 05:41:07 +00:00
Guido van Rossum ed87ad876b Minimal test for __del__ hook. 2001-10-30 02:33:02 +00:00
Tim Peters a427a2b8d0 Rename "dictionary" (type and constructor) to "dict". 2001-10-29 22:25:45 +00:00
Tim Peters 4d85953fe6 dictionary() constructor:
+ Change keyword arg name from "x" to "items".  People passing a mapping
  object can stretch their imaginations <wink>.
+ Simplify the docstring text.
2001-10-27 18:27:48 +00:00
Tim Peters 9fda73cdd1 dict_constructor(): The last test was passing for the wrong reason (it
was intended to verify that sub-sequences of lengths 1 and 3 raise
ValueError, but was actually testing string lengths).
2001-10-26 20:57:38 +00:00
Tim Peters 1fc240e851 Generalize dictionary() to accept a sequence of 2-sequences. At the
outer level, the iterator protocol is used for memory-efficiency (the
outer sequence may be very large if fully materialized); at the inner
level, PySequence_Fast() is used for time-efficiency (these should
always be sequences of length 2).

dictobject.c, new functions PyDict_{Merge,Update}FromSeq2.  These are
wholly analogous to PyDict_{Merge,Update}, but process a sequence-of-2-
sequences argument instead of a mapping object.  For now, I left these
functions file static, so no corresponding doc changes.  It's tempting
to change dict.update() to allow a sequence-of-2-seqs argument too.

Also changed the name of dictionary's keyword argument from "mapping"
to "x".  Got a better name?  "mapping_or_sequence_of_pairs" isn't
attractive, although more so than "mosop" <wink>.

abstract.h, abstract.tex:  Added new PySequence_Fast_GET_SIZE function,
much faster than going thru the all-purpose PySequence_Size.

libfuncs.tex:
- Document dictionary().
- Fiddle tuple() and list() to admit that their argument is optional.
- The long-winded repetitions of "a sequence, a container that supports
  iteration, or an iterator object" is getting to be a PITA.  Many
  months ago I suggested factoring this out into "iterable object",
  where the definition of that could include being explicit about
  generators too (as is, I'm not sure a reader outside of PythonLabs
  could guess that "an iterator object" includes a generator call).
- Please check my curly braces -- I'm going blind <0.9 wink>.

abstract.c, PySequence_Tuple():  When PyObject_GetIter() fails, leave
its error msg alone now (the msg it produces has improved since
PySequence_Tuple was generalized to accept iterable objects, and
PySequence_Tuple was also stomping on the msg in cases it shouldn't
have even before PyObject_GetIter grew a better msg).
2001-10-26 05:06:50 +00:00
Guido van Rossum 6661be3bed Allow assignment to newinstance.__dict__. 2001-10-26 04:26:12 +00:00
Fred Drake 4bf018b138 Fixed denial-of-weak-ref-support test; Jeremy changed the error message
used by the weakref code since he didn't like the word "referencable".
Is it really necessary to be more specific than to test for TypeError here,
though?
2001-10-22 21:45:25 +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
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
Tim Peters c993315b18 SF bug [#468061] __str__ ignored in str subclass.
object.c, PyObject_Str:  Don't try to optimize anything except exact
string objects here; in particular, let str subclasses go thru tp_str,
same as non-str objects.  This allows overrides of tp_str to take
effect.

stringobject.c:
+ string_print (str's tp_print):  If the argument isn't an exact string
  object, get one from PyObject_Str.

+ string_str (str's tp_str):  Make a genuine-string copy of the object if
  it's of a proper str subclass type.  str() applied to a str subclass
  that doesn't override __str__ ends up here.

test_descr.py:  New str_of_str_subclass() test.
2001-10-16 20:18:24 +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
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 12b22ff6d7 Add a bunch of tests for a list subclass that would have caught the
previous embarrassment (typeobject.c checking crashing minidom).
2001-10-09 20:36:44 +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
Guido van Rossum 4570455813 Change all occurrences of verify(x == y) into vereq(x, y), since when
this type of test fails, vereq() does a better job of reporting than
verify().

Change vereq(x, y) to use "not x == y" rather than "x != y" -- it
makes a difference is some overloading tests.
2001-10-08 16:35:45 +00:00