Commit Graph

69 Commits

Author SHA1 Message Date
Tim Peters 4324aa3572 Cruft cleanup: Removed the unused last_is_sticky argument from the internal
_PyTuple_Resize().
2001-05-28 22:30:08 +00:00
Tim Peters cb8d368b82 Reimplement PySequence_Contains() and instance_contains(), so they work
safely together and don't duplicate logic (the common logic was factored
out into new private API function _PySequence_IterContains()).
Visible change:
    some_complex_number  in  some_instance
no longer blows up if some_instance has __getitem__ but neither
__contains__ nor __iter__.  test_iter changed to ensure that remains true.
2001-05-05 21:05:01 +00:00
Tim Peters 75f8e35ef4 Generalize PySequence_Count() (operator.countOf) to work with iterators. 2001-05-05 11:33:43 +00:00
Tim Peters de9725f135 Make 'x in y' and 'x not in y' (PySequence_Contains) play nice w/ iterators.
NEEDS DOC CHANGES
A few more AttributeErrors turned into TypeErrors, but in test_contains
this time.
The full story for instance objects is pretty much unexplainable, because
instance_contains() tries its own flavor of iteration-based containment
testing first, and PySequence_Contains doesn't get a chance at it unless
instance_contains() blows up.  A consequence is that
    some_complex_number in some_instance
dies with a TypeError unless some_instance.__class__ defines __iter__ but
does not define __getitem__.
2001-05-05 10:06:17 +00:00
Tim Peters 12d0a6c78a Fix a tiny and unlikely memory leak. Was there before too, and actually
several of these turned up and got fixed during the iteration crusade.
2001-05-05 04:10:25 +00:00
Tim Peters 6912d4ddf0 Generalize tuple() to work nicely with iterators.
NEEDS DOC CHANGES.
This one surprised me!  While I expected tuple() to be a no-brainer, turns
out it's actually dripping with consequences:
1. It will *allow* the popular PySequence_Fast() to work with any iterable
   object (code for that not yet checked in, but should be trivial).
2. It caused two std tests to fail.  This because some places used
   PyTuple_Sequence() (the C spelling of tuple()) as an indirect way to test
   whether something *is* a sequence.  But tuple() code only looked for the
   existence of sq->item to determine that, and e.g. an instance passed
   that test whether or not it supported the other operations tuple()
   needed (e.g., __len__).  So some things the tests *expected* to fail
   with an AttributeError now fail with a TypeError instead.  This looks
   like an improvement to me; e.g., test_coercion used to produce 559
   TypeErrors and 2 AttributeErrors, and now they're all TypeErrors.  The
   error details are more informative too, because the places calling this
   were *looking* for TypeErrors in order to replace the generic tuple()
   "not a sequence" msg with their own more specific text, and
   AttributeErrors snuck by that.
2001-05-05 03:56:37 +00:00
Tim Peters f4848dac41 Make PyIter_Next() a little smarter (wrt its knowledge of iterator
internals) so clients can be a lot dumber (wrt their knowledge).
2001-05-05 00:14:56 +00:00
Tim Peters 6ad22c41c2 Plug a memory leak in list(), when appending to the result list. 2001-05-02 07:12:39 +00:00
Tim Peters f553f89d45 Generalize list(seq) to work with iterators. This also generalizes list()
to no longer insist that len(seq) be defined.
NEEDS DOC CHANGES.
This is meant to be a model for how other functions of this ilk (max,
filter, etc) can be generalized similarly.  Feel encouraged to grab your
favorite and convert it!
Note some cute consequences:
    list(file) == file.readlines() == list(file.xreadlines())
    list(dict) == dict.keys()
    list(dict.iteritems()) = dict.items()
    list(xrange(i, j, k)) == range(i, j, k)
2001-05-01 20:45:31 +00:00
Guido van Rossum 213c7a6aa5 Mondo changes to the iterator stuff, without changing how Python code
sees it (test_iter.py is unchanged).

