stop using eval

This commit is contained in:
Bryan Stitt 2013-11-11 22:03:20 -08:00
parent d93c642c5c
commit 91061752cf
5 changed files with 16 additions and 10 deletions

View File

@ -100,7 +100,7 @@ def print_help_cb(self, opt, value, parser):
def run_command(cmd, password = None, args = []): def run_command(cmd, password = None, args = []):
cmd_runner = Commands(wallet, network) cmd_runner = Commands(wallet, network)
func = eval('cmd_runner.' + cmd) func = getattr(cmd_runner, cmd)
cmd_runner.password = password cmd_runner.password = password
try: try:
result = func(*args[1:]) result = func(*args[1:])
@ -126,7 +126,7 @@ if __name__ == '__main__':
if is_android: if is_android:
config_options = {'portable':True, 'verbose':True, 'gui':'android', 'auto_cycle':True} config_options = {'portable':True, 'verbose':True, 'gui':'android', 'auto_cycle':True}
else: else:
config_options = eval(str(options)) config_options = vars(options)
for k, v in config_options.items(): for k, v in config_options.items():
if v is None: config_options.pop(k) if v is None: config_options.pop(k)

View File

@ -39,6 +39,8 @@ class Console(QtGui.QPlainTextEdit):
def run_script(self, filename): def run_script(self, filename):
with open(filename) as f: with open(filename) as f:
script = f.read() script = f.read()
# eval is generally considered bad practice. use it wisely!
result = eval(script, self.namespace, self.namespace) result = eval(script, self.namespace, self.namespace)
@ -209,6 +211,7 @@ class Console(QtGui.QPlainTextEdit):
sys.stdout = stdoutProxy(self.appendPlainText) sys.stdout = stdoutProxy(self.appendPlainText)
try: try:
try: try:
# eval is generally considered bad practice. use it wisely!
result = eval(command, self.namespace, self.namespace) result = eval(command, self.namespace, self.namespace)
if result != None: if result != None:
if self.is_json: if self.is_json:
@ -216,6 +219,7 @@ class Console(QtGui.QPlainTextEdit):
else: else:
self.appendPlainText(repr(result)) self.appendPlainText(repr(result))
except SyntaxError: except SyntaxError:
# exec is generally considered bad practice. use it wisely!
exec command in self.namespace exec command in self.namespace
except SystemExit: except SystemExit:
self.close() self.close()

View File

@ -18,7 +18,7 @@
import threading, time, Queue, os, sys, shutil import threading, time, Queue, os, sys, shutil
from util import user_dir, appdata_dir, print_error from util import user_dir, appdata_dir, print_error, hex_to_int
from bitcoin import * from bitcoin import *
@ -118,7 +118,7 @@ class Blockchain(threading.Thread):
try: try:
assert prev_hash == header.get('prev_block_hash') assert prev_hash == header.get('prev_block_hash')
assert bits == header.get('bits') assert bits == header.get('bits')
assert eval('0x'+_hash) < target assert hex_to_int(_hash) < target
except Exception: except Exception:
return False return False
@ -149,7 +149,7 @@ class Blockchain(threading.Thread):
_hash = self.hash_header(header) _hash = self.hash_header(header)
assert previous_hash == header.get('prev_block_hash') assert previous_hash == header.get('prev_block_hash')
assert bits == header.get('bits') assert bits == header.get('bits')
assert eval('0x'+_hash) < target assert hex_to_int(_hash) < target
previous_header = header previous_header = header
previous_hash = _hash previous_hash = _hash
@ -175,7 +175,7 @@ class Blockchain(threading.Thread):
try: try:
assert prev_hash == header.get('prev_block_hash') assert prev_hash == header.get('prev_block_hash')
assert bits == header.get('bits') assert bits == header.get('bits')
assert eval('0x'+_hash) < target assert hex_to_int(_hash) < target
except Exception: except Exception:
# this can be caused by a reorg. # this can be caused by a reorg.
print_error("verify header failed"+ repr(header)) print_error("verify header failed"+ repr(header))
@ -200,7 +200,6 @@ class Blockchain(threading.Thread):
def header_from_string(self, s): def header_from_string(self, s):
hex_to_int = lambda s: eval('0x' + s[::-1].encode('hex'))
h = {} h = {}
h['version'] = hex_to_int(s[0:4]) h['version'] = hex_to_int(s[0:4])
h['prev_block_hash'] = hash_encode(s[4:36]) h['prev_block_hash'] = hash_encode(s[4:36])
@ -306,7 +305,7 @@ class Blockchain(threading.Thread):
c = c[2:] c = c[2:]
i -= 1 i -= 1
c = eval('0x'+c[0:6]) c = hex_to_int(c[0:6])
if c > 0x800000: if c > 0x800000:
c /= 256 c /= 256
i += 1 i += 1

View File

@ -112,8 +112,8 @@ class Commands:
cmd = known_commands[method] cmd = known_commands[method]
if cmd.requires_password and self.wallet.use_encryption: if cmd.requires_password and self.wallet.use_encryption:
self.password = apply(password_getter,()) self.password = apply(password_getter,())
f = eval('self.'+method) f = getattr(self, method)
result = apply(f,args) result = f(*args)
self.password = None self.password = None
if self._callback: if self._callback:
apply(self._callback, ()) apply(self._callback, ())

View File

@ -5,6 +5,9 @@ from datetime import datetime
is_verbose = True is_verbose = True
def hex_to_int(s):
return int('0x' + s[::-1].encode('hex'), 16)
class MyEncoder(json.JSONEncoder): class MyEncoder(json.JSONEncoder):
def default(self, obj): def default(self, obj):