Commit Graph

150 Commits

Author SHA1 Message Date
Armin Rigo 314861c568 Minor bugs in the __index__ code (PEP 357), with tests. 2006-03-30 14:04:02 +00:00
Neal Norwitz badc086543 Stop duplicating code and handle slice indices consistently and correctly
wrt to ssize_t.
2006-03-23 06:03:08 +00:00
Neal Norwitz 29892cc386 Update function name to reflect params and stop casting to long to avoid losing data 2006-03-20 01:55:26 +00:00
Guido van Rossum 38fff8c4e4 Checking in the code for PEP 357.
This was mostly written by Travis Oliphant.
I've inspected it all; Neal Norwitz and MvL have also looked at it
(in an earlier incarnation).
2006-03-07 18:50:55 +00:00
Neal Norwitz 1fc4b776d4 Change some sequnce APIs to use Py_ssize_t. 2006-03-04 18:49:58 +00:00
Martin v. Löwis 15e62742fa Revert backwards-incompatible const changes. 2006-02-27 16:46:16 +00:00
Martin v. Löwis eb079f1c25 Use Py_ssize_t for counts and sizes.
Convert Py_ssize_t using PyInt_FromSsize_t
2006-02-16 14:32:27 +00:00
Martin v. Löwis 18e165558b Merge ssize_t branch. 2006-02-15 17:27:45 +00:00
Armin Rigo f5b3e36493 Renamed _length_cue() to __length_hint__(). See:
http://mail.python.org/pipermail/python-dev/2006-February/060524.html
2006-02-11 21:32:43 +00:00
Neal Norwitz 0c6e2f1640 Remove some shadowed variables 2006-01-08 06:13:44 +00:00
Armin Rigo fd163f92ce SF patch #1390657:
* set sq_repeat and sq_concat to NULL for user-defined new-style
  classes, as a way to fix a number of related problems.  See
  test_descr.notimplemented()).  One of these problems was fixed
  in r25556 and r25557 but many more existed; this is a general
  fix and thus reverts r25556-r25557.

* to avoid having PySequence_Repeat()/PySequence_Concat() failing
  on user-defined classes, they now fall back to nb_add/nb_mul if
  sq_concat/sq_repeat are not defined and the arguments appear to
  be sequences.

* added tests.

Backport candidate.
2005-12-29 15:59:19 +00:00
Raymond Hettinger 6b27cda643 Convert iterator __len__() methods to a private API. 2005-09-24 21:23:05 +00:00
Raymond Hettinger a710b331da SF bug #1242657: list(obj) can swallow KeyboardInterrupt
Fix over-aggressive PyErr_Clear().  The same code fragment appears in
various guises in list.extend(), map(), filter(), zip(), and internally
in PySequence_Tuple().
2005-08-21 11:03:59 +00:00
Michael W. Hudson 0edc7a03e2 Fix:
[ 1229429 ] missing Py_DECREF in PyObject_CallMethod

Add a test in test_enumerate, which is a bit random, but suffices
(reversed_new calls PyObject_CallMethod under some circumstances).
2005-07-12 10:21:19 +00:00
Brett Cannon c3647ac93e Make subclasses of int, long, complex, float, and unicode perform type
conversion using the proper magic slot (e.g., __int__()).  Also move conversion
code out of PyNumber_*() functions in the C API into the nb_* function.

Applied patch #1109424.  Thanks Walter Doewald.
2005-04-26 03:45:26 +00:00
Raymond Hettinger 193814c308 Small boost to PySequence_Fast(). Lists build faster than tuples for
unsized iterable inputs.
2004-12-18 19:00:59 +00:00
Raymond Hettinger e6bdb37e5b Add missing decref. 2004-12-16 15:10:21 +00:00
Raymond Hettinger 4d01259fb2 SF bug #1085744: Performance issues with PySequence_Tuple()
* Added missing error checks.
* Fixed O(n**2) growth pattern.  Modeled after lists to achieve linear
  amortized resizing.  Improves construction of "tuple(it)" when "it" is
  large and does not have a __len__ method.  Other cases are unaffected.
