@@ -82,10 +82,10 @@ def _samefile(src, dst):
8282 return (os .path .normcase (os .path .abspath (src )) ==
8383 os .path .normcase (os .path .abspath (dst )))
8484
85- def copyfile (src , dst , symlinks = False ):
85+ def copyfile (src , dst , * , follow_symlinks = True ):
8686 """Copy data from src to dst.
8787
88- If optional flag `symlinks` is set and ` src` is a symbolic link, a new
88+ If follow_symlinks is not set and src is a symbolic link, a new
8989 symlink will be created instead of copying the file it points to.
9090
9191 """
@@ -103,23 +103,23 @@ def copyfile(src, dst, symlinks=False):
103103 if stat .S_ISFIFO (st .st_mode ):
104104 raise SpecialFileError ("`%s` is a named pipe" % fn )
105105
106- if symlinks and os .path .islink (src ):
106+ if not follow_symlinks and os .path .islink (src ):
107107 os .symlink (os .readlink (src ), dst )
108108 else :
109109 with open (src , 'rb' ) as fsrc :
110110 with open (dst , 'wb' ) as fdst :
111111 copyfileobj (fsrc , fdst )
112112 return dst
113113
114- def copymode (src , dst , symlinks = False ):
114+ def copymode (src , dst , * , follow_symlinks = True ):
115115 """Copy mode bits from src to dst.
116116
117- If the optional flag `symlinks` is set, symlinks aren't followed if and
118- only if both `src` and `dst` are symlinks. If `lchmod` isn't available (eg.
119- Linux), in these cases, this method does nothing.
117+ If follow_symlinks is not set, symlinks aren't followed if and only
118+ if both `src` and `dst` are symlinks. If `lchmod` isn't available
119+ (e.g. Linux) this method does nothing.
120120
121121 """
122- if symlinks and os .path .islink (src ) and os .path .islink (dst ):
122+ if not follow_symlinks and os .path .islink (src ) and os .path .islink (dst ):
123123 if hasattr (os , 'lchmod' ):
124124 stat_func , chmod_func = os .lstat , os .lchmod
125125 else :
@@ -133,38 +133,38 @@ def copymode(src, dst, symlinks=False):
133133 chmod_func (dst , stat .S_IMODE (st .st_mode ))
134134
135135if hasattr (os , 'listxattr' ):
136- def _copyxattr (src , dst , symlinks = False ):
136+ def _copyxattr (src , dst , * , follow_symlinks = True ):
137137 """Copy extended filesystem attributes from `src` to `dst`.
138138
139139 Overwrite existing attributes.
140140
141- If the optional flag `symlinks ` is set , symlinks won't be followed.
141+ If `follow_symlinks ` is false , symlinks won't be followed.
142142
143143 """
144144
145- for name in os .listxattr (src , follow_symlinks = symlinks ):
145+ for name in os .listxattr (src , follow_symlinks = follow_symlinks ):
146146 try :
147- value = os .getxattr (src , name , follow_symlinks = symlinks )
148- os .setxattr (dst , name , value , follow_symlinks = symlinks )
147+ value = os .getxattr (src , name , follow_symlinks = follow_symlinks )
148+ os .setxattr (dst , name , value , follow_symlinks = follow_symlinks )
149149 except OSError as e :
150150 if e .errno not in (errno .EPERM , errno .ENOTSUP , errno .ENODATA ):
151151 raise
152152else :
153153 def _copyxattr (* args , ** kwargs ):
154154 pass
155155
156- def copystat (src , dst , symlinks = False ):
156+ def copystat (src , dst , * , follow_symlinks = True ):
157157 """Copy all stat info (mode bits, atime, mtime, flags) from src to dst.
158158
159- If the optional flag `symlinks ` is set, symlinks aren't followed if and
159+ If the optional flag `follow_symlinks ` is not set, symlinks aren't followed if and
160160 only if both `src` and `dst` are symlinks.
161161
162162 """
163163 def _nop (* args , ns = None , follow_symlinks = None ):
164164 pass
165165
166166 # follow symlinks (aka don't not follow symlinks)
167- follow = not ( symlinks and os .path .islink (src ) and os .path .islink (dst ))
167+ follow = follow_symlinks or not ( os .path .islink (src ) and os .path .islink (dst ))
168168 if follow :
169169 # use the real function if it exists
170170 def lookup (name ):
@@ -205,37 +205,37 @@ def lookup(name):
205205 break
206206 else :
207207 raise
208- _copyxattr (src , dst , symlinks = follow )
208+ _copyxattr (src , dst , follow_symlinks = follow )
209209
210- def copy (src , dst , symlinks = False ):
210+ def copy (src , dst , * , follow_symlinks = True ):
211211 """Copy data and mode bits ("cp src dst"). Return the file's destination.
212212
213213 The destination may be a directory.
214214
215- If the optional flag `symlinks` is set , symlinks won't be followed. This
215+ If follow_symlinks is false , symlinks won't be followed. This
216216 resembles GNU's "cp -P src dst".
217217
218218 """
219219 if os .path .isdir (dst ):
220220 dst = os .path .join (dst , os .path .basename (src ))
221- copyfile (src , dst , symlinks = symlinks )
222- copymode (src , dst , symlinks = symlinks )
221+ copyfile (src , dst , follow_symlinks = follow_symlinks )
222+ copymode (src , dst , follow_symlinks = follow_symlinks )
223223 return dst
224224
225- def copy2 (src , dst , symlinks = False ):
225+ def copy2 (src , dst , * , follow_symlinks = True ):
226226 """Copy data and all stat info ("cp -p src dst"). Return the file's
227227 destination."
228228
229229 The destination may be a directory.
230230
231- If the optional flag `symlinks` is set , symlinks won't be followed. This
231+ If follow_symlinks is false , symlinks won't be followed. This
232232 resembles GNU's "cp -P src dst".
233233
234234 """
235235 if os .path .isdir (dst ):
236236 dst = os .path .join (dst , os .path .basename (src ))
237- copyfile (src , dst , symlinks = symlinks )
238- copystat (src , dst , symlinks = symlinks )
237+ copyfile (src , dst , follow_symlinks = follow_symlinks )
238+ copystat (src , dst , follow_symlinks = follow_symlinks )
239239 return dst
240240
241241def ignore_patterns (* patterns ):
@@ -307,7 +307,7 @@ def copytree(src, dst, symlinks=False, ignore=None, copy_function=copy2,
307307 # code with a custom `copy_function` may rely on copytree
308308 # doing the right thing.
309309 os .symlink (linkto , dstname )
310- copystat (srcname , dstname , symlinks = symlinks )
310+ copystat (srcname , dstname , follow_symlinks = not symlinks )
311311 else :
312312 # ignore dangling symlink if the flag is on
313313 if not os .path .exists (linkto ) and ignore_dangling_symlinks :
0 commit comments