@@ -189,13 +189,48 @@ def CheckOptimizedUnicode(self):
189189 def tearDown (self ):
190190 self .con .close ()
191191
192+ class TextFactoryTestsWithEmbeddedZeroBytes (unittest .TestCase ):
193+ def setUp (self ):
194+ self .con = sqlite .connect (":memory:" )
195+ self .con .execute ("create table test (value text)" )
196+ self .con .execute ("insert into test (value) values (?)" , ("a\x00 b" ,))
197+
198+ def CheckString (self ):
199+ # text_factory defaults to str
200+ row = self .con .execute ("select value from test" ).fetchone ()
201+ self .assertIs (type (row [0 ]), str )
202+ self .assertEqual (row [0 ], "a\x00 b" )
203+
204+ def CheckBytes (self ):
205+ self .con .text_factory = bytes
206+ row = self .con .execute ("select value from test" ).fetchone ()
207+ self .assertIs (type (row [0 ]), bytes )
208+ self .assertEqual (row [0 ], b"a\x00 b" )
209+
210+ def CheckBytearray (self ):
211+ self .con .text_factory = bytearray
212+ row = self .con .execute ("select value from test" ).fetchone ()
213+ self .assertIs (type (row [0 ]), bytearray )
214+ self .assertEqual (row [0 ], b"a\x00 b" )
215+
216+ def CheckCustom (self ):
217+ # A custom factory should receive a bytes argument
218+ self .con .text_factory = lambda x : x
219+ row = self .con .execute ("select value from test" ).fetchone ()
220+ self .assertIs (type (row [0 ]), bytes )
221+ self .assertEqual (row [0 ], b"a\x00 b" )
222+
223+ def tearDown (self ):
224+ self .con .close ()
225+
192226def suite ():
193227 connection_suite = unittest .makeSuite (ConnectionFactoryTests , "Check" )
194228 cursor_suite = unittest .makeSuite (CursorFactoryTests , "Check" )
195229 row_suite_compat = unittest .makeSuite (RowFactoryTestsBackwardsCompat , "Check" )
196230 row_suite = unittest .makeSuite (RowFactoryTests , "Check" )
197231 text_suite = unittest .makeSuite (TextFactoryTests , "Check" )
198- return unittest .TestSuite ((connection_suite , cursor_suite , row_suite_compat , row_suite , text_suite ))
232+ text_zero_bytes_suite = unittest .makeSuite (TextFactoryTestsWithEmbeddedZeroBytes , "Check" )
233+ return unittest .TestSuite ((connection_suite , cursor_suite , row_suite_compat , row_suite , text_suite , text_zero_bytes_suite ))
199234
200235def test ():
201236 runner = unittest .TextTestRunner ()
0 commit comments