- Added a tp_iternext slot, which calls the iterator's next() method;
  this is much faster for built-in iterators over built-in types
  such as lists and dicts, speeding up pybench's ForLoop with about
  25% compared to Python 2.1.  (Now there's a good argument for
  iterators. ;-)

- Renamed the built-in sequence iterator SeqIter, affecting the C API
  functions for it.  (This frees up the PyIter prefix for generic
  iterator operations.)

- Added PyIter_Check(obj), which checks that obj's type has a
  tp_iternext slot and that the proper feature flag is set.

- Added PyIter_Next(obj) which calls the tp_iternext slot.  It has a
  somewhat complex return condition due to the need for speed: when it
  returns NULL, it may not have set an exception condition, meaning
  the iterator is exhausted; when the exception StopIteration is set
  (or a derived exception class), it means the same thing; any other
  exception means some other error occurred.
2001-04-23 14:08:49 +00:00
Guido van Rossum 59d1d2b434 Iterators phase 1. This comprises:
new slot tp_iter in type object, plus new flag Py_TPFLAGS_HAVE_ITER
new C API PyObject_GetIter(), calls tp_iter
new builtin iter(), with two forms: iter(obj), and iter(function, sentinel)
new internal object types iterobject and calliterobject
new exception StopIteration
new opcodes for "for" loops, GET_ITER and FOR_ITER (also supported by dis.py)
new magic number for .pyc files
new special method for instances: __iter__() returns an iterator
iteration over dictionaries: "for x in dict" iterates over the keys
iteration over files: "for x in file" iterates over lines

TODO:

documentation
test suite
decide whether to use a different way to spell iter(function, sentinal)
decide whether "for key in dict" is a good idea
use iterators in map/filter/reduce, min/max, and elsewhere (in/not in?)
speed tuning (make next() a slot tp_next???)
2001-04-20 19:13:02 +00:00
Guido van Rossum 823649d544 Move the code implementing isinstance() and issubclass() to new C
APIs, PyObject_IsInstance() and PyObject_IsSubclass() -- both
returning an int, or -1 for errors.
2001-03-21 18:40:58 +00:00
Guido van Rossum c31896960a Rich comparisons fall-out:
- Renamed Py_TPFLAGS_NEWSTYLENUMBER to Py_TPFLAGS_CHECKTYPES.

