FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

fix: The DB API Binary function accepts bytes data by jimfulton · Pull Request #630 · googleapis/python-bigquery · GitHub

This repository was archived by the owner on Mar 6, 2026. It is now read-only.
/ python-bigquery Public archive
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (2) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
20 changes: 16 additions & 4 deletions google/cloud/bigquery/dbapi/types.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,28 @@
TimestampFromTicks = datetime.datetime.fromtimestamp


def Binary(string):
def Binary(data):
"""Contruct a DB-API binary value.

Args:
string (str): A string to encode as a binary value.
data (bytes-like): An object containing binary data and that
can be converted to bytes with the `bytes` builtin.

Returns:
bytes: The UTF-8 encoded bytes representing the string.
bytes: The binary data as a bytes object.
"""
return string.encode("utf-8")
if isinstance(data, int):
# This is not the conversion we're looking for, because it
# will simply create a bytes object of the given size.
raise TypeError("cannot convert `int` object to binary")

try:
return bytes(data)
except TypeError:
if isinstance(data, str):
return data.encode("utf-8")
else:
raise


def TimeFromTicks(ticks, tz=None):
Expand Down
32 changes: 28 additions & 4 deletions tests/unit/test_dbapi_types.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import datetime
import unittest

import pytest

import google.cloud._helpers
from google.cloud.bigquery.dbapi import types

Expand All @@ -26,10 +28,6 @@ def test_binary_type(self):
self.assertEqual("STRUCT", types.BINARY)
self.assertNotEqual("STRING", types.BINARY)

def test_binary_constructor(self):
self.assertEqual(types.Binary(u"hello"), b"hello")
self.assertEqual(types.Binary(u"\u1f60"), u"\u1f60".encode("utf-8"))

def test_timefromticks(self):
somedatetime = datetime.datetime(
2017, 2, 18, 12, 47, 26, tzinfo=google.cloud._helpers.UTC
Expand All @@ -40,3 +38,29 @@ def test_timefromticks(self):
types.TimeFromTicks(ticks, google.cloud._helpers.UTC),
datetime.time(12, 47, 26, tzinfo=google.cloud._helpers.UTC),
)


class CustomBinary:
def __bytes__(self):
return b"Google"


@pytest.mark.parametrize(
"raw,expected",
[
(u"hello", b"hello"),
(u"\u1f60", u"\u1f60".encode("utf-8")),
(b"hello", b"hello"),
(bytearray(b"hello"), b"hello"),
(memoryview(b"hello"), b"hello"),
(CustomBinary(), b"Google"),
],
)
def test_binary_constructor(raw, expected):
assert types.Binary(raw) == expected


@pytest.mark.parametrize("bad", (42, 42.0, None))
def test_invalid_binary_constructor(bad):
with pytest.raises(TypeError):
types.Binary(bad)

Back | FazBrowse Home | New Git URL