@@ -4084,17 +4084,62 @@ posix_replace(PyObject *self, PyObject *args, PyObject *kwargs)
40844084}
40854085
40864086PyDoc_STRVAR (posix_rmdir__doc__ ,
4087- "rmdir(path)\n\n\
4088- Remove a directory." );
4087+ "rmdir(path, *, dir_fd=None)\n\n\
4088+ Remove a directory.\n\
4089+ \n\
4090+ If dir_fd is not None, it should be a file descriptor open to a directory,\n\
4091+ and path should be relative; path will then be relative to that directory.\n\
4092+ dir_fd may not be implemented on your platform.\n\
4093+ If it is unavailable, using it will raise a NotImplementedError." );
40894094
40904095static PyObject *
4091- posix_rmdir (PyObject * self , PyObject * args )
4096+ posix_rmdir (PyObject * self , PyObject * args , PyObject * kwargs )
40924097{
4098+ path_t path ;
4099+ int dir_fd = DEFAULT_DIR_FD ;
4100+ static char * keywords [] = {"path" , "dir_fd" , NULL };
4101+ int result ;
4102+ PyObject * return_value = NULL ;
4103+
4104+ memset (& path , 0 , sizeof (path ));
4105+ if (!PyArg_ParseTupleAndKeywords (args , kwargs , "O&|$O&:rmdir" , keywords ,
4106+ path_converter , & path ,
4107+ #ifdef HAVE_UNLINKAT
4108+ dir_fd_converter , & dir_fd
4109+ #else
4110+ dir_fd_unavailable , & dir_fd
4111+ #endif
4112+ ))
4113+ return NULL ;
4114+
4115+ Py_BEGIN_ALLOW_THREADS
40934116#ifdef MS_WINDOWS
4094- return win32_1str (args , "rmdir" , "y:rmdir" , RemoveDirectoryA , "U:rmdir" , RemoveDirectoryW );
4117+ if (path .wide )
4118+ result = RemoveDirectoryW (path .wide );
4119+ else
4120+ result = RemoveDirectoryA (path .narrow );
4121+ result = !result ; /* Windows, success=1, UNIX, success=0 */
40954122#else
4096- return posix_1str (args , "O&:rmdir" , rmdir );
4123+ #ifdef HAVE_UNLINKAT
4124+ if (dir_fd != DEFAULT_DIR_FD )
4125+ result = unlinkat (dir_fd , path .narrow , AT_REMOVEDIR );
4126+ else
40974127#endif
4128+ result = rmdir (path .narrow );
4129+ #endif
4130+ Py_END_ALLOW_THREADS
4131+
4132+ if (result ) {
4133+ return_value = path_error ("rmdir" , & path );
4134+ goto exit ;
4135+ }
4136+
4137+ return_value = Py_None ;
4138+ Py_INCREF (Py_None );
4139+
4140+ exit :
4141+ path_cleanup (& path );
4142+ return return_value ;
40984143}
40994144
41004145
@@ -4186,68 +4231,54 @@ BOOL WINAPI Py_DeleteFileW(LPCWSTR lpFileName)
41864231#endif /* MS_WINDOWS */
41874232
41884233PyDoc_STRVAR (posix_unlink__doc__ ,
4189- "unlink(path, *, dir_fd=None, rmdir=False )\n\n\
4234+ "unlink(path, *, dir_fd=None)\n\n\
41904235Remove a file (same as remove()).\n\
41914236\n\
41924237If dir_fd is not None, it should be a file descriptor open to a directory,\n\
41934238 and path should be relative; path will then be relative to that directory.\n\
41944239dir_fd may not be implemented on your platform.\n\
4195- If it is unavailable, using it will raise a NotImplementedError.\n\
4196- If rmdir is True, unlink will behave like os.rmdir()." );
4240+ If it is unavailable, using it will raise a NotImplementedError." );
41974241
41984242PyDoc_STRVAR (posix_remove__doc__ ,
4199- "remove(path, *, dir_fd=None, rmdir=False )\n\n\
4243+ "remove(path, *, dir_fd=None)\n\n\
42004244Remove a file (same as unlink()).\n\
42014245\n\
42024246If dir_fd is not None, it should be a file descriptor open to a directory,\n\
42034247 and path should be relative; path will then be relative to that directory.\n\
42044248dir_fd may not be implemented on your platform.\n\
4205- If it is unavailable, using it will raise a NotImplementedError.\n\
4206- If rmdir is True, remove will behave like os.rmdir()." );
4249+ If it is unavailable, using it will raise a NotImplementedError." );
42074250
42084251static PyObject *
42094252posix_unlink (PyObject * self , PyObject * args , PyObject * kwargs )
42104253{
42114254 path_t path ;
42124255 int dir_fd = DEFAULT_DIR_FD ;
4213- int remove_dir = 0 ;
4214- static char * keywords [] = {"path" , "dir_fd" , "rmdir" , NULL };
4256+ static char * keywords [] = {"path" , "dir_fd" , NULL };
42154257 int result ;
42164258 PyObject * return_value = NULL ;
42174259
42184260 memset (& path , 0 , sizeof (path ));
4219- if (!PyArg_ParseTupleAndKeywords (args , kwargs , "O&|$O&p :unlink" , keywords ,
4261+ if (!PyArg_ParseTupleAndKeywords (args , kwargs , "O&|$O&:unlink" , keywords ,
42204262 path_converter , & path ,
42214263#ifdef HAVE_UNLINKAT
4222- dir_fd_converter , & dir_fd ,
4264+ dir_fd_converter , & dir_fd
42234265#else
4224- dir_fd_unavailable , & dir_fd ,
4266+ dir_fd_unavailable , & dir_fd
42254267#endif
4226- & remove_dir ))
4268+ ))
42274269 return NULL ;
42284270
42294271 Py_BEGIN_ALLOW_THREADS
42304272#ifdef MS_WINDOWS
4231- if (remove_dir ) {
4232- if (path .wide )
4233- result = RemoveDirectoryW (path .wide );
4234- else
4235- result = RemoveDirectoryA (path .narrow );
4236- }
4237- else {
4238- if (path .wide )
4239- result = Py_DeleteFileW (path .wide );
4240- else
4241- result = DeleteFileA (path .narrow );
4242- }
4273+ if (path .wide )
4274+ result = Py_DeleteFileW (path .wide );
4275+ else
4276+ result = DeleteFileA (path .narrow );
42434277 result = !result ; /* Windows, success=1, UNIX, success=0 */
42444278#else
4245- if (remove_dir && (dir_fd == DEFAULT_DIR_FD ))
4246- result = rmdir (path .narrow );
4247- else
42484279#ifdef HAVE_UNLINKAT
42494280 if (dir_fd != DEFAULT_DIR_FD )
4250- result = unlinkat (dir_fd , path .narrow , remove_dir ? AT_REMOVEDIR : 0 );
4281+ result = unlinkat (dir_fd , path .narrow , 0 );
42514282 else
42524283#endif /* HAVE_UNLINKAT */
42534284 result = unlink (path .narrow );
@@ -10806,7 +10837,9 @@ static PyMethodDef posix_methods[] = {
1080610837 {"replace" , (PyCFunction )posix_replace ,
1080710838 METH_VARARGS | METH_KEYWORDS ,
1080810839 posix_replace__doc__ },
10809- {"rmdir" , posix_rmdir , METH_VARARGS , posix_rmdir__doc__ },
10840+ {"rmdir" , (PyCFunction )posix_rmdir ,
10841+ METH_VARARGS | METH_KEYWORDS ,
10842+ posix_rmdir__doc__ },
1081010843 {"stat" , (PyCFunction )posix_stat ,
1081110844 METH_VARARGS | METH_KEYWORDS ,
1081210845 posix_stat__doc__ },
0 commit comments