mirror of https://github.com/python/cpython
merge heads
This commit is contained in:
commit
d87963ecf6
|
@ -750,12 +750,21 @@ Decimal objects
|
|||
|
||||
.. method:: remainder_near(other[, context])
|
||||
|
||||
Compute the modulo as either a positive or negative value depending on
|
||||
which is closest to zero. For instance, ``Decimal(10).remainder_near(6)``
|
||||
returns ``Decimal('-2')`` which is closer to zero than ``Decimal('4')``.
|
||||
Return the remainder from dividing *self* by *other*. This differs from
|
||||
``self % other`` in that the sign of the remainder is chosen so as to
|
||||
minimize its absolute value. More precisely, the return value is
|
||||
``self - n * other`` where ``n`` is the integer nearest to the exact
|
||||
value of ``self / other``, and if two integers are equally near then the
|
||||
even one is chosen.
|
||||
|
||||
If both are equally close, the one chosen will have the same sign as
|
||||
*self*.
|
||||
If the result is zero then its sign will be the sign of *self*.
|
||||
|
||||
>>> Decimal(18).remainder_near(Decimal(10))
|
||||
Decimal('-2')
|
||||
>>> Decimal(25).remainder_near(Decimal(10))
|
||||
Decimal('5')
|
||||
>>> Decimal(35).remainder_near(Decimal(10))
|
||||
Decimal('-5')
|
||||
|
||||
.. method:: rotate(other[, context])
|
||||
|
||||
|
|
|
@ -240,11 +240,12 @@ reading the source code for these modules.
|
|||
43/3 0.533 0.012 0.749 0.250 pobject.py:99(evaluate)
|
||||
...
|
||||
|
||||
The first line indicates that 2706 calls were monitored. Of those calls, 2004
|
||||
were :dfn:`primitive`. We define :dfn:`primitive` to mean that the call was not
|
||||
induced via recursion. The next line: ``Ordered by: standard name``, indicates
|
||||
that the text string in the far right column was used to sort the output. The
|
||||
column headings include:
|
||||
The first line indicates that 2706 calls were monitored. Of those
|
||||
calls, 2004 were :dfn:`primitive`. We define :dfn:`primitive` to
|
||||
mean that the call was not induced via recursion. The next line:
|
||||
``Ordered by: standard name``, indicates that the text string in
|
||||
the far right column was used to sort the output. The column
|
||||
headings include:
|
||||
|
||||
ncalls
|
||||
for the number of calls,
|
||||
|
@ -266,14 +267,14 @@ reading the source code for these modules.
|
|||
filename:lineno(function)
|
||||
provides the respective data of each function
|
||||
|
||||
When there are two numbers in the first column (for example, ``43/3``), then the
|
||||
latter is the number of primitive calls, and the former is the actual number of
|
||||
calls. Note that when the function does not recurse, these two values are the
|
||||
same, and only the single figure is printed.
|
||||
When there are two numbers in the first column (for example,
|
||||
``43/3``), then the latter is the number of primitive calls, and
|
||||
the former is the actual number of calls. Note that when the
|
||||
function does not recurse, these two values are the same, and only
|
||||
the single figure is printed.
|
||||
|
||||
If *sort* is given, it can be one of ``'stdname'`` (sort by filename:lineno),
|
||||
``'calls'`` (sort by number of calls), ``'time'`` (sort by total time) or
|
||||
``'cumulative'`` (sort by cumulative time). The default is ``'stdname'``.
|
||||
If *sort* is given, it can be one of values allowed for *key*
|
||||
parameter from :meth:`pstats.Stats.sort_stats`.
|
||||
|
||||
|
||||
.. function:: runctx(command, globals, locals, filename=None)
|
||||
|
@ -291,19 +292,22 @@ Analysis of the profiler data is done using the :class:`pstats.Stats` class.
|
|||
|
||||
.. class:: Stats(*filenames, stream=sys.stdout)
|
||||
|
||||
This class constructor creates an instance of a "statistics object" from a
|
||||
*filename* (or set of filenames). :class:`Stats` objects are manipulated by
|
||||
methods, in order to print useful reports. You may specify an alternate output
|
||||
stream by giving the keyword argument, ``stream``.
|
||||
This class constructor creates an instance of a "statistics object"
|
||||
from a *filename* (or set of filenames). :class:`Stats` objects
|
||||
are manipulated by methods, in order to print useful reports. You
|
||||
may specify an alternate output stream by giving the keyword
|
||||
argument, ``stream``.
|
||||
|
||||
The file selected by the above constructor must have been created by the
|
||||
corresponding version of :mod:`profile` or :mod:`cProfile`. To be specific,
|
||||
there is *no* file compatibility guaranteed with future versions of this
|
||||
profiler, and there is no compatibility with files produced by other profilers.
|
||||
If several files are provided, all the statistics for identical functions will
|
||||
be coalesced, so that an overall view of several processes can be considered in
|
||||
a single report. If additional files need to be combined with data in an
|
||||
existing :class:`Stats` object, the :meth:`add` method can be used.
|
||||
The file selected by the above constructor must have been created
|
||||
by the corresponding version of :mod:`profile` or :mod:`cProfile`.
|
||||
To be specific, there is *no* file compatibility guaranteed with
|
||||
future versions of this profiler, and there is no compatibility
|
||||
with files produced by other profilers. If several files are
|
||||
provided, all the statistics for identical functions will be
|
||||
coalesced, so that an overall view of several processes can be
|
||||
considered in a single report. If additional files need to be
|
||||
combined with data in an existing :class:`Stats` object, the
|
||||
:meth:`add` method can be used.
|
||||
|
||||
.. (such as the old system profiler).
|
||||
|
||||
|
@ -318,15 +322,17 @@ The :class:`Stats` Class
|
|||
|
||||
.. method:: Stats.strip_dirs()
|
||||
|
||||
This method for the :class:`Stats` class removes all leading path information
|
||||
from file names. It is very useful in reducing the size of the printout to fit
|
||||
within (close to) 80 columns. This method modifies the object, and the stripped
|
||||
information is lost. After performing a strip operation, the object is
|
||||
considered to have its entries in a "random" order, as it was just after object
|
||||
initialization and loading. If :meth:`strip_dirs` causes two function names to
|
||||
be indistinguishable (they are on the same line of the same filename, and have
|
||||
the same function name), then the statistics for these two entries are
|
||||
accumulated into a single entry.
|
||||
This method for the :class:`Stats` class removes all leading path
|
||||
information from file names. It is very useful in reducing the
|
||||
size of the printout to fit within (close to) 80 columns. This
|
||||
method modifies the object, and the stripped information is lost.
|
||||
After performing a strip operation, the object is considered to
|
||||
have its entries in a "random" order, as it was just after object
|
||||
initialization and loading. If :meth:`strip_dirs` causes two
|
||||
function names to be indistinguishable (they are on the same line
|
||||
of the same filename, and have the same function name), then the
|
||||
statistics for these two entries are accumulated into a single
|
||||
entry.
|
||||
|
||||
|
||||
.. method:: Stats.add(*filenames)
|
||||
|
@ -340,23 +346,25 @@ The :class:`Stats` Class
|
|||
|
||||
.. method:: Stats.dump_stats(filename)
|
||||
|
||||
Save the data loaded into the :class:`Stats` object to a file named *filename*.
|
||||
The file is created if it does not exist, and is overwritten if it already
|
||||
exists. This is equivalent to the method of the same name on the
|
||||
:class:`profile.Profile` and :class:`cProfile.Profile` classes.
|
||||
Save the data loaded into the :class:`Stats` object to a file named
|
||||
*filename*. The file is created if it does not exist, and is
|
||||
overwritten if it already exists. This is equivalent to the method
|
||||
of the same name on the :class:`profile.Profile` and
|
||||
:class:`cProfile.Profile` classes.
|
||||
|
||||
|
||||
.. method:: Stats.sort_stats(*keys)
|
||||
|
||||
This method modifies the :class:`Stats` object by sorting it according to the
|
||||
supplied criteria. The argument is typically a string identifying the basis of
|
||||
a sort (example: ``'time'`` or ``'name'``).
|
||||
This method modifies the :class:`Stats` object by sorting it
|
||||
according to the supplied criteria. The argument is typically a
|
||||
string identifying the basis of a sort (example: ``'time'`` or
|
||||
``'name'``).
|
||||
|
||||
When more than one key is provided, then additional keys are used as secondary
|
||||
criteria when there is equality in all keys selected before them. For example,
|
||||
``sort_stats('name', 'file')`` will sort all the entries according to their
|
||||
function name, and resolve all ties (identical function names) by sorting by
|
||||
file name.
|
||||
When more than one key is provided, then additional keys are used
|
||||
as secondary criteria when there is equality in all keys selected
|
||||
before them. For example, ``sort_stats('name', 'file')`` will sort
|
||||
all the entries according to their function name, and resolve all
|
||||
ties (identical function names) by sorting by file name.
|
||||
|
||||
Abbreviations can be used for any key names, as long as the abbreviation is
|
||||
unambiguous. The following are the keys currently defined:
|
||||
|
@ -368,10 +376,16 @@ The :class:`Stats` Class
|
|||
+------------------+----------------------+
|
||||
| ``'cumulative'`` | cumulative time |
|
||||
+------------------+----------------------+
|
||||
| ``'cumtime'`` | cumulative time |
|
||||
+------------------+----------------------+
|
||||
| ``'file'`` | file name |
|
||||
+------------------+----------------------+
|
||||
| ``'filename'`` | file name |
|
||||
+------------------+----------------------+
|
||||
| ``'module'`` | file name |
|
||||
+------------------+----------------------+
|
||||
| ``'ncalls'`` | call count |
|
||||
+------------------+----------------------+
|
||||
| ``'pcalls'`` | primitive call count |
|
||||
+------------------+----------------------+
|
||||
| ``'line'`` | line number |
|
||||
|
@ -384,51 +398,60 @@ The :class:`Stats` Class
|
|||
+------------------+----------------------+
|
||||
| ``'time'`` | internal time |
|
||||
+------------------+----------------------+
|
||||
| ``'tottime'`` | internal time |
|
||||
+------------------+----------------------+
|
||||
|
||||
Note that all sorts on statistics are in descending order (placing most time
|
||||
consuming items first), where as name, file, and line number searches are in
|
||||
ascending order (alphabetical). The subtle distinction between ``'nfl'`` and
|
||||
``'stdname'`` is that the standard name is a sort of the name as printed, which
|
||||
means that the embedded line numbers get compared in an odd way. For example,
|
||||
lines 3, 20, and 40 would (if the file names were the same) appear in the string
|
||||
order 20, 3 and 40. In contrast, ``'nfl'`` does a numeric compare of the line
|
||||
numbers. In fact, ``sort_stats('nfl')`` is the same as ``sort_stats('name',
|
||||
'file', 'line')``.
|
||||
Note that all sorts on statistics are in descending order (placing
|
||||
most time consuming items first), where as name, file, and line
|
||||
number searches are in ascending order (alphabetical). The subtle
|
||||
distinction between ``'nfl'`` and ``'stdname'`` is that the
|
||||
standard name is a sort of the name as printed, which means that
|
||||
the embedded line numbers get compared in an odd way. For example,
|
||||
lines 3, 20, and 40 would (if the file names were the same) appear
|
||||
in the string order 20, 3 and 40. In contrast, ``'nfl'`` does a
|
||||
numeric compare of the line numbers. In fact,
|
||||
``sort_stats('nfl')`` is the same as ``sort_stats('name', 'file',
|
||||
'line')``.
|
||||
|
||||
For backward-compatibility reasons, the numeric arguments ``-1``, ``0``, ``1``,
|
||||
and ``2`` are permitted. They are interpreted as ``'stdname'``, ``'calls'``,
|
||||
``'time'``, and ``'cumulative'`` respectively. If this old style format
|
||||
(numeric) is used, only one sort key (the numeric key) will be used, and
|
||||
additional arguments will be silently ignored.
|
||||
For backward-compatibility reasons, the numeric arguments ``-1``,
|
||||
``0``, ``1``, and ``2`` are permitted. They are interpreted as
|
||||
``'stdname'``, ``'calls'``, ``'time'``, and ``'cumulative'``
|
||||
respectively. If this old style format (numeric) is used, only one
|
||||
sort key (the numeric key) will be used, and additional arguments
|
||||
will be silently ignored.
|
||||
|
||||
.. For compatibility with the old profiler,
|
||||
|
||||
|
||||
.. method:: Stats.reverse_order()
|
||||
|
||||
This method for the :class:`Stats` class reverses the ordering of the basic list
|
||||
within the object. Note that by default ascending vs descending order is
|
||||
properly selected based on the sort key of choice.
|
||||
This method for the :class:`Stats` class reverses the ordering of
|
||||
the basic list within the object. Note that by default ascending
|
||||
vs descending order is properly selected based on the sort key of
|
||||
choice.
|
||||
|
||||
.. This method is provided primarily for compatibility with the old profiler.
|
||||
|
||||
|
||||
.. method:: Stats.print_stats(*restrictions)
|
||||
|
||||
This method for the :class:`Stats` class prints out a report as described in the
|
||||
:func:`profile.run` definition.
|
||||
This method for the :class:`Stats` class prints out a report as
|
||||
described in the :func:`profile.run` definition.
|
||||
|
||||
The order of the printing is based on the last :meth:`sort_stats` operation done
|
||||
on the object (subject to caveats in :meth:`add` and :meth:`strip_dirs`).
|
||||
The order of the printing is based on the last :meth:`sort_stats`
|
||||
operation done on the object (subject to caveats in :meth:`add` and
|
||||
:meth:`strip_dirs`).
|
||||
|
||||
The arguments provided (if any) can be used to limit the list down to the
|
||||
significant entries. Initially, the list is taken to be the complete set of
|
||||
profiled functions. Each restriction is either an integer (to select a count of
|
||||
lines), or a decimal fraction between 0.0 and 1.0 inclusive (to select a
|
||||
percentage of lines), or a regular expression (to pattern match the standard
|
||||
name that is printed; as of Python 1.5b1, this uses the Perl-style regular
|
||||
expression syntax defined by the :mod:`re` module). If several restrictions are
|
||||
provided, then they are applied sequentially. For example::
|
||||
The arguments provided (if any) can be used to limit the list down
|
||||
to the significant entries. Initially, the list is taken to be the
|
||||
complete set of profiled functions. Each restriction is either an
|
||||
integer (to select a count of lines), or a decimal fraction between
|
||||
0.0 and 1.0 inclusive (to select a percentage of lines), or a
|
||||
regular expression (to pattern match the standard name that is
|
||||
printed; as of Python 1.5b1, this uses the Perl-style regular
|
||||
expression syntax defined by the :mod:`re` module). If several
|
||||
restrictions are provided, then they are applied sequentially. For
|
||||
example::
|
||||
|
||||
print_stats(.1, 'foo:')
|
||||
|
||||
|
@ -455,17 +478,19 @@ The :class:`Stats` Class
|
|||
non-parenthesized number repeats the cumulative time spent in the function
|
||||
at the right.
|
||||
|
||||
* With :mod:`cProfile`, each caller is preceded by three numbers: the number of
|
||||
times this specific call was made, and the total and cumulative times spent in
|
||||
the current function while it was invoked by this specific caller.
|
||||
* With :mod:`cProfile`, each caller is preceded by three numbers:
|
||||
the number of times this specific call was made, and the total
|
||||
and cumulative times spent in the current function while it was
|
||||
invoked by this specific caller.
|
||||
|
||||
|
||||
.. method:: Stats.print_callees(*restrictions)
|
||||
|
||||
This method for the :class:`Stats` class prints a list of all function that were
|
||||
called by the indicated function. Aside from this reversal of direction of
|
||||
calls (re: called vs was called by), the arguments and ordering are identical to
|
||||
the :meth:`print_callers` method.
|
||||
This method for the :class:`Stats` class prints a list of all
|
||||
function that were called by the indicated function. Aside from
|
||||
this reversal of direction of calls (re: called vs was called by),
|
||||
the arguments and ordering are identical to the
|
||||
:meth:`print_callers` method.
|
||||
|
||||
|
||||
.. _profile-limits:
|
||||
|
@ -566,29 +591,33 @@ you want to the :class:`Profile` class constructor::
|
|||
The resulting profiler will then call :func:`your_time_func`.
|
||||
|
||||
:class:`profile.Profile`
|
||||
:func:`your_time_func` should return a single number, or a list of numbers whose
|
||||
sum is the current time (like what :func:`os.times` returns). If the function
|
||||
returns a single time number, or the list of returned numbers has length 2, then
|
||||
you will get an especially fast version of the dispatch routine.
|
||||
:func:`your_time_func` should return a single number, or a list of
|
||||
numbers whose sum is the current time (like what :func:`os.times`
|
||||
returns). If the function returns a single time number, or the
|
||||
list of returned numbers has length 2, then you will get an
|
||||
especially fast version of the dispatch routine.
|
||||
|
||||
Be warned that you should calibrate the profiler class for the timer function
|
||||
that you choose. For most machines, a timer that returns a lone integer value
|
||||
will provide the best results in terms of low overhead during profiling.
|
||||
(:func:`os.times` is *pretty* bad, as it returns a tuple of floating point
|
||||
values). If you want to substitute a better timer in the cleanest fashion,
|
||||
derive a class and hardwire a replacement dispatch method that best handles your
|
||||
timer call, along with the appropriate calibration constant.
|
||||
Be warned that you should calibrate the profiler class for the
|
||||
timer function that you choose. For most machines, a timer that
|
||||
returns a lone integer value will provide the best results in terms
|
||||
of low overhead during profiling. (:func:`os.times` is *pretty*
|
||||
bad, as it returns a tuple of floating point values). If you want
|
||||
to substitute a better timer in the cleanest fashion, derive a
|
||||
class and hardwire a replacement dispatch method that best handles
|
||||
your timer call, along with the appropriate calibration constant.
|
||||
|
||||
:class:`cProfile.Profile`
|
||||
:func:`your_time_func` should return a single number. If it returns
|
||||
integers, you can also invoke the class constructor with a second argument
|
||||
specifying the real duration of one unit of time. For example, if
|
||||
:func:`your_integer_time_func` returns times measured in thousands of seconds,
|
||||
you would construct the :class:`Profile` instance as follows::
|
||||
:func:`your_time_func` should return a single number. If it
|
||||
returns integers, you can also invoke the class constructor with a
|
||||
second argument specifying the real duration of one unit of time.
|
||||
For example, if :func:`your_integer_time_func` returns times
|
||||
measured in thousands of seconds, you would construct the
|
||||
:class:`Profile` instance as follows::
|
||||
|
||||
pr = profile.Profile(your_integer_time_func, 0.001)
|
||||
|
||||
As the :mod:`cProfile.Profile` class cannot be calibrated, custom timer
|
||||
functions should be used with care and should be as fast as possible. For the
|
||||
best results with a custom timer, it might be necessary to hard-code it in the C
|
||||
source of the internal :mod:`_lsprof` module.
|
||||
As the :mod:`cProfile.Profile` class cannot be calibrated, custom
|
||||
timer functions should be used with care and should be as fast as
|
||||
possible. For the best results with a custom timer, it might be
|
||||
necessary to hard-code it in the C source of the internal
|
||||
:mod:`_lsprof` module.
|
||||
|
|
|
@ -654,7 +654,7 @@ TESTFN_UNDECODABLE = None
|
|||
for name in (b'abc\xff', b'\xe7w\xf0'):
|
||||
try:
|
||||
os.fsdecode(name)
|
||||
except UnicodeDecodeErorr:
|
||||
except UnicodeDecodeError:
|
||||
TESTFN_UNDECODABLE = name
|
||||
break
|
||||
|
||||
|
|
|
@ -239,7 +239,7 @@ class TestInsort(unittest.TestCase):
|
|||
else:
|
||||
f = self.module.insort_right
|
||||
f(insorted, digit)
|
||||
self.assertEqual(sorted(insorted), insorted)
|
||||
self.assertEqual(sorted(insorted), insorted)
|
||||
|
||||
def test_backcompatibility(self):
|
||||
self.assertEqual(self.module.insort, self.module.insort_right)
|
||||
|
|
|
@ -311,14 +311,10 @@ class CommonTest(GenericTest):
|
|||
@unittest.skipIf(sys.platform == 'darwin',
|
||||
"Mac OS X denies the creation of a directory with an invalid utf8 name")
|
||||
def test_nonascii_abspath(self):
|
||||
name = b'\xe7w\xf0'
|
||||
if sys.platform == 'win32':
|
||||
try:
|
||||
os.fsdecode(name)
|
||||
except UnicodeDecodeError:
|
||||
self.skipTest("the filename %a is not decodable "
|
||||
"from the ANSI code page %s"
|
||||
% (name, sys.getfilesystemencoding()))
|
||||
if support.TESTFN_UNDECODABLE:
|
||||
name = support.TESTFN_UNDECODABLE
|
||||
else:
|
||||
name = b'a\xffb\xe7w\xf0'
|
||||
|
||||
# Test non-ASCII, non-UTF8 bytes in the path.
|
||||
with warnings.catch_warnings():
|
||||
|
|
|
@ -2051,65 +2051,82 @@ class OSErrorTests(unittest.TestCase):
|
|||
class Str(str):
|
||||
pass
|
||||
|
||||
self.filenames = []
|
||||
self.bytes_filenames = []
|
||||
self.unicode_filenames = []
|
||||
if support.TESTFN_UNENCODABLE is not None:
|
||||
decoded = support.TESTFN_UNENCODABLE
|
||||
else:
|
||||
decoded = support.TESTFN
|
||||
self.filenames.append(decoded)
|
||||
self.filenames.append(Str(decoded))
|
||||
self.unicode_filenames.append(decoded)
|
||||
self.unicode_filenames.append(Str(decoded))
|
||||
if support.TESTFN_UNDECODABLE is not None:
|
||||
encoded = support.TESTFN_UNDECODABLE
|
||||
else:
|
||||
encoded = os.fsencode(support.TESTFN)
|
||||
self.filenames.append(encoded)
|
||||
self.filenames.append(memoryview(encoded))
|
||||
self.bytes_filenames.append(encoded)
|
||||
self.bytes_filenames.append(memoryview(encoded))
|
||||
|
||||
self.filenames = self.bytes_filenames + self.unicode_filenames
|
||||
|
||||
def test_oserror_filename(self):
|
||||
funcs = [
|
||||
(os.chdir,),
|
||||
(os.chmod, 0o777),
|
||||
(os.lchown, 0, 0),
|
||||
(os.listdir,),
|
||||
(os.lstat,),
|
||||
(os.open, os.O_RDONLY),
|
||||
(os.rename, "dst"),
|
||||
(os.replace, "dst"),
|
||||
(os.rmdir,),
|
||||
(os.stat,),
|
||||
(os.truncate, 0),
|
||||
(os.unlink,),
|
||||
(self.filenames, os.chdir,),
|
||||
(self.filenames, os.chmod, 0o777),
|
||||
(self.filenames, os.listdir,),
|
||||
(self.filenames, os.lstat,),
|
||||
(self.filenames, os.open, os.O_RDONLY),
|
||||
(self.filenames, os.rmdir,),
|
||||
(self.filenames, os.stat,),
|
||||
(self.filenames, os.unlink,),
|
||||
]
|
||||
if hasattr(os, "chown"):
|
||||
funcs.append((os.chown, 0, 0))
|
||||
if sys.platform == "win32":
|
||||
funcs.extend((
|
||||
(os._getfullpathname,),
|
||||
(os._isdir,),
|
||||
(self.bytes_filenames, os.rename, b"dst"),
|
||||
(self.bytes_filenames, os.replace, b"dst"),
|
||||
(self.unicode_filenames, os.rename, "dst"),
|
||||
(self.unicode_filenames, os.replace, "dst"),
|
||||
))
|
||||
else:
|
||||
funcs.extend((
|
||||
(self.filenames, os.rename, "dst"),
|
||||
(self.filenames, os.replace, "dst"),
|
||||
))
|
||||
if hasattr(os, "chown"):
|
||||
funcs.append((self.filenames, os.chown, 0, 0))
|
||||
if hasattr(os, "lchown"):
|
||||
funcs.append((self.filenames, os.lchown, 0, 0))
|
||||
if hasattr(os, "truncate"):
|
||||
funcs.append((self.filenames, os.truncate, 0))
|
||||
if hasattr(os, "chflags"):
|
||||
funcs.extend((
|
||||
(os.chflags, 0),
|
||||
(os.lchflags, 0),
|
||||
(self.filenames, os.chflags, 0),
|
||||
(self.filenames, os.lchflags, 0),
|
||||
))
|
||||
if hasattr(os, "chroot"):
|
||||
funcs.append((os.chroot,))
|
||||
funcs.append((self.filenames, os.chroot,))
|
||||
if hasattr(os, "link"):
|
||||
funcs.append((os.link, "dst"))
|
||||
if sys.platform == "win32":
|
||||
funcs.append((self.bytes_filenames, os.link, b"dst"))
|
||||
funcs.append((self.unicode_filenames, os.link, "dst"))
|
||||
else:
|
||||
funcs.append((self.filenames, os.link, "dst"))
|
||||
if hasattr(os, "listxattr"):
|
||||
funcs.extend((
|
||||
(os.listxattr,),
|
||||
(os.getxattr, "user.test"),
|
||||
(os.setxattr, "user.test", b'user'),
|
||||
(os.removexattr, "user.test"),
|
||||
(self.filenames, os.listxattr,),
|
||||
(self.filenames, os.getxattr, "user.test"),
|
||||
(self.filenames, os.setxattr, "user.test", b'user'),
|
||||
(self.filenames, os.removexattr, "user.test"),
|
||||
))
|
||||
if hasattr(os, "lchmod"):
|
||||
funcs.append((os.lchmod, 0o777))
|
||||
funcs.append((self.filenames, os.lchmod, 0o777))
|
||||
if hasattr(os, "readlink"):
|
||||
funcs.append((os.readlink,))
|
||||
if sys.platform == "win32":
|
||||
funcs.append((self.unicode_filenames, os.readlink,))
|
||||
else:
|
||||
funcs.append((self.filenames, os.readlink,))
|
||||
|
||||
for func, *func_args in funcs:
|
||||
for name in self.filenames:
|
||||
for filenames, func, *func_args in funcs:
|
||||
for name in filenames:
|
||||
try:
|
||||
func(name, *func_args)
|
||||
except OSError as err:
|
||||
|
|
|
@ -1034,17 +1034,6 @@ win32_error(char* function, const char* filename)
|
|||
return PyErr_SetFromWindowsErr(errno);
|
||||
}
|
||||
|
||||
static PyObject *
|
||||
win32_error_unicode(char* function, wchar_t* filename)
|
||||
{
|
||||
/* XXX - see win32_error for comments on 'function' */
|
||||
errno = GetLastError();
|
||||
if (filename)
|
||||
return PyErr_SetFromWindowsErrWithUnicodeFilename(errno, filename);
|
||||
else
|
||||
return PyErr_SetFromWindowsErr(errno);
|
||||
}
|
||||
|
||||
static PyObject *
|
||||
win32_error_object(char* function, PyObject* filename)
|
||||
{
|
||||
|
@ -1119,44 +1108,6 @@ posix_1str(const char *func_name, PyObject *args, char *format,
|
|||
|
||||
|
||||
#ifdef MS_WINDOWS
|
||||
static PyObject*
|
||||
win32_1str(PyObject* args, char* func,
|
||||
char* format, BOOL (__stdcall *funcA)(LPCSTR),
|
||||
char* wformat, BOOL (__stdcall *funcW)(LPWSTR))
|
||||
{
|
||||
PyObject *uni;
|
||||
const char *ansi;
|
||||
BOOL result;
|
||||
|
||||
if (PyArg_ParseTuple(args, wformat, &uni))
|
||||
{
|
||||
wchar_t *wstr = PyUnicode_AsUnicode(uni);
|
||||
if (wstr == NULL)
|
||||
return NULL;
|
||||
Py_BEGIN_ALLOW_THREADS
|
||||
result = funcW(wstr);
|
||||
Py_END_ALLOW_THREADS
|
||||
if (!result)
|
||||
return win32_error_object(func, uni);
|
||||
Py_INCREF(Py_None);
|
||||
return Py_None;
|
||||
}
|
||||
PyErr_Clear();
|
||||
|
||||
if (!PyArg_ParseTuple(args, format, &ansi))
|
||||
return NULL;
|
||||
if (win32_warn_bytes_api())
|
||||
return NULL;
|
||||
Py_BEGIN_ALLOW_THREADS
|
||||
result = funcA(ansi);
|
||||
Py_END_ALLOW_THREADS
|
||||
if (!result)
|
||||
return win32_error(func, ansi);
|
||||
Py_INCREF(Py_None);
|
||||
return Py_None;
|
||||
|
||||
}
|
||||
|
||||
/* This is a reimplementation of the C library's chdir function,
|
||||
but one that produces Win32 errors instead of DOS error codes.
|
||||
chdir is essentially a wrapper around SetCurrentDirectory; however,
|
||||
|
@ -2533,7 +2484,7 @@ posix_chmod(PyObject *self, PyObject *args, PyObject *kwargs)
|
|||
Py_END_ALLOW_THREADS
|
||||
|
||||
if (!result) {
|
||||
return_value = win32_error_object("chmod", path.object);
|
||||
return_value = path_error(&path);
|
||||
goto exit;
|
||||
}
|
||||
#else /* MS_WINDOWS */
|
||||
|
@ -2989,11 +2940,13 @@ posix_getcwd(int use_bytes)
|
|||
return NULL;
|
||||
}
|
||||
if (!len) {
|
||||
if (wbuf2 != wbuf) free(wbuf2);
|
||||
return win32_error("getcwdu", NULL);
|
||||
if (wbuf2 != wbuf)
|
||||
free(wbuf2);
|
||||
return PyErr_SetFromWindowsErr(0);
|
||||
}
|
||||
resobj = PyUnicode_FromWideChar(wbuf2, len);
|
||||
if (wbuf2 != wbuf) free(wbuf2);
|
||||
if (wbuf2 != wbuf)
|
||||
free(wbuf2);
|
||||
return resobj;
|
||||
}
|
||||
|
||||
|
@ -3101,7 +3054,7 @@ posix_link(PyObject *self, PyObject *args, PyObject *kwargs)
|
|||
Py_END_ALLOW_THREADS
|
||||
|
||||
if (!result) {
|
||||
return_value = win32_error_object("link", dst.object);
|
||||
return_value = path_error(&src);
|
||||
goto exit;
|
||||
}
|
||||
#else
|
||||
|
@ -3225,8 +3178,7 @@ posix_listdir(PyObject *self, PyObject *args, PyObject *kwargs)
|
|||
if (error == ERROR_FILE_NOT_FOUND)
|
||||
goto exit;
|
||||
Py_DECREF(list);
|
||||
list = NULL;
|
||||
win32_error_unicode("FindFirstFileW", wnamebuf);
|
||||
list = path_error(&path);
|
||||
goto exit;
|
||||
}
|
||||
do {
|
||||
|
@ -3255,7 +3207,7 @@ posix_listdir(PyObject *self, PyObject *args, PyObject *kwargs)
|
|||
it got to the end of the directory. */
|
||||
if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
|
||||
Py_DECREF(list);
|
||||
list = win32_error_unicode("FindNextFileW", wnamebuf);
|
||||
list = path_error(&path);
|
||||
goto exit;
|
||||
}
|
||||
} while (result == TRUE);
|
||||
|
@ -3282,7 +3234,7 @@ posix_listdir(PyObject *self, PyObject *args, PyObject *kwargs)
|
|||
if (error == ERROR_FILE_NOT_FOUND)
|
||||
goto exit;
|
||||
Py_DECREF(list);
|
||||
list = win32_error("FindFirstFile", namebuf);
|
||||
list = path_error(&path);
|
||||
goto exit;
|
||||
}
|
||||
do {
|
||||
|
@ -3310,7 +3262,7 @@ posix_listdir(PyObject *self, PyObject *args, PyObject *kwargs)
|
|||
it got to the end of the directory. */
|
||||
if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
|
||||
Py_DECREF(list);
|
||||
list = win32_error("FindNextFile", namebuf);
|
||||
list = path_error(&path);
|
||||
goto exit;
|
||||
}
|
||||
} while (result == TRUE);
|
||||
|
@ -3320,7 +3272,7 @@ exit:
|
|||
if (FindClose(hFindFile) == FALSE) {
|
||||
if (list != NULL) {
|
||||
Py_DECREF(list);
|
||||
list = win32_error_object("FindClose", path.object);
|
||||
list = path_error(&path);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -3569,7 +3521,7 @@ posix__getfileinformation(PyObject *self, PyObject *args)
|
|||
return posix_error();
|
||||
|
||||
if (!GetFileInformationByHandle(hFile, &info))
|
||||
return win32_error("_getfileinformation", NULL);
|
||||
return PyErr_SetFromWindowsErr(0);
|
||||
|
||||
return Py_BuildValue("iii", info.dwVolumeSerialNumber,
|
||||
info.nFileIndexHigh,
|
||||
|
@ -3658,7 +3610,7 @@ posix_mkdir(PyObject *self, PyObject *args, PyObject *kwargs)
|
|||
Py_END_ALLOW_THREADS
|
||||
|
||||
if (!result) {
|
||||
return_value = win32_error_object("mkdir", path.object);
|
||||
return_value = path_error(&path);
|
||||
goto exit;
|
||||
}
|
||||
#else
|
||||
|
@ -3828,7 +3780,7 @@ internal_rename(PyObject *args, PyObject *kwargs, int is_replace)
|
|||
Py_END_ALLOW_THREADS
|
||||
|
||||
if (!result) {
|
||||
return_value = win32_error_object(function_name, dst.object);
|
||||
return_value = path_error(&src);
|
||||
goto exit;
|
||||
}
|
||||
|
||||
|
@ -4396,6 +4348,7 @@ posix_utime(PyObject *self, PyObject *args, PyObject *kwargs)
|
|||
PyObject *return_value = NULL;
|
||||
|
||||
memset(&path, 0, sizeof(path));
|
||||
path.function_name = "utime";
|
||||
#if UTIME_HAVE_FD
|
||||
path.allow_fd = 1;
|
||||
#endif
|
||||
|
@ -4484,7 +4437,7 @@ posix_utime(PyObject *self, PyObject *args, PyObject *kwargs)
|
|||
FILE_FLAG_BACKUP_SEMANTICS, NULL);
|
||||
Py_END_ALLOW_THREADS
|
||||
if (hFile == INVALID_HANDLE_VALUE) {
|
||||
win32_error_object("utime", path.object);
|
||||
path_error(&path);
|
||||
goto exit;
|
||||
}
|
||||
|
||||
|
@ -4493,7 +4446,7 @@ posix_utime(PyObject *self, PyObject *args, PyObject *kwargs)
|
|||
GetSystemTime(&now);
|
||||
if (!SystemTimeToFileTime(&now, &mtime) ||
|
||||
!SystemTimeToFileTime(&now, &atime)) {
|
||||
win32_error("utime", NULL);
|
||||
PyErr_SetFromWindowsErr(0);
|
||||
goto exit;
|
||||
}
|
||||
}
|
||||
|
@ -4506,7 +4459,7 @@ posix_utime(PyObject *self, PyObject *args, PyObject *kwargs)
|
|||
as that may confuse the user into believing that
|
||||
something is wrong with the file, when it also
|
||||
could be the time stamp that gives a problem. */
|
||||
win32_error("utime", NULL);
|
||||
PyErr_SetFromWindowsErr(0);
|
||||
goto exit;
|
||||
}
|
||||
#else /* MS_WINDOWS */
|
||||
|
@ -6736,7 +6689,7 @@ posix_symlink(PyObject *self, PyObject *args, PyObject *kwargs)
|
|||
Py_END_ALLOW_THREADS
|
||||
|
||||
if (!result) {
|
||||
return_value = win32_error_object("symlink", src.object);
|
||||
return_value = path_error(&src);
|
||||
goto exit;
|
||||
}
|
||||
|
||||
|
@ -6752,7 +6705,7 @@ posix_symlink(PyObject *self, PyObject *args, PyObject *kwargs)
|
|||
Py_END_ALLOW_THREADS
|
||||
|
||||
if (result) {
|
||||
return_value = path_error(&dst);
|
||||
return_value = path_error(&src);
|
||||
goto exit;
|
||||
}
|
||||
#endif
|
||||
|
@ -7104,12 +7057,6 @@ posix_open(PyObject *self, PyObject *args, PyObject *kwargs)
|
|||
Py_END_ALLOW_THREADS
|
||||
|
||||
if (fd == -1) {
|
||||
#ifdef MS_WINDOWS
|
||||
/* force use of posix_error here for exact backwards compatibility */
|
||||
if (path.wide)
|
||||
return_value = posix_error();
|
||||
else
|
||||
#endif
|
||||
return_value = path_error(&path);
|
||||
goto exit;
|
||||
}
|
||||
|
@ -7648,7 +7595,7 @@ posix_fstat(PyObject *self, PyObject *args)
|
|||
Py_END_ALLOW_THREADS
|
||||
if (res != 0) {
|
||||
#ifdef MS_WINDOWS
|
||||
return win32_error("fstat", NULL);
|
||||
return PyErr_SetFromWindowsErr(0);
|
||||
#else
|
||||
return posix_error();
|
||||
#endif
|
||||
|
@ -7694,7 +7641,7 @@ posix_pipe(PyObject *self, PyObject *noargs)
|
|||
BOOL ok;
|
||||
ok = CreatePipe(&read, &write, NULL, 0);
|
||||
if (!ok)
|
||||
return win32_error("CreatePipe", NULL);
|
||||
return PyErr_SetFromWindowsErr(0);
|
||||
read_fd = _open_osfhandle((Py_intptr_t)read, 0);
|
||||
write_fd = _open_osfhandle((Py_intptr_t)write, 1);
|
||||
return Py_BuildValue("(ii)", read_fd, write_fd);
|
||||
|
|
Loading…
Reference in New Issue