Skip to content

Commit

Permalink
Fix accessing global variables from hybrid methods and properties
Browse files Browse the repository at this point in the history
  • Loading branch information
kozlovsky committed Jan 6, 2019
1 parent aa16b4f commit e5f06c8
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 7 deletions.
2 changes: 1 addition & 1 deletion pony/orm/sqltranslation.py
Original file line number Diff line number Diff line change
Expand Up @@ -882,7 +882,7 @@ def apply_lambda(translator, func_id, filter_num, order_by, func_ast, argnames,
translator.code_key = func_id
translator.filter_num = filter_num
translator.extractors.update(extractors)
translator.vars = vars.copy() if vars is not None else None
translator.vars = vars
translator.vartypes = translator.vartypes.copy() # make HashableDict mutable again
translator.vartypes.update(vartypes)

Expand Down
44 changes: 38 additions & 6 deletions pony/orm/tests/test_hybrid_methods_and_properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,22 @@

db = Database('sqlite', ':memory:')

sep = ' '

class Person(db.Entity):
id = PrimaryKey(int)
first_name = Required(str)
last_name = Required(str)
favorite_color = Optional(str)
cars = Set(lambda: Car)

@property
def full_name(self):
return self.first_name + ' ' + self.last_name
return self.first_name + sep + self.last_name

@property
def full_name_2(self):
return concat(self.first_name, ' ', self.last_name) # tests using of function `concat` from external scope
return concat(self.first_name, sep, self.last_name) # tests using of function `concat` from external scope

@property
def has_car(self):
Expand Down Expand Up @@ -60,10 +63,10 @@ class Car(db.Entity):
db.generate_mapping(create_tables=True)

with db_session:
p1 = Person(first_name='Alexander', last_name='Kozlovsky', favorite_color='white')
p2 = Person(first_name='Alexei', last_name='Malashkevich', favorite_color='green')
p3 = Person(first_name='Vitaliy', last_name='Abetkin')
p4 = Person(first_name='Alexander', last_name='Tischenko', favorite_color='blue')
p1 = Person(id=1, first_name='Alexander', last_name='Kozlovsky', favorite_color='white')
p2 = Person(id=2, first_name='Alexei', last_name='Malashkevich', favorite_color='green')
p3 = Person(id=3, first_name='Vitaliy', last_name='Abetkin')
p4 = Person(id=4, first_name='Alexander', last_name='Tischenko', favorite_color='blue')

c1 = Car(brand='Peugeot', model='306', owner=p1, year=2006, price=14000, color='red')
c2 = Car(brand='Honda', model='Accord', owner=p1, year=2007, price=13850, color='white')
Expand Down Expand Up @@ -157,6 +160,35 @@ def test15(self):
result = Person.find_by_full_name('Alexander Tischenko')
self.assertEqual(set(obj.last_name for obj in result), {'Tischenko'})

@db_session
def test16(self):
result = Person.select(lambda p: p.full_name == 'Alexander Kozlovsky')
self.assertEqual(set(p.id for p in result), {1})

@db_session
def test17(self):
global sep
sep = '.'
try:
result = Person.select(lambda p: p.full_name == 'Alexander.Kozlovsky')
self.assertEqual(set(p.id for p in result), {1})
finally:
sep = ' '

@db_session
def test18(self):
result = Person.select().filter(lambda p: p.full_name == 'Alexander Kozlovsky')
self.assertEqual(set(p.id for p in result), {1})

@db_session
def test19(self):
global sep
sep = '.'
try:
result = Person.select().filter(lambda p: p.full_name == 'Alexander.Kozlovsky')
self.assertEqual(set(p.id for p in result), {1})
finally:
sep = ' '

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

0 comments on commit e5f06c8

Please sign in to comment.