Skip to content
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

Builds fails if stdin is redirected to empty input #469

Closed
Jenselme opened this issue Dec 4, 2015 · 4 comments
Closed

Builds fails if stdin is redirected to empty input #469

Jenselme opened this issue Dec 4, 2015 · 4 comments

Comments

@Jenselme
Copy link

Jenselme commented Dec 4, 2015

Hi,

While packaging csvkit for fedora and OpenSuse, we (I and @tigerfoot) found out that tests that relying on stdin fail with this message:

[   31s] ======================================================================
[   31s] ERROR: test_no_inference (tests.test_utilities.test_csvsql.TestCSVSQL)
[   31s] ----------------------------------------------------------------------
[   31s] Traceback (most recent call last):
[   31s]   File "/home/abuild/rpmbuild/BUILD/csvkit-0.9.1/tests/test_utilities/test_csvsql.py", line 37, in test_no_inference
[   31s]     utility.main()
[   31s]   File "/home/abuild/rpmbuild/BUILD/csvkit-0.9.1/csvkit/utilities/csvsql.py", line 105, in main
[   31s]     **self.reader_kwargs
[   31s]   File "/home/abuild/rpmbuild/BUILD/csvkit-0.9.1/csvkit/table.py", line 223, in from_csv
[   31s]     headers = next(rows)
[   31s]   File "/home/abuild/rpmbuild/BUILD/csvkit-0.9.1/csvkit/py3.py", line 21, in __next__
[   31s]     return next(self.reader)
[   31s] StopIteration

From what we figured out, the problem is that you add sys.stdin to the list of input files if the script is not run on a tty. But on the packages build system, stdin is redirected to something empty. This causes the build to fail.

Below is the patch that allow us to solve this issue. Is it really needed to add sys.stdin to input files?

diff -rup a/csvkit/utilities/csvsql.py b/csvkit/utilities/csvsql.py
--- a/csvkit/utilities/csvsql.py    2015-11-19 19:37:48.000000000 +0100
+++ b/csvkit/utilities/csvsql.py    2015-11-30 18:19:56.369200131 +0100
@@ -54,14 +54,6 @@ class CSVSQL(CSVKitUtility):
         else:
             table_names = []

-        # If one or more filenames are specified, we need to add stdin ourselves (if available)
-        if sys.stdin not in self.input_files:
-            try:
-                if not sys.stdin.isatty():
-                    self.input_files.insert(0, sys.stdin)
-            except:
-                pass
-
         # Create an SQLite database in memory if no connection string is specified
         if query and not connection_string:
             connection_string = "sqlite:///:memory:"
Only in b/csvkit/utilities: csvsql.py.orig
diff -rup a/tests/test_utilities/test_csvsql.py b/tests/test_utilities/test_csvsql.py
--- a/tests/test_utilities/test_csvsql.py   2015-11-19 19:37:48.000000000 +0100
+++ b/tests/test_utilities/test_csvsql.py   2015-11-30 18:18:43.737197120 +0100
@@ -90,7 +90,8 @@ class TestCSVSQL(unittest.TestCase):

             sql = output_file.getvalue()

-            self.assertTrue('CREATE TABLE stdin' in sql)
+    # This one doesn't work on build systems koji,obs etc
+   #        self.assertTrue('CREATE TABLE stdin' in sql)
             self.assertTrue('CREATE TABLE dummy' in sql)

     def test_query(self):

Originaly reported on OpenSuse packaging. See: http://opensuse.14.x6.nabble.com/python3-test-a-strange-failure-only-during-build-td5052784.html.

@jpmckinney
Copy link
Member

How can I reproduce this error? (without setting up an OpenSUSE build system).

@jpmckinney
Copy link
Member

Introduced in #276

Reproducible with:

< /dev/null csvsql -v --query "select a from stdin"

@onyxfish
Copy link
Collaborator

Paging @jeroenjanssens: do you recall the specific reason for this block of code?

I think maybe it had something to do with using a piped file and a filesystem file in the same process, though that should also be possible using the - magic filename.

jpmckinney pushed a commit that referenced this issue Jan 23, 2016
@jpmckinney
Copy link
Member

Fixed in #510 without figuring out csvsql's logic.

jpmckinney pushed a commit that referenced this issue Jan 25, 2016
Don't error when reading from /dev/null, fixes #469
lcorbasson pushed a commit to lcorbasson/csvkit that referenced this issue Sep 7, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants