From 3df5581084743b95204e66b6a8110762addb4253 Mon Sep 17 00:00:00 2001 From: Vitalii Abetkin Date: Thu, 11 Jun 2020 13:47:32 +0300 Subject: [PATCH 1/4] add Extractor class with `def extractor` the created function holds all the variables, including `globals` and `locals` and prevents them from garbage collection --- pony/orm/asttranslation.py | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/pony/orm/asttranslation.py b/pony/orm/asttranslation.py index 6c9068b60..c57749626 100644 --- a/pony/orm/asttranslation.py +++ b/pony/orm/asttranslation.py @@ -361,13 +361,26 @@ def create_extractors(code_key, tree, globals, locals, special_functions, const_ extractors = {} for node in pretranslator.externals: src = node.src = ast2src(node) - if src == '.0': - def extractor(globals, locals): - return locals['.0'] + if src != '.0': + extractor = Extractor('.0') else: code = compile(src, src, 'eval') - def extractor(globals, locals, code=code): - return eval(code, globals, locals) + extractor = Extractor(src, code) extractors[src] = extractor result = extractors_cache[code_key] = tree, extractors return result + + +class Extractor: + + def __init__(self, src, code=None): + self.src = src + self.code = code + + def __call__(self, globals, locals): + if self.src == '.0': + return locals['.0'] + return eval(self.code, globals, locals) + + def __repr__(self): + return 'Extractor(%s)' % self.src \ No newline at end of file From 57955504c550e447e9f6a13bd36c32ae4c79d2f4 Mon Sep 17 00:00:00 2001 From: Vitalii Abetkin Date: Thu, 11 Jun 2020 13:55:28 +0300 Subject: [PATCH 2/4] newline at EOF --- pony/orm/asttranslation.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pony/orm/asttranslation.py b/pony/orm/asttranslation.py index c57749626..d51e6f66e 100644 --- a/pony/orm/asttranslation.py +++ b/pony/orm/asttranslation.py @@ -383,4 +383,4 @@ def __call__(self, globals, locals): return eval(self.code, globals, locals) def __repr__(self): - return 'Extractor(%s)' % self.src \ No newline at end of file + return 'Extractor(%s)' % self.src From 1bf6eb4a7c0bd7ffc898ab18531d80a71a42f32d Mon Sep 17 00:00:00 2001 From: Vitalii Abetkin Date: Thu, 11 Jun 2020 14:01:31 +0300 Subject: [PATCH 3/4] typo --- pony/orm/asttranslation.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pony/orm/asttranslation.py b/pony/orm/asttranslation.py index d51e6f66e..5d3bd6811 100644 --- a/pony/orm/asttranslation.py +++ b/pony/orm/asttranslation.py @@ -361,7 +361,7 @@ def create_extractors(code_key, tree, globals, locals, special_functions, const_ extractors = {} for node in pretranslator.externals: src = node.src = ast2src(node) - if src != '.0': + if src == '.0': extractor = Extractor('.0') else: code = compile(src, src, 'eval') From 67a051eb76b1720b2441921e8cbfc3ed08f6c5c1 Mon Sep 17 00:00:00 2001 From: Vitalii Abetkin Date: Thu, 11 Jun 2020 16:01:08 +0300 Subject: [PATCH 4/4] PY2 corrections --- pony/orm/asttranslation.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pony/orm/asttranslation.py b/pony/orm/asttranslation.py index 5d3bd6811..e818e2d1f 100644 --- a/pony/orm/asttranslation.py +++ b/pony/orm/asttranslation.py @@ -371,7 +371,7 @@ def create_extractors(code_key, tree, globals, locals, special_functions, const_ return result -class Extractor: +class Extractor(object): def __init__(self, src, code=None): self.src = src