Skip to content

bpo-28167: Remove platform.linux_distribution #6871

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

Merged
merged 2 commits into from
May 16, 2018
Merged
Show file tree
Hide file tree
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
27 changes: 0 additions & 27 deletions Doc/library/platform.rst
Original file line number Diff line number Diff line change
Expand Up @@ -243,33 +243,6 @@ Mac OS Platform
Unix Platforms
--------------


.. function:: dist(distname='', version='', id='', supported_dists=('SuSE','debian','redhat','mandrake',...))

This is another name for :func:`linux_distribution`.

.. deprecated-removed:: 3.5 3.8
See alternative like the `distro <https://pypi.org/project/distro>`_ package.

.. function:: linux_distribution(distname='', version='', id='', supported_dists=('SuSE','debian','redhat','mandrake',...), full_distribution_name=1)

Tries to determine the name of the Linux OS distribution name.

``supported_dists`` may be given to define the set of Linux distributions to
look for. It defaults to a list of currently supported Linux distributions
identified by their release file name.

If ``full_distribution_name`` is true (default), the full distribution read
from the OS is returned. Otherwise the short name taken from
``supported_dists`` is used.

Returns a tuple ``(distname,version,id)`` which defaults to the args given as
parameters. ``id`` is the item in parentheses after the version number. It
is usually the version codename.

.. deprecated-removed:: 3.5 3.8
See alternative like the `distro <https://pypi.org/project/distro>`_ package.

.. function:: libc_ver(executable=sys.executable, lib='', version='', chunksize=2048)