2004-12-16 10:38:38 +00:00
Raymond Hettinger 1be1a79ff9 SF bug #1030557: PyMapping_Check crashes when argument is NULL
Make PySequence_Check() and PyMapping_Check() handle NULL inputs.  This
goes beyond what most of the other checks do, but it is nice defensive
programming and solves the OP's problem.
2004-09-19 06:00:15 +00:00
Neil Schemenauer 3a313e3655 Check the type of values returned by __int__, __float__, __long__,
__oct__, and __hex__.  Raise TypeError if an invalid type is
returned.  Note that PyNumber_Int and PyNumber_Long can still
return ints or longs.  Fixes SF bug #966618.
2004-07-19 16:29:17 +00:00
Raymond Hettinger 10c660673e SF bug #952866: "can't multiply sequence *by* non-int"
Minor wording fix.
2004-05-12 21:35:06 +00:00
Raymond Hettinger ed9192e2ae Improve previous checkin to use a slot check instead of equivalent
attribute name lookup.
2004-04-05 08:14:48 +00:00
Raymond Hettinger e2eda606a8 Improve accuracy of sequence and mapping checks. 2004-04-04 08:51:41 +00:00
Tim Peters 5f112eb43b recursive_isinstance(), recursive_issubclass(): New code here returned
NULL in case of error, but the functions are declared to return int.
MSVC 6 properly complains about that.  Return -1 on error instead.
2004-03-21 16:59:09 +00:00
Brett Cannon 4f65331483 Limit the nesting depth of a tuple passed as the second argument to
isinstance() or issubclass() to the recursion limit of the interpreter.
2004-03-20 22:52:14 +00:00
Raymond Hettinger 969d8c0c8c Add missing decref 2004-03-17 05:24:23 +00:00
Raymond Hettinger 8ca92ae54c Eliminate a big block of duplicate code in PySequence_List() by
exposing _PyList_Extend().
2004-03-11 09:13:12 +00:00
Raymond Hettinger 2fb702966c SF Patch #871704: Py_SequenceFast can mask errors
(Contributed by Greg Chapman.)

Since this only changes the error message, I doubt that it should be
backported.
2004-01-11 23:26:51 +00:00
Raymond Hettinger b86269db45 Apply pre-sizing optimization to a broader class of objects.
Formerly, the length was only fetched from sequence objects.
Now, any object that reports its length can benefit from pre-sizing.
2004-01-04 11:00:08 +00:00
Raymond Hettinger 7832cd6141 Apply tuple/list pre-sizing optimization to a broader class of objects.
Formerly, length data fetched from sequence objects.
Now, any object that reports its length can benefit from pre-sizing.

