mirror of
https://github.com/python/cpython.git
synced 2025-09-27 02:39:58 +00:00
Merged revisions 73073-73074,73089 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk ........ r73073 | benjamin.peterson | 2009-05-31 09:43:00 -0500 (Sun, 31 May 2009) | 1 line remove function import ........ r73074 | benjamin.peterson | 2009-05-31 10:00:27 -0500 (Sun, 31 May 2009) | 1 line __enter__ and __exit__ must be on the class ........ r73089 | andrew.kuchling | 2009-05-31 19:14:19 -0500 (Sun, 31 May 2009) | 1 line The class for regexes isn't called RegexObject any more; correct the text ........
This commit is contained in:
parent
be5f3712be
commit
8cc7d88bbc
3 changed files with 27 additions and 19 deletions
|
@ -257,7 +257,7 @@ matches with them.
|
||||||
Compiling Regular Expressions
|
Compiling Regular Expressions
|
||||||
-----------------------------
|
-----------------------------
|
||||||
|
|
||||||
Regular expressions are compiled into :class:`RegexObject` instances, which have
|
Regular expressions are compiled into pattern objects, which have
|
||||||
methods for various operations such as searching for pattern matches or
|
methods for various operations such as searching for pattern matches or
|
||||||
performing string substitutions. ::
|
performing string substitutions. ::
|
||||||
|
|
||||||
|
@ -336,7 +336,7 @@ Performing Matches
|
||||||
------------------
|
------------------
|
||||||
|
|
||||||
Once you have an object representing a compiled regular expression, what do you
|
Once you have an object representing a compiled regular expression, what do you
|
||||||
do with it? :class:`RegexObject` instances have several methods and attributes.
|
do with it? Pattern objects have several methods and attributes.
|
||||||
Only the most significant ones will be covered here; consult the :mod:`re` docs
|
Only the most significant ones will be covered here; consult the :mod:`re` docs
|
||||||
for a complete listing.
|
for a complete listing.
|
||||||
|
|
||||||
|
@ -427,8 +427,8 @@ Trying these methods will soon clarify their meaning::
|
||||||
and :meth:`end` return the starting and ending index of the match. :meth:`span`
|
and :meth:`end` return the starting and ending index of the match. :meth:`span`
|
||||||
returns both start and end indexes in a single tuple. Since the :meth:`match`
|
returns both start and end indexes in a single tuple. Since the :meth:`match`
|
||||||
method only checks if the RE matches at the start of a string, :meth:`start`
|
method only checks if the RE matches at the start of a string, :meth:`start`
|
||||||
will always be zero. However, the :meth:`search` method of :class:`RegexObject`
|
will always be zero. However, the :meth:`search` method of patterns
|
||||||
instances scans through the string, so the match may not start at zero in that
|
scans through the string, so the match may not start at zero in that
|
||||||
case. ::
|
case. ::
|
||||||
|
|
||||||
>>> print(p.match('::: message'))
|
>>> print(p.match('::: message'))
|
||||||
|
@ -450,7 +450,7 @@ in a variable, and then check if it was ``None``. This usually looks like::
|
||||||
else:
|
else:
|
||||||
print('No match')
|
print('No match')
|
||||||
|
|
||||||
Two :class:`RegexObject` methods return all of the matches for a pattern.
|
Two pattern methods return all of the matches for a pattern.
|
||||||
:meth:`findall` returns a list of matching strings::
|
:meth:`findall` returns a list of matching strings::
|
||||||
|
|
||||||
>>> p = re.compile('\d+')
|
>>> p = re.compile('\d+')
|
||||||
|
@ -475,10 +475,10 @@ instances as an :term:`iterator`. [#]_ ::
|
||||||
Module-Level Functions
|
Module-Level Functions
|
||||||
----------------------
|
----------------------
|
||||||
|
|
||||||
You don't have to create a :class:`RegexObject` and call its methods; the
|
You don't have to create a pattern object and call its methods; the
|
||||||
:mod:`re` module also provides top-level functions called :func:`match`,
|
:mod:`re` module also provides top-level functions called :func:`match`,
|
||||||
:func:`search`, :func:`findall`, :func:`sub`, and so forth. These functions
|
:func:`search`, :func:`findall`, :func:`sub`, and so forth. These functions
|
||||||
take the same arguments as the corresponding :class:`RegexObject` method, with
|
take the same arguments as the corresponding pattern method, with
|
||||||
the RE string added as the first argument, and still return either ``None`` or a
|
the RE string added as the first argument, and still return either ``None`` or a
|
||||||
:class:`MatchObject` instance. ::
|
:class:`MatchObject` instance. ::
|
||||||
|
|
||||||
|
@ -487,12 +487,12 @@ the RE string added as the first argument, and still return either ``None`` or a
|
||||||
>>> re.match(r'From\s+', 'From amk Thu May 14 19:12:10 1998')
|
>>> re.match(r'From\s+', 'From amk Thu May 14 19:12:10 1998')
|
||||||
<re.MatchObject instance at 80c5978>
|
<re.MatchObject instance at 80c5978>
|
||||||
|
|
||||||
Under the hood, these functions simply produce a :class:`RegexObject` for you
|
Under the hood, these functions simply create a pattern object for you
|
||||||
and call the appropriate method on it. They also store the compiled object in a
|
and call the appropriate method on it. They also store the compiled object in a
|
||||||
cache, so future calls using the same RE are faster.
|
cache, so future calls using the same RE are faster.
|
||||||
|
|
||||||
Should you use these module-level functions, or should you get the
|
Should you use these module-level functions, or should you get the
|
||||||
:class:`RegexObject` and call its methods yourself? That choice depends on how
|
pattern and call its methods yourself? That choice depends on how
|
||||||
frequently the RE will be used, and on your personal coding style. If the RE is
|
frequently the RE will be used, and on your personal coding style. If the RE is
|
||||||
being used at only one point in the code, then the module functions are probably
|
being used at only one point in the code, then the module functions are probably
|
||||||
more convenient. If a program contains a lot of regular expressions, or re-uses
|
more convenient. If a program contains a lot of regular expressions, or re-uses
|
||||||
|
@ -1031,7 +1031,7 @@ Modifying Strings
|
||||||
|
|
||||||
Up to this point, we've simply performed searches against a static string.
|
Up to this point, we've simply performed searches against a static string.
|
||||||
Regular expressions are also commonly used to modify strings in various ways,
|
Regular expressions are also commonly used to modify strings in various ways,
|
||||||
using the following :class:`RegexObject` methods:
|
using the following pattern methods:
|
||||||
|
|
||||||
+------------------+-----------------------------------------------+
|
+------------------+-----------------------------------------------+
|
||||||
| Method/Attribute | Purpose |
|
| Method/Attribute | Purpose |
|
||||||
|
@ -1051,7 +1051,7 @@ using the following :class:`RegexObject` methods:
|
||||||
Splitting Strings
|
Splitting Strings
|
||||||
-----------------
|
-----------------
|
||||||
|
|
||||||
The :meth:`split` method of a :class:`RegexObject` splits a string apart
|
The :meth:`split` method of a pattern splits a string apart
|
||||||
wherever the RE matches, returning a list of the pieces. It's similar to the
|
wherever the RE matches, returning a list of the pieces. It's similar to the
|
||||||
:meth:`split` method of strings but provides much more generality in the
|
:meth:`split` method of strings but provides much more generality in the
|
||||||
delimiters that you can split by; :meth:`split` only supports splitting by
|
delimiters that you can split by; :meth:`split` only supports splitting by
|
||||||
|
@ -1196,10 +1196,10 @@ hexadecimal::
|
||||||
'Call 0xffd2 for printing, 0xc000 for user code.'
|
'Call 0xffd2 for printing, 0xc000 for user code.'
|
||||||
|
|
||||||
When using the module-level :func:`re.sub` function, the pattern is passed as
|
When using the module-level :func:`re.sub` function, the pattern is passed as
|
||||||
the first argument. The pattern may be a string or a :class:`RegexObject`; if
|
the first argument. The pattern may be provided as an object or as a string; if
|
||||||
you need to specify regular expression flags, you must either use a
|
you need to specify regular expression flags, you must either use a
|
||||||
:class:`RegexObject` as the first parameter, or use embedded modifiers in the
|
pattern object as the first parameter, or use embedded modifiers in the
|
||||||
pattern, e.g. ``sub("(?i)b+", "x", "bbbb BBBB")`` returns ``'x x'``.
|
pattern string, e.g. ``sub("(?i)b+", "x", "bbbb BBBB")`` returns ``'x x'``.
|
||||||
|
|
||||||
|
|
||||||
Common Problems
|
Common Problems
|
||||||
|
|
|
@ -58,8 +58,12 @@ class SemLock(object):
|
||||||
def _make_methods(self):
|
def _make_methods(self):
|
||||||
self.acquire = self._semlock.acquire
|
self.acquire = self._semlock.acquire
|
||||||
self.release = self._semlock.release
|
self.release = self._semlock.release
|
||||||
self.__enter__ = self._semlock.__enter__
|
|
||||||
self.__exit__ = self._semlock.__exit__
|
def __enter__(self):
|
||||||
|
return self._semlock.__enter__()
|
||||||
|
|
||||||
|
def __exit__(self, *args):
|
||||||
|
return self._semlock.__exit__(*args)
|
||||||
|
|
||||||
def __getstate__(self):
|
def __getstate__(self):
|
||||||
assert_spawning(self)
|
assert_spawning(self)
|
||||||
|
@ -181,11 +185,15 @@ class Condition(object):
|
||||||
self._woken_count, self._wait_semaphore) = state
|
self._woken_count, self._wait_semaphore) = state
|
||||||
self._make_methods()
|
self._make_methods()
|
||||||
|
|
||||||
|
def __enter__(self):
|
||||||
|
return self._lock.__enter__()
|
||||||
|
|
||||||
|
def __exit__(self, *args):
|
||||||
|
return self._lock.__exit__(*args)
|
||||||
|
|
||||||
def _make_methods(self):
|
def _make_methods(self):
|
||||||
self.acquire = self._lock.acquire
|
self.acquire = self._lock.acquire
|
||||||
self.release = self._lock.release
|
self.release = self._lock.release
|
||||||
self.__enter__ = self._lock.__enter__
|
|
||||||
self.__exit__ = self._lock.__exit__
|
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
try:
|
try:
|
||||||
|
|
|
@ -6,6 +6,7 @@ if __name__ != 'test.support':
|
||||||
import contextlib
|
import contextlib
|
||||||
import errno
|
import errno
|
||||||
import functools
|
import functools
|
||||||
|
import gc
|
||||||
import socket
|
import socket
|
||||||
import sys
|
import sys
|
||||||
import os
|
import os
|
||||||
|
@ -630,7 +631,6 @@ def gc_collect():
|
||||||
longer than expected. This function tries its best to force all garbage
|
longer than expected. This function tries its best to force all garbage
|
||||||
objects to disappear.
|
objects to disappear.
|
||||||
"""
|
"""
|
||||||
import gc
|
|
||||||
gc.collect()
|
gc.collect()
|
||||||
gc.collect()
|
gc.collect()
|
||||||
gc.collect()
|
gc.collect()
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue