This patch changes calls of .split('\n') on string objects to
.splitlines(). I think this is easier to read, but it also has a
functional benefit:

# With carriage returns
print('1\n2\r3\r\n4\n'.split('\n'))
['1', '2\r3\r', '4', '']
print('1\n2\r3\r\n4\n'.splitlines())
['1', '2', '3', '4']

# Or more commonly, empty strings
print(''.split('\n'))
['']
print(''.splitlines())
[]

This should help reduce the amount of unexpected newlines in the
output of Makefiles and other generated files.

Collin
From 480aa5e79833c69bb1582ea2e7b39b2beacbf8aa Mon Sep 17 00:00:00 2001
From: Collin Funk <collin.fu...@gmail.com>
Date: Wed, 20 Mar 2024 03:02:45 -0700
Subject: [PATCH] gnulib-tool.py: Prefer str.splitlines() over str.split('\n').

* pygnulib/GLEmiter.py (GLEmiter.autoconfSnippet, GLEmiter.autoconfSnippets)
(GLEmiter.lib_Makefile_am): Replace all occurrences of ".split('\n')"
with ".splitlines()".
* pygnulib/GLImport.py (GLImport._update_ignorelist_, GLImport.execute):
Likewise.
* pygnulib/GLModuleSystem.py (GLModuleSystem.list, GLModule.getStatuses)
(GLModule.getFiles, GLModule.getDependenciesWithoutConditions)
(GLModule.getDependenciesWithConditions): Likewise.
* pygnulib/GLTestDir.py (GLTestDir.execute): Likewise.
* pygnulib/main.py (main): Likewise.
---
 ChangeLog                  | 14 ++++++++++++++
 pygnulib/GLEmiter.py       |  8 ++++----
 pygnulib/GLImport.py       |  6 +++---
 pygnulib/GLModuleSystem.py | 10 +++++-----
 pygnulib/GLTestDir.py      |  4 ++--
 pygnulib/main.py           |  6 +++---
 6 files changed, 31 insertions(+), 17 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index fc095fd92c..8350fcbdae 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,17 @@
+2024-03-20  Collin Funk  <collin.fu...@gmail.com>
+
+	gnulib-tool.py: Prefer str.splitlines() over str.split('\n').
+	* pygnulib/GLEmiter.py (GLEmiter.autoconfSnippet, GLEmiter.autoconfSnippets)
+	(GLEmiter.lib_Makefile_am): Replace all occurrences of ".split('\n')"
+	with ".splitlines()".
+	* pygnulib/GLImport.py (GLImport._update_ignorelist_, GLImport.execute):
+	Likewise.
+	* pygnulib/GLModuleSystem.py (GLModuleSystem.list, GLModule.getStatuses)
+	(GLModule.getFiles, GLModule.getDependenciesWithoutConditions)
+	(GLModule.getDependenciesWithConditions): Likewise.
+	* pygnulib/GLTestDir.py (GLTestDir.execute): Likewise.
+	* pygnulib/main.py (main): Likewise.
+
 2024-03-20  Collin Funk  <collin.fu...@gmail.com>
 
 	gnulib-tool.py: Don't print empty Automake snippets.
diff --git a/pygnulib/GLEmiter.py b/pygnulib/GLEmiter.py
index 8c2e3b5540..b5db7d5cd7 100644
--- a/pygnulib/GLEmiter.py
+++ b/pygnulib/GLEmiter.py
@@ -236,7 +236,7 @@ class GLEmiter(object):
             snippet = snippet.replace('${gl_include_guard_prefix}',
                                       include_guard_prefix)
             lines = [ line
-                      for line in snippet.split('\n')
+                      for line in snippet.splitlines()
                       if line.strip() ]
             snippet = lines_to_multiline(lines)
             pattern = re.compile('^(.*)$', re.M)
@@ -264,7 +264,7 @@ class GLEmiter(object):
                 pattern = re.compile(regex, re.M)
                 emit = pattern.sub(repl, emit)
         lines = [ line
-                  for line in emit.split('\n')
+                  for line in emit.splitlines()
                   if line.strip() ]
         emit = lines_to_multiline(lines)
         return emit
@@ -447,7 +447,7 @@ class GLEmiter(object):
                         shellvar = module.getShellVar()
                         emit += '  AM_CONDITIONAL([%s], [$%s])\n' % (condname, shellvar)
         lines = [ line
-                  for line in emit.split('\n')
+                  for line in emit.splitlines()
                   if line.strip() ]
         emit = lines_to_multiline(lines)
         return emit
