|
From: <aa-...@us...> - 2022-11-03 16:11:44
|
Revision: 9208
http://sourceforge.net/p/docutils/code/9208
Author: aa-turner
Date: 2022-11-03 16:11:41 +0000 (Thu, 03 Nov 2022)
Log Message:
-----------
Use the built-in ``open`` instead of ``FileInput``
This simplifies logic to use the standard ``open`` function, and
simultaneously refactors usages to the context manager protocol
for more explicit resource management
Modified Paths:
--------------
trunk/docutils/docutils/parsers/rst/directives/misc.py
trunk/docutils/docutils/parsers/rst/directives/tables.py
trunk/docutils/docutils/writers/_html_base.py
trunk/docutils/docutils/writers/latex2e/__init__.py
trunk/docutils/test/test_dependencies.py
Modified: trunk/docutils/docutils/parsers/rst/directives/misc.py
===================================================================
--- trunk/docutils/docutils/parsers/rst/directives/misc.py 2022-11-03 11:15:40 UTC (rev 9207)
+++ trunk/docutils/docutils/parsers/rst/directives/misc.py 2022-11-03 16:11:41 UTC (rev 9208)
@@ -70,17 +70,21 @@
tab_width = self.options.get(
'tab-width', self.state.document.settings.tab_width)
try:
- include_file = io.FileInput(source_path=path,
- encoding=encoding,
- error_handler=e_handler)
+ with open(path,
+ encoding=encoding,
+ errors=e_handler) as include_file:
+ rawtext = include_file.read()
except UnicodeEncodeError:
- raise self.severe('Problems with "%s" directive path:\n'
- 'Cannot encode input file path "%s" '
- '(wrong locale?).' %
- (self.name, path))
+ raise self.severe(f'Problems with "{self.name}" directive path:\n'
+ f'Cannot encode input file path "{path}" '
+ f'(wrong locale?).')
except OSError as error:
- raise self.severe('Problems with "%s" directive path:\n%s.' %
- (self.name, io.error_string(error)))
+ error = io.InputError(error.errno, error.strerror, path)
+ raise self.severe(f'Problems with "{self.name}" directive '
+ f'path:\n{io.error_string(error)}.')
+ except UnicodeError as error:
+ raise self.severe(f'Problem with "{self.name}" directive:\n'
+ + io.error_string(error))
else:
self.state.document.settings.record_dependencies.add(path)
@@ -87,15 +91,9 @@
# Get to-be-included content
startline = self.options.get('start-line', None)
endline = self.options.get('end-line', None)
- try:
- if startline or (endline is not None):
- lines = include_file.readlines()
- rawtext = ''.join(lines[startline:endline])
- else:
- rawtext = include_file.read()
- except UnicodeError as error:
- raise self.severe('Problem with "%s" directive:\n%s' %
- (self.name, io.error_string(error)))
+ if startline or (endline is not None):
+ lines = rawtext.splitlines(keepends=True)
+ rawtext = ''.join(lines[startline:endline])
# start-after/end-before: no restrictions on newlines in match-text,
# and no restrictions on matching inside lines vs. line boundaries
after_text = self.options.get('start-after', None)
@@ -250,21 +248,21 @@
self.options['file']))
path = utils.relative_path(None, path)
try:
- raw_file = io.FileInput(source_path=path,
- encoding=encoding,
- error_handler=e_handler)
+ with open(path,
+ encoding=encoding,
+ errors=e_handler) as raw_file:
+ text = raw_file.read()
except OSError as error:
- raise self.severe('Problems with "%s" directive path:\n%s.'
- % (self.name, io.error_string(error)))
+ error = io.InputError(error.errno, error.strerror, path)
+ raise self.severe(f'Problems with "{self.name}" directive '
+ f'path:\n{io.error_string(error)}.')
+ except UnicodeError as error:
+ raise self.severe(f'Problem with "{self.name}" directive:\n'
+ + io.error_string(error))
else:
# 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)
- try:
- text = raw_file.read()
- except UnicodeError as error:
- raise self.severe('Problem with "%s" directive:\n%s'
- % (self.name, io.error_string(error)))
attributes['source'] = path
elif 'url' in self.options:
source = self.options['url']
Modified: trunk/docutils/docutils/parsers/rst/directives/tables.py
===================================================================
--- trunk/docutils/docutils/parsers/rst/directives/tables.py 2022-11-03 11:15:40 UTC (rev 9207)
+++ trunk/docutils/docutils/parsers/rst/directives/tables.py 2022-11-03 16:11:41 UTC (rev 9208)
@@ -316,10 +316,10 @@
self.options['file']))
source = utils.relative_path(None, source)
try:
- csv_file = io.FileInput(source_path=source,
- encoding=encoding,
- error_handler=error_handler)
- csv_data = csv_file.read().splitlines()
+ with open(source,
+ encoding=encoding,
+ errors=error_handler) as csv_file:
+ csv_data = csv_file.read().splitlines()
except OSError as error:
severe = self.reporter.severe(
'Problems with "%s" directive path:\n%s.'
Modified: trunk/docutils/docutils/writers/_html_base.py
===================================================================
--- trunk/docutils/docutils/writers/_html_base.py 2022-11-03 11:15:40 UTC (rev 9207)
+++ trunk/docutils/docutils/writers/_html_base.py 2022-11-03 16:11:41 UTC (rev 9208)
@@ -393,8 +393,8 @@
adjust_path = bool(self.settings.stylesheet_path)
if self.settings.embed_stylesheet:
try:
- content = docutils.io.FileInput(source_path=path,
- encoding='utf-8').read()
+ with open(path, encoding='utf-8') as f:
+ content = f.read()
except OSError as err:
msg = f'Cannot embed stylesheet: {err}'
self.document.reporter.error(msg)
Modified: trunk/docutils/docutils/writers/latex2e/__init__.py
===================================================================
--- trunk/docutils/docutils/writers/latex2e/__init__.py 2022-11-03 11:15:40 UTC (rev 9207)
+++ trunk/docutils/docutils/writers/latex2e/__init__.py 2022-11-03 16:11:41 UTC (rev 9208)
@@ -23,7 +23,6 @@
except ImportError:
import docutils.utils.roman as roman
-import docutils
from docutils import frontend, nodes, languages, writers, utils
from docutils.transforms import writer_aux
from docutils.utils.math import pick_math_environment, unichar2tex
@@ -1404,8 +1403,8 @@
if is_package:
path = base + '.sty' # ensure extension
try:
- content = docutils.io.FileInput(source_path=path,
- encoding='utf-8').read()
+ with open(path, encoding='utf-8') as f:
+ content = f.read()
except OSError as err:
msg = f'Cannot embed stylesheet:\n {err}'
self.document.reporter.error(msg)
Modified: trunk/docutils/test/test_dependencies.py
===================================================================
--- trunk/docutils/test/test_dependencies.py 2022-11-03 11:15:40 UTC (rev 9207)
+++ trunk/docutils/test/test_dependencies.py 2022-11-03 16:11:41 UTC (rev 9208)
@@ -46,9 +46,8 @@
**settings)
recorder.close()
# Read the record file:
- record = docutils.io.FileInput(source_path=recordfile,
- encoding='utf-8')
- return record.read().splitlines()
+ with open(recordfile, encoding='utf-8') as record:
+ return record.read().splitlines()
def test_dependencies_xml(self):
# Note: currently, raw input files are read (and hence recorded) while
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|