@@ -817,6 +817,8 @@ def __init__(
817817 self ._backend_added = False
818818 self ._backend = _InMemoryBackend (config = self )
819819
820+ self ._backend_active = False
821+
820822 @abc .abstractmethod
821823 def _add_backend_to_config (self , backend : _InMemoryBackend ) -> None :
822824 """Adds the backend to the config object."""
@@ -833,15 +835,26 @@ def __enter__(self) -> Self:
833835 the context endures, Git operations will use the sum total configuration that
834836 includes the in-memory configuration.
835837
838+ The resulting context is non-reentrant. It must be exited before it can be
839+ entered again, or else a ``RuntimeError`` is raised.
840+
836841 :returns: ``self``
837842
838843 :raises TypeError: If this is a read-only snapshot of the configuration.
844+ :raises RuntimeError: If the config context manager has already been entered but
845+ not exited.
839846 """
840847 if self ._is_snapshot :
841848 raise TypeError (
842849 'A read-only config snapshot cannot be used as a context manager, '
843850 'because its backend data cannot be changed.'
844851 )
852+ if self ._backend_active :
853+ raise RuntimeError (
854+ 'The config context manager is non-reentrant. You must exit the active '
855+ 'context before attempting to re-enter it.'
856+ )
857+ self ._backend_active = True
845858 if not self ._backend_added :
846859 self ._add_backend_to_config (self ._backend )
847860 self ._backend_added = True
@@ -865,6 +878,7 @@ def __exit__(
865878 if self ._backend_added :
866879 self ._set_write_order (self ._default_write_order ())
867880 self ._backend .clear ()
881+ self ._backend_active = False
868882 return False
869883
870884 def _set_write_order (self , levels : Sequence [ConfigLevel ]) -> None :
@@ -873,7 +887,7 @@ def _set_write_order(self, levels: Sequence[ConfigLevel]) -> None:
873887 By default, when libgit2 creates a ``git_config`` object for a repository, it sets
874888 the write order to ``{ GIT_CONFIG_LEVEL_LOCAL }``. This means that writes go
875889 only to the local config in ``.git/config`` and nowhere else. We need to change
876- this to ``{ GIT_CONFIG_LEVEL_APP }` when entering the context and then back to
890+ this to ``{ GIT_CONFIG_LEVEL_APP }`` when entering the context and then back to
877891 ``{ GIT_CONFIG_LEVEL_LOCAL }`` when exiting the context. For the non-repo "default"
878892 config, the default we want to restore is ``{ GIT_CONFIG_LEVEL_GLOBAL,
879893 GIT_CONFIG_LEVEL_XDG, GIT_CONFIG_LEVEL_SYSTEM, GIT_CONFIG_LEVEL_PROGRAMDATA }``.
@@ -909,8 +923,9 @@ class DefaultConfig(_InMemoryAppBackendConfig):
909923 usual order. When the context manager exits, the in-memory backend's contents are
910924 erased, undoing any changes made to it.
911925
912- The context manager can be re-entered and then re-exited repeatedly; it is not a
913- one-use-only operation.
926+ The context manager can be entered, exited, then re-entered and re-exited repeatedly;
927+ it is not a one-use-only operation. However, an active context is non-reentrant. You
928+ must exit the context before re-entering, or else a ``RuntimeError`` is raised.
914929
915930 Only writeable ``DefaultConfig`` objects can be used as a context manager.
916931 Read-only snapshot ``DefaultConfig`` objects cannot.
@@ -1019,8 +1034,9 @@ class RepositoryConfig(_InMemoryAppBackendConfig):
10191034 in-memory backend's contents are erased, undoing any changes made to it and
10201035 allowing write operations to resume affecting the local configuration.
10211036
1022- The context manager can be re-entered and then re-exited repeatedly; it is not a
1023- one-use-only operation.
1037+ The context manager can be entered, exited, then re-entered and re-exited repeatedly;
1038+ it is not a one-use-only operation. However, an active context is non-reentrant. You
1039+ must exit the context before re-entering, or else a ``RuntimeError`` is raised.
10241040
10251041 Only writeable ``RepositoryConfig`` objects can be used as a context manager.
10261042 Read-only snapshot ``RepositoryConfig`` objects cannot.
@@ -1321,7 +1337,7 @@ def read_lock(self) -> Generator[None, None, None]:
13211337 transaction).
13221338 """
13231339 if self ._is_snapshot :
1324- # If it's a snapshot, it's read-only.
1340+ # If it's a snapshot, it's read-only, so lock nothing .
13251341 yield
13261342 return
13271343
@@ -1483,8 +1499,16 @@ def __init__(
14831499 self ._c_handle = ffi .new_handle (self )
14841500 self ._c_iterator = c_iterator
14851501 self ._c_entries : dict [int , 'PyGitConfigIteratorEntryC' ] = {}
1502+ self ._closed = False
1503+
1504+ def close (self ) -> None :
1505+ if not self ._closed :
1506+ self ._closed = True
1507+ self ._generator .close ()
14861508
14871509 def __next__ (self ) -> tuple [str , _InMemoryBackend .Entry ]:
1510+ if self ._closed :
1511+ raise StopIteration ()
14881512 return next (self ._generator )
14891513
14901514 def __enter__ (self ) -> Self :
@@ -1497,9 +1521,13 @@ def __exit__(
14971521 exc_val : BaseException | None ,
14981522 exc_tb : TracebackType | None ,
14991523 ) -> Literal [False ]:
1524+ self .close ()
15001525 self ._backend ._iterators .pop (id (self ), None )
15011526 return False
15021527
1528+ def __del__ (self ) -> None :
1529+ self .close ()
1530+
15031531
15041532@ffi .def_extern ()
15051533def _config_memory_backend_open (
@@ -1570,11 +1598,13 @@ def _config_memory_backend_get(
15701598 backend_wrapper = ffi .cast ('_pygit_in_memory_backend *' , backend )
15711599 self = cast (_InMemoryBackend , ffi .from_handle (backend_wrapper .self ))
15721600 try :
1573- key = ffi .string (name ).decode ('utf-8' ).lower ()
1574- if key not in self ._read_data or not self ._read_data [key ]:
1575- return C .GIT_ENOTFOUND
1601+ key = ffi .string (name ).decode ('utf-8' )
1602+ key_lowered = key .casefold ()
1603+ with self .read_lock ():
1604+ if key_lowered not in self ._read_data or not self ._read_data [key_lowered ]:
1605+ return C .GIT_ENOTFOUND
1606+ value = self ._read_data [key_lowered ][- 1 ]
15761607
1577- value = self ._read_data [key ][0 ]
15781608 entry = ffi .new ('_pygit_in_memory_backend_entry *' )
15791609 ptr = int (ffi .cast ('uintptr_t' , entry ))
15801610 entry .owner = backend_wrapper
@@ -1617,9 +1647,11 @@ def _config_memory_backend_set(
16171647 try :
16181648 if self ._is_snapshot :
16191649 return C .GIT_EREADONLY
1620- key = ffi .string (name ).decode ('utf-8' ).lower ()
1650+ key = ffi .string (name ).decode ('utf-8' )
1651+ key_lowered = key .casefold ()
16211652 decoded_value = ffi .string (value ).decode ('utf-8' )
1622- self ._write_data [key ] = [_InMemoryBackend .Entry (key , decoded_value )]
1653+ with self .write_lock ():
1654+ self ._write_data [key_lowered ] = [_InMemoryBackend .Entry (key , decoded_value )]
16231655 except BaseException as e :
16241656 self .store_exception (e )
16251657 return C .GIT_EUSER
@@ -1657,16 +1689,19 @@ def _config_memory_backend_set_multivar(
16571689 try :
16581690 if self ._is_snapshot :
16591691 return C .GIT_EREADONLY
1660- key = ffi .string (name ).decode ('utf-8' ).lower ()
1692+ key = ffi .string (name ).decode ('utf-8' )
1693+ key_lowered = key .casefold ()
16611694 with self .write_lock ():
1662- if key in self ._write_data and regexp != ffi .NULL :
1695+ if key_lowered in self ._write_data and regexp != ffi .NULL :
16631696 expression = re .compile (ffi .string (regexp ).decode ('utf-8' ))
1664- self ._write_data [key ] = [
1665- v for v in self ._write_data [key ] if not expression .search (v .value )
1697+ self ._write_data [key_lowered ] = [
1698+ v
1699+ for v in self ._write_data [key_lowered ]
1700+ if not expression .search (v .value )
16661701 ]
1667- elif key not in self ._write_data :
1668- self ._write_data [key ] = []
1669- self ._write_data [key ].append (
1702+ elif key_lowered not in self ._write_data :
1703+ self ._write_data [key_lowered ] = []
1704+ self ._write_data [key_lowered ].append (
16701705 _InMemoryBackend .Entry (key , ffi .string (value ).decode ('utf-8' )),
16711706 )
16721707 except BaseException as e :
@@ -1698,11 +1733,12 @@ def _config_memory_backend_del(
16981733 try :
16991734 if self ._is_snapshot :
17001735 return C .GIT_EREADONLY
1701- key = ffi .string (name ).decode ('utf-8' ).lower ()
1736+ key = ffi .string (name ).decode ('utf-8' )
1737+ key_lowered = key .casefold ()
17021738 with self .write_lock ():
1703- if key not in self ._write_data :
1739+ if key_lowered not in self ._write_data :
17041740 return C .GIT_ENOTFOUND
1705- del self ._write_data [key ]
1741+ del self ._write_data [key_lowered ]
17061742 except BaseException as e :
17071743 self .store_exception (e )
17081744 return C .GIT_EUSER
@@ -1734,16 +1770,19 @@ def _config_memory_backend_del_multivar(
17341770 try :
17351771 if self ._is_snapshot :
17361772 return C .GIT_EREADONLY
1737- key = ffi .string (name ).decode ('utf-8' ).lower ()
1773+ key = ffi .string (name ).decode ('utf-8' )
1774+ key_lowered = key .casefold ()
17381775 with self .write_lock ():
1739- if key not in self ._write_data :
1776+ if key_lowered not in self ._write_data :
17401777 return C .GIT_ENOTFOUND
17411778 if regexp == ffi .NULL :
1742- del self ._write_data [key ]
1779+ del self ._write_data [key_lowered ]
17431780 else :
17441781 expression = re .compile (ffi .string (regexp ).decode ('utf-8' ))
1745- self ._write_data [key ] = [
1746- v for v in self ._write_data [key ] if not expression .search (v .value )
1782+ self ._write_data [key_lowered ] = [
1783+ v
1784+ for v in self ._write_data [key_lowered ]
1785+ if not expression .search (v .value )
17471786 ]
17481787 except BaseException as e :
17491788 self .store_exception (e )
0 commit comments