Skip to content

Commit 0a093b3

Browse files
authored
Merge pull request #281 from byexamples/Issue-280-Fix-Log-Lock-With-a-Non-None-Dummy-Lock
Issue 280 fix log lock with a non none dummy lock
2 parents a8bea76 + e4a1542 commit 0a093b3

File tree

4 files changed

+49
-7
lines changed

4 files changed

+49
-7
lines changed

.check-secrets.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/bin/bash
2+
if [ -x .check-secrets-impl.sh ]; then
3+
. .check-secrets-impl.sh
4+
elif [ -f .check-secrets-impl.sh ]; then
5+
exit 1
6+
else
7+
exit 0
8+
fi

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@ prof-traces
1717
.workflow-log
1818
test/ds/good.args
1919
test/autocomplete_byexample.sh
20+
.check-secrets-impl.sh

.pre-commit-config.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,13 @@ repos:
4242
rev: v2.29.0
4343
hooks:
4444
- id: commitizen
45+
46+
- repo: local
47+
hooks:
48+
- id: check-secrets
49+
name: check secrets in files
50+
language: script
51+
entry: .check-secrets.sh
52+
always_run: true
53+
verbose: true
54+
stages: ["push"]

byexample/log.py

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,35 @@ def log_with(logger_name, child=True):
334334
finally:
335335
_logger_stack.pop()
336336

337+
class _DummyRLock:
338+
'''
339+
A dummy reentrant lock to emulate a lock (acquire/release) but without
340+
any overhead. Of course, without any mutual exclusion either!
341+
342+
We use _DummyRLock for XStreamHandler where we are OK with a thread-unsafe
343+
implementation because XStreamHandler's emit() will handle the thread-safety
344+
or it will delegate the call to someone than manages the thread-safety itself
345+
(the concerns set).
346+
'''
347+
def __init__(self):
348+
self._cnt = 0
349+
350+
def acquire(self, *args, **kargs):
351+
self._cnt += 1
352+
353+
def release(self):
354+
self._cnt -= 1
355+
self._cnt = max(self._cnt, 0)
356+
357+
def locked(self):
358+
return self._cnt > 0
359+
360+
def __enter__(self):
361+
self.acquire()
362+
return self
363+
364+
def __exit__(self, exc_type, exc_value, traceback):
365+
self.release()
337366

338367
class XStreamHandler(logging.StreamHandler):
339368
def __init__(self, *args, **kargs):
@@ -366,13 +395,7 @@ def emit(self, record):
366395
self.handleError(record)
367396

368397
def createLock(self):
369-
self.lock = None
370-
371-
def acquire(self):
372-
return
373-
374-
def release(self):
375-
return
398+
self.lock = _DummyRLock()
376399

377400
def handleError(self, record):
378401
# handleError is called when an error happend during the logging.

0 commit comments

Comments
 (0)