Commit Graph

2458 Commits

Author SHA1 Message Date
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
Neal Norwitz 82c5a86d7c Oops, this is supposed to be disabled by default. 2006-02-16 07:30:11 +00:00
Martin v. Löwis e0e89f7920 Revert 42400. 2006-02-16 06:59:22 +00:00
Martin v. Löwis 2c95cc6d72 Support %zd in PyErr_Format and PyString_FromFormat. 2006-02-16 06:54:25 +00:00
Neal Norwitz 26efe402c2 Get rid of compiler warnings (gcc 3.3.4 on x86) 2006-02-16 06:21:57 +00:00
Tim Peters 15231548d2 doubletounicode(), longtounicode():
Py_SAFE_DOWNCAST can evaluate its first argument multiple
times in a debug build.  This caused two distinct assert-
failures in test_unicode run under a debug build.  Rewrote
the code in trivial ways so that multiple evaluation of the
first argument doesn't hurt.
2006-02-16 01:08:01 +00:00
Thomas Wouters 4701af5bf5 Remove two unused Py_ssize_t variables (merge glitches, looks like.) 2006-02-15 23:10:32 +00:00
Thomas Wouters b1410fb433 Avoid unused variables when SIZEOF_SIZE_T == SIZEOF_LONG. Also normalize
whitespace.
2006-02-15 23:08:56 +00:00
Martin v. Löwis 18e165558b Merge ssize_t branch. 2006-02-15 17:27:45 +00:00
Armin Rigo 967aa8b349 * Refcount leak. It was just a reference to Py_None, but still.
* Allow the 3rd argument to generator.throw() to be None.
  The 'raise' statement does the same, and anyway it follows the
  general policy that optional arguments of built-ins should, when
  reasonable, have a default value specifiable from Python.
2006-02-14 15:50:44 +00:00
Thomas Wouters c45251a485 SF patch #1397960: When mixing file-iteration and
readline/readlines/read/readinto, loudly break by raising ValueError, rather
than silently deliver data out of order or hitting EOF prematurely.

Probably not a bugfix candidate, even though it affects no 'working' code.
2006-02-12 11:53:32 +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
Thomas Wouters 553489ab1d As discussed on python-dev, silence three gcc-4.0.x warnings, using assert()
to protect against actual uninitialized usage.

Objects/longobject.c: In function ‘PyLong_AsDouble’:
Objects/longobject.c:655: warning: ‘e’ may be used uninitialized in this function

Objects/longobject.c: In function ‘long_true_divide’:
Objects/longobject.c:2263: warning: ‘aexp’ may be used uninitialized in this function
Objects/longobject.c:2263: warning: ‘bexp’ may be used uninitialized in this function
2006-02-01 21:32:04 +00:00
Neal Norwitz bab05c9604 Fix SF #1412837, compile failed with Watcom compiler 2006-01-24 06:06:11 +00:00
Neal Norwitz fc76d633e8 - Patch #1400181, fix unicode string formatting to not use the locale.
This is how string objects work.  u'%f' could use , instead of .
  for the decimal point.  Now both strings and unicode always use periods.

This is the code that would break:

import locale
locale.setlocale(locale.LC_NUMERIC, 'de_DE')
u'%.1f' % 1.0
assert '1.0' == u'%.1f' % 1.0

I couldn't create a test case which fails, but this fixes the problem.