Tries to determine the libc version against which the file executable (defaults
Expand Down
161 changes: 5 additions & 156 deletions Lib/platform.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,6 @@
# Standard Unix uses /dev/null
DEV_NULL = '/dev/null'

# Directory to search for configuration information on Unix.
# Constant used by test_platform to test linux_distribution().
_UNIXCONFDIR = '/etc'

### Platform specific APIs

_libc_search = re.compile(b'(__libc_init)'
Expand Down Expand Up @@ -249,138 +245,6 @@ def _dist_try_harder(distname, version, id):

return distname, version, id

_release_filename = re.compile(r'(\w+)[-_](release|version)', re.ASCII)
_lsb_release_version = re.compile(r'(.+)'
r' release '
r'([\d.]+)'
r'[^(]*(?:\((.+)\))?', re.ASCII)
_release_version = re.compile(r'([^0-9]+)'
r'(?: release )?'
r'([\d.]+)'
r'[^(]*(?:\((.+)\))?', re.ASCII)

# See also http://www.novell.com/coolsolutions/feature/11251.html
# and http://linuxmafia.com/faq/Admin/release-files.html
# and http://data.linux-ntfs.org/rpm/whichrpm
# and http://www.die.net/doc/linux/man/man1/lsb_release.1.html

_supported_dists = (
'SuSE', 'debian', 'fedora', 'redhat', 'centos',
'mandrake', 'mandriva', 'rocks', 'slackware', 'yellowdog', 'gentoo',
'UnitedLinux', 'turbolinux', 'arch', 'mageia')

def _parse_release_file(firstline):

# Default to empty 'version' and 'id' strings. Both defaults are used
# when 'firstline' is empty. 'id' defaults to empty when an id can not
# be deduced.
version = ''
id = ''

# Parse the first line
m = _lsb_release_version.match(firstline)
if m is not None:
# LSB format: "distro release x.x (codename)"
return tuple(m.groups())

# Pre-LSB format: "distro x.x (codename)"
m = _release_version.match(firstline)
if m is not None:
return tuple(m.groups())

# Unknown format... take the first two words
l = firstline.strip().split()
if l:
version = l[0]
if len(l) > 1:
id = l[1]
return '', version, id

def linux_distribution(distname='', version='', id='',

supported_dists=_supported_dists,
full_distribution_name=1):
import warnings
warnings.warn("dist() and linux_distribution() functions are deprecated "
"in Python 3.5", DeprecationWarning, stacklevel=2)
return _linux_distribution(distname, version, id, supported_dists,
full_distribution_name)

def _linux_distribution(distname, version, id, supported_dists,
full_distribution_name):

""" Tries to determine the name of the Linux OS distribution name.

The function first looks for a distribution release file in
/etc and then reverts to _dist_try_harder() in case no
suitable files are found.

supported_dists may be given to define the set of Linux
distributions to look for. It defaults to a list of currently
supported Linux distributions identified by their release file
name.

If full_distribution_name is true (default), the full
distribution read from the OS is returned. Otherwise the short
name taken from supported_dists is used.

Returns a tuple (distname, version, id) which default to the
args given as parameters.

"""
try:
etc = os.listdir(_UNIXCONFDIR)
except OSError:
# Probably not a Unix system
return distname, version, id
etc.sort()
for file in etc:
m = _release_filename.match(file)
if m is not None:
_distname, dummy = m.groups()
if _distname in supported_dists:
distname = _distname
break
else:
return _dist_try_harder(distname, version, id)

# Read the first line
with open(os.path.join(_UNIXCONFDIR, file), 'r',
encoding='utf-8', errors='surrogateescape') as f:
firstline = f.readline()
_distname, _version, _id = _parse_release_file(firstline)

if _distname and full_distribution_name:
distname = _distname
if _version:
version = _version
if _id:
id = _id
return distname, version, id

# To maintain backwards compatibility:

def dist(distname='', version='', id='',

supported_dists=_supported_dists):

""" Tries to determine the name of the Linux OS distribution name.

The function first looks for a distribution release file in
/etc and then reverts to _dist_try_harder() in case no
suitable files are found.

Returns a tuple (distname, version, id) which default to the
args given as parameters.

"""
import warnings
warnings.warn("dist() and linux_distribution() functions are deprecated "
"in Python 3.5", DeprecationWarning, stacklevel=2)
return _linux_distribution(distname, version, id,
supported_dists=supported_dists,
full_distribution_name=0)

def popen(cmd, mode='r', bufsize=-1):

""" Portable popen() interface.
Expand Down Expand Up @@ -1338,26 +1202,11 @@ def platform(aliased=0, terse=0):
platform = _platform(system, release, version, csd)

elif system in ('Linux',):
# Linux based systems
with warnings.catch_warnings():
# see issue #1322 for more information
warnings.filterwarnings(
'ignore',
r'dist\(\) and linux_distribution\(\) '
'functions are deprecated .*',
DeprecationWarning,
)
distname, distversion, distid = dist('')
if distname and not terse:
platform = _platform(system, release, machine, processor,
'with',
distname, distversion, distid)
else:
# If the distribution name is unknown check for libc vs. glibc
libcname, libcversion = libc_ver(sys.executable)
platform = _platform(system, release, machine, processor,
'with',
libcname+libcversion)
# check for libc vs. glibc
libcname, libcversion = libc_ver(sys.executable)
platform = _platform(system, release, machine, processor,
'with',
libcname+libcversion)
elif system == 'Java':
# Java platforms
r, v, vminfo, (os_name, os_version, os_arch) = java_ver()
Expand Down
65 changes: 0 additions & 65 deletions Lib/test/test_platform.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,16 +259,6 @@ def test_mac_ver_with_fork(self):
self.assertEqual(cpid, pid)
self.assertEqual(sts, 0)

def test_dist(self):
with warnings.catch_warnings():
warnings.filterwarnings(
'ignore',
r'dist\(\) and linux_distribution\(\) '
'functions are deprecated .*',
PendingDeprecationWarning,
)
res = platform.dist()

def test_libc_ver(self):
import os
if os.path.isdir(sys.executable) and \
Expand All @@ -279,23 +269,6 @@ def test_libc_ver(self):
executable = sys.executable
res = platform.libc_ver(executable)

def test_parse_release_file(self):

for input, output in (
# Examples of release file contents:
('SuSE Linux 9.3 (x86-64)', ('SuSE Linux ', '9.3', 'x86-64')),
('SUSE LINUX 10.1 (X86-64)', ('SUSE LINUX ', '10.1', 'X86-64')),
('SUSE LINUX 10.1 (i586)', ('SUSE LINUX ', '10.1', 'i586')),
('Fedora Core release 5 (Bordeaux)', ('Fedora Core', '5', 'Bordeaux')),
('Red Hat Linux release 8.0 (Psyche)', ('Red Hat Linux', '8.0', 'Psyche')),
('Red Hat Linux release 9 (Shrike)', ('Red Hat Linux', '9', 'Shrike')),
('Red Hat Enterprise Linux release 4 (Nahant)', ('Red Hat Enterprise Linux', '4', 'Nahant')),
('CentOS release 4', ('CentOS', '4', None)),
('Rocks release 4.2.1 (Cydonia)', ('Rocks', '4.2.1', 'Cydonia')),
('', ('', '', '')), # If there's nothing there.
):
self.assertEqual(platform._parse_release_file(input), output)

def test_popen(self):
mswindows = (sys.platform == "win32")

Expand Down Expand Up @@ -328,43 +301,5 @@ def test_popen(self):
returncode = ret >> 8
self.assertEqual(returncode, len(data))

def test_linux_distribution_encoding(self):
# Issue #17429
with tempfile.TemporaryDirectory() as tempdir:
filename = os.path.join(tempdir, 'fedora-release')
with open(filename, 'w', encoding='utf-8') as f:
f.write('Fedora release 19 (Schr\xf6dinger\u2019s Cat)\n')

with mock.patch('platform._UNIXCONFDIR', tempdir):
with warnings.catch_warnings():
warnings.filterwarnings(
'ignore',
r'dist\(\) and linux_distribution\(\) '
'functions are deprecated .*',
PendingDeprecationWarning,
)
distname, version, distid = platform.linux_distribution()

self.assertEqual(distname, 'Fedora')
self.assertEqual(version, '19')
self.assertEqual(distid, 'Schr\xf6dinger\u2019s Cat')


class DeprecationTest(unittest.TestCase):

def test_dist_deprecation(self):
with self.assertWarns(DeprecationWarning) as cm:
platform.dist()
self.assertEqual(str(cm.warning),
'dist() and linux_distribution() functions are '
'deprecated in Python 3.5')

def test_linux_distribution_deprecation(self):
with self.assertWarns(DeprecationWarning) as cm:
platform.linux_distribution()
self.assertEqual(str(cm.warning),
'dist() and linux_distribution() functions are '
'deprecated in Python 3.5')

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