From 6f4b452742bfcfa1fce817f205ceb0508f720b8b Mon Sep 17 00:00:00 2001 From: Nick Wellnhofer Date: Thu, 15 May 2025 23:43:32 +0200 Subject: [PATCH] parser: Stop using ctxt->linenumbers I think this was used to avoid setting the `line` member before it was added (20+ years ago). --- HTMLparser.c | 3 --- SAX2.c | 2 -- globals.c | 16 +++++++--------- include/libxml/parser.h | 2 +- parser.c | 3 --- parserInternals.c | 15 +++++---------- python/generator.py | 2 +- python/tests/thread2.py | 22 +++++++++++----------- xmlreader.c | 2 -- xmlschemas.c | 1 - 10 files changed, 25 insertions(+), 43 deletions(-) diff --git a/HTMLparser.c b/HTMLparser.c index 9a8e2c05..2bd7405f 100644 --- a/HTMLparser.c +++ b/HTMLparser.c @@ -4527,7 +4527,6 @@ htmlInitParserCtxt(htmlParserCtxtPtr ctxt, const htmlSAXHandler *sax, ctxt->myDoc = NULL; ctxt->wellFormed = 1; ctxt->replaceEntities = 0; - ctxt->linenumbers = xmlLineNumbersDefaultValue; ctxt->keepBlanks = xmlKeepBlanksDefaultValue; ctxt->html = INSERT_INITIAL; ctxt->vctxt.flags = XML_VCTXT_USE_PCTXT; @@ -5601,8 +5600,6 @@ htmlCtxtSetOptionsInternal(xmlParserCtxtPtr ctxt, int options, int keepMask) */ ctxt->dictNames = 0; - ctxt->linenumbers = 1; - /* * Allow XML_PARSE_NOENT which many users set on the HTML parser. */ diff --git a/SAX2.c b/SAX2.c index c4108035..62b195cd 100644 --- a/SAX2.c +++ b/SAX2.c @@ -903,7 +903,6 @@ xmlSAX2AppendChild(xmlParserCtxtPtr ctxt, xmlNodePtr node) { node->parent = parent; if ((node->type != XML_TEXT_NODE) && - (ctxt->linenumbers) && (ctxt->input != NULL)) { if ((unsigned) ctxt->input->line < (unsigned) USHRT_MAX) node->line = ctxt->input->line; @@ -2537,7 +2536,6 @@ xmlSAX2Text(xmlParserCtxtPtr ctxt, const xmlChar *ch, int len, if ((lastChild != NULL) && (type == XML_TEXT_NODE) && - (ctxt->linenumbers) && (ctxt->input != NULL)) { if ((unsigned) ctxt->input->line < (unsigned) USHRT_MAX) lastChild->line = ctxt->input->line; diff --git a/globals.c b/globals.c index ea682d91..2bad8c7f 100644 --- a/globals.c +++ b/globals.c @@ -214,7 +214,6 @@ static int xmlDoValidityCheckingDefaultValueThrDef = 0; static int xmlGetWarningsDefaultValueThrDef = 1; static int xmlLoadExtDtdDefaultValueThrDef = 0; static int xmlPedanticParserDefaultValueThrDef = 0; -static int xmlLineNumbersDefaultValueThrDef = 0; static int xmlKeepBlanksDefaultValueThrDef = 1; static int xmlSubstituteEntitiesDefaultValueThrDef = 0; @@ -554,7 +553,6 @@ xmlInitGlobalState(xmlGlobalStatePtr gs) { xmlDoValidityCheckingDefaultValueThrDef; gs->getWarningsDefaultValue = xmlGetWarningsDefaultValueThrDef; gs->keepBlanksDefaultValue = xmlKeepBlanksDefaultValueThrDef; - gs->lineNumbersDefaultValue = xmlLineNumbersDefaultValueThrDef; gs->loadExtDtdDefaultValue = xmlLoadExtDtdDefaultValueThrDef; gs->pedanticParserDefaultValue = xmlPedanticParserDefaultValueThrDef; gs->substituteEntitiesDefaultValue = @@ -900,13 +898,13 @@ int xmlThrDefKeepBlanksDefaultValue(int v) { return ret; } -int xmlThrDefLineNumbersDefaultValue(int v) { - int ret; - xmlMutexLock(&xmlThrDefMutex); - ret = xmlLineNumbersDefaultValueThrDef; - xmlLineNumbersDefaultValueThrDef = v; - xmlMutexUnlock(&xmlThrDefMutex); - return ret; +/** + * Set per-thread default value. + * + * @deprecated Has no effect. + */ +int xmlThrDefLineNumbersDefaultValue(int v ATTRIBUTE_UNUSED) { + return 1; } int xmlThrDefLoadExtDtdDefaultValue(int v) { diff --git a/include/libxml/parser.h b/include/libxml/parser.h index 21d4b991..fc7efcf6 100644 --- a/include/libxml/parser.h +++ b/include/libxml/parser.h @@ -436,7 +436,7 @@ struct _xmlParserCtxt { * should the external subset be loaded */ int loadsubset; - /* set line number in element content */ + /* unused */ int linenumbers XML_DEPRECATED_MEMBER; /** * @deprecated Use xmlCtxtGetCatalogs() and xmlCtxtSetCatalogs() diff --git a/parser.c b/parser.c index 7dd30546..5248d002 100644 --- a/parser.c +++ b/parser.c @@ -12449,7 +12449,6 @@ xmlCreateURLParserCtxt(const char *filename, int options) options |= XML_PARSE_UNZIP; xmlCtxtUseOptions(ctxt, options); - ctxt->linenumbers = 1; input = xmlLoadResource(ctxt, filename, NULL, XML_RESOURCE_MAIN_DOCUMENT); if (input == NULL) { @@ -13185,8 +13184,6 @@ xmlCtxtSetOptionsInternal(xmlParserCtxtPtr ctxt, int options, int keepMask) xmlDictSetLimit(ctxt->dict, 0); } - ctxt->linenumbers = 1; - return(options & ~allMask); } diff --git a/parserInternals.c b/parserInternals.c index 3dc097be..4838c2e7 100644 --- a/parserInternals.c +++ b/parserInternals.c @@ -2749,7 +2749,6 @@ xmlInitSAXParserCtxt(xmlParserCtxtPtr ctxt, const xmlSAXHandler *sax, if (ctxt->pedantic) { ctxt->options |= XML_PARSE_PEDANTIC; } - ctxt->linenumbers = xmlLineNumbersDefaultValue; ctxt->keepBlanks = xmlKeepBlanksDefaultValue; if (ctxt->keepBlanks == 0) { ctxt->sax->ignorableWhitespace = xmlSAX2IgnorableWhitespace; @@ -3353,21 +3352,17 @@ xmlPedanticParserDefault(int val) { } /** - * Set and return the previous value for enabling line numbers in elements - * contents. This may break on old application and is turned off by default. + * Has no effect. * - * @deprecated The modern options API always enables line numbers. + * @deprecated Line numbers are always enabled. * * @param val int 0 or 1 - * @returns the last value for 0 for no substitution, 1 for substitution. + * @returns 1 */ int -xmlLineNumbersDefault(int val) { - int old = xmlLineNumbersDefaultValue; - - xmlLineNumbersDefaultValue = val; - return(old); +xmlLineNumbersDefault(int val ATTRIBUTE_UNUSED) { + return(1); } /** diff --git a/python/generator.py b/python/generator.py index 112367b6..05464305 100755 --- a/python/generator.py +++ b/python/generator.py @@ -92,7 +92,7 @@ functions = { 'xmlParserSetLineNumbers': ( 'Switch on the generation of line number for elements nodes.', ['void', None, None], - [['ctxt', 'xmlParserCtxt *', 'the parser context'], ['linenumbers', 'int', '1 to save line numbers']], + [['ctxt', 'xmlParserCtxt *', 'the parser context'], ['linenumbers', 'int', 'unused']], 'python_accessor', None), 'xmlDebugMemory': ( 'Switch on the generation of line number for elements nodes. Also returns the number of bytes allocated and not freed by libxml2 since memory debugging was switched on.', diff --git a/python/tests/thread2.py b/python/tests/thread2.py index d7db2b94..2fef52c2 100755 --- a/python/tests/thread2.py +++ b/python/tests/thread2.py @@ -27,19 +27,19 @@ class ErrorHandler: self.errors.append(str) self.lock.release() -def getLineNumbersDefault(): - old = libxml2.lineNumbersDefault(0) - libxml2.lineNumbersDefault(old) +def getPedanticParserDefault(): + old = libxml2.pedanticParserDefault(0) + libxml2.pedanticParserDefault(old) return old -def test(expectedLineNumbersDefault): +def test(expectedPedanticParserDefault): time.sleep(1) global failed # check a per thread-global - if expectedLineNumbersDefault != getLineNumbersDefault(): + if expectedPedanticParserDefault != getPedanticParserDefault(): failed = 1 print("FAILED to obtain correct value for " \ - "lineNumbersDefault in thread %d" % get_ident()) + "pedanticParserDefault in thread %d" % get_ident()) # check the global error handler # (which is NOT per-thread in the python bindings) try: @@ -54,7 +54,7 @@ eh = ErrorHandler() libxml2.registerErrorHandler(eh.handler,"") # set on the main thread only -libxml2.lineNumbersDefault(1) +libxml2.pedanticParserDefault(1) test(1) ec = len(eh.errors) if ec == 0: @@ -63,7 +63,7 @@ if ec == 0: ts = [] for i in range(THREADS_COUNT): - # expect 0 for lineNumbersDefault because + # expect 0 for pedanticParserDefault because # the new value has been set on the main thread only ts.append(Thread(target=test,args=(0,))) for t in ts: @@ -75,11 +75,11 @@ if len(eh.errors) != ec+THREADS_COUNT*ec: print("FAILED: did not obtain the correct number of errors") sys.exit(1) -# set lineNumbersDefault for future new threads -libxml2.thrDefLineNumbersDefaultValue(1) +# set pedanticParserDefault for future new threads +libxml2.thrDefPedanticParserDefaultValue(1) ts = [] for i in range(THREADS_COUNT): - # expect 1 for lineNumbersDefault + # expect 1 for pedanticParserDefault ts.append(Thread(target=test,args=(1,))) for t in ts: t.start() diff --git a/xmlreader.c b/xmlreader.c index 7faa68ff..d90bf5b6 100644 --- a/xmlreader.c +++ b/xmlreader.c @@ -2060,7 +2060,6 @@ xmlNewTextReader(xmlParserInputBuffer *input, const char *URI) { } ret->ctxt->parseMode = XML_PARSE_READER; ret->ctxt->_private = ret; - ret->ctxt->linenumbers = 1; ret->ctxt->dictNames = 1; ret->allocs = XML_TEXTREADER_CTXT; /* @@ -4838,7 +4837,6 @@ xmlTextReaderSetup(xmlTextReader *reader, reader->dict = reader->ctxt->dict; } reader->ctxt->_private = reader; - reader->ctxt->linenumbers = 1; reader->ctxt->dictNames = 1; /* * use the parser dictionary to allocate all elements and attributes names diff --git a/xmlschemas.c b/xmlschemas.c index c3a9b2d7..fca1834b 100644 --- a/xmlschemas.c +++ b/xmlschemas.c @@ -28328,7 +28328,6 @@ xmlSchemaValidateStreamInternal(xmlSchemaValidCtxtPtr ctxt, xmlSchemaSAXPlugPtr plug = NULL; int ret; - pctxt->linenumbers = 1; xmlSchemaValidateSetLocator(ctxt, xmlSchemaValidateStreamLocator, pctxt); ctxt->parserCtxt = pctxt;