Author: andrewjlawrence Branch: winconsoleio Changeset: r96779:a89938555d38 Date: 2019-06-08 23:12 +0100 http://bitbucket.org/pypy/pypy/changeset/a89938555d38/
Log: Implemented a couple more methods. diff --git a/pypy/module/_io/interp_io.py b/pypy/module/_io/interp_io.py --- a/pypy/module/_io/interp_io.py +++ b/pypy/module/_io/interp_io.py @@ -1,4 +1,5 @@ import os +import sys from pypy.interpreter.error import OperationError, oefmt from pypy.interpreter.gateway import interp2app, unwrap_spec @@ -8,6 +9,7 @@ from pypy.module._io.interp_textio import W_TextIOWrapper from pypy.module.posix import interp_posix +_WIN32 = sys.platform == 'win32' class Cache: def __init__(self, space): @@ -90,8 +92,15 @@ w_result = None try: + rawclass = W_FileIO + if _WIN32: + from pypy.module._io.interp_win32consoleio import W_WinConsoleIO, _pyio_get_console_type + if _pyio_get_console_type(space, w_file) != '\0': + rawclass = W_WinConsoleIO + encoding = "utf-8" + w_raw = space.call_function( - space.gettypefor(W_FileIO), w_file, space.newtext(rawmode), + space.gettypefor(rawclass), w_file, space.newtext(rawmode), space.newbool(bool(closefd)), w_opener) w_result = w_raw diff --git a/pypy/module/_io/interp_win32consoleio.py b/pypy/module/_io/interp_win32consoleio.py --- a/pypy/module/_io/interp_win32consoleio.py +++ b/pypy/module/_io/interp_win32consoleio.py @@ -1,8 +1,9 @@ import sys import os -from pypy.interpreter.error import oefmt -from pypy.interpreter.typedef import TypeDef +from pypy.interpreter.error import OperationError, oefmt +from pypy.interpreter.typedef import ( + TypeDef, generic_new_descr, GetSetProperty) from pypy.interpreter.gateway import WrappedDefault, interp2app, unwrap_spec from pypy.module._io.interp_iobase import (W_RawIOBase, DEFAULT_BUFFER_SIZE) from pypy.interpreter.unicodehelper import fsdecode @@ -219,9 +220,33 @@ lltype.free(self.buf, flavor='raw') return None + + def repr_w(self, space): + typename = space.type(self).name + try: + w_name = space.getattr(self, space.newtext("name")) + except OperationError as e: + if not e.match(space, space.w_Exception): + raise + return space.newtext("<%s>" % (typename,)) + else: + name_repr = space.text_w(space.repr(w_name)) + return space.newtext("<%s name=%s>" % (typename, name_repr)) + + def fileno_w(self, space): + if self.fd < 0 and self.handle != rwin32.INVALID_HANDLE_VALUE: + if self.writable: + self.fd = rwin32.open_osfhandle(self.handle, rwin32._O_WRONLY | rwin32._O_BINARY) + else: + self.fd = rwin32.open_osfhandle(self.handle, rwin32._O_RDONLY | rwin32._O_BINARY) + if self.fd < 0: + return err_mode("fileno") + + return space.newint(self.fd) W_WinConsoleIO.typedef = TypeDef( '_io._WinConsoleIO', W_WinConsoleIO.typedef, - #__new__ = interp2app(W_FileIO.descr_new.im_func), + __new__ = generic_new_descr(W_WinConsoleIO), __init__ = interp2app(W_WinConsoleIO.descr_init), + __repr__ = interp2app(W_WinConsoleIO.repr_w), ) diff --git a/rpython/rlib/rwin32.py b/rpython/rlib/rwin32.py --- a/rpython/rlib/rwin32.py +++ b/rpython/rlib/rwin32.py @@ -249,6 +249,13 @@ raise WindowsError(ERROR_INVALID_HANDLE, "Invalid file handle") return handle + _open_osfhandle = rffi.llexternal('_open_osfhandle', [rffi.INTP, rffi.INT], rffi.INT) + + def open_osfhandle(handle, flags): + fd = _open_osfhandle(handle, flags) + with FdValidator(fd): + return fd + def build_winerror_to_errno(): """Build a dictionary mapping windows error numbers to POSIX errno. The function returns the dict, and the default value for codes not diff --git a/rpython/rlib/rwin32file.py b/rpython/rlib/rwin32file.py --- a/rpython/rlib/rwin32file.py +++ b/rpython/rlib/rwin32file.py @@ -14,7 +14,7 @@ class CConfigGlobal: _compilation_info_ = ExternalCompilationInfo( - includes = ['windows.h', 'winbase.h', 'sys/stat.h'], + includes = ['windows.h', 'winbase.h', 'sys/stat.h', 'fcntl.h'], ) ERROR_FILE_NOT_FOUND = platform.ConstantInteger( 'ERROR_FILE_NOT_FOUND') @@ -39,6 +39,13 @@ _S_IFREG = platform.ConstantInteger('_S_IFREG') _S_IFCHR = platform.ConstantInteger('_S_IFCHR') _S_IFIFO = platform.ConstantInteger('_S_IFIFO') + _O_APPEND = platform.ConstantInteger('_O_APPEND') + _O_CREAT = platform.ConstantInteger('_O_CREAT') + _O_EXCL = platform.ConstantInteger('_O_EXCL') + _O_RDONLY = platform.ConstantInteger('_O_RDONLY') + _O_RDWR = platform.ConstantInteger('_O_RDWR') + _O_TRUNC = platform.ConstantInteger('_O_TRUNC') + _O_WRONLY = platform.ConstantInteger('_O_WRONLY') FILE_TYPE_UNKNOWN = platform.ConstantInteger('FILE_TYPE_UNKNOWN') FILE_TYPE_CHAR = platform.ConstantInteger('FILE_TYPE_CHAR') FILE_TYPE_PIPE = platform.ConstantInteger('FILE_TYPE_PIPE') _______________________________________________ pypy-commit mailing list pypy-commit@python.org https://mail.python.org/mailman/listinfo/pypy-commit