Commit Graph

77 Commits

Author SHA1 Message Date
Barry Warsaw 46b629c101 Accepted Raymond's patch to combine mapping and keyword arguments, with slight
modification.  Also, renamed the positional argument to '__mapping' to further
reduce the chance of duplicate keyword arguments.
2004-09-13 14:35:04 +00:00
Barry Warsaw f43e8bd7b4 Template: remove __slots__ since that interferes with the ability to mix in
Template and unicode classes.
2004-09-10 18:30:42 +00:00
Raymond Hettinger 1f7d6a633f __slots__ went missing from Template. 2004-09-10 06:21:22 +00:00
Barry Warsaw 12827c1fa9 Many updates to PEP 292 templates. Summary:
- Template no longer inherits from unicode.

- SafeTemplate is removed.  Now Templates have both a substitute() and a
  safe_substitute() method, so we don't need separate classes.  No more
  __mod__() operator.

- Adopt Tim Peter's idea for giving Template a metaclass, which makes the
  delimiter, the identifier pattern, or the entire pattern easy to override
  and document, while retaining efficiency of class-time compilation of the
  regexp.

- More informative ValueError messages which will help a user narrow down the
  bogus delimiter to the line and column in the original string (helpful for
  long triple quoted strings).
2004-09-10 03:08:08 +00:00
Raymond Hettinger 0d58e2be0b Minor improvements to the template code.
* Add comment bars segregating this code from the rest.
* Improve readability of the re pattern with indentation and comments on
  the same line.
* Replace the groupdict() and get() pair with a direct call to group()
  which does the same thing.
2004-08-26 00:21:13 +00:00
Barry Warsaw 8bee76106e PEP 292 classes Template and SafeTemplate are added to the string module.
This patch includes test cases and documentation updates, as well as NEWS file
updates.

This patch also updates the sre modules so that they don't import the string
module, breaking direct circular imports.
2004-08-25 02:22:30 +00:00
Hye-Shik Chang 3ae811b57d Add rsplit method for str and unicode builtin types.
SF feature request #801847.
Original patch is written by Sean Reifschneider.
2003-12-15 18:49:53 +00:00
Raymond Hettinger 4f8f976576 Add optional fillchar argument to ljust(), rjust(), and center() string methods. 2003-11-26 08:21:35 +00:00
Skip Montanaro 0b87444b1b tweak the docstring to not be so focused on 1.6. 2003-10-03 14:05:26 +00:00
Neal Norwitz ffe33b7f24 Attempt to make all the various string *strip methods the same.
* Doc - add doc for when functions were added
 * UserString
 * string object methods
 * string module functions
'chars' is used for the last parameter everywhere.

These changes will be backported, since part of the changes
have already been made, but they were inconsistent.
2003-04-10 22:35:32 +00:00
Neal Norwitz a4864a2464 Update the docstring to match the code. Will backport. 2002-11-14 03:31:32 +00:00
Martin v. Löwis 1f04610b49 Make strip behave as documented. Will backport to 2.2.3. 2002-11-08 12:09:59 +00:00
Martin v. Löwis 5357c6511d Convert empty string literal to string. Speed up creation of idmap. 2002-10-14 20:03:40 +00:00
Walter Dörwald 65230a2de7 Remove uses of the string and types modules:
x in string.whitespace => x.isspace()
type(x) in types.StringTypes => isinstance(x, basestring)
isinstance(x, types.StringTypes) => isinstance(x, basestring)
type(x) is types.StringType => isinstance(x, str)
type(x) == types.StringType => isinstance(x, str)
string.split(x, ...) => x.split(...)
string.join(x, y) => y.join(x)
string.zfill(x, ...) => x.zfill(...)
string.count(x, ...) => x.count(...)
hasattr(types, "UnicodeType") => try: unicode except NameError:
type(x) != types.TupleTuple => not isinstance(x, tuple)
isinstance(x, types.TupleType) => isinstance(x, tuple)
type(x) is types.IntType => isinstance(x, int)

Do not mention the string module in the rlcompleter docstring.

