Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/s3transfer/compat.py: 39%
38 statements
« prev ^ index » next coverage.py v7.3.2, created at 2023-12-08 06:51 +0000
« prev ^ index » next coverage.py v7.3.2, created at 2023-12-08 06:51 +0000
1# Copyright 2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2#
3# Licensed under the Apache License, Version 2.0 (the "License"). You
4# may not use this file except in compliance with the License. A copy of
5# the License is located at
6#
7# http://aws.amazon.com/apache2.0/
8#
9# or in the "license" file accompanying this file. This file is
10# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
11# ANY KIND, either express or implied. See the License for the specific
12# language governing permissions and limitations under the License.
13import errno
14import inspect
15import os
16import socket
17import sys
19from botocore.compat import six
21if sys.platform.startswith('win'):
22 def rename_file(current_filename, new_filename):
23 try:
24 os.remove(new_filename)
25 except OSError as e:
26 if not e.errno == errno.ENOENT:
27 # We only want to a ignore trying to remove
28 # a file that does not exist. If it fails
29 # for any other reason we should be propagating
30 # that exception.
31 raise
32 os.rename(current_filename, new_filename)
33else:
34 rename_file = os.rename
37def accepts_kwargs(func):
38 return inspect.getfullargspec(func)[2]
41# In python 3, socket.error is OSError, which is too general
42# for what we want (i.e FileNotFoundError is a subclass of OSError).
43# In python 3, all the socket related errors are in a newly created
44# ConnectionError.
45SOCKET_ERROR = ConnectionError
46MAXINT = None
49def seekable(fileobj):
50 """Backwards compat function to determine if a fileobj is seekable
52 :param fileobj: The file-like object to determine if seekable
54 :returns: True, if seekable. False, otherwise.
55 """
56 # If the fileobj has a seekable attr, try calling the seekable()
57 # method on it.
58 if hasattr(fileobj, 'seekable'):
59 return fileobj.seekable()
60 # If there is no seekable attr, check if the object can be seeked
61 # or telled. If it can, try to seek to the current position.
62 elif hasattr(fileobj, 'seek') and hasattr(fileobj, 'tell'):
63 try:
64 fileobj.seek(0, 1)
65 return True
66 except OSError:
67 # If an io related error was thrown then it is not seekable.
68 return False
69 # Else, the fileobj is not seekable
70 return False
73def readable(fileobj):
74 """Determines whether or not a file-like object is readable.
76 :param fileobj: The file-like object to determine if readable
78 :returns: True, if readable. False otherwise.
79 """
80 if hasattr(fileobj, 'readable'):
81 return fileobj.readable()
83 return hasattr(fileobj, 'read')
86def fallocate(fileobj, size):
87 if hasattr(os, 'posix_fallocate'):
88 os.posix_fallocate(fileobj.fileno(), 0, size)
89 else:
90 fileobj.truncate(size)
93# Import at end of file to avoid circular dependencies
94from multiprocessing.managers import BaseManager # noqa: F401,E402