Skip to content

bpo-36146: Refactor setup.py #12068

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

Closed
wants to merge 2 commits into from
Closed

bpo-36146: Refactor setup.py #12068

wants to merge 2 commits into from

Conversation

vstinner
Copy link
Member

@vstinner vstinner commented Feb 27, 2019

The detect_modules() method of setup.py became longer and longer over
the years. It is now 1128 lines long. It's way too long: it becomes
very hard to track the lifetime of a variable and many variables are
overriden on purpose or not. Shorter functions help to track the
lifetime of variables, ease review and reduce the number of bugs.

Changes:

  • Fix detect_ctypes(): it no longer modifies global inc_dirs on
    macOS
  • self.srcdir is now always an absolute path
  • Add MACOS and MS_WINDOWS constants
  • Add TEST_EXTENSIONS to allow to not build extensions used to test
    Python (ex: _testcapi)
  • Add MATH_LIBS which is the libm library

Other refactoring changes:

  • Reorder imports

  • Replace "from distutils.errors import *"
    with "from distutils.errors import CCompilerError, DistutilsError"
    to be able to use static analyzers like pyflakes

  • Rename globals to upper case to better distinguish if a variable is
    global or locale:

    • Rename cross_compiling to CROSS_COMPILING
    • Rename host_platform to HOST_PLATFORM
    • Rename disabled_module_list to DISABLED_MODULE_LIST
  • Don't run code at module top level, but at the beginning of main()

  • Move 'missing' and 'srcdir' from detect_modules() local variable to
    PyBuildExt attributes

  • Remove exts local variable from detect_modules(): add add_ext()
    method which append directly the module to self.extensions.

  • _detect_openssl() and _detect_nis() now directly add the extension
    and have no return value.

  • Split very long detect_modules() method into subfunctions, add
    many "detect_xxx" methods.

https://bugs.python.org/issue36146

The detect_modules() method of setup.py became longer and longer over
the years. It is now 1128 lines long. It's way too long: it becomes
very hard to track the lifetime of a variable and many variables are
overriden on purpose or not. Shorter functions help to track the
lifetime of variables, ease review and reduce the number of bugs.

Changes:

* Fix detect_ctypes(): it no longer modifies global inc_dirs on
  macOS
* self.srcdir is now always an absolute path
* Add MACOS and MS_WINDOWS constants
* Add TEST_EXTENSIONS to allow to not build extensions used to test
  Python (ex: _testcapi)
* Add MATH_LIBS which is the libm library

Other refactoring changes:

* Reorder imports
* Replace "from distutils.errors import *"
  with "from distutils.errors import CCompilerError, DistutilsError"
  to be able to use static analyzers like pyflakes
* Rename globals to upper case to better distinguish if a variable is
  global or locale:

  * Rename cross_compiling to CROSS_COMPILING
  * Rename host_platform to HOST_PLATFORM
  * Rename disabled_module_list to DISABLED_MODULE_LIST

* Don't run code at module top level, but at the beginning of main()
* Move 'missing' and 'srcdir' from detect_modules() local variable to
  PyBuildExt attributes
* Remove exts local variable from detect_modules(): add add_ext()
  method which append directly the module to self.extensions.
* _detect_openssl() and _detect_nis() now directly add the extension
  and have no return value.
* Split very long detect_modules() method into subfunctions, add
  many "detect_xxx" methods.
@vstinner
Copy link
Member Author

@pablogsal @serhiy-storchaka @methane @gpshead @pxinwr: Would you mind to review this large refactoring change? I basically moved a lot of code, but I made some additional changes described in the commit message (and PR description).

@vstinner
Copy link
Member Author

Since the change shouldn't modify the behavior of setup.py of any way, I decided to skip the bpo number and NEWS entry.

@matrixise
Copy link
Member

@vstinner don't you prefer to use pathlib.Path instead of os.path.XYZ ?

def build_extensions(self):
self.srcdir = sysconfig.get_config_var('srcdir')
Copy link
Member

@matrixise matrixise Feb 27, 2019

Choose a reason for hiding this comment

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

maybe you could use pathlib.Path ?

Copy link
Member Author

Choose a reason for hiding this comment

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

setup.py is used to build Python. When setup.py is called, most Python modules are not available. I prefer to use the bare minimum from the stdlib. For example, "import subprocess" fails with "ModuleNotFoundError: No module named _posixsubprocess". That's why os.system() is used.

Moreover, I prefer to limit changes in this PR.

setup.py Outdated
@@ -425,7 +477,7 @@ def check_extension_import(self, ext):

# Workaround for Cygwin: Cygwin currently has fork issues when many
# modules have been imported
if host_platform == 'cygwin':
if HOST_PLATFORM == 'cygwin':
Copy link
Member

Choose a reason for hiding this comment

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

add CYGWIN = (HOST_PLATFORM == 'cygwin') after MS_WINDOWS ?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, I hesitated to add this one. Done, I added CYGWIN.

@matrixise
Copy link
Member

@vstinner maybe you prefer to keep the compatibility with python2 and avoid pathlib.Path.

@matrixise
Copy link
Member

@vstinner excepted the issues with ftp://www.pythontest.net your code could be merged.

@ned-deily ned-deily self-requested a review February 27, 2019 23:02
@ned-deily
Copy link
Member

Yikes! Please do not merge this until I've had a chance to better review and test various build configurations. It would be a lot easier without so many gratuitous changes.

@vstinner vstinner changed the title Refactor setup.py bpo-36146: Refactor setup.py Feb 28, 2019
@vstinner
Copy link
Member Author

Yikes! Please do not merge this until I've had a chance to better review and test various build configurations. It would be a lot easier without so many gratuitous changes.

I'm sorry, my intent wasn't to scare you :-) I agree that this PR modifies too many unrelated things at once, and so I close it in favor of shorter changes which would be easier to review.

I created https://bugs.python.org/issue36146 to track this work and I started with PR #12093 to convert globals to upper case and add some new constants. This PR should be straightforward not controversial in any way :-)

@vstinner vstinner closed this Feb 28, 2019
@vstinner vstinner deleted the refactor_setup_py branch February 28, 2019 15:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants