2.12.2021, 9:00 - 11:00: Due to updates GitLab may be unavailable for some minutes between 09:00 and 11:00.

Commit a2f60b18 authored by eckhart's avatar eckhart
Browse files

- Replaced process-local global variables by thread-local variables

parent c7b7a76b
DHParser Version 0.8.4 (27.11.2018)
DHParser Version 0.8.4 (10.11.2018)
...................................
- lookahead parsers can now be unit-tested (experimental)
- customized (faster) deepcopy of syntax trees
- better customization of XML-serialization of syntaxtrees
- better customization of XML-serialization of syntax-trees
- tollkit.GLOBALS as container for globel (actually thread-local)
variables and parameters added
DHParser Version 0.8.3 (20.8.2018)
......
......@@ -101,7 +101,7 @@ from DHParser import logging, is_filename, load_if_file, MockParser, \\
remove_nodes, remove_content, remove_brackets, replace_parser, remove_anonymous_tokens, \\
keep_children, is_one_of, not_one_of, has_content, apply_if, remove_first, remove_last, \\
remove_anonymous_empty, keep_nodes, traverse_locally, strip, lstrip, rstrip, \\
replace_content, replace_content_by, error_on, recompile_grammar, THREAD_LOCAL
replace_content, replace_content_by, error_on, recompile_grammar, GLOBALS
'''.format(dhparserdir=dhparserdir)
......
......@@ -35,7 +35,7 @@ from DHParser.parse import Grammar, mixin_comment, Forward, RegExp, Whitespace,
from DHParser.preprocess import nil_preprocessor, PreprocessorFunc
from DHParser.syntaxtree import Node, RootNode, WHITESPACE_PTYPE, TOKEN_PTYPE
from DHParser.toolkit import load_if_file, escape_re, md5, sane_parser_name, re, expand_table, \
THREAD_LOCAL, typing
GLOBALS, typing
from DHParser.transform import TransformationFunc, traverse, remove_brackets, \
reduce_single_child, replace_by_single_child, remove_expendables, \
remove_tokens, flatten, forbid, assert_content, remove_infix_operator
......@@ -196,11 +196,11 @@ def grammar_changed(grammar_class, grammar_source: str) -> bool:
def get_ebnf_grammar() -> EBNFGrammar:
try:
grammar = THREAD_LOCAL.ebnf_grammar_singleton
grammar = GLOBALS.ebnf_grammar_singleton
return grammar
except AttributeError:
THREAD_LOCAL.ebnf_grammar_singleton = EBNFGrammar()
return THREAD_LOCAL.ebnf_grammar_singleton
GLOBALS.ebnf_grammar_singleton = EBNFGrammar()
return GLOBALS.ebnf_grammar_singleton
########################################################################
......@@ -249,10 +249,10 @@ def EBNFTransform() -> TransformationFunc:
def get_ebnf_transformer() -> TransformationFunc:
try:
transformer = THREAD_LOCAL.EBNF_transformer_singleton
transformer = GLOBALS.EBNF_transformer_singleton
except AttributeError:
THREAD_LOCAL.EBNF_transformer_singleton = EBNFTransform()
transformer = THREAD_LOCAL.EBNF_transformer_singleton
GLOBALS.EBNF_transformer_singleton = EBNFTransform()
transformer = GLOBALS.EBNF_transformer_singleton
return transformer
......@@ -277,10 +277,10 @@ def get_preprocessor() -> PreprocessorFunc:
GRAMMAR_FACTORY = '''
def get_grammar() -> {NAME}Grammar:
try:
grammar = THREAD_LOCAL.{NAME}_{ID}_grammar_singleton
grammar = GLOBALS.{NAME}_{ID}_grammar_singleton
except AttributeError:
THREAD_LOCAL.{NAME}_{ID}_grammar_singleton = {NAME}Grammar()
grammar = THREAD_LOCAL.{NAME}_{ID}_grammar_singleton
GLOBALS.{NAME}_{ID}_grammar_singleton = {NAME}Grammar()
grammar = GLOBALS.{NAME}_{ID}_grammar_singleton
return grammar
'''
......@@ -291,10 +291,10 @@ def {NAME}Transform() -> TransformationDict:
def get_transformer() -> TransformationFunc:
try:
transformer = THREAD_LOCAL.{NAME}_{ID}_transformer_singleton
transformer = GLOBALS.{NAME}_{ID}_transformer_singleton
except AttributeError:
THREAD_LOCAL.{NAME}_{ID}_transformer_singleton = {NAME}Transform()
transformer = THREAD_LOCAL.{NAME}_{ID}_transformer_singleton
GLOBALS.{NAME}_{ID}_transformer_singleton = {NAME}Transform()
transformer = GLOBALS.{NAME}_{ID}_transformer_singleton
return transformer
'''
......@@ -302,12 +302,12 @@ def get_transformer() -> TransformationFunc:
COMPILER_FACTORY = '''
def get_compiler(grammar_name="{NAME}", grammar_source="") -> {NAME}Compiler:
try:
compiler = THREAD_LOCAL.{NAME}_{ID}_compiler_singleton
compiler = GLOBALS.{NAME}_{ID}_compiler_singleton
compiler.set_grammar_name(grammar_name, grammar_source)
except AttributeError:
THREAD_LOCAL.{NAME}_{ID}_compiler_singleton = \\
GLOBALS.{NAME}_{ID}_compiler_singleton = \\
{NAME}Compiler(grammar_name, grammar_source)
compiler = THREAD_LOCAL.{NAME}_{ID}_compiler_singleton
compiler = GLOBALS.{NAME}_{ID}_compiler_singleton
return compiler
'''
......@@ -988,9 +988,9 @@ class EBNFCompiler(Compiler):
def get_ebnf_compiler(grammar_name="", grammar_source="") -> EBNFCompiler:
try:
compiler = THREAD_LOCAL.ebnf_compiler_singleton
compiler = GLOBALS.ebnf_compiler_singleton
compiler.set_grammar_name(grammar_name, grammar_source)
return compiler
except AttributeError:
THREAD_LOCAL.ebnf_compiler_singleton = EBNFCompiler(grammar_name, grammar_source)
return THREAD_LOCAL.ebnf_compiler_singleton
GLOBALS.ebnf_compiler_singleton = EBNFCompiler(grammar_name, grammar_source)
return GLOBALS.ebnf_compiler_singleton
......@@ -57,7 +57,7 @@ import threading
from DHParser.error import line_col
from DHParser.stringview import StringView
from DHParser.syntaxtree import Node
from DHParser.toolkit import is_filename, escape_control_characters, THREAD_LOCAL, typing
from DHParser.toolkit import is_filename, escape_control_characters, GLOBALS, typing
from typing import List, Tuple, Union
__all__ = ('log_dir',
......@@ -90,7 +90,7 @@ def log_dir() -> str:
"""
# the try-except clauses in the following are precautions for multithreading
try:
dirname = THREAD_LOCAL.LOGGING # raises a name error if LOGGING is not defined
dirname = GLOBALS.LOGGING # raises a name error if LOGGING is not defined
if not dirname:
raise AttributeError # raise a name error if LOGGING evaluates to False
except AttributeError:
......@@ -126,18 +126,18 @@ def logging(dirname="LOGS"):
if dirname and not isinstance(dirname, str):
dirname = "LOGS" # be fail tolerant here...
try:
save = THREAD_LOCAL.LOGGING
save = GLOBALS.LOGGING
except AttributeError:
save = ""
THREAD_LOCAL.LOGGING = dirname or ""
GLOBALS.LOGGING = dirname or ""
yield
THREAD_LOCAL.LOGGING = save
GLOBALS.LOGGING = save
def is_logging() -> bool:
"""-> True, if logging is turned on."""
try:
return bool(THREAD_LOCAL.LOGGING)
return bool(GLOBALS.LOGGING)
except AttributeError:
return False
......
......@@ -58,7 +58,7 @@ __all__ = ('escape_re',
'compile_python_object',
'smart_list',
'sane_parser_name',
'THREAD_LOCAL')
'GLOBALS')
#######################################################################
......@@ -68,7 +68,7 @@ __all__ = ('escape_re',
#######################################################################
THREAD_LOCAL = threading.local()
GLOBALS = threading.local()
def escape_re(strg: str) -> str:
......
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