audk/BaseTools/Source/Python/UPT/Parser/InfDefineSectionParser.py
Mike Beaton 6820004b3e BaseTools: Fix multiple 'invalid escape sequence' warnings in tests
In Python 3.12 invalid escape sequences in strings moved from
DeprecationWarning to SyntaxWarning
(ref https://docs.python.org/3/whatsnew/changelog.html#python-3-12-0-final
and search for gh-98401). In a future Python version this will become
SyntaxError.

Multiple instances of these SyntaxWarnings are currently printed when
running the BaseTools tests using Python 3.12 (though without actually
failing the affected tests).

This commit updates all lines which were causing this type of warning.

Typical examples which needed fixing are:

- "BaseTools\Source\Python" representing a path: "\S" and "\P" are invalid
escape sequences, therefore left unchanged, therefore the test works
(with a warning in Python 3.12). r"BaseTools\Source\Python" represents
the same string, but with escapes turned off completely thus no warning.

- Where '\t\s' is used as a regex pattern, then chr(9) + '\\s' is sent
to the regex parser (with a warning in Python 3.12) since '\s' is not a
valid Python escape sequence. This works correctly, though arguably for
the wrong reasons. r'\t\s' sends the same as '\\t\\s', as originally
intended and with no warning.

(Note that ' and " are not fundamentally different in Python.)

Signed-off-by: Mike Beaton <mjsbeaton@gmail.com>
2024-09-23 04:55:53 +00:00

192 lines
6.5 KiB
Python

## @file
# This file contained the parser for define sections in INF file
#
# Copyright (c) 2011 - 2018, Intel Corporation. All rights reserved.<BR>
#
# SPDX-License-Identifier: BSD-2-Clause-Patent
#
'''
InfDefineSectionParser
'''
##
# Import Modules
#
import re
from Library import DataType as DT
from Library import GlobalData
from Library.Parsing import MacroParser
from Library.Misc import GetSplitValueList
from Library.ParserValidate import IsValidArch
from Object.Parser.InfCommonObject import InfLineCommentObject
from Object.Parser.InfDefineObject import InfDefMember
from Parser.InfParserMisc import InfExpandMacro
from Object.Parser.InfMisc import ErrorInInf
from Logger import StringTable as ST
from Parser.InfParserMisc import InfParserSectionRoot
## __GetValidateArchList
#
#
def GetValidateArchList(LineContent):
TempArch = ''
ArchList = []
ValidateAcrhPatten = re.compile(r"^\s*#\s*VALID_ARCHITECTURES\s*=\s*.*$", re.DOTALL)
if ValidateAcrhPatten.match(LineContent):
TempArch = GetSplitValueList(LineContent, DT.TAB_EQUAL_SPLIT, 1)[1]
TempArch = GetSplitValueList(TempArch, '(', 1)[0]
ArchList = re.split(r'\s+', TempArch)
NewArchList = []
for Arch in ArchList:
if IsValidArch(Arch):
NewArchList.append(Arch)
ArchList = NewArchList
return ArchList
class InfDefinSectionParser(InfParserSectionRoot):
def InfDefineParser(self, SectionString, InfSectionObject, FileName, SectionComment):
if SectionComment:
pass
#
# Parser Defines section content and fill self._ContentList dict.
#
StillCommentFalg = False
HeaderComments = []
SectionContent = ''
ArchList = []
_ContentList = []
_ValueList = []
#
# Add WORKSPACE to global Marco dict.
#
self.FileLocalMacros['WORKSPACE'] = GlobalData.gWORKSPACE
for Line in SectionString:
LineContent = Line[0]
LineNo = Line[1]
TailComments = ''
LineComment = None
LineInfo = ['', -1, '']
LineInfo[0] = FileName
LineInfo[1] = LineNo
LineInfo[2] = LineContent
if LineContent.strip() == '':
continue
#
# The first time encountered VALIDATE_ARCHITECHERS will be considered as support arch list.
#
if not ArchList:
ArchList = GetValidateArchList(LineContent)
#
# Parser Comment
#
if LineContent.strip().startswith(DT.TAB_COMMENT_SPLIT):
#
# Last line is comments, and this line go on.
#
if StillCommentFalg:
HeaderComments.append(Line)
SectionContent += LineContent + DT.END_OF_LINE
continue
#
# First time encounter comment
#
else:
#
# Clear original data
#
HeaderComments = []
HeaderComments.append(Line)
StillCommentFalg = True
SectionContent += LineContent + DT.END_OF_LINE
continue
else:
StillCommentFalg = False
if len(HeaderComments) >= 1:
LineComment = InfLineCommentObject()
LineCommentContent = ''
for Item in HeaderComments:
LineCommentContent += Item[0] + DT.END_OF_LINE
LineComment.SetHeaderComments(LineCommentContent)
#
# Find Tail comment.
#
if LineContent.find(DT.TAB_COMMENT_SPLIT) > -1:
TailComments = LineContent[LineContent.find(DT.TAB_COMMENT_SPLIT):]
LineContent = LineContent[:LineContent.find(DT.TAB_COMMENT_SPLIT)]
if LineComment is None:
LineComment = InfLineCommentObject()
LineComment.SetTailComments(TailComments)
#
# Find Macro
#
Name, Value = MacroParser((LineContent, LineNo),
FileName,
DT.MODEL_META_DATA_HEADER,
self.FileLocalMacros)
if Name is not None:
self.FileLocalMacros[Name] = Value
continue
#
# Replace with [Defines] section Macro
#
LineContent = InfExpandMacro(LineContent,
(FileName, LineContent, LineNo),
self.FileLocalMacros,
None, True)
SectionContent += LineContent + DT.END_OF_LINE
TokenList = GetSplitValueList(LineContent, DT.TAB_EQUAL_SPLIT, 1)
if len(TokenList) < 2:
ErrorInInf(ST.ERR_INF_PARSER_DEFINE_ITEM_NO_VALUE,
LineInfo=LineInfo)
_ValueList[0:len(TokenList)] = TokenList
if not _ValueList[0]:
ErrorInInf(ST.ERR_INF_PARSER_DEFINE_ITEM_NO_NAME,
LineInfo=LineInfo)
if not _ValueList[1]:
ErrorInInf(ST.ERR_INF_PARSER_DEFINE_ITEM_NO_VALUE,
LineInfo=LineInfo)
Name, Value = _ValueList[0], _ValueList[1]
InfDefMemberObj = InfDefMember(Name, Value)
if (LineComment is not None):
InfDefMemberObj.Comments.SetHeaderComments(LineComment.GetHeaderComments())
InfDefMemberObj.Comments.SetTailComments(LineComment.GetTailComments())
InfDefMemberObj.CurrentLine.SetFileName(self.FullPath)
InfDefMemberObj.CurrentLine.SetLineString(LineContent)
InfDefMemberObj.CurrentLine.SetLineNo(LineNo)
_ContentList.append(InfDefMemberObj)
HeaderComments = []
TailComments = ''
#
# Current Define section archs
#
if not ArchList:
ArchList = ['COMMON']
InfSectionObject.SetAllContent(SectionContent)
InfSectionObject.SetDefines(_ContentList, Arch=ArchList)