Issue #16121: Fix line number accounting in shlex
diff --git a/Lib/shlex.py b/Lib/shlex.py
index e7c8acc..6114d2d 100644
--- a/Lib/shlex.py
+++ b/Lib/shlex.py
@@ -48,6 +48,7 @@
         self.state = ' '
         self.pushback = deque()
         self.lineno = 1
+        self._lines_found = 0
         self.debug = 0
         self.token = ''
         self.filestack = deque()
@@ -118,12 +119,23 @@
         return raw
 
     def read_token(self):
+        if self._lines_found:
+            self.lineno += self._lines_found
+            self._lines_found = 0
+
+        i = 0
         quoted = False
         escapedstate = ' '
         while True:
+            i += 1
             nextchar = self.instream.read(1)
             if nextchar == '\n':
-                self.lineno = self.lineno + 1
+                # In case newline is the first character increment lineno
+                if i == 1:
+                    self.lineno += 1
+                else:
+                    self._lines_found += 1
+
             if self.debug >= 3:
                 print "shlex: in state", repr(self.state), \
                       "I see character:", repr(nextchar)
@@ -143,6 +155,7 @@
                         continue
                 elif nextchar in self.commenters:
                     self.instream.readline()
+                    # Not considered a token so incrementing lineno directly
                     self.lineno = self.lineno + 1
                 elif self.posix and nextchar in self.escape:
                     escapedstate = 'a'
@@ -210,6 +223,7 @@
                         continue
                 elif nextchar in self.commenters:
                     self.instream.readline()
+                    # Not considered a token so incrementing lineno directly
                     self.lineno = self.lineno + 1
                     if self.posix:
                         self.state = ' '
diff --git a/Lib/test/test_shlex.py b/Lib/test/test_shlex.py
index 6c35f49..2325651 100644
--- a/Lib/test/test_shlex.py
+++ b/Lib/test/test_shlex.py
@@ -178,6 +178,15 @@
                              "%s: %s != %s" %
                              (self.data[i][0], l, self.data[i][1:]))
 
+    def testLineNumbers(self):
+        data = '"a \n b \n c"\n"x"\n"y"'
+        for is_posix in (True, False):
+            s = shlex.shlex(data, posix=is_posix)
+            for i in (1, 4, 5):
+                s.read_token()
+                self.assertEqual(s.lineno, i)
+
+
 # Allow this test to be used with old shlex.py
 if not getattr(shlex, "split", None):
     for methname in dir(ShlexTest):