Commit ee76c134 authored by Chris Mumford's avatar Chris Mumford Committed by Commit Bot

[sqlite] Converted extract_sqlite_api to python3.

Required changes:

1. Python 3 exceptions no longer have a message attribute so
   created a new ExtractError exception.
2. Switched from the imp to the importlib module.

Bug: none
Change-Id: I5c8b08dca1534e235309464cf5e054578b9ed2f8
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2000981
Commit-Queue: Victor Costan <pwnall@chromium.org>
Reviewed-by: default avatarDarwin Huang <huangdarwin@chromium.org>
Reviewed-by: default avatarVictor Costan <pwnall@chromium.org>
Cr-Commit-Position: refs/heads/master@{#731718}
parent 5450f5c0
......@@ -13,6 +13,6 @@ def CheckChangeOnUpload(input_api, output_api):
results += input_api.RunTests(
input_api.canned_checks.GetUnitTests(input_api, output_api, [
'scripts/extract_sqlite_api_unittest.py'
]))
], env=None, run_on_python2=False, run_on_python3=True))
return results
#!/usr/bin/env python
#!/usr/bin/env python3
#
# Copyright 2018 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
......@@ -18,6 +18,10 @@ For example, the following renaming macro is produced for sqlite3_initialize().
import re
import sys
class ExtractError(ValueError):
def __init__(self, message):
self.message = message
def ExtractLineTuples(string):
'''Returns a list of lines, with start/end whitespace stripped.
......@@ -197,7 +201,7 @@ IDENTIFIER_RE = re.compile(r'^[a-zA-Z_0-9]+$')
def ExtractApiExport(macro_names, api_export_macro, statement):
'''Extracts the symbol name from a statement exporting a function.
Returns None if the statement does not export a symbol. Throws ValueError if
Returns None if the statement does not export a symbol. Throws ExtractError if
the parser cannot understand the statement.
'''
# See http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf, section 6.7
......@@ -237,7 +241,7 @@ def ExtractApiExport(macro_names, api_export_macro, statement):
seen_simple_type = False
for word in words:
if word in UNSUPPORTED_KEYWORDS:
raise ValueError("Unsupported keyword %s" % word)
raise ExtractError("Unsupported keyword %s" % word)
if word in QUALIFIER_KEYWORDS:
continue
......@@ -249,7 +253,7 @@ def ExtractApiExport(macro_names, api_export_macro, statement):
if word in COMPOSITE_TYPE_SPECIFIERS:
if seen_simple_type:
raise ValueError('Mixed simple (struct_name) and composite (int) types')
raise ExtractError('Mixed simple (struct_name) and composite (int) types')
seen_composite_type = True
continue
......@@ -260,16 +264,16 @@ def ExtractApiExport(macro_names, api_export_macro, statement):
if not seen_composite_type and not seen_simple_type:
seen_simple_type = True
if IDENTIFIER_RE.match(word) is None:
raise ValueError(
raise ExtractError(
"%s parsed as type name, which doesn't make sense" % word)
continue
if IDENTIFIER_RE.match(word) is None:
raise ValueError(
raise ExtractError(
"%s parsed as symbol name, which doesn't make sense" % word)
return word
raise ValueError('Failed to find symbol name')
raise ExtractError('Failed to find symbol name')
def ExportedSymbolLine(symbol_prefix, symbol, statement_tuple):
......@@ -311,7 +315,7 @@ def ProcessSource(api_export_macro, symbol_prefix, header_line, footer_line,
if symbol_name:
output_lines.append(
ExportedSymbolLine(symbol_prefix, symbol_name, statement_tuple))
except ValueError as exception:
except ExtractError as exception:
output_lines.append(ExportedExceptionLine(exception, statement_tuple))
output_lines.sort()
......
#!/usr/bin/env python
#!/usr/bin/env python3
# Copyright 2018 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
......@@ -8,7 +8,7 @@
These tests should be getting picked up by the PRESUBMIT.py in this directory.
"""
import imp
from importlib.machinery import SourceFileLoader
import os
import shutil
import sys
......@@ -20,7 +20,7 @@ class ExtractSqliteApiUnittest(unittest.TestCase):
self.test_root = tempfile.mkdtemp()
source_path = os.path.join(
os.path.dirname(os.path.realpath(__file__)), 'extract_sqlite_api.py')
self.extractor = imp.load_source('extract_api', source_path)
self.extractor = SourceFileLoader('extract_api', source_path).load_module()
def tearDown(self):
if self.test_root:
......@@ -198,17 +198,21 @@ class ExtractSqliteApiUnittest(unittest.TestCase):
set(['SQLITE_DEPRECATED']), 'SQLITE_API',
'NOT_SQLITE_API struct sqlite_type sqlite3_sleep(int ms)'))
with self.assertRaisesRegexp(ValueError, 'Mixed simple .* and composite'):
with self.assertRaisesRegex(self.extractor.ExtractError,
'Mixed simple .* and composite'):
self.extractor.ExtractApiExport(
set(), 'SQLITE_API', 'SQLITE_API void int sqlite3_sleep(int ms)')
with self.assertRaisesRegexp(ValueError, 'Unsupported keyword struct'):
with self.assertRaisesRegex(self.extractor.ExtractError,
'Unsupported keyword struct'):
self.extractor.ExtractApiExport(
set(), 'SQLITE_API',
'SQLITE_API struct sqlite_type sqlite3_sleep(int ms)')
with self.assertRaisesRegexp(ValueError, 'int\+\+ parsed as type name'):
with self.assertRaisesRegex(self.extractor.ExtractError,
'int\+\+ parsed as type name'):
self.extractor.ExtractApiExport(
set(), 'SQLITE_API', 'SQLITE_API int++ sqlite3_sleep(int ms)')
with self.assertRaisesRegexp(ValueError, 'sqlite3\+sleep parsed as symbol'):
with self.assertRaisesRegex(self.extractor.ExtractError,
'sqlite3\+sleep parsed as symbol'):
self.extractor.ExtractApiExport(
set(), 'SQLITE_API', 'SQLITE_API int sqlite3+sleep(int ms)')
......@@ -228,7 +232,7 @@ class ExtractSqliteApiUnittest(unittest.TestCase):
self.assertEqual(
'// TODO: Lines 42-44 -- Something went wrong',
self.extractor.ExportedExceptionLine(
ValueError('Something went wrong'),
self.extractor.ExtractError('Something went wrong'),
(42, 44, 'SQLITE_API int chrome_sqlite3_sleep(int ms)')))
def testProcessSource(self):
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment