Availability: Unix, Windows.
+.. data:: SEEK_SET
+ SEEK_CUR
+ SEEK_END
+
+ Parameters to the :func:`lseek` function. Their values are 0, 1, and 2,
+ respectively. Availability: Windows, Unix.
+
+ .. versionadded:: 2.5
+
+
.. function:: open(file, flags[, mode])
Open the file *file* and set various flags according to *flags* and possibly its
For a description of the flag and mode values, see the C run-time documentation;
flag constants (like :const:`O_RDONLY` and :const:`O_WRONLY`) are defined in
- this module too (see below).
+ this module too (see :ref:`open-constants`). In particular, on Windows adding
+ :const:`O_BINARY` is needed to open files in binary mode.
Availability: Unix, Windows.
:func:`fdopen`, or :data:`sys.stdout` or :data:`sys.stderr`, use its
:meth:`~file.write` method.
+
+.. _open-constants:
+
+``open()`` flag constants
+~~~~~~~~~~~~~~~~~~~~~~~~~
+
The following constants are options for the *flags* parameter to the
:func:`~os.open` function. They can be combined using the bitwise OR operator
``|``. Some of them are not available on all platforms. For descriptions of
the C library.
-.. data:: SEEK_SET
- SEEK_CUR
- SEEK_END
-
- Parameters to the :func:`lseek` function. Their values are 0, 1, and 2,
- respectively. Availability: Windows, Unix.
-
- .. versionadded:: 2.5
-
-
.. _os-file-dir:
Files and Directories
.. method:: socket.recv_into(buffer[, nbytes[, flags]])
Receive up to *nbytes* bytes from the socket, storing the data into a buffer
- rather than creating a new string. If *nbytes* is not specified (or 0),
- receive up to the size available in the given buffer. See the Unix manual page
- :manpage:`recv(2)` for the meaning of the optional argument *flags*; it defaults
- to zero.
+ rather than creating a new string. If *nbytes* is not specified (or 0),
+ receive up to the size available in the given buffer. Returns the number of
+ bytes received. See the Unix manual page :manpage:`recv(2)` for the meaning
+ of the optional argument *flags*; it defaults to zero.
.. versionadded:: 2.5
locking/synchronization.
+Customizing instance and subclass checks
+----------------------------------------
+
+.. versionadded:: 2.6
+
+The following methods are used to override the default behavior of the
+:func:`isinstance` and :func:`issubclass` built-in functions.
+
+In particular, the metaclass :class:`abc.ABCMeta` implements these methods in
+order to allow the addition of Abstract Base Classes (ABCs) as "virtual base
+classes" to any class or type (including built-in types), and including to other
+ABCs.
+
+.. method:: class.__instancecheck__(self, instance)
+
+ Return true if *instance* should be considered a (direct or indirect)
+ instance of *class*. If defined, called to implement ``isinstance(instance,
+ class)``.
+
+
+.. method:: class.__subclasscheck__(self, subclass)
+
+ Return true if *subclass* should be considered a (direct or indirect)
+ subclass of *class*. If defined, called to implement ``issubclass(subclass,
+ class)``.
+
+
+Note that these methods are looked up on the type (metaclass) of a class. They
+cannot be defined as class methods in the actual class. This is consistent with
+the lookup of special methods that are called on instances, only that in this
+case the instance is itself a class.
+
+.. seealso::
+
+ :pep:`3119` - Introducing Abstract Base Classes
+ Includes the specification for customizing :func:`isinstance` and
+ :func:`issubclass` behavior through :meth:`__instancecheck__` and
+ :meth:`__subclasscheck__`, with motivation for this functionality in the
+ context of adding Abstract Base Classes (see the :mod:`abc` module) to the
+ language.
+
+
.. _callable-types:
Emulating callable objects
# Reliably quote a string as a single argument for /bin/sh
-_safechars = string.ascii_letters + string.digits + '!@%_-+=:,./' # Safe unquoted
-_funnychars = '"`$\\' # Unsafe inside "double quotes"
+# Safe unquoted
+_safechars = frozenset(string.ascii_letters + string.digits + '@%_-+=:,./')
def quote(file):
- ''' return a shell-escaped version of the file string '''
+ """Return a shell-escaped version of the file string."""
for c in file:
if c not in _safechars:
break
if not file:
return "''"
return file
- if '\'' not in file:
- return '\'' + file + '\''
- res = ''
- for c in file:
- if c in _funnychars:
- c = '\\' + c
- res = res + c
- return '"' + res + '"'
+ # use single quotes, and put single quotes into double quotes
+ # the string $'b is then quoted as '$'"'"'b'
+ return "'" + file.replace("'", "'\"'\"'") + "'"
self.assertEqual(open(TESTFN).read(), d)
def testQuoting(self):
- safeunquoted = string.ascii_letters + string.digits + '!@%_-+=:,./'
- unsafe = '"`$\\'
+ safeunquoted = string.ascii_letters + string.digits + '@%_-+=:,./'
+ unsafe = '"`$\\!'
+ self.assertEqual(pipes.quote(''), "''")
self.assertEqual(pipes.quote(safeunquoted), safeunquoted)
self.assertEqual(pipes.quote('test file name'), "'test file name'")
for u in unsafe:
"'test%sname'" % u)
for u in unsafe:
self.assertEqual(pipes.quote("test%s'name'" % u),
- '"test\\%s\'name\'"' % u)
-
- self.assertEqual(pipes.quote(''), "''")
+ "'test%s'\"'\"'name'\"'\"''" % u)
def testRepr(self):
t = pipes.Template()
- Raise a TypeError when trying to delete a T_STRING_INPLACE struct member.
-- Issue #1583863: An unicode subclass can now override the __unicode__ method
+- Issue #1583863: An unicode subclass can now override the __unicode__ method.
+
+- Issue #7507: Quote "!" in pipes.quote(); it is special to some shells.
- Issue #7544: Preallocate thread memory before creating the thread to avoid
a fatal error in low memory condition.
Permissions History
-------------------
+- Jean-Paul Calderone was given commit access on April 6 2010 by
+ GFB, at suggestion of Michael Foord and others.
+
+- Brian Curtin was given commit access on March 24 2010 by MvL.
+
- Florent Xicluna was given commit access on February 25 2010 by
MvL, based on Antoine Pitrou's recommendation.