MCPcopy Create free account
hub / github.com/RustPython/RustPython / copyfileobj

Function copyfileobj

Lib/pathlib/_os.py:114–166  ·  view source on GitHub ↗

Copy data from file-like object source_f to file-like object target_f.

(source_f, target_f)

Source from the content-addressed store, hash-verified

112
113
114def copyfileobj(source_f, target_f):
115 """
116 Copy data from file-like object source_f to file-like object target_f.
117 """
118 try:
119 source_fd = source_f.fileno()
120 target_fd = target_f.fileno()
121 except Exception:
122 pass # Fall through to generic code.
123 else:
124 try:
125 # Use OS copy-on-write where available.
126 if _ficlone:
127 try:
128 _ficlone(source_fd, target_fd)
129 return
130 except OSError as err:
131 if err.errno not in (EBADF, EOPNOTSUPP, ETXTBSY, EXDEV):
132 raise err
133
134 # Use OS copy where available.
135 if _fcopyfile:
136 try:
137 _fcopyfile(source_fd, target_fd)
138 return
139 except OSError as err:
140 if err.errno not in (EINVAL, ENOTSUP):
141 raise err
142 if _copy_file_range:
143 try:
144 _copy_file_range(source_fd, target_fd)
145 return
146 except OSError as err:
147 if err.errno not in (ETXTBSY, EXDEV):
148 raise err
149 if _sendfile:
150 try:
151 _sendfile(source_fd, target_fd)
152 return
153 except OSError as err:
154 if err.errno != ENOTSOCK:
155 raise err
156 except OSError as err:
157 # Produce more useful error messages.
158 err.filename = source_f.name
159 err.filename2 = target_f.name
160 raise err
161
162 # Last resort: copy with fileobj read() and write().
163 read_source = source_f.read
164 write_target = target_f.write
165 while buf := read_source(1024 * 1024):
166 write_target(buf)
167
168
169def magic_open(path, mode='r', buffering=-1, encoding=None, errors=None,

Callers 2

_copy_from_fileMethod · 0.90
_copy_fromMethod · 0.90

Calls 5

_ficloneFunction · 0.85
_copy_file_rangeFunction · 0.85
_sendfileFunction · 0.85
_fcopyfileFunction · 0.70
filenoMethod · 0.45

Tested by

no test coverage detected