This partially applies SF patch http://www.python.org/sf/562373
(with basestring instead of string). (It excludes the changes to
unittest.py and does not change the os.stat stuff.)
2002-06-03 15:58:32 +00:00
Walter Dörwald 068325ef92 Apply the second version of SF patch http://www.python.org/sf/536241
Add a method zfill to str, unicode and UserString and change
Lib/string.py accordingly.

This activates the zfill version in unicodeobject.c that was
commented out and implements the same in stringobject.c. It also
adds the test for unicode support in Lib/string.py back in and
uses repr() instead() of str() (as it was before Lib/string.py 1.62)
2002-04-15 13:36:47 +00:00
Andrew M. Kuchling 102d1208a8 [Bug #536241] string.zfill() produces mangled output for a Unicode string.
Walter Doerwald provided a patch, which I've modified in two ways:

1) (Uncontroversial) Removed code to make module work in earlier versions of
   Python without the unicode() built-in

2) (Poss. controversial) Instead of making string.zfill take the repr()
   of non-string objects, take the str().

Should a warning be added to this branch of the code so that the automatic
str() can be deprecated?

2.2.2 bugfix candidate, assuming the repr()->str() change is deemed OK.
2002-03-29 16:20:33 +00:00
Fred Drake 1453754fcd string.split() docstring described the interpretation of the maxsplit
argument incorrectly.
This closes SF bug #505997.
2002-01-30 16:15:13 +00:00
Fred Drake 960fdf9ac3 Added the constants ascii_letters, ascii_lowercase, and ascii_uppercase
to the string module.  This was determined to be the right approach in
SF bug #226706.
2001-07-20 18:38:26 +00:00
Skip Montanaro 78349072f7 removed __all__ from several modules 2001-02-18 03:30:53 +00:00
Skip Montanaro 0de65807e6 bunch more __all__ lists
also modified check_all function to suppress all warnings since they aren't
relevant to what this test is doing (allows quiet checking of regsub, for
instance)
2001-02-15 22:15:14 +00:00
Tim Peters 6b6b39e8b6 Nuke accurate but confusing and unhelpful comments about split vs splitfields. 2001-02-09 20:18:41 +00:00
Eric S. Raymond e37340edf2 String method conversion. 2001-02-09 16:56:44 +00:00
Tim Peters 495ad3c8cc Whitespace normalization. 2001-01-15 01:36:40 +00:00
Guido van Rossum 5aff7752eb Make string.translate(s, table) work for Unicode s. Two things are
required to work around restrictions on the arguments of
u.translate():

1) don't pass the deletions argument if it's empty;

2) convert table to Unicode if s is Unicode.

This fixes SF bug #124060.
2000-12-19 02:39:08 +00:00
Fred Drake fd64c5908f Fix serious typo!
Add the new constants to the module docstring.
2000-09-18 19:38:11 +00:00
Fred Drake 6b2320fa6b Richard Mortier <rmm1002@users.sourceforge.net>:
Add the constants "printable" and "punctuation" to the string module.
2000-09-18 16:46:17 +00:00
Thomas Wouters 7e47402264 Spelling fixes supplied by Rob W. W. Hooft. All these are fixes in either
comments, docstrings or error messages. I fixed two minor things in
test_winreg.py ("didn't" -> "Didn't" and "Didnt" -> "Didn't").

There is a minor style issue involved: Guido seems to have preferred English
grammar (behaviour, honour) in a couple places. This patch changes that to
American, which is the more prominent style in the source. I prefer English
myself, so if English is preferred, I'd be happy to supply a patch myself ;)
2000-07-16 12:04:32 +00:00
Fred Drake 046d27215f Martin von Löwis <martin@loewis.home.cs.tu-berlin.de>:
This patch delegates more string functions to string object methods,
uses the varargs delegation syntax, and stops using stringold.