- Use PyObject_RichCompareBool() in PySequence_Contains().
2001-01-17 15:29:42 +00:00
Neil Schemenauer 5a1f015bee Massive changes as per PEP 208. Read it for details. 2001-01-04 01:39:06 +00:00
Thomas Wouters dc9100f57d Fix for SF bug #115987: PyInstance_HalfBinOp does not initialize the
result-object-pointer that is passed in, when an exception occurs during
coercion. The pointer has to be explicitly initialized in the caller to avoid
putting trash on the Python stack.
2000-10-05 12:43:25 +00:00
Thomas Wouters f2b332dc7e Cosmetic cleanup by Vladimir. 2000-09-02 08:34:40 +00:00
Guido van Rossum bb8be93a50 Rewritten some pieces of PyNumber_InPlaceAdd() for clarity. 2000-09-01 23:27:32 +00:00
Thomas Wouters cadd5b6b58 Fix grouping, again. This time properly :-) Sorry, guys. 2000-09-01 07:53:25 +00:00
Guido van Rossum 04127de434 Add parens suggested by gcc -Wall. 2000-09-01 02:39:00 +00:00
Thomas Wouters 6b958f7d7b Fix grouping: this is how I intended it, misguided as I was in boolean
operator associativity.
2000-08-31 07:02:19 +00:00
Fred Drake 562f62aa9b Removed compiler warning about wanting explicit grouping around &&
expression next to a || expression; this is a readability-inspired
warning from GCC.
2000-08-31 05:15:44 +00:00
Thomas Wouters e289e0bd0c Support for the in-place operations introduced by augmented assignment. Only
the list object supports this currently, but other candidates are
gladly accepted (like arraymodule and such.)
2000-08-24 20:08:19 +00:00
Thomas Wouters e266e42c9c Addendum to previous change: now that 'f' is not unconditionally
initialized in the 'if (..)', do so manually.
2000-08-23 23:31:34 +00:00
Thomas Wouters bf6cfa5f8e Add extra check on whether 'tp_as_number' is still non-NULL after coercion,
in the PyNumber_* functions. Also, remove unnecessary tests from
PyNumber_Multiply: after BINOP(), neither argument can be an instance.
2000-08-23 23:16:10 +00:00
Thomas Wouters 1d75a79c00 Apply SF patch #101029: call __getitem__ with a proper slice object if there
is no __getslice__ available. Also does the same for C extension types.
Includes rudimentary documentation (it could use a cross reference to the
section on slice objects, I couldn't figure out how to do that) and a test
suite for all Python __hooks__ I could think of, including the new
behaviour.
2000-08-17 22:37:32 +00:00
Guido van Rossum c4a19e7fe9 Remobe beopen/cnri/cwi copyrights, according to CNRI instructions.
This doesn't change the copyright status for these files -- just the
markings!  Doing it on the main branch for these three files for which
the HEAD revision was pushed back into 1.6.
2000-08-03 16:42:14 +00:00
Thomas Wouters a534594fc7 ANSIfication: remove very-old-varargs code, fix function declarations so
they include prototypes.
2000-07-22 23:59:33 +00:00
Marc-André Lemburg cf5f358784 Restore PyXXX_Length() APIs for binary compatibility.
New code will see the macros and therefore use the PyXXX_Size()
APIs instead.
By Thomas Wouters.
2000-07-17 09:22:55 +00:00
Jeremy Hylton 6253f83b0a change abstract size functions PySequence_Size &c.
add macros for backwards compatibility with C source
2000-07-12 12:56:19 +00:00
Fred Drake 4201b9e420 type_error(): Added "const" to signature to eliminate warning with -Wall. 2000-07-09 04:34:13 +00:00
Fred Drake 799124718d ANSI-fication of the sources. 2000-07-09 04:06:11 +00:00
Tim Peters dbd9ba6a6c Nuke all remaining occurrences of Py_PROTO and Py_FPROTO. 2000-07-09 03:09:57 +00:00
Guido van Rossum ffcc3813d8 Change copyright notice - 2nd try. 2000-06-30 23:58:06 +00:00
Guido van Rossum fd71b9e9d4 Change copyright notice. 2000-06-30 23:50:40 +00:00
Andrew M. Kuchling 74042d6e5d Patch from /F:
this patch introduces PySequence_Fast and PySequence_Fast_GET_ITEM,
and modifies the list.extend method to accept any kind of sequence.
2000-06-18 18:43:14 +00:00
Guido van Rossum 9e896b37c7 Marc-Andre's third try at this bulk patch seems to work (except that
his copy of test_contains.py seems to be broken -- the lines he
deleted were already absent).  Checkin messages:


New Unicode support for int(), float(), complex() and long().

- new APIs PyInt_FromUnicode() and PyLong_FromUnicode()
- added support for Unicode to PyFloat_FromString()
- new encoding API PyUnicode_EncodeDecimal() which converts
  Unicode to a decimal char* string (used in the above new
  APIs)
- shortcuts for calls like int(<int object>) and float(<float obj>)
- tests for all of the above

Unicode compares and contains checks:
- comparing Unicode and non-string types now works; TypeErrors
  are masked, all other errors such as ValueError during
  Unicode coercion are passed through (note that PyUnicode_Compare
  does not implement the masking -- PyObject_Compare does this)
- contains now works for non-string types too; TypeErrors are
  masked and 0 returned; all other errors are passed through

Better testing support for the standard codecs.

Misc minor enhancements, such as an alias dbcs for the mbcs codec.

Changes:
- PyLong_FromString() now applies the same error checks as
  does PyInt_FromString(): trailing garbage is reported
  as error and not longer silently ignored. The only characters
  which may be trailing the digits are 'L' and 'l' -- these
  are still silently ignored.
