Skip to content

Commit

Permalink
Small Fixes
Browse files Browse the repository at this point in the history
 - Added .venv and .envrc to .gitignore (I use direnv and venv to keep
   my python environments isolated - I hope this is okay!)
 - Removed print statements I left in dom_tag during debugging
 - Replaced global incrementing int with UUID for contextvar ID
   generation - this zeroes the risk of race-hazards/collisions
 - _get_thread_context now returns a tuple vs. a hash of a tuple.
   Functionally not much changes - the underlying dictionary will still
   use the same hashing function but the only difference is that _if_
   there is a collision, the dictionary will still be able to return the
   correct element
  • Loading branch information
tlonny committed Dec 14, 2023
1 parent be2f92b commit 5746232
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 6 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,5 @@ nosetests.xml

.idea
.idea/
.venv/
.envrc
9 changes: 3 additions & 6 deletions dominate/dom_tag.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from functools import wraps
import threading
from asyncio import get_event_loop
from uuid import uuid4
from contextvars import ContextVar

try:
Expand Down Expand Up @@ -54,15 +55,11 @@
# We use this to store a unique ID for each async context. We then use thie ID to
# form the key (in _get_thread_context) that is used to index the _with_context defaultdict.
# The presense of this key ensures that each async context has its own stack and doesn't conflict.
async_context_id_counter = 5
async_context_id = ContextVar('async_context_id', default = None)

def _get_async_context_id():
global async_context_id_counter
if async_context_id.get() is None:
async_context_id.set(async_context_id_counter)
async_context_id_counter += 1
print(async_context_id.get())
async_context_id.set(uuid4().hex)
return async_context_id.get()

def _get_thread_context():
Expand All @@ -80,7 +77,7 @@ def _get_thread_context():
# A runtime error is raised if there is no async loop...
except RuntimeError:
pass
return hash(tuple(context))
return tuple(context)

class dom_tag(object):
is_single = False # Tag does not require matching end tag (ex. <hr/>)
Expand Down

0 comments on commit 5746232

Please sign in to comment.