From df99c4e7e7d9d7b234802372cf3c1ad89ab8fc74 Mon Sep 17 00:00:00 2001 From: milde Date: Mon, 25 Jun 2012 14:56:51 +0000 Subject: [PATCH] io.FileInput/io.FileOutput: no system-exit on IOError. The `handle_io_errors` option is ignored and will be removed in a future release. git-svn-id: https://docutils.svn.sourceforge.net/svnroot/docutils/trunk@7466 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/HISTORY.txt | 5 +++++ docutils/RELEASE-NOTES.txt | 19 ++++++---------- docutils/docutils/core.py | 6 +----- docutils/docutils/io.py | 23 +++++--------------- docutils/docutils/parsers/rst/directives/misc.py | 25 ++++++++++------------ docutils/docutils/parsers/rst/directives/tables.py | 9 ++++---- docutils/docutils/writers/html4css1/__init__.py | 3 +-- docutils/docutils/writers/latex2e/__init__.py | 3 +-- 8 files changed, 34 insertions(+), 59 deletions(-) diff --git a/docutils/HISTORY.txt b/docutils/HISTORY.txt index 4e9890ad9..a82a8fb84 100644 --- a/docutils/HISTORY.txt +++ b/docutils/HISTORY.txt @@ -22,6 +22,11 @@ Changes Since 0.9.1 - ``docutils/math``, ``docutils/error_reporting.py``, and ``docutils/urischemes.py`` moved to the utils package. +* docutils/io.py + + - FileInput/FileOutput: no system-exit on IOError. The `handle_io_errors` + option is ignored and will be removed in a future release. + Release 0.9.1 (2012-06-17) ========================== diff --git a/docutils/RELEASE-NOTES.txt b/docutils/RELEASE-NOTES.txt index 5dce2bbab..30a056f8c 100644 --- a/docutils/RELEASE-NOTES.txt +++ b/docutils/RELEASE-NOTES.txt @@ -19,19 +19,7 @@ more detailed list of changes, please see the `Docutils History`_. Future changes ============== -* docutils.io.FileInput/FileOutput will no longer do a - system-exit on IOError by default. - - Roadmap: - - :0.10: change of default behaviour to the equivalent of - ``handle_io_errors=False``, - ignore and deprecate the `handle_io_errors` option. - (allows us to clean up Docutils code and remove the error handling - code from the FileInput/FileOutput classes) - :0.10 + n: deprecation warning to stderr if FileInput/FileOutput - is called with `handle_io_errors`, - :0.10 + n+1: remove the `handle_io_errors` option. + :0.10 + n: remove the `handle_io_errors` option from io.FileInput/Output. Changes Since 0.9.1 @@ -55,6 +43,11 @@ Changes Since 0.9.1 except ImportError: import docutils.utils.math as math +* docutils/io.py + + - FileInput/FileOutput: no system-exit on IOError. + The `handle_io_errors` option is ignored. + .. _Python 3 compatibility: README.html#python-3-compatibility diff --git a/docutils/docutils/core.py b/docutils/docutils/core.py index 03fd4e549..7ba37706b 100644 --- a/docutils/docutils/core.py +++ b/docutils/docutils/core.py @@ -176,8 +176,7 @@ class Publisher: try: self.source = self.source_class( source=source, source_path=source_path, - encoding=self.settings.input_encoding, - handle_io_errors=False) + encoding=self.settings.input_encoding) except TypeError: self.source = self.source_class( source=source, source_path=source_path, @@ -192,9 +191,6 @@ class Publisher: destination=destination, destination_path=destination_path, encoding=self.settings.output_encoding, error_handler=self.settings.output_encoding_error_handler) - # Raise IOError instead of system exit with `tracback == True` - # TODO: change io.FileInput's default behaviour and remove this hack - self.destination.handle_io_errors=False def apply_transforms(self): self.document.transformer.populate_from_components( diff --git a/docutils/docutils/io.py b/docutils/docutils/io.py index ff7a5b428..60a398708 100644 --- a/docutils/docutils/io.py +++ b/docutils/docutils/io.py @@ -4,7 +4,7 @@ """ I/O classes provide a uniform API for low-level input and output. Subclasses -will exist for a variety of input/output mechanisms. +exist for a variety of input/output mechanisms. """ __docformat__ = 'reStructuredText' @@ -204,7 +204,7 @@ class FileInput(Input): """ def __init__(self, source=None, source_path=None, encoding=None, error_handler='strict', - autoclose=True, handle_io_errors=True, mode='rU'): + autoclose=True, handle_io_errors=None, mode='rU'): """ :Parameters: - `source`: either a file-like object (which is read directly), or @@ -214,14 +214,13 @@ class FileInput(Input): - `error_handler`: the encoding error handler to use. - `autoclose`: close automatically after read (except when `sys.stdin` is the source). - - `handle_io_errors`: summarize I/O errors here, and exit? + - `handle_io_errors`: ignored, deprecated, will be removed. - `mode`: how the file is to be opened (see standard function `open`). The default 'rU' provides universal newline support for text files. """ Input.__init__(self, source, source_path, encoding, error_handler) self.autoclose = autoclose - self.handle_io_errors = handle_io_errors self._stderr = ErrorOutput() if source is None: @@ -236,12 +235,6 @@ class FileInput(Input): try: self.source = open(source_path, mode, **kwargs) except IOError, error: - if handle_io_errors: - print >>self._stderr, ErrorString(error) - print >>self._stderr, ( - u'Unable to open source file for reading ("%s").' - u'Exiting.' % source_path) - sys.exit(1) raise InputError(error.errno, error.strerror, source_path) else: self.source = sys.stdin @@ -310,7 +303,7 @@ class FileOutput(Output): def __init__(self, destination=None, destination_path=None, encoding=None, error_handler='strict', autoclose=True, - handle_io_errors=True, mode=None): + handle_io_errors=None, mode=None): """ :Parameters: - `destination`: either a file-like object (which is written @@ -322,7 +315,7 @@ class FileOutput(Output): - `error_handler`: the encoding error handler to use. - `autoclose`: close automatically after write (except when `sys.stdout` or `sys.stderr` is the destination). - - `handle_io_errors`: summarize I/O errors here, and exit? + - `handle_io_errors`: ignored, deprecated, will be removed. - `mode`: how the file is to be opened (see standard function `open`). The default is 'w', providing universal newline support for text files. @@ -331,7 +324,6 @@ class FileOutput(Output): encoding, error_handler) self.opened = True self.autoclose = autoclose - self.handle_io_errors = handle_io_errors if mode is not None: self.mode = mode self._stderr = ErrorOutput() @@ -377,11 +369,6 @@ class FileOutput(Output): try: self.destination = open(self.destination_path, self.mode, **kwargs) except IOError, error: - if self.handle_io_errors: - print >>self._stderr, ErrorString(error) - print >>self._stderr, (u'Unable to open destination file' - u" for writing ('%s'). Exiting." % self.destination_path) - sys.exit(1) raise OutputError(error.errno, error.strerror, self.destination_path) self.opened = True diff --git a/docutils/docutils/parsers/rst/directives/misc.py b/docutils/docutils/parsers/rst/directives/misc.py index 5c87edb6e..6edaf6fa9 100644 --- a/docutils/docutils/parsers/rst/directives/misc.py +++ b/docutils/docutils/parsers/rst/directives/misc.py @@ -64,15 +64,14 @@ class Include(Directive): path = nodes.reprunicode(path) encoding = self.options.get( 'encoding', self.state.document.settings.input_encoding) + e_handler=self.state.document.settings.input_encoding_error_handler tab_width = self.options.get( 'tab-width', self.state.document.settings.tab_width) try: self.state.document.settings.record_dependencies.add(path) - include_file = io.FileInput( - source_path=path, encoding=encoding, - error_handler=(self.state.document.settings.\ - input_encoding_error_handler), - handle_io_errors=None) + include_file = io.FileInput(source_path=path, + encoding=encoding, + error_handler=e_handler) except UnicodeEncodeError, error: raise self.severe(u'Problems with "%s" directive path:\n' 'Cannot encode input file path "%s" ' @@ -186,6 +185,7 @@ class Raw(Directive): attributes = {'format': ' '.join(self.arguments[0].lower().split())} encoding = self.options.get( 'encoding', self.state.document.settings.input_encoding) + e_handler=self.state.document.settings.input_encoding_error_handler if self.content: if 'file' in self.options or 'url' in self.options: raise self.error( @@ -203,11 +203,9 @@ class Raw(Directive): self.options['file'])) path = utils.relative_path(None, path) try: - raw_file = io.FileInput( - source_path=path, encoding=encoding, - error_handler=(self.state.document.settings.\ - input_encoding_error_handler), - handle_io_errors=None) + raw_file = io.FileInput(source_path=path, + encoding=encoding, + error_handler=e_handler) # TODO: currently, raw input files are recorded as # dependencies even if not used for the chosen output format. self.state.document.settings.record_dependencies.add(path) @@ -231,10 +229,9 @@ class Raw(Directive): except (urllib2.URLError, IOError, OSError), error: raise self.severe(u'Problems with "%s" directive URL "%s":\n%s.' % (self.name, self.options['url'], ErrorString(error))) - raw_file = io.StringInput( - source=raw_text, source_path=source, encoding=encoding, - error_handler=(self.state.document.settings.\ - input_encoding_error_handler)) + raw_file = io.StringInput(source=raw_text, source_path=source, + encoding=encoding, + error_handler=e_handler) try: text = raw_file.read() except UnicodeError, error: diff --git a/docutils/docutils/parsers/rst/directives/tables.py b/docutils/docutils/parsers/rst/directives/tables.py index 9ae2365c5..0758f82e5 100644 --- a/docutils/docutils/parsers/rst/directives/tables.py +++ b/docutils/docutils/parsers/rst/directives/tables.py @@ -244,6 +244,7 @@ class CSVTable(Table): """ encoding = self.options.get( 'encoding', self.state.document.settings.input_encoding) + error_handler = self.state.document.settings.input_encoding_error_handler if self.content: # CSV data is from directive content. if 'file' in self.options or 'url' in self.options: @@ -270,11 +271,9 @@ class CSVTable(Table): source = utils.relative_path(None, source) try: self.state.document.settings.record_dependencies.add(source) - csv_file = io.FileInput( - source_path=source, encoding=encoding, - error_handler=(self.state.document.settings.\ - input_encoding_error_handler), - handle_io_errors=None) + csv_file = io.FileInput(source_path=source, + encoding=encoding, + error_handler=error_handler) csv_data = csv_file.read().splitlines() except IOError, error: severe = self.state_machine.reporter.severe( diff --git a/docutils/docutils/writers/html4css1/__init__.py b/docutils/docutils/writers/html4css1/__init__.py index 6d6c17cc8..2f71d0451 100644 --- a/docutils/docutils/writers/html4css1/__init__.py +++ b/docutils/docutils/writers/html4css1/__init__.py @@ -373,8 +373,7 @@ class HTMLTranslator(nodes.NodeVisitor): if self.settings.embed_stylesheet: try: content = io.FileInput(source_path=path, - encoding='utf-8', - handle_io_errors=False).read() + encoding='utf-8').read() self.settings.record_dependencies.add(path) except IOError, err: msg = u"Cannot embed stylesheet '%s': %s." % ( diff --git a/docutils/docutils/writers/latex2e/__init__.py b/docutils/docutils/writers/latex2e/__init__.py index a6acf7335..bb8982510 100644 --- a/docutils/docutils/writers/latex2e/__init__.py +++ b/docutils/docutils/writers/latex2e/__init__.py @@ -1333,8 +1333,7 @@ class LaTeXTranslator(nodes.NodeVisitor): path = base + '.sty' # ensure extension try: content = io.FileInput(source_path=path, - encoding='utf-8', - handle_io_errors=False).read() + encoding='utf-8').read() self.settings.record_dependencies.add(path) except IOError, err: msg = u"Cannot embed stylesheet '%s':\n %s." % ( -- 2.11.4.GIT