Skip to content

Commit

Permalink
Fix assertion error on nested PrefetchContext creation in before_inse…
Browse files Browse the repository at this point in the history
…rt query during flush caused by another select
  • Loading branch information
kozlovsky committed Jun 1, 2020
1 parent 166fc48 commit 4e04978
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
15 changes: 10 additions & 5 deletions pony/orm/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,11 +298,11 @@ def copy(self):
result.entities_to_prefetch = self.entities_to_prefetch.copy()
return result
def __enter__(self):
assert local.prefetch_context is None
local.prefetch_context = self
local.prefetch_context_stack.append(self)
def __exit__(self, exc_type, exc_val, exc_tb):
assert local.prefetch_context is self
local.prefetch_context = None
stack = local.prefetch_context_stack
assert stack and stack[-1] is self
stack.pop()
def get_frozen_attrs_to_prefetch(self, entity):
attrs_to_prefetch = self.attrs_to_prefetch_dict.get(entity, ())
if type(attrs_to_prefetch) is set:
Expand All @@ -329,11 +329,16 @@ def __init__(local):
local.db2cache = {}
local.db_context_counter = 0
local.db_session = None
local.prefetch_context = None
local.prefetch_context_stack = []
local.current_user = None
local.perms_context = None
local.user_groups_cache = {}
local.user_roles_cache = defaultdict(dict)
@property
def prefetch_context(local):
if local.prefetch_context_stack:
return local.prefetch_context_stack[-1]
return None
def push_debug_state(local, debug, show_values):
local.debug_stack.append((local.debug, local.show_values))
if not suppress_debug_change:
Expand Down
8 changes: 8 additions & 0 deletions pony/orm/tests/test_hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,14 @@ def do_before_insert(person):
p4 = Person(id=4, name='Bob', age=16)
db.flush()

@db_session
def test_4(self):
global do_before_insert
def do_before_insert(person):
some_person = Person.select().first() # creates nested prefetch_context
p4 = Person(id=4, name='Bob', age=16)
Person.select().first()


if __name__ == '__main__':
unittest.main()

0 comments on commit 4e04978

Please sign in to comment.