Closes SourceForge patch #100712.
2000-07-03 07:23:13 +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 8f0c5a7742 Marc-Andre Lemburg: the maxsplit argument for split() and replace()
now defaults to -1, not to 0.  Passing an explicit zero doesn't split
or replace at all.
2000-03-10 23:22:10 +00:00
Fred Drake 857c4c36b9 Detabify.
I ran "expand" instead of using Skip's patch, but it's all the same.
2000-02-10 16:21:11 +00:00
Guido van Rossum e7b146fb3b The third and final doc-string sweep by Ka-Ping Yee.
The attached patches update the standard library so that all modules
have docstrings beginning with one-line summaries.

A new docstring was added to formatter.  The docstring for os.py
was updated to mention nt, os2, ce in addition to posix, dos, mac.
2000-02-04 15:28:42 +00:00
Fred Drake e4f13660f8 split() docstring: Made signature and description for the first
parameter match.  Error pointed out by François
                    Pinard <pinard@iro.umontreal.ca> on c.l.py.
1999-11-04 19:19:48 +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 1b7aec35c4 Fix PR#31 -- zfill() mishandles empty string. 1999-10-11 22:15:41 +00:00
Guido van Rossum 9a34523e19 As Tim Peters points out, ``from string import *'' should not set re to None.
Also rename safe_env to _safe_env.
1998-04-20 14:01:00 +00:00
Guido van Rossum a6bb6be95f Strip argument to atol and atof to match what strop does better. 1998-03-30 17:22:30 +00:00
Guido van Rossum 8ca842066c A few lines were indented using spaces instead of tabs -- fix them. 1998-03-26 20:56:10 +00:00
Guido van Rossum 23e21e7cf3 Minor editing corrections. 1997-12-29 19:57:36 +00:00
Guido van Rossum 2003204ba7 Added doc string, provided by Charles Waldman (with some reformatting
and a little editing my me).
1997-12-29 19:26:28 +00:00
Guido van Rossum d0753e20b2 At Barry's suggestion, plug the security leak by using an empty
__builtins__ for all calls to eval().  This still allows someone to
write string.atof("[1]*1000000") (which Jim Fulton worries about) but
effectively disables access to system modules and functions.
1997-12-10 22:59:55 +00:00
Guido van Rossum 90d62ab0a1 Since this module is used as a fallback in case no built-in modules
have been configured, string.atof() should not fail when "import re"
fails (usually because pcre is not there).

This opens up a tiny security hole: *if* an attacker can make "import
re" fail, they can also make string.atof(arbitrary_string) evaluate
the arbitrary string.  Nothing to keep me awake at night...
1997-12-10 22:35:02 +00:00
Guido van Rossum 06ba34c5d4 In string.split(), honor maxsplit (if > 0).
In string.splitfields(), ignore maxsplit if <= 0, rather than ignoring
maxsplit=0 but effectively treating negative numbers the same as
maxsplit=1.  Also made the test for maxsplit slightly more efficient
(set it to the length of the string when <= 0 so the test for its
presence can be omitted from the loop).
1997-12-01 15:25:19 +00:00
Guido van Rossum 9694fcab53 Convert all remaining *simple* cases of regex usage to re usage. 1997-10-22 21:00:49 +00:00
Guido van Rossum 1510565cb5 Add optional 4th argument to count(), matching find() etc.
Also change all occurrences of "x == None" to "x is None" (not that it
matters much, these functions are all reimplemented in strop -- but
count() is not).
1997-10-20 23:31:15 +00:00
Guido van Rossum 21aa0ef351 Changed my mind on replace().
It's now replace(str, old, new, maxsplit=0).
Note new ordering of parameters (string first);
this is more consistent with translate().
1997-04-02 05:49:46 +00:00
Guido van Rossum 1eb9a81eb9 Added new functions replace() and replace1(). 1997-03-25 16:50:31 +00:00
Guido van Rossum 7b7c578616 Add optional 4th argument to [r]find and [r]index (end of slice). 1997-03-14 04:13:56 +00:00
Guido van Rossum f480c674b1 Use correct separator for capwords(s, sep). 1996-08-26 15:55:00 +00:00
Guido van Rossum 34f173110f Add optional separator character to capwords(), for completeness. 1996-08-20 20:25:41 +00:00