- string.ato?() now directly interface to int(), long() and
  float(). The error strings are now a little different, but
  the type still remains the same. These functions are now
  ready to get declared obsolete ;-)
- PyNumber_Int() now also does a check for embedded NULL chars
  in the input string; PyNumber_Long() already did this (and
  still does)

Followed by:

Looks like I've gone a step too far there... (and test_contains.py
seem to have a bug too).

I've changed back to reporting all errors in PyUnicode_Contains()
and added a few more test cases to test_contains.py (plus corrected
the join() NameError).
2000-04-05 20:11:21 +00:00
Guido van Rossum 4c08d554b9 Many changes for Unicode, by Marc-Andre Lemburg. 2000-03-10 22:55:18 +00:00
Guido van Rossum 9611e0b462 Patch by Moshe Zadka: remove the string special case in
PySequence_Contains() now that string objects have this code in their
tp_contains.
2000-03-07 15:54:45 +00:00
Guido van Rossum 46c6b20392 Patch by Mozhe Zadka, for __contains__ (overloading 'in'). This
patches PySequence_Contains() to check for a valid sq_contains field.
More to follow.
2000-02-28 15:01:46 +00:00
Andrew M. Kuchling 0f223d2418 Allow using long integers as arguments to PyObject_GetItem(), _SetItem(),
and _DelItem().
In sequence multiplication by a long, only call PyErr_Occurred() when the
    value returned is -1.
2000-02-23 22:21:50 +00:00
Andrew M. Kuchling 1991ddc3e1 Make multiplying a sequence by a long integer (5L * 'b') legal 2000-02-14 22:22:04 +00:00
Barry Warsaw 226ae6ca12 Mainlining the string_methods branch. See branch revision log
messages for specific changes.
1999-10-12 19:54:53 +00:00
Guido van Rossum 031d0e5feb Patch by Charles Waldman -- remove unneeded and even harmful test for
float to the negative power (which is already and better done in
floatobject.c.)
1999-01-10 16:56:58 +00:00
Guido van Rossum f5046d1aea Remove prototype for PyOS_strtol -- Chris Herborth. 1998-12-10 16:54:48 +00:00
Guido van Rossum 9d904b9389 Believe it or not, Solaris 2.6 strtod() can move the end pointer
*beyond* the null byte at the end of the input string, when the input
is inf(inity).  Discovered by Greg Ward.
1998-10-01 20:35:46 +00:00
Guido van Rossum 21308243ca Better error messages when a sequence is indexed with a non-integer.
Previously, this said "unsubscriptable object"; in 1.5.1, the reverse
problem existed, where None[''] would complain about a non-integer
index.  This fix does the right thing in all cases (for get, set and
del item).
1998-08-13 16:44:44 +00:00
Guido van Rossum df3d8756b7 Better error messages when raising ValueError for int and long
literals.  (The previous version of this code would not show the
offending input, even though there was code that attempted this.)
1998-08-04 15:02:01 +00:00
Guido van Rossum 5dba9e8aef Add special case to PySequence_List() so that list() of a list is
faster (using PyList_GetSlice()).  Also added a test for a NULL
argument, as with PySequence_Tuple().  (Hmm...  Better names for these
two would be PyList_FromSequence() and PyTuple_FromSequence().  Oh well.)
1998-07-10 18:03:50 +00:00
Guido van Rossum bfc725bf64 Changed PySequence_List() and PySequence_Tuple() to support
"indefinite length" sequences.  These should still have a length, but
the length is only used as a hint -- the actual length of the sequence
is determined by the item that raises IndexError, which may be either
smaller or larger than what len() returns.  (This is a novelty; map(),
filter() and reduce() only allow the actual length to be larger than
what len() returns, not shorter.  I'll fix that shortly.)
1998-07-10 16:22:44 +00:00
Guido van Rossum 3b2b34790f Fix the tests for various anomalies in the string-to-numbers
conversions.  Formerly, for example, int('-') would return 0 instead
of raising ValueError, and int(' 0') would raise ValueError
(complaining about a null byte!) instead of 0...
1998-06-22 03:54:15 +00:00