On one sample timing, it gave a threefold speedup for list(s) where s
was a set object.
2004-01-04 06:08:16 +00:00
Raymond Hettinger 98779e0e36 Fix Greg Ward's error message nit: PyObject_SetItem and PySequenceSetItem
had slightly different error messages.
2003-10-27 09:22:16 +00:00
Raymond Hettinger 8049dde8d7 Removed duplicate test from inner loop.
The PyIter_Check is already performed by PyObject_GetIter.
2003-03-01 01:44:32 +00:00
Guido van Rossum 6921eca227 Make PyNumber_Check() a bit more careful, since all sorts of things
now have tp_as_number.  Check for nb_int or nb_float.
2003-02-18 16:36:28 +00:00
Guido van Rossum e5b130bcdb Add missing cast in previous fix. 2003-02-12 03:36:05 +00:00
Guido van Rossum 03bc7d3c4d SF #532767: isinstance(x, X) should work when x is a proxy for an X
instance, as long as x.__class__ is X or a subclass thereof.
Did a little cleanup of PyObject_IsInstance() too.
2003-02-12 03:32:58 +00:00
Guido van Rossum f6c9ba8457 Fold long lines. 2003-02-10 16:05:43 +00:00
Guido van Rossum a3a243e977 Merge to trunk from release branch:
Plug the leak that Tim just reported.
2002-12-31 19:50:03 +00:00
Neil Schemenauer d4b0fea43a Always try nb_* slots before trying sq_concat, sq_inplace_concat, sq_repeat,
andsq_inplace_repeat.  This fixes a number of corner case bugs (see #624807).

Consolidate the int and long sequence repeat code.  Before the change, integers
checked for integer overflow but longs did not.
2002-12-30 20:18:15 +00:00
Walter Dörwald 7e5c6a02eb Change issubclass() so that recursive tuples (directly or indirectly
containing class objects) are allowed as the second argument.
This makes issubclass() more similar to isinstance() where recursive
tuples are allowed too.
2002-12-12 19:14:08 +00:00
Walter Dörwald d9a6ad3beb Enhance issubclass() and PyObject_IsSubclass() so that a tuple is
supported as the second argument. This has the same meaning as
for isinstance(), i.e. issubclass(X, (A, B)) is equivalent
to issubclass(X, A) or issubclass(X, B). Compared to isinstance(),
this patch does not search the tuple recursively for classes, i.e.
any entry in the tuple that is not a class, will result in a
TypeError.

This closes SF patch #649608.
2002-12-12 16:41:44 +00:00
Raymond Hettinger c2e095f6f4 Fix typo in abstract.c which caused __rpow__ to not be invoked.
Added related testcase.
Closes SF bug #643260.
2002-12-07 10:05:27 +00:00
Neil Schemenauer b808c5cf2d Simplify use of NB_BINOP and NB_TERNOP by making them do the pointer
dereference rather than the caller.
2002-11-24 01:34:49 +00:00
Neil Schemenauer d46fbc322d Remove special handling of str and unicode in PyNumber_InPlaceRemainder. They
both have a nb_remainer slot.
2002-11-24 01:07:42 +00:00
Neil Schemenauer 26db587485 str and unicode objects now have a __mod__ slot so don't special case them in
PyNumber_Remainder().  This fixes SF bug #615506 and allows string and unicode
subclasses to override __mod__.
2002-11-18 16:04:52 +00:00
Michael W. Hudson 2801fe1c8f Use PyList_CheckExact and PyTuple_CheckExact for checking whether
PySequence_Fast needs to do anything siginificant.
2002-11-05 18:05:49 +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
Martin v. Löwis 75d2d94e0f Patch #554716: Use __va_copy where available. 2002-07-28 10:23:27 +00:00
Raymond Hettinger 478d47a168 Close SF bug 563740. complex() now finds __complex__() in new style classes.
Made conversion failure error messages consistent between types.
Added related unittests.
2002-06-06 15:45:38 +00:00
Thomas Heller aee2d5f975 Better isinstance error message.
Closes SF patch # 560250.

Bugfix candidate IMO.
2002-06-05 12:55:19 +00:00
Barry Warsaw f16951cffe abstract_get_bases(): Clarify exactly what the return values and
states can be for this function, and ensure that only AttributeErrors
are masked.  Any other exception raised via the equivalent of
getattr(cls, '__bases__') should be propagated up.

abstract_issubclass(): If abstract_get_bases() returns NULL, we must
call PyErr_Occurred() to see if an exception is being propagated, and
return -1 or 0 as appropriate.  This is the specific fix for a problem
whereby if getattr(derived, '__bases__') raised an exception, an
"undetected error" would occur (under a debug build).  This nasty
situation was uncovered when writing a security proxy extension type
for the Zope3 project, where the security proxy raised a Forbidden
exception on getattr of __bases__.

PyObject_IsInstance(), PyObject_IsSubclass(): After both calls to
abstract_get_bases(), where we're setting the TypeError if the return
value is NULL, we must first check to see if an exception occurred,
and /not/ mask an existing exception.

Neil Schemenauer should double check that these changes don't break
his ExtensionClass examples (there aren't any test cases for those
examples and abstract_get_bases() was added by him in response to
problems with ExtensionClass).  Neil, please add test cases if
possible!

I belive this is a bug fix candidate for Python 2.2.2.
2002-04-23 22:45:44 +00:00