@@ -1002,7 +1002,7 @@ AC_DEFUN([%V1%_LIBSOURCES], [
                       if module.isNonTests() ]
             lines = [ line
                       for link in links
-                      for line in link.split('\n')
+                      for line in link.splitlines()
                       if line != '' ]
             pattern = re.compile(' when linking with libtool.*')
             lines = [ pattern.sub('', line)
diff --git a/pygnulib/GLImport.py b/pygnulib/GLImport.py
index 4c7b62a8df..289de6a3b9 100644
--- a/pygnulib/GLImport.py
+++ b/pygnulib/GLImport.py
@@ -801,7 +801,7 @@ AC_DEFUN([%s_FILE_LIST], [\n''' % macro_prefix
             if dirs_added or dirs_removed:
                 with codecs.open(joinpath(destdir, srcpath), 'rb', 'UTF-8') as file:
                     srcdata = file.read()
-                dirs_ignore = sorted(set(srcdata.split('\n')))
+                dirs_ignore = sorted(set(srcdata.splitlines()))
                 dirs_ignore = [ line
                                 for line in dirs_ignore
                                 if line.strip() ]
@@ -1435,7 +1435,7 @@ AC_DEFUN([%s_FILE_LIST], [\n''' % macro_prefix
         includes_if = list()
         for module in modules:
             include = module.getInclude()
-            for include in include.split('\n'):
+            for include in include.splitlines():
                 if '%s#if' % constants.NL in '%s%s' % (constants.NL, include):
                     includes_if += [include]
                 # if '%s#if' % constants.NL in '%s%s' % (constants.NL, include)
@@ -1458,7 +1458,7 @@ AC_DEFUN([%s_FILE_LIST], [\n''' % macro_prefix
                   for module in self.moduletable['main'] ]
         lines = [ line
                   for link in links
-                  for line in link.split('\n')
+                  for line in link.splitlines()
                   if line != '' ]
         lines = sorted(set(lines))
         if lines:
diff --git a/pygnulib/GLModuleSystem.py b/pygnulib/GLModuleSystem.py
index a96a2988b5..61a909d564 100644
--- a/pygnulib/GLModuleSystem.py
+++ b/pygnulib/GLModuleSystem.py
@@ -155,7 +155,7 @@ class GLModuleSystem(object):
                 os.chdir(DIRS['cwd'])
 
         listing = [ line
-                    for line in result.split('\n')
+                    for line in result.splitlines()
                     if line.strip() ]
         if len(localpath) > 0:
             listing = [ subend('.diff', '', line)
@@ -491,7 +491,7 @@ class GLModule(object):
         if 'statuses' not in self.cache:
             snippet = self.getStatus()
             result = [ line.strip()
-                       for line in snippet.split('\n')
+                       for line in snippet.splitlines()
                        if line.strip() ]
             self.cache['statuses'] = result
         return self.cache['statuses']
@@ -526,7 +526,7 @@ class GLModule(object):
         if 'files' not in self.cache:
             snippet = self.sections.get('Files', '')
             result = [ line.strip()
-                       for line in snippet.split('\n')
+                       for line in snippet.splitlines()
                        if line.strip() ]
             result.append(joinpath('m4', '00gnulib.m4'))
             result.append(joinpath('m4', 'zzgnulib.m4'))
@@ -562,7 +562,7 @@ class GLModule(object):
         if 'dependenciesWithoutCond' not in self.cache:
             snippet = self.getDependencies()
             lines = [ line.strip()
-                      for line in snippet.split('\n')
+                      for line in snippet.splitlines()
                       if line.strip() ]
             pattern = re.compile(' *\\[.*$')
             lines = [ pattern.sub('', line)
@@ -583,7 +583,7 @@ class GLModule(object):
         if 'dependenciesWithCond' not in self.cache:
             snippet = self.getDependencies()
             lines = [ line.strip()
-                      for line in snippet.split('\n')
+                      for line in snippet.splitlines()
                       if line.strip() ]
             pattern = re.compile(' *\\[')
             result = []
diff --git a/pygnulib/GLTestDir.py b/pygnulib/GLTestDir.py
index c450f74099..9fd73d99fe 100644
--- a/pygnulib/GLTestDir.py
+++ b/pygnulib/GLTestDir.py
@@ -464,7 +464,7 @@ class GLTestDir(object):
                     else:
                         snippet = module.getAutoconfEarlySnippet()
                         lines = [ line
-                                  for line in snippet.split('\n')
+                                  for line in snippet.splitlines()
                                   if line.strip() ]
                         snippet = lines_to_multiline(lines)
                         pattern = re.compile('AC_REQUIRE\\(\\[([^()]*)\\]\\)', re.M)
@@ -581,7 +581,7 @@ class GLTestDir(object):
             if solution:
                 snippet = module.getAutoconfEarlySnippet()
                 lines = [ line
-                          for line in snippet.split('\n')
+                          for line in snippet.splitlines()
                           if line.strip() ]
                 snippet = lines_to_multiline(lines)
                 pattern = re.compile('AC_REQUIRE\\(\\[([^()]*)\\]\\)', re.M)
diff --git a/pygnulib/main.py b/pygnulib/main.py
index a3e7f86387..acf8e83e52 100644
--- a/pygnulib/main.py
+++ b/pygnulib/main.py
@@ -854,7 +854,7 @@ def main():
                             result += proc.stdout.read().decode("UTF-8")
                         os.chdir(DIRS['cwd'])
                 listing = [ line
-                            for line in result.split('\n')
+                            for line in result.splitlines()
                             if line.strip() ]
                 # Remove modules/ prefix from each file name.
                 pattern = re.compile('^modules/')
@@ -1068,7 +1068,7 @@ def main():
         args = ['find', '.', '-type', 'f', '-print']
         remaining = sp.check_output(args).decode(ENCS['shell'])
         lines = [ line.strip()
-                  for line in remaining.split('\n')
+                  for line in remaining.splitlines()
                   if line.strip() ]
         remaining = ' '.join(lines)
         if remaining:
@@ -1097,7 +1097,7 @@ def main():
         args = ['find', '.', '-type', 'f', '-print']
         remaining = sp.check_output(args).decode(ENCS['shell'])
         lines = [ line.strip()
-                  for line in remaining.split('\n')
+                  for line in remaining.splitlines()
                   if line.strip() ]
         remaining = ' '.join(lines)
         if remaining:
-- 
2.44.0

Reply via email to