Author: Amaury Forgeot d'Arc <amaur...@gmail.com> Branch: more-rposix Changeset: r74406:a3131c39081f Date: 2014-11-08 22:16 +0100 http://bitbucket.org/pypy/pypy/changeset/a3131c39081f/
Log: Move many functions at the same time... diff --git a/rpython/rlib/rposix.py b/rpython/rlib/rposix.py --- a/rpython/rlib/rposix.py +++ b/rpython/rlib/rposix.py @@ -270,44 +270,10 @@ def lstat(path): return os.lstat(_as_bytes(path)) - @specialize.argtype(0) def statvfs(path): return os.statvfs(_as_bytes(path)) - -@specialize.argtype(0) -def unlink(path): - return os.unlink(_as_bytes(path)) - -@specialize.argtype(0, 1) -def rename(path1, path2): - return os.rename(_as_bytes(path1), _as_bytes(path2)) - -@specialize.argtype(0) -def chmod(path, mode): - return os.chmod(_as_bytes(path), mode) - -@specialize.argtype(0) -def chdir(path): - return os.chdir(_as_bytes(path)) - -@specialize.argtype(0) -def mkdir(path, mode=0777): - return os.mkdir(_as_bytes(path), mode) - -@specialize.argtype(0) -def rmdir(path): - return os.rmdir(_as_bytes(path)) - -@specialize.argtype(0) -def mkfifo(path, mode): - os.mkfifo(_as_bytes(path), mode) - -@specialize.argtype(0) -def mknod(path, mode, device): - os.mknod(_as_bytes(path), mode, device) - @specialize.argtype(0, 1) def symlink(src, dest): os.symlink(_as_bytes(src), _as_bytes(dest)) @@ -431,12 +397,63 @@ #___________________________________________________________________ +c_chdir = external('chdir', [rffi.CCHARP], rffi.INT) c_fchdir = external('fchdir', [rffi.INT], rffi.INT) c_access = external(UNDERSCORE_ON_WIN32 + 'access', [rffi.CCHARP, rffi.INT], rffi.INT) c_waccess = external(UNDERSCORE_ON_WIN32 + 'waccess', [rffi.CWCHARP, rffi.INT], rffi.INT) +@replace_os_function('chdir') +@specialize.argtype(0) +def chdir(path): + if not _WIN32: + handle_posix_error('chdir', c_chdir(_as_bytes0(path))) + else: + traits = _preferred_traits(path) + from rpython.rlib.rwin32file import make_win32_traits + win32traits = make_win32_traits(traits) + path = traits._as_str0(path) + + # 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, it also needs to set "magic" environment variables + # indicating the per-drive current directory, which are of the + # form =<drive>: + if not win32traits.SetCurrentDirectory(path): + raise rwin32.lastWindowsError() + MAX_PATH = rwin32.MAX_PATH + assert MAX_PATH > 0 + + with traits.scoped_alloc_buffer(MAX_PATH) as path: + res = win32traits.GetCurrentDirectory(MAX_PATH + 1, path.raw) + if not res: + raise rwin32.lastWindowsError() + res = rffi.cast(lltype.Signed, res) + assert res > 0 + if res <= MAX_PATH + 1: + new_path = path.str(res) + else: + with traits.scoped_alloc_buffer(res) as path: + res = win32traits.GetCurrentDirectory(res, path.raw) + if not res: + raise rwin32.lastWindowsError() + res = rffi.cast(lltype.Signed, res) + assert res > 0 + new_path = path.str(res) + if traits.str is unicode: + if new_path[0] == u'\\' or new_path[0] == u'/': # UNC path + return + magic_envvar = u'=' + new_path[0] + u':' + else: + if new_path[0] == '\\' or new_path[0] == '/': # UNC path + return + magic_envvar = '=' + new_path[0] + ':' + if not win32traits.SetEnvironmentVariable(magic_envvar, new_path): + raise rwin32.lastWindowsError() + @replace_os_function('fchdir') def fchdir(fd): validate_fd(fd) @@ -800,6 +817,152 @@ #___________________________________________________________________ +c_readlink = external('readlink', + [rffi.CCHARP, rffi.CCHARP, rffi.SIZE_T], rffi.SSIZE_T) + +@replace_os_function('readlink') +def readlink(path): + path = _as_bytes0(path) + bufsize = 1023 + while True: + buf = lltype.malloc(rffi.CCHARP.TO, bufsize, flavor='raw') + res = widen(c_readlink(path, buf, bufsize)) + if res < 0: + lltype.free(buf, flavor='raw') + error = get_errno() # failed + raise OSError(error, "readlink failed") + elif res < bufsize: + break # ok + else: + # buf too small, try again with a larger buffer + lltype.free(buf, flavor='raw') + bufsize *= 4 + # convert the result to a string + result = rffi.charp2strn(buf, res) + lltype.free(buf, flavor='raw') + return result + +c_isatty = external(UNDERSCORE_ON_WIN32 + 'isatty', [rffi.INT], rffi.INT) + +@replace_os_function('isatty') +def isatty(fd): + if not is_valid_fd(fd): + return False + return c_isatty(fd) != 0 + +c_strerror = external('strerror', [rffi.INT], rffi.CCHARP, + releasegil=False) + +@replace_os_function('strerror') +def strerror(errnum): + res = c_strerror(errnum) + if not res: + raise ValueError("os_strerror failed") + return rffi.charp2str(res) + +c_system = external('system', [rffi.CCHARP], rffi.INT) + +@replace_os_function('system') +def system(command): + return widen(c_system(command)) + +c_unlink = external('unlink', [rffi.CCHARP], rffi.INT) +c_mkdir = external('mkdir', [rffi.CCHARP, rffi.MODE_T], rffi.INT) +c_rmdir = external(UNDERSCORE_ON_WIN32 + 'rmdir', [rffi.CCHARP], rffi.INT) +c_wrmdir = external(UNDERSCORE_ON_WIN32 + 'wrmdir', [rffi.CWCHARP], rffi.INT) + +@replace_os_function('unlink') +@specialize.argtype(0) +def unlink(path): + if not _WIN32: + handle_posix_error('unlink', c_unlink(_as_bytes0(path))) + else: + traits = _preferred_traits(path) + win32traits = make_win32_traits(traits) + if not win32traits.DeleteFile(traits.as_str0(path)): + raise rwin32.lastWindowsError() + +@replace_os_function('mkdir') +@specialize.argtype(0) +def mkdir(path, mode=0o777): + if not _WIN32: + handle_posix_error('mkdir', c_mkdir(_as_bytes0(path), mode)) + else: + traits = _preferred_traits(path) + win32traits = make_win32_traits(traits) + if not win32traits.CreateDirectory(traits.as_str0(path), None): + raise rwin32.lastWindowsError() + +@replace_os_function('rmdir') +@specialize.argtype(0) +def rmdir(path): + if _prefer_unicode(path): + handle_posix_error('wrmdir', c_wrmdir(_as_unicode0(path))) + else: + handle_posix_error('rmdir', c_rmdir(_as_bytes0(path))) + +c_chmod = external('chmod', [rffi.CCHARP, rffi.MODE_T], rffi.INT) +c_fchmod = external('fchmod', [rffi.INT, rffi.MODE_T], rffi.INT) +c_rename = external('rename', [rffi.CCHARP, rffi.CCHARP], rffi.INT) + +@replace_os_function('chmod') +@specialize.argtype(0) +def chmod(path, mode): + if not _WIN32: + handle_posix_error('chmod', c_chmod(_as_bytes0(path), mode)) + else: + traits = _preferred_traits(path) + win32traits = make_win32_traits(traits) + path = traits.as_str0(path) + attr = win32traits.GetFileAttributes(path) + if attr == win32traits.INVALID_FILE_ATTRIBUTES: + raise rwin32.lastWindowsError() + if mode & 0200: # _S_IWRITE + attr &= ~win32traits.FILE_ATTRIBUTE_READONLY + else: + attr |= win32traits.FILE_ATTRIBUTE_READONLY + if not win32traits.SetFileAttributes(path, attr): + raise rwin32.lastWindowsError() + +@replace_os_function('fchmod') +def fchmod(fd, mode): + handle_posix_error('fchmod', c_fchmod(fd, mode)) + +@replace_os_function('rename') +@specialize.argtype(0, 1) +def rename(path1, path2): + if not _WIN32: + handle_posix_error('rename', + c_rename(_as_bytes0(path1), _as_bytes0(path2))) + else: + traits = _preferred_traits(path) + win32traits = make_win32_traits(traits) + path1 = traits.as_str0(path1) + path2 = traits.as_str0(path2) + if not win32traits.MoveFile(path1, path2): + raise rwin32.lastWindowsError() + +c_mkfifo = external('mkfifo', [rffi.CCHARP, rffi.MODE_T], rffi.INT) +c_mknod = external('mknod', [rffi.CCHARP, rffi.MODE_T, rffi.INT], rffi.INT, + macro=True) +# # xxx: actually ^^^ dev_t + +@replace_os_function('mkfifo') +@specialize.argtype(0) +def mkfifo(path, mode): + handle_posix_error('mkfifo', c_mkfifo(_as_bytes0(path), mode)) + +@replace_os_function('mknod') +@specialize.argtype(0) +def mknod(path, mode, dev): + handle_posix_error('mknod', c_mknod(_as_bytes0(path), mode, dev)) + +c_umask = external(UNDERSCORE_ON_WIN32 + 'umask', [rffi.MODE_T], rffi.MODE_T) + +@replace_os_function('umask') +def umask(newmask): + return widen(c_umask(newmask)) + c_chown = external('chown', [rffi.CCHARP, rffi.INT, rffi.INT], rffi.INT) c_lchown = external('lchown', [rffi.CCHARP, rffi.INT, rffi.INT], rffi.INT) c_fchown = external('fchown', [rffi.INT, rffi.INT, rffi.INT], rffi.INT) diff --git a/rpython/rtyper/module/ll_os.py b/rpython/rtyper/module/ll_os.py --- a/rpython/rtyper/module/ll_os.py +++ b/rpython/rtyper/module/ll_os.py @@ -173,246 +173,6 @@ separate_module_sources = ["\n".join(defs)] )) - @registering_if(os, 'readlink') - def register_os_readlink(self): - os_readlink = self.llexternal('readlink', - [rffi.CCHARP, rffi.CCHARP, rffi.SIZE_T], - rffi.INT) - # XXX SSIZE_T in POSIX.1-2001 - - def os_readlink_llimpl(path): - bufsize = 1023 - while True: - l_path = rffi.str2charp(path) - buf = lltype.malloc(rffi.CCHARP.TO, bufsize, - flavor='raw') - res = rffi.cast(lltype.Signed, os_readlink(l_path, buf, bufsize)) - lltype.free(l_path, flavor='raw') - if res < 0: - error = rposix.get_errno() # failed - lltype.free(buf, flavor='raw') - raise OSError(error, "readlink failed") - elif res < bufsize: - break # ok - else: - # buf too small, try again with a larger buffer - lltype.free(buf, flavor='raw') - bufsize *= 4 - # convert the result to a string - result = rffi.charp2strn(buf, res) - lltype.free(buf, flavor='raw') - return result - - return extdef([str0], str0, - "ll_os.ll_os_readlink", - llimpl=os_readlink_llimpl) - - @registering(os.isatty) - def register_os_isatty(self): - os_isatty = self.llexternal(UNDERSCORE_ON_WIN32 + 'isatty', - [rffi.INT], rffi.INT) - - def isatty_llimpl(fd): - if not rposix.is_valid_fd(fd): - return False - res = rffi.cast(lltype.Signed, os_isatty(rffi.cast(rffi.INT, fd))) - return res != 0 - - return extdef([int], bool, llimpl=isatty_llimpl, - export_name="ll_os.ll_os_isatty") - - @registering(os.strerror) - def register_os_strerror(self): - os_strerror = self.llexternal('strerror', [rffi.INT], rffi.CCHARP, releasegil=False) - - def strerror_llimpl(errnum): - res = os_strerror(rffi.cast(rffi.INT, errnum)) - if not res: - raise ValueError("os_strerror failed") - return rffi.charp2str(res) - - return extdef([int], str, llimpl=strerror_llimpl, - export_name="ll_os.ll_os_strerror") - - @registering(os.system) - def register_os_system(self): - os_system = self.llexternal('system', [rffi.CCHARP], rffi.INT) - - def system_llimpl(command): - res = os_system(command) - return rffi.cast(lltype.Signed, res) - - return extdef([str0], int, llimpl=system_llimpl, - export_name="ll_os.ll_os_system") - - @registering_str_unicode(os.unlink) - def register_os_unlink(self, traits): - os_unlink = self.llexternal(traits.posix_function_name('unlink'), - [traits.CCHARP], rffi.INT) - - def unlink_llimpl(pathname): - res = rffi.cast(lltype.Signed, os_unlink(pathname)) - if res < 0: - raise OSError(rposix.get_errno(), "os_unlink failed") - - if sys.platform == 'win32': - from rpython.rlib.rwin32file import make_win32_traits - win32traits = make_win32_traits(traits) - - @func_renamer('unlink_llimpl_%s' % traits.str.__name__) - def unlink_llimpl(path): - if not win32traits.DeleteFile(path): - raise rwin32.lastWindowsError() - - return extdef([traits.str0], s_None, llimpl=unlink_llimpl, - export_name=traits.ll_os_name('unlink')) - - @registering_str_unicode(os.chdir) - def register_os_chdir(self, traits): - os_chdir = self.llexternal(traits.posix_function_name('chdir'), - [traits.CCHARP], rffi.INT) - - def os_chdir_llimpl(path): - res = rffi.cast(lltype.Signed, os_chdir(path)) - if res < 0: - raise OSError(rposix.get_errno(), "os_chdir failed") - - # On Windows, use an implementation that will produce Win32 errors - if sys.platform == 'win32': - from rpython.rtyper.module.ll_win32file import make_chdir_impl - os_chdir_llimpl = make_chdir_impl(traits) - - return extdef([traits.str0], s_None, llimpl=os_chdir_llimpl, - export_name=traits.ll_os_name('chdir')) - - @registering_str_unicode(os.mkdir) - def register_os_mkdir(self, traits): - os_mkdir = self.llexternal(traits.posix_function_name('mkdir'), - [traits.CCHARP, rffi.MODE_T], rffi.INT) - - if sys.platform == 'win32': - from rpython.rlib.rwin32file import make_win32_traits - win32traits = make_win32_traits(traits) - - @func_renamer('mkdir_llimpl_%s' % traits.str.__name__) - def os_mkdir_llimpl(path, mode): - if not win32traits.CreateDirectory(path, None): - raise rwin32.lastWindowsError() - else: - def os_mkdir_llimpl(pathname, mode): - res = os_mkdir(pathname, mode) - res = rffi.cast(lltype.Signed, res) - if res < 0: - raise OSError(rposix.get_errno(), "os_mkdir failed") - - return extdef([traits.str0, int], s_None, llimpl=os_mkdir_llimpl, - export_name=traits.ll_os_name('mkdir')) - - @registering_str_unicode(os.rmdir) - def register_os_rmdir(self, traits): - os_rmdir = self.llexternal(traits.posix_function_name('rmdir'), - [traits.CCHARP], rffi.INT) - - def rmdir_llimpl(pathname): - res = rffi.cast(lltype.Signed, os_rmdir(pathname)) - if res < 0: - raise OSError(rposix.get_errno(), "os_rmdir failed") - - return extdef([traits.str0], s_None, llimpl=rmdir_llimpl, - export_name=traits.ll_os_name('rmdir')) - - @registering_str_unicode(os.chmod) - def register_os_chmod(self, traits): - os_chmod = self.llexternal(traits.posix_function_name('chmod'), - [traits.CCHARP, rffi.MODE_T], rffi.INT) - - def chmod_llimpl(path, mode): - res = rffi.cast(lltype.Signed, os_chmod(path, rffi.cast(rffi.MODE_T, mode))) - if res < 0: - raise OSError(rposix.get_errno(), "os_chmod failed") - - if sys.platform == 'win32': - from rpython.rtyper.module.ll_win32file import make_chmod_impl - chmod_llimpl = make_chmod_impl(traits) - - return extdef([traits.str0, int], s_None, llimpl=chmod_llimpl, - export_name=traits.ll_os_name('chmod')) - - @registering_if(os, 'fchmod') - def register_os_fchmod(self): - os_fchmod = self.llexternal('fchmod', [rffi.INT, rffi.MODE_T], - rffi.INT) - - def fchmod_llimpl(fd, mode): - mode = rffi.cast(rffi.MODE_T, mode) - res = rffi.cast(lltype.Signed, os_fchmod(fd, mode)) - if res < 0: - raise OSError(rposix.get_errno(), "os_fchmod failed") - - return extdef([int, int], s_None, "ll_os.ll_os_fchmod", - llimpl=fchmod_llimpl) - - @registering_str_unicode(os.rename) - def register_os_rename(self, traits): - os_rename = self.llexternal(traits.posix_function_name('rename'), - [traits.CCHARP, traits.CCHARP], rffi.INT) - - def rename_llimpl(oldpath, newpath): - res = rffi.cast(lltype.Signed, os_rename(oldpath, newpath)) - if res < 0: - raise OSError(rposix.get_errno(), "os_rename failed") - - if sys.platform == 'win32': - from rpython.rlib.rwin32file import make_win32_traits - win32traits = make_win32_traits(traits) - - @func_renamer('rename_llimpl_%s' % traits.str.__name__) - def rename_llimpl(oldpath, newpath): - if not win32traits.MoveFile(oldpath, newpath): - raise rwin32.lastWindowsError() - - return extdef([traits.str0, traits.str0], s_None, llimpl=rename_llimpl, - export_name=traits.ll_os_name('rename')) - - @registering_str_unicode(getattr(os, 'mkfifo', None)) - def register_os_mkfifo(self, traits): - os_mkfifo = self.llexternal(traits.posix_function_name('mkfifo'), - [traits.CCHARP, rffi.MODE_T], rffi.INT) - - def mkfifo_llimpl(path, mode): - res = rffi.cast(lltype.Signed, os_mkfifo(path, mode)) - if res < 0: - raise OSError(rposix.get_errno(), "os_mkfifo failed") - - return extdef([traits.str0, int], s_None, llimpl=mkfifo_llimpl, - export_name=traits.ll_os_name('mkfifo')) - - @registering_str_unicode(getattr(os, 'mknod', None)) - def register_os_mknod(self, traits): - os_mknod = self.llexternal(traits.posix_function_name('mknod'), - [traits.CCHARP, rffi.MODE_T, rffi.INT], - rffi.INT) # xxx: actually ^^^ dev_t - - def mknod_llimpl(path, mode, dev): - res = rffi.cast(lltype.Signed, os_mknod(path, mode, dev)) - if res < 0: - raise OSError(rposix.get_errno(), "os_mknod failed") - - return extdef([traits.str0, int, int], s_None, llimpl=mknod_llimpl, - export_name=traits.ll_os_name('mknod')) - - @registering(os.umask) - def register_os_umask(self): - os_umask = self.llexternal(UNDERSCORE_ON_WIN32 + 'umask', - [rffi.MODE_T], rffi.MODE_T) - - def umask_llimpl(newmask): - res = os_umask(rffi.cast(rffi.MODE_T, newmask)) - return rffi.cast(lltype.Signed, res) - - return extdef([int], int, llimpl=umask_llimpl, - export_name="ll_os.ll_os_umask") - @registering_if(os, 'kill', sys.platform != 'win32') def register_os_kill(self): os_kill = self.llexternal('kill', [rffi.PID_T, rffi.INT], diff --git a/rpython/rtyper/module/ll_win32file.py b/rpython/rtyper/module/ll_win32file.py deleted file mode 100644 --- a/rpython/rtyper/module/ll_win32file.py +++ /dev/null @@ -1,115 +0,0 @@ -""" -The Windows implementation of some posix modules, -based on the Win32 API. -""" -from __future__ import with_statement - -from rpython.rtyper.lltypesystem import lltype, rffi -from rpython.translator.tool.cbuild import ExternalCompilationInfo -from rpython.rtyper.tool import rffi_platform as platform -from rpython.tool.sourcetools import func_renamer -from rpython.rlib.objectmodel import specialize - - -#_______________________________________________________________ -# chdir - -def make_chdir_impl(traits): - from rpython.rlib import rwin32 - from rpython.rlib.rwin32file import make_win32_traits - - win32traits = make_win32_traits(traits) - - if traits.str is unicode: - def isUNC(path): - return path[0] == u'\\' or path[0] == u'/' - def magic_envvar(path): - return u'=' + path[0] + u':' - else: - def isUNC(path): - return path[0] == '\\' or path[0] == '/' - def magic_envvar(path): - return '=' + path[0] + ':' - - @func_renamer('chdir_llimpl_%s' % traits.str.__name__) - def chdir_llimpl(path): - """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, - it also needs to set "magic" environment variables indicating - the per-drive current directory, which are of the form =<drive>: - """ - if not win32traits.SetCurrentDirectory(path): - raise rwin32.lastWindowsError() - MAX_PATH = rwin32.MAX_PATH - assert MAX_PATH > 0 - - with traits.scoped_alloc_buffer(MAX_PATH) as path: - res = win32traits.GetCurrentDirectory(MAX_PATH + 1, path.raw) - if not res: - raise rwin32.lastWindowsError() - res = rffi.cast(lltype.Signed, res) - assert res > 0 - if res <= MAX_PATH + 1: - new_path = path.str(res) - else: - with traits.scoped_alloc_buffer(res) as path: - res = win32traits.GetCurrentDirectory(res, path.raw) - if not res: - raise rwin32.lastWindowsError() - res = rffi.cast(lltype.Signed, res) - assert res > 0 - new_path = path.str(res) - if isUNC(new_path): - return - if not win32traits.SetEnvironmentVariable(magic_envvar(new_path), new_path): - raise rwin32.lastWindowsError() - - return chdir_llimpl - -#_______________________________________________________________ -# chmod - -def make_chmod_impl(traits): - from rpython.rlib import rwin32 - from rpython.rlib.rwin32file import make_win32_traits - - win32traits = make_win32_traits(traits) - - @func_renamer('chmod_llimpl_%s' % traits.str.__name__) - def chmod_llimpl(path, mode): - attr = win32traits.GetFileAttributes(path) - if attr == win32traits.INVALID_FILE_ATTRIBUTES: - raise rwin32.lastWindowsError() - if mode & 0200: # _S_IWRITE - attr &= ~win32traits.FILE_ATTRIBUTE_READONLY - else: - attr |= win32traits.FILE_ATTRIBUTE_READONLY - if not win32traits.SetFileAttributes(path, attr): - raise rwin32.lastWindowsError() - - return chmod_llimpl - -#_______________________________________________________________ -# getfullpathname - -def make_getfullpathname_impl(traits): - from rpython.rlib import rwin32 - win32traits = make_win32_traits(traits) - - @func_renamer('getfullpathname_llimpl_%s' % traits.str.__name__) - def getfullpathname_llimpl(path): - nBufferLength = rwin32.MAX_PATH + 1 - lpBuffer = lltype.malloc(traits.CCHARP.TO, nBufferLength, flavor='raw') - try: - res = win32traits.GetFullPathName( - path, rffi.cast(rwin32.DWORD, nBufferLength), - lpBuffer, lltype.nullptr(win32traits.LPSTRP.TO)) - if res == 0: - raise rwin32.lastWindowsError("_getfullpathname failed") - result = traits.charp2str(lpBuffer) - return result - finally: - lltype.free(lpBuffer, flavor='raw') - - return getfullpathname_llimpl _______________________________________________ pypy-commit mailing list pypy-commit@python.org https://mail.python.org/mailman/listinfo/pypy-commit