Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow specifying future compilation features in sigtools.support #34

Merged
merged 1 commit into from
Feb 19, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions sigtools/support.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

"""

import __future__
import re
import sys
import itertools
Expand Down Expand Up @@ -180,15 +181,19 @@ def func_code(names, return_annotation, annotations, posoarg_n,
code.append(f' return {{{return_keyvalues}}}')
return '\n'.join(code)

def make_func(code, locals=None, name='func'):
def make_func(source, locals=None, name='func', future_features=()):
"""Executes the given code and returns the object named func from
the resulting namespace."""
if locals is None:
locals = {}
flags = 0
for feature in future_features:
flags |= getattr(__future__, feature).compiler_flag
code = compile(source, "<sigtools.support>", "exec", flags)
exec(code, { "modifiers": modifiers }, locals)
return locals[name]

def f(sig_str, ret=_util.UNSET, *, pre='', locals=None, name='func', **kwargs):
def f(sig_str, ret=_util.UNSET, *, pre='', locals=None, name='func', future_features=(), **kwargs):
"""Creates a dummy function that has the signature represented by
``sig_str`` and returns a tuple containing the arguments passed,
in order.
Expand All @@ -210,8 +215,15 @@ def f(sig_str, ret=_util.UNSET, *, pre='', locals=None, name='func', **kwargs):
{'b': 2, 'a': 1, 'kwargs': {'d': 6}, 'args': (3, 4)}
"""
return make_func(
func_code(*read_sig(sig_str, ret=ret, **kwargs), name=name, pre=pre),
locals=locals, name=name)
func_code(
*read_sig(sig_str, ret=ret, **kwargs),
name=name,
pre=pre,
),
locals=locals,
name=name,
future_features=future_features,
)

def s(*args, **kwargs):
"""Creates a signature from the given string representation of one.
Expand Down