Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_readline(self):
f = LockedFile('test.txt', 'wb')
f.write(to_bytes('abc\n'))
f.write(to_bytes('123\n'))
f.close()
f = LockedFile('test.txt', 'rb')
rl = f.readline()
self.assertTrue(to_bytes('abc') in rl)
rl = f.readline()
self.assertTrue(to_bytes('123') in rl)
f.close()
f = LockedFile('test.txt', 'rb')
rls = f.readlines()
f.close()
self.assertEqual(len(rls), 2)
def testRun(self):
db = DAL(DEFAULT_URI, check_reserved=['all'])
rname = _quote(db, 'a very complicated fieldname')
for ft in ['string', 'text', 'password', 'upload', 'blob']:
db.define_table('tt', Field('aa', ft, default='', rname=rname))
cv = 'x'
self.assertEqual(isinstance(db.tt.insert(aa='x'), long), True)
if IS_MONGODB and not PY2 and ft == 'blob':
cv = to_bytes(cv)
self.assertEqual(db().select(db.tt.aa)[0].aa, cv)
drop(db.tt)
db.define_table('tt', Field('aa', 'integer', default=1, rname=rname))
self.assertEqual(isinstance(db.tt.insert(aa=3), long), True)
self.assertEqual(db().select(db.tt.aa)[0].aa, 3)
drop(db.tt)
db.define_table('tt', Field('aa', 'double', default=1, rname=rname))
self.assertEqual(isinstance(db.tt.insert(aa=3.1), long), True)
self.assertEqual(db().select(db.tt.aa)[0].aa, 3.1)
drop(db.tt)
db.define_table('tt', Field('aa', 'boolean', default=True, rname=rname))
self.assertEqual(isinstance(db.tt.insert(aa=True), long), True)
self.assertEqual(db().select(db.tt.aa)[0].aa, True)
drop(db.tt)
db.define_table('tt', Field('aa', 'json', default={}, rname=rname))
self.assertEqual(isinstance(db.tt.insert(aa={}), long), True)
def test_readline(self):
f = LockedFile('test.txt', 'wb')
f.write(to_bytes('abc\n'))
f.write(to_bytes('123\n'))
f.close()
f = LockedFile('test.txt', 'rb')
rl = f.readline()
self.assertTrue(to_bytes('abc') in rl)
rl = f.readline()
self.assertTrue(to_bytes('123') in rl)
f.close()
f = LockedFile('test.txt', 'rb')
rls = f.readlines()
f.close()
self.assertEqual(len(rls), 2)
def worker(fh):
time.sleep(2)
fh.close()
f = LockedFile('test.txt', mode='wb')
f.write(to_bytes('test ok'))
t1 = threading.Thread(target=worker, args=(f, ))
t1.start()
start = int(time.time())
content = read_locked('test.txt')
end = int(time.time())
t1.join()
# it took at least 2 seconds to read
self.assertTrue(end - start >= 2)
self.assertEqual(content, to_bytes('test ok'))
self.formkey = None
self.cached_helper = None
if readonly or request.method=='GET':
if self.record:
self.vars = self.record
else:
post_vars = request.forms
self.submitted = True
process = False
# we only a process a form if it is POST and the formkey matches (correct formname and crsf)
# notice we never expose the crsf uuid, we only use to sign the form uuid
if request.method == 'POST':
if csrf_uuid:
code, signature = post_vars['_formkey'].split('/')
expected = hmac.new(to_bytes(csrf_uuid), to_bytes(self.form_name+'/'+code)).hexdigest()
if signature == expected:
process = True
elif post_vars.get('_formkey') == self.form_name:
process = True
if process:
if not post_vars.get('_delete'):
for field in self.table:
if field.writable:
value = post_vars.get(field.name)
# FIX THIS deal with set_self_id before validate
(value, error) = field.validate(value)
if field.type == 'upload':
delete = post_vars.get('_delete_'+field.name)
if value is not None and hasattr(value,'file'):
value = field.store(value.file,
value.filename,
def log(self, message, table=None):
isabs = None
logfilename = self.adapter.adapter_args.get("logfile", "sql.log")
writelog = bool(logfilename)
if writelog:
isabs = os.path.isabs(logfilename)
if table and table._dbt and writelog and self.adapter.folder:
if isabs:
table._loggername = logfilename
else:
table._loggername = pjoin(self.adapter.folder, logfilename)
logfile = self.file_open(table._loggername, "ab")
logfile.write(to_bytes(message))
self.file_close(logfile)
def _blob(self, value):
if isinstance(value, basestring) and value == '':
value = None
return MongoBlob(value) if PY2 else to_bytes(value)
return self.custom_retrieve(name, path)
if self.authorize or isinstance(self_uploadfield, str):
row = self.db(self == name).select().first()
if not row:
raise NotFoundException
if self.authorize and not self.authorize(row):
raise NotAuthorizedException
file_properties = self.retrieve_file_properties(name, path)
filename = file_properties["filename"]
if isinstance(self_uploadfield, str): # ## if file is in DB
stream = BytesIO(to_bytes(row[self_uploadfield] or ""))
elif isinstance(self_uploadfield, Field):
blob_uploadfield_name = self_uploadfield.uploadfield
query = self_uploadfield == name
data = self_uploadfield.table(query)[blob_uploadfield_name]
stream = BytesIO(to_bytes(data))
elif self.uploadfs:
# ## if file is on pyfilesystem
stream = self.uploadfs.open(text_type(name), "rb")
else:
# ## if file is on regular filesystem
# this is intentionally a string with filename and not a stream
# this propagates and allows stream_file_or_304_or_206 to be called
fullname = pjoin(file_properties["path"], name)
if nameonly:
return (filename, fullname)
stream = open(fullname, "rb")
return (filename, stream)
def _blob(self, value):
decoded = b64decode(to_bytes(value))
try:
decoded = to_native(decoded)
except:
pass
return decoded
def adapt(self, value):
if PY2:
if not isinstance(value, string_types):
value = str(value)
value = to_bytes(value)
try:
value.decode(self.adapter.db_codec)
except:
value = value.decode("latin1").encode(self.adapter.db_codec)
else:
value = to_unicode(value)
return self.adapter.adapt(value)