Will backport.
2006-01-10 06:03:13 +00:00
Neal Norwitz 0c6e2f1640 Remove some shadowed variables 2006-01-08 06:13:44 +00:00
Neal Norwitz 76dc081dd9 strlen() returns a size_t, get rid of 64-bit warning 2006-01-08 06:13:13 +00:00
Neal Norwitz d43069ce95 Fix icc warnings: remove (sometimes) unused variable conditionally 2006-01-08 01:12:10 +00:00
Neal Norwitz b2da01b27c Fix icc warnings: remove unused variable 2006-01-08 01:11:25 +00:00
Martin v. Löwis dea59e5755 Stop maintaining the buildno file.
Also, stop determining Unicode sizes with PyString_GET_SIZE.
2006-01-05 10:00:36 +00:00
Neal Norwitz 50bf51a3a9 Fix ref/memory leak introduced in rev 41845. 2006-01-02 02:46:54 +00:00
Tim Peters 60b29961dc Fixed English in a comment; trimmed trailing whitespace;
no code changes.
2006-01-01 01:19:23 +00:00
Armin Rigo 037d1e0ff3 SF bug #1153075: "PyXxx_Check(x) trusts x->ob_type->tp_mro".
A patch by mwh to check that user-defined mro's are reasonable
enough.
2005-12-29 17:07:39 +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
Neal Norwitz 7c460740ed Check return result for error 2005-12-18 08:02:38 +00:00
Hye-Shik Chang 835b243c71 Bug #1379994: Fix *unicode_escape codecs to encode r'\' as r'\\'
just like string codecs.
2005-12-17 04:38:31 +00:00
Neal Norwitz a716eabca7 Revert r41662 and the part of 41552 that originally caused the problem
(calling ftell(stdin) doesn't seem defined).  So we won't test errors
from ftell unless we can do it portably.
2005-12-15 05:25:09 +00:00
Hye-Shik Chang e237d50390 Add a workaround for file.ftell() to raise IOError for ttys.
ftell(3) on BSD doesn't set errno even for ttys and returns useless
values.
2005-12-13 16:44:02 +00:00
Neal Norwitz ba2fa637d6 en_sit will be freed when en is DECREF'd. Don't double free. 2005-12-11 20:55:10 +00:00
Jeremy Hylton af68c874a6 Add const to several API functions that take char *.
In C++, it's an error to pass a string literal to a char* function
without a const_cast().  Rather than require every C++ extension
module to put a cast around string literals, fix the API to state the
const-ness.

I focused on parts of the API where people usually pass literals:
PyArg_ParseTuple() and friends, Py_BuildValue(), PyMethodDef, the type
slots, etc.  Predictably, there were a large set of functions that
needed to be fixed as a result of these changes.  The most pervasive
change was to make the keyword args list passed to
PyArg_ParseTupleAndKewords() to be a const char *kwlist[].

One cast was required as a result of the changes:  A type object
mallocs the memory for its tp_doc slot and later frees it.
PyTypeObject says that tp_doc is const char *; but if the type was
created by type_new(), we know it is safe to cast to char *.
2005-12-10 18:50:16 +00:00
Michael W. Hudson b78a5fc004 Fix bug
[ 1346144 ] Segfaults from unaligned loads in floatobject.c

by using memcpy and not just blinding casting char* to double*.

Thanks to Rune Holm for the report.
2005-12-05 00:27:49 +00:00
Walter Dörwald d4fff1731c Fix leaked reference to None. 2005-11-28 22:15:56 +00:00
Neal Norwitz e5e5aa4ea6 Do a better job of not inlining Py_ADDRESS_IN_RANGE() for newer gcc's.
Perhaps Py_NO_INLINE should be moved to pyport.h or some other header?
2005-11-13 18:55:39 +00:00
Neal Norwitz 6576bd844f Prevent name pollution by making lots of internal functions static. 2005-11-13 18:41:28 +00:00
Armin Rigo c6686b7c7e Added proper reflection on instances of <type 'method-wrapper'>, e.g.
'[].__add__', to match what the other internal descriptor types provide:
'__objclass__' attribute, '__self__' member, and reasonable repr and
comparison.

Added a test.
2005-11-07 08:38:00 +00:00
Andrew M. Kuchling 8294de5673 Another comment typo fix 2005-11-02 16:36:12 +00:00
Walter Dörwald 2e2c02fedb Fix typo in comment. 2005-11-02 08:57:11 +00:00
Martin v. Löwis ab0f947a21 Remove .cvsignore files, as they live in svn:ignore
properties now.
2005-10-30 22:01:41 +00:00
Fred Drake db390c1ad8 fix typos, mostly in comments 2005-10-28 14:39:47 +00:00
Jeremy Hylton ec97a28b60 Fix a bunch of imports to use code.h instead of compile.h.
Remove duplicate declarations from compile.h
2005-10-21 14:58:06 +00:00
Michael W. Hudson b2308bb9be Fix bug:
[ 1327110 ] wrong TypeError traceback in generator expressions

by removing the code that can stomp on the users' TypeError raised by the
iterable argument to ''.join() -- PySequence_Fast (now?) gives a perfectly
reasonable message itself.  Also, a couple of tests.
2005-10-21 11:45:01 +00:00
Jeremy Hylton 3e0055f8c6 Merge ast-branch to head
This change implements a new bytecode compiler, based on a
transformation of the parse tree to an abstract syntax defined in
Parser/Python.asdl.

The compiler implementation is not complete, but it is in stable
enough shape to run the entire test suite excepting two disabled
tests.
2005-10-20 19:59:25 +00:00
Marc-André Lemburg 2cb94aba12 Enhance the performance of two important Unicode character
type lookups: whitespace and linebreak.

These lookup tables are from the Python 1.6 version with the addition
of the 205F code point which was added as whitespace code point to Unicode
since then.
2005-10-20 19:06:35 +00:00
Neal Norwitz 95c1e5065c SF bug #1331563 ] string_subscript doesn't check for failed PyMem_Malloc. Will backport 2005-10-20 04:15:52 +00:00
Marc-André Lemburg 5c4a9d6591 Whitespace corrections. 2005-10-19 22:39:02 +00:00
Marc-André Lemburg e115ec832c Bug fix for [ 1331062 ] utf 7 codec broken.
Backport candidate.
2005-10-19 22:33:31 +00:00
Walter Dörwald d1c1e10f70 Part of SF patch #1313939: Speedup charmap decoding by extending
PyUnicode_DecodeCharmap() the accept a unicode string as the mapping
argument which is used as a mapping table.

This code isn't used by any of the codecs yet.
2005-10-06 20:29:57 +00:00
Georg Brandl d45014b236 Fix PyString_Format so that the "%s" format works again when Unicode is not
enabled.
2005-10-01 17:06:00 +00:00
Armin Rigo ec862b907a (pedronis, arigo)
segfault when a class contain a non-list value in the (undocumented)
special attribute __slotnames__.
2005-09-24 22:58:41 +00:00
Raymond Hettinger 6b27cda643 Convert iterator __len__() methods to a private API. 2005-09-24 21:23:05 +00:00
Skip Montanaro acb1424106 The key to the various sort columns got lost. Pulled from
http://mail.python.org/pipermail/python-dev/2002-July/026876.html
2005-09-23 17:14:22 +00:00
Guido van Rossum 630db60a55 - On 64-bit platforms, when __len__() returns a value that cannot be
represented as a C int, raise OverflowError.

(Forward port from 2.4.2; the patch to classobject.c was already in
but needed a correction in the error message text.)
2005-09-20 18:49:54 +00:00
Guido van Rossum ba3e6ec0c9 A minor fix for 64-bit platforms: when __len__() returns Python int
containing a value that doesn't fit in a C int, raise OverflowError
rather than truncating silently (and having 50% chance of hitting the
"it should be >= 0" error).
2005-09-19 22:42:41 +00:00
Raymond Hettinger 9bda1d6f64 No longer ignore exceptions raised by comparisons during key lookup.
Inspired by Armin Rigo's suggestion to do the same with dictionaries.
2005-09-16 07:14:21 +00:00
Georg Brandl c404ff2f2d patch [ 1118729 ] Error in representation of complex numbers(again) 2005-09-16 06:42:26 +00:00
Neil Schemenauer ab61923637 Fix bug in last checkin (2.231). To match previous behavior, unicode
subclasses should be substituted as-is and not have tp_str called on
them.
2005-08-31 23:02:05 +00:00
Walter Dörwald a47d1c08d0 SF bug #1251300: On UCS-4 builds the "unicode-internal" codec will now complain
about illegal code points. The codec now supports PEP 293 style error handlers.
(This is a variant of the Nik Haldimann's patch that detects truncated data)
2005-08-30 10:23:14 +00:00
Georg Brandl 02c42871cf Disallow keyword arguments for type constructors that don't use them.
(fixes bug #1119418)
2005-08-26 06:42:30 +00:00
Raymond Hettinger 9c1491f37c * Add a fast equality check path for frozensets where the hash value has
already been computed.
* Apply a GET_SIZE macro().
2005-08-24 00:24:40 +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
Raymond Hettinger d8e133865d Add shortcuts for a|a and a&a. 2005-08-17 12:27:17 +00:00
Raymond Hettinger f81e45023e Fix nits. 2005-08-17 02:19:36 +00:00
Raymond Hettinger f408ddf4a0 Results of a line-by-line comparison back to dictobject.c.
* set_merge() cannot assume that the table doesn't resize during iteration.

* convert some unnecessary tests to asserts -- they were necessary in
  dictobject.c because PyDict_Next() is a public function.  The same is
  not true for set_next().

* re-arrange the order of functions to more closely match the order
  in dictobject.c.  This makes it must easier to compare the two
  and ought to simplify any issues of maintaining both.
2005-08-17 00:27:42 +00:00
Raymond Hettinger c47e01d020 Numerous fix-ups to C API and docs. Added tests for C API. 2005-08-16 10:44:15 +00:00
Raymond Hettinger 994c2c1c69 DECREF --> XDECREF 2005-08-16 03:54:11 +00:00
Raymond Hettinger beb3101b05 Add a C API for sets and frozensets. 2005-08-16 03:47:52 +00:00
Raymond Hettinger ce8185e642 More function re-ordering (placing like functions together). 2005-08-13 09:28:48 +00:00
Raymond Hettinger ed6c1ef8c3 * Bring lookkey() and lookkey_string() closer to dict version.
* Use set_next() for looping in issubset() and frozenset_hash().
* Re-order the presentation of cmp and hash functions.
2005-08-13 08:28:03 +00:00
Phillip J. Eby 00148226df Fix a too-aggressive assert (see SF#1257960). Previously, gen_iternext
was never called during interpreter shutdown GC, so the f_back!=NULL
assertion was correct.  Now that generators get close()d during GC,
the assertion was being triggered because the generator close() was being
called as the top-level frame.  However, nothing actually is broken by
this; it's just that the condition was unexpected in previous Python
versions.
2005-08-13 03:29:00 +00:00
Raymond Hettinger b02c35e208 * Fix SF #1257731. Make __contains__(), remove(), and discard() only do
a frozenset conversion when the initial search attempt fails with a
  TypeError and the key is some type of set.  Add a testcase.

* Eliminate a duplicate if-stmt.
2005-08-12 20:48:39 +00:00
Neil Schemenauer cf52c07843 Change the %s format specifier for str objects so that it returns a
unicode instance if the argument is not an instance of basestring and
calling __str__ on the argument returns a unicode instance.
2005-08-12 17:34:58 +00:00
Raymond Hettinger c991db240c * Add short-circuit code for in-place operations with self (such as
s|=s, s&=s, s-=s, or s^=s).  Add related tests.

* Improve names for several variables and functions.

* Provide alternate table access functions (next, contains, add, and discard)
  that work with an entry argument instead of just a key.  This improves
  set-vs-set operations because we already have a hash value for each key
  and can avoid unnecessary calls to PyObject_Hash().  Provides a 5% to 20%
  speed-up for quick hashing elements like strings and integers.  Provides
  much more substantial improvements for slow hashing elements like tuples
  or objects defining a custom __hash__() function.

* Have difference operations resize() when 1/5 of the elements are dummies.
  Formerly, it was 1/6.  The new ratio triggers less frequently and only
  in cases that it can resize quicker and with greater benefit.  The right
  answer is probably either 1/4, 1/5, or 1/6.  Picked the middle value for
  an even trade-off between resize time and the space/time costs of dummy
  entries.
2005-08-11 07:58:45 +00:00
Raymond Hettinger bc841a1464 * Bring in INIT_NONZERO_SET_SLOTS macro from dictionary code.
* Bring in free list from dictionary code.
* Improve several comments.
* Differencing can leave many dummy entries.  If more than
  1/6 are dummies, then resize them away.
* Factor-out common code with new macro, PyAnySet_CheckExact.
2005-08-07 13:02:53 +00:00
Raymond Hettinger 99220fabb1 * Removed checked_error flag which no longer provides any benefit.
* Have issubset() control its own loop instead of using set_next_internal().
2005-08-06 18:57:13 +00:00
Raymond Hettinger 5ba0cbe392 * set_new() doesn't need to zero the structure a second time after tp_alloc
has already done the job.
* Use a macro form of PyErr_Occurred() inside the set_lookkey() function.
2005-08-06 18:31:24 +00:00
Raymond Hettinger fe889f3c62 Factor away a redundant clear() function. 2005-08-06 05:43:39 +00:00
Raymond Hettinger a580c47c6d * Improve a variable name: entry0 --> table.
* Give set_lookkey_string() a fast alternate path when no dummy entries
  are present.
* Have set_swap_bodies() reset the hash field to -1 whenever either of
  bodies is not a frozenset.  Maintains the invariant of regular sets
  always having -1 in the hash field; otherwise, any mutation would make
  the hash value invalid.
* Use an entry pointer to simplify the code in frozenset_hash().
2005-08-05 17:19:54 +00:00
Raymond Hettinger a9d9936d10 * Move copyright notice to top and indicate derivation from sets.py and
dictobject.c.
* Have frozenset_hash() use entry->hash instead of re-computing each
  individual hash with PyObject_Hash(o);
* Finalize the dummy entry before a system exit.
2005-08-05 00:01:15 +00:00
Raymond Hettinger 67962ab1bb Model set.pop() after dict.popitem(). 2005-08-02 03:45:16 +00:00
Phillip J. Eby 0d6615fd29 PEP 342 implementation. Per Guido's comments, the generator throw()
method still needs to support string exceptions, and allow None for the
third argument.  Documentation updates are needed, too.
2005-08-02 00:46:46 +00:00
Raymond Hettinger d794666048 * Improve code for the empty frozenset singleton:
- Handle both frozenset() and frozenset([]).
  - Do not use singleton for frozenset subclasses.
  - Finalize the singleton.
  - Add test cases.
* Factor-out set_update_internal() from set_update().  Simplifies the
  code for several internal callers.
* Factor constant expressions out of loop in set_merge_internal().
* Minor comment touch-ups.
2005-08-01 21:39:29 +00:00
Hye-Shik Chang e295676c87 Fix build on gcc: PySetIter_Type should be static in definition
part also.
2005-08-01 05:26:41 +00:00
Raymond Hettinger 06d8cf8ceb Improve variable names. 2005-07-31 15:36:06 +00:00
Raymond Hettinger 9dcb17cb1a Fix frozenset() ref count and a comment typo. 2005-07-31 13:09:28 +00:00
Raymond Hettinger 934d63eb40 Comment on the set_swap_bodies() helper function. 2005-07-31 01:33:10 +00:00
Raymond Hettinger 9f1a6796eb Revised the set() and frozenset() implementaion to use its own internal
data structure instead of using dictionaries.  Reduces memory consumption
by 1/3 and provides modest speed-ups for most set operations.
2005-07-31 01:16:36 +00:00
Tim Peters de7990b8af SF bug #1238681: freed pointer is used in longobject.c:long_pow().
In addition, long_pow() skipped a necessary (albeit extremely unlikely
to trigger) error check when converting an int modulus to long.

Alas, I was unable to write a test case that crashed due to either
cause.

Bugfix candidate.
2005-07-17 23:45:23 +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
Tim Peters ecc6e6a54e SF bug 1185883: PyObject_Realloc can't safely take over a block currently
managed by C, because it's possible for the block to be smaller than the
new requested size, and at the end of allocated VM.  Trying to copy over
nbytes bytes to a Python small-object block can segfault then, and there's
no portable way to avoid this (we would have to know how many bytes
starting at p are addressable, and std C has no means to determine that).

Bugfix candidate.  Should be backported to 2.4, but I'm out of time.
2005-07-10 22:30:55 +00:00
Michael W. Hudson 3095ad0650 Apparently some compiler gives a warning on
float y = x;

when x is a double.  Go figure.
2005-06-30 00:02:26 +00:00
Raymond Hettinger 3296e696db SF bug #1224347: int/long unification and hex()
Hex longs now print with lowercase letters like their int counterparts.
2005-06-29 23:29:56 +00:00
Raymond Hettinger bff60aeb93 Insert missing flag. 2005-06-19 08:42:20 +00:00
Raymond Hettinger bb999b5925 SF patch #1200018: Restore GC support to set objects
Reverts 1.26 and 1.27.
And adds cycle testing.
2005-06-18 21:00:26 +00:00
Anthony Baxter 5661699995 fix object.__divmod__.__doc__
backport candidate
2005-06-03 14:12:21 +00:00
Michael W. Hudson ba283e2b7f This is my patch:
[ 1181301 ] make float packing copy bytes when they can

which hasn't been reviewed, despite numerous threats to check it in
anyway if noone reviews it.  Please read the diff on the checkin list,
at least!

The basic idea is to examine the bytes of some 'probe values' to see if
the current platform is a IEEE 754-ish platform, and if so
_PyFloat_{Pack,Unpack}{4,8} just copy bytes around.

The rest is hair for testing, and tests.
2005-05-27 15:23:20 +00:00
Skip Montanaro bbf12ba7b2 Disallow opening files with modes 'aU' or 'wU' as specified by PEP
278. Closes bug 967182.
2005-05-20 03:07:06 +00:00
Armin Rigo 7726dc0a8e Fixed a quite misleading comment: a "not" should not have been there. 2005-05-15 15:32:08 +00:00
Raymond Hettinger 186e739d29 SF patch #1200051: Small optimization for PyDict_Merge()
(Contributed by Barry Warsaw and Matt Messier.)
2005-05-14 18:08:25 +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
Barry Warsaw c8d907c60b As per discussion on python-dev, descriptors defined in C with a NULL setter
now raise AttributeError instead of TypeError, for consistency with their
pure-Python equivalent.
2005-04-19 23:43:40 +00:00
Raymond Hettinger 1356f785c1 SF bug #1183742: PyDict_Copy() can return non-NULL value on error 2005-04-15 15:58:42 +00:00
Michael W. Hudson e2749cb264 Fix for rather inaccurately titled bug
[ 1165306 ] Property access with decorator makes interpreter crash

Don't allow the creation of unbound methods with NULL im_class, because
attempting to call such crashes.

Backport candidate.
2005-03-30 16:32:10 +00:00
Raymond Hettinger e6c470f255 SF bug #1770766: weakref proxy has incorrect __nonzero__ behavior. 2005-03-27 03:04:54 +00:00
Raymond Hettinger b67cc80bb9 SF bug #1155938: Missing None check for __init__(). 2005-03-03 16:45:19 +00:00
Martin v. Löwis 6ce7ed23d0 Revert previous checkin on getargs 'L' code. Try to convert all
numbers in PyLong_AsLongLong, and update test suite accordingly.
Backported to 2.4.
2005-03-03 12:26:35 +00:00
Raymond Hettinger 57e7447c44 * Beef-up tests for str.count().
* Speed-up str.count() by using memchr() to fly between first char matches.
2005-02-20 09:54:53 +00:00
Raymond Hettinger 7cbf1bcb3e * Beef-up testing of str.__contains__() and str.find().
* Speed-up "x in y" where x has more than one character.

The existing code made excessive calls to the expensive memcmp() function.
The new code uses memchr() to rapidly find a start point for memcmp().
In addition to knowing that the first character is a match, the new code
also checks that the last character is a match.  This significantly reduces
the incidence of false starts (saving memcmp() calls and making quadratic
behavior less likely).

Improves the timings on:
    python -m timeit -r7 -s"x='a'*1000" "'ab' in x"
    python -m timeit -r7 -s"x='a'*1000" "'bc' in x"

Once this code has proven itself, then string_find_internal() should refer
to it rather than running its own version.  Also, something similar may
apply to unicode objects.
2005-02-20 04:07:08 +00:00
Michael W. Hudson ee319f66ab Fix
[ 1124295 ] Function's __name__ no longer accessible in restricted mode

which I introduced with a bit of mindless copy-paste when making
__name__ writable.  You can't assign to __name__ in restricted mode,
which I'm going to pretend was intentional :)
2005-02-17 10:37:21 +00:00
Raymond Hettinger 07ead17318 Code simplification -- eliminate lookup when value is known in advance. 2005-02-05 23:42:57 +00:00
Michael W. Hudson faa7648ffe More bug #1077106 stuff, sorry -- modem induced impatiece!
This should go on whatever bugfix branches the other fetches up on.
2005-01-31 17:09:25 +00:00
Armin Rigo a174813113 Dima Dorfman's patch for coercion/comparison of C types (patch #995939), with
a minor change after the coercion, to accept two objects not necessarily of
the same type but with the same tp_compare.
2004-12-23 22:13:13 +00:00
Raymond Hettinger 5d01aa4f6a Bug #1079011: Incorrect error message (somewhat) 2004-12-19 20:45:20 +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 665174834a Remove PyRange_New(). 2004-12-03 11:45:13 +00:00
Marc-André Lemburg a9cadcd41b Correct the handling of 0-termination of PyUnicode_AsWideChar()
and its usage in PyLocale_strcoll().

Clarify the documentation on this.

Thanks to Andreas Degert for pointing this out.
2004-11-22 13:02:31 +00:00
Raymond Hettinger 15056a5202 SF 1062353: set pickling problems
Support automatic pickling of dictionaries in instance of set subclasses.
2004-11-09 07:25:31 +00:00
Peter Astrand f8e74b12b0 If close() fails in file_dealloc, then print an error message to
stderr. close() can fail if the user is out-of-quota, for example.
Fixes #959379.
2004-11-07 14:15:28 +00:00
Tim Peters ead8b7ab30 SF 1055820: weakref callback vs gc vs threads
In cyclic gc, clear weakrefs to unreachable objects before allowing any
Python code (weakref callbacks or __del__ methods) to run.

This is a critical bugfix, affecting all versions of Python since weakrefs
were introduced.  I'll backport to 2.3.
2004-10-30 23:09:22 +00:00
Armin Rigo 89a39461bf Wrote down the invariants of some common objects whose structure is
exposed in header files.  Fixed a few comments in these headers.

As we might have expected, writing down invariants systematically exposed a
(minor) bug.  In this case, function objects have a writeable func_code
attribute, which could be set to code objects with the wrong number of
free variables.  Calling the resulting function segfaulted the interpreter.
Added a corresponding test.
2004-10-28 16:32:00 +00:00
Raymond Hettinger 561fbf138d SF bug #1054139: serious string hashing error in 2.4b1
_PyString_Resize() readied strings for mutation but did not invalidate
the cached hash value.
2004-10-26 01:52:37 +00:00
Marc-André Lemburg 204bd6d9d2 Applied patch for [ 1047269 ] Buffer overwrite in PyUnicode_AsWideChar.
Python 2.3.x candidate.
2004-10-15 07:45:05 +00:00
Raymond Hettinger fb09f0e85c Finalize the freelist of list objects. 2004-10-07 03:58:07 +00:00
Raymond Hettinger 6429a4727e Use Py_CLEAR(). Add unrelated test. 2004-09-28 01:51:35 +00:00
Raymond Hettinger aa241e0149 Checkin Tim's fix to an error discussed on python-dev.
Also, add a testcase.

Formerly, the list_extend() code used several local variables to remember
its state across iterations.  Since an iteration could call arbitrary
Python code, it was possible for the list state to be changed.  The new
code uses dynamic structure references instead of C locals.  So, they
are always up-to-date.

After list_resize() is called, its size has been updated but the new
cells are filled with NULLs.  These needed to be filled before arbitrary
iteration code was called; otherwise, that code could attempt to modify
a list that was in a semi-invalid state.  The solution was to change
the ob->size field back to a value reflecting the actual number of valid
cells.
2004-09-26 19:24:20 +00:00
Brett Cannon a5ca2e7220 Remove 'extern' declaration for _Py_SwappedOp. 2004-09-25 01:37:24 +00:00
Neil Schemenauer 927a57fbeb Ensure negative offsets cannot be passed to buffer(). When composing
buffers, compute the new buffer size based on the old buffer size.
Fixes SF bug #1034242.
2004-09-24 19:17:26 +00:00
Neil Schemenauer fb6ba07d9c Fix buffer offset calculation (need to compute it before changing
'base').  Fixes SF bug #1033720.  Move offset sanity checking to
buffer_from_memory().
2004-09-24 15:41:27 +00:00
Tim Peters e1c69b3f6f float_richcompare(): Use the new Py_IS_NAN macro to ensure that, on
platforms where that macro works, NaN compared to an int or long works
the same as NaN compared to a finite float.
2004-09-23 19:22:41 +00:00
Tim Peters 307fa78107 SF bug #513866: Float/long comparison anomaly.
When an integer is compared to a float now, the int isn't coerced to float.
This avoids spurious overflow exceptions and insane results.  This should
compute correct results, without raising spurious exceptions, in all cases
now -- although I expect that what happens when an int/long is compared to
a NaN is still a platform accident.

Note that we had potential problems here even with "short" ints, on boxes
where sizeof(long)==8.  There's #ifdef'ed code here to handle that, but
I can't test it as intended.  I tested it by changing the #ifdef to
trigger on my 32-bit box instead.

I suppose this is a bugfix candidate, but I won't backport it.  It's
long-winded (for speed) and messy (because the problem is messy).  Note
that this also depends on a previous 2.4 patch that introduced
_Py_SwappedOp[] as an extern.
2004-09-23 08:06:40 +00:00
Tim Peters f4aca755bc A static swapped_op[] array was defined in 3 different C files, & I think
I need to define it again.  Bite the bullet and define it once as an
extern, _Py_SwappedOp[].
2004-09-23 02:39:37 +00:00
Martin v. Löwis 729d47db09 Patch #1024670: Support int objects in PyLong_AsUnsignedLong[Mask]. 2004-09-20 06:17:46 +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
Skip Montanaro 6543b45b0c Initialize sep and seplen to suppress warning from gcc. 2004-09-16 03:28:13 +00:00
Thomas Heller ca0d2cb66e Add a missing line continuation character. 2004-09-15 11:41:32 +00:00
Michael W. Hudson 1fd00a1b71 Make the word "module" appear in the error string for calling the
module type with silly arguments.  (The exact name can be quibbled
over, if you care).

This was partially inspired by bug #1014215 and so on, but is also
just a good idea.
2004-09-14 17:19:09 +00:00
Michael W. Hudson 1593f502e8 Move a comment back to its rightful location. 2004-09-14 17:09:47 +00:00
Walter Dörwald 065a32f550 Make the hint about the None default less ambiguous. 2004-09-14 09:45:10 +00:00
Walter Dörwald 782afc5927 Enhance the docstrings for unicode.split() and string.split()
to make it clear that it is possible to pass None as the
separator argument to get the default "any whitespace" separator.
2004-09-14 09:40:45 +00:00
Raymond Hettinger a84f3abb9e SF #1022910: Conserve memory with list.pop()
The list resizing scheme only downsized when more than 16 elements were
removed in a single step:  del a[100:120].   As a result, the list would
never shrink when popping elements off one at a time.

This patch makes it shrink whenever more than half of the space is unused.

Also, at Tim's suggestion, renamed _new_size to new_allocated.  This makes
the code easier to understand.
2004-09-12 19:53:07 +00:00
Andrew M. Kuchling 55be9eab38 Typo fix: 'comparisions' is not a word 2004-09-10 12:59:54 +00:00
Walter Dörwald 69652035bc SF patch #998993: The UTF-8 and the UTF-16 stateful decoders now support
decoding incomplete input (when the input stream is temporarily exhausted).
codecs.StreamReader now implements buffering, which enables proper
readline support for the UTF-16 decoders. codecs.StreamReader.read()
has a new argument chars which specifies the number of characters to
return. codecs.StreamReader.readline() and codecs.StreamReader.readlines()
have a new argument keepends. Trailing "\n"s will be stripped from the lines
if keepends is false. Added C APIs PyUnicode_DecodeUTF8Stateful and
PyUnicode_DecodeUTF16Stateful.
2004-09-07 20:24:22 +00:00
Raymond Hettinger 75ccea3777 SF patch #1020188: Use Py_CLEAR where necessary to avoid crashes
(Contributed by Dima Dorfman)
2004-09-01 07:02:44 +00:00
Tim Peters cd97da3b1d long_pow(): Fix more instances of leaks in error cases.
Bugfix candidate -- although long_pow() is so different now I doubt a
patch would apply to 2.3.
2004-08-30 02:58:59 +00:00
Tim Peters 47e52ee0c5 SF patch 936813: fast modular exponentiation
This checkin is adapted from part 2 (of 3) of Trevor Perrin's patch set.

BACKWARD INCOMPATIBILITY:  SHIFT must now be divisible by 5.  AFAIK,
nobody will care.  long_pow() could be complicated to worm around that,
if necessary.

long_pow():
  - BUGFIX:  This leaked the base and power when the power was negative
    (and so the computation delegated to float pow).
  - Instead of doing right-to-left exponentiation, do left-to-right.  This
    is more efficient for small bases, which is the common case.
  - In addition, if the exponent is large (more than FIVEARY_CUTOFF
    digits), precompute [a**i % c for i in range(32)], and go left to
    right 5 bits at a time.
l_divmod():
  - The signature changed so that callers who don't want the quotient,
    or don't want the remainder, can pass NULL in the slot they don't
    want.  This saves them from having to declare a vrbl for unwanted
    stuff, and remembering to decref it.
long_mod(), long_div(), long_classic_div():
  - Adjust to new l_divmod() signature, and simplified as a result.
2004-08-30 02:44:38 +00:00
Tim Peters 0973b99e1c SF patch 936813: fast modular exponentiation
This checkin is adapted from part 1 (of 3) of Trevor Perrin's patch set.

x_mul()
  - sped a little by optimizing the C
  - sped a lot (~2X) if it's doing a square; note that long_pow() squares
    often
k_mul()
  - more cache-friendly now if it's doing a square
KARATSUBA_CUTOFF
  - boosted; gradeschool mult is quicker now, and it may have been too low
    for many platforms anyway
KARATSUBA_SQUARE_CUTOFF
  - new
  - since x_mul is a lot faster at squaring now, the point at which
    Karatsuba pays for squaring is much higher than for general mult
2004-08-29 22:16:50 +00:00
Tim Peters 91879ab8ea PyUnicode_Join(): Bozo Alert. While this is chugging along, it may
need to convert str objects from the iterable to unicode.  So, if
someone set the system default encoding to something nasty enough,
the conversion process could mutate the input iterable as a side
effect, and PySequence_Fast doesn't hide that from us if the input was
a list.  IOW, can't assume the size of PySequence_Fast's result is
invariant across PyUnicode_FromObject() calls.
2004-08-27 22:35:44 +00:00
Tim Peters 05eba1fdc8 PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
much to reduce the size of the code, but greatly improves its clarity.
It's also quicker in what's probably the most common case (the argument
iterable is a list).  Against it, if the iterable isn't a list or a tuple,
a temp tuple is materialized containing the entire input sequence, and
that's a bigger temp memory burden.  Yawn.
2004-08-27 21:32:02 +00:00
Tim Peters 894c512c2f PyUnicode_Join(): Missed a spot where I intended a cast from size_t to
int.  I sure wish MS would gripe about that!  Whatever, note that the
statement above it guarantees that the cast loses no info.
2004-08-27 05:08:36 +00:00