Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions changelog.d/3395.change.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Included a performance optimization: ``setuptools.build_meta`` no longer tries
to :func:`compile` the setup script code before :func:`exec`-ing it.
2 changes: 1 addition & 1 deletion setuptools/build_meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ def run_setup(self, setup_script='setup.py'):
with _open_setup_script(__file__) as f:
code = f.read().replace(r'\r\n', r'\n')

exec(compile(code, __file__, 'exec'), locals())
exec(code, locals())
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only difference I can think here is that if the setup script has a syntax error, compile would fail so no instruction gets executed. I don't know how exec works, but it could be the case it gets to execute something before failing... I don't think this is relevant though because the PEP 517 hooks are supposed to be executed in isolated subcommands, so it is fine for the hook to crash.

However, I might be failing to see other problems...

Alternatively we could also try.

 try: exec(code, locals()) except Exception: exec(compile(code, __file__, 'exec'), locals())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's my understanding that if there is a syntax error, nothing will ever be executed, so no concerns here.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An important difference is that ­— when an exception gets raised — the stacktrace is now broken. The second argument to compile tells python where to find the source code when displaying the stacktrace. That's exactly what #3577 wanted to improve.

Calling compile before exec doesn't seem to have any noticeable effect on performance:

$ python -m timeit -s 'code = "1 + 1 #" + "A"*10000' 'exec(code)' 20000 loops, best of 5: 10.7 usec per loop $ python -m timeit -s 'code = "1 + 1 #" + "A"*10000' 'exec(compile(code, __file__, "exec"))' 20000 loops, best of 5: 10.7 usec per loop 

I don't see how that could make any difference. To execute code the code needs to get compiled before. It doesn't matter if compile gets executed explicitly.


def get_requires_for_build_wheel(self, config_settings=None):
return self._get_build_requires(
Expand Down