File tree Expand file tree Collapse file tree 2 files changed +22
-2
lines changed Expand file tree Collapse file tree 2 files changed +22
-2
lines changed Original file line number Diff line number Diff line change @@ -809,8 +809,9 @@ def upload_from_file(self,
809
809
:rtype: :class:`gcloud.bigquery.jobs.LoadTableFromStorageJob`
810
810
:returns: the job instance used to load the data (e.g., for
811
811
querying status)
812
- :raises: :class:`ValueError` if size is not passed in and can not be
813
- determined
812
+ :raises: :class:`ValueError` if ``size`` is not passed in and can not
813
+ be determined, or if the ``file_obj`` can be detected to be
814
+ a file opened in text mode.
814
815
"""
815
816
client = self ._require_client (client )
816
817
connection = client .connection
@@ -820,6 +821,12 @@ def upload_from_file(self,
820
821
if rewind :
821
822
file_obj .seek (0 , os .SEEK_SET )
822
823
824
+ mode = getattr (file_obj , 'mode' , None )
825
+ if mode is not None and mode != 'rb' :
826
+ raise ValueError (
827
+ "Cannot upload files opened in text mode: use "
828
+ "open(filename, mode='rb')" )
829
+
823
830
# Get the basic stats about the file.
824
831
total_bytes = size
825
832
if total_bytes is None :
Original file line number Diff line number Diff line change @@ -1289,6 +1289,19 @@ def _row_data(row):
1289
1289
self .assertEqual (req ['path' ], '/%s' % PATH )
1290
1290
self .assertEqual (req ['data' ], SENT )
1291
1291
1292
+ def test_upload_from_file_text_mode_file_failure (self ):
1293
+
1294
+ class TextModeFile (object ):
1295
+ mode = 'r'
1296
+
1297
+ conn = _Connection ()
1298
+ client = _Client (project = self .PROJECT , connection = conn )
1299
+ dataset = _Dataset (client )
1300
+ file_obj = TextModeFile ()
1301
+ table = self ._makeOne (self .TABLE_NAME , dataset = dataset )
1302
+ with self .assertRaises (ValueError ):
1303
+ table .upload_from_file (file_obj , 'CSV' , size = 1234 )
1304
+
1292
1305
def test_upload_from_file_size_failure (self ):
1293
1306
conn = _Connection ()
1294
1307
client = _Client (project = self .PROJECT , connection = conn )
You can’t perform that action at this time.
0 commit comments