eval

How to rescue an eval in Ruby?

£可爱£侵袭症+ 提交于 2019-12-08 23:10:58
问题 I'm trying to figure out how to rescue syntax errors that come up when eval() ing code in Ruby 1.8.6. I would expect the following Ruby code: #!/usr/bin/ruby good_str = "(1+1)" bad_str = "(1+1" # syntax error: missing closing paren begin puts eval(good_str) puts eval(bad_str) rescue => exc puts "RESCUED!" end to produce the following result when run: 2 RESCUED! Instead, what I get is: 2 eval_rescue.rb:8: (eval):1: compile error (SyntaxError) (eval):1: syntax error, unexpected $end, expecting

python中eval不安全功能实验

最后都变了- 提交于 2019-12-08 22:41:52
一、查看系统中的文件目录的内容 二、查看磁盘使用情况 三、甚至可以更改文件内容 四、进行删除操作 总结: eval可以用来执行任何有权限执行的脚本,存在巨大的安全性问题,一定要慎用 来源: 51CTO 作者: wx5dcb7577ac572 链接: https://blog.51cto.com/14612701/2452494

How to evaluate a custom math expression in Python

廉价感情. 提交于 2019-12-08 19:44:44
问题 I'm writing a custom dice rolling parser (snicker if you must) in python. Basically, I want to use standard math evaluation but add the 'd' operator: #xdy sum = 0 for each in range(x): sum += randInt(1, y) return sum So that, for example, 1d6+2d6+2d6-72+4d100 = (5)+(1+1)+(6+2)-72+(5+39+38+59) = 84 I was using regex to replace all 'd's with the sum and then using eval, but my regex fell apart when dealing with parentheses on either side. Is there a faster way to go about this than implementing

Python 2 list comprehension and eval

你离开我真会死。 提交于 2019-12-08 19:14:28
问题 How do you have a multiple line statement in either a list comprehension or eval? I was trying to turn this code: def f(x, y, b=''): for i in x: if i in y: y.remove(i) i *= 2 b += i return b Into a lambda function like so: j=lambda x,y:''.join(eval('y.remove(i);i*2')if i in y else i for i in x) In both x is a string such as 'onomatopoeia' and y is a list such as ['o','a','o'] . But for some reason, it returns a syntax error. Can anyone explain this? 回答1: First, you probably shouldn't rewrite

js 对象 / json / jsonb / jsonp 区别

情到浓时终转凉″ 提交于 2019-12-08 18:01:25
一、JSON vs JS 对象 1、区别 区别 Javascript 对象 Json 含义 对象的实例 一种数据格式(序列化格式) 传输 不能传输 可以 跨平台 传输,轻量级 格式 1.键不加引号、加单引号、双引号都行 2.值可以是函数、对象、字符串、数字、boolean 等 1. 键必须得加双引号 2. 值不能为函数/undefined/NaN 注: 序列化格式 :早期有 XML,后来基于 javascript 诞生了更轻量的 JSON,再后来还有YAML。 针对于科学使用的大量数据集合,例如气候,海洋模型和卫星数据,开发了特定的二进制序列化标准,例如HDF,netCDF和较旧的GRIB。 2、序列化 (1)序列化操作 正序列化 - JSON.stringify() 反序列化 - JSON.parse() 、(不推荐)eval() // 初始化 JS 对象 var obj_origin = { a: 1, b: "2", 'c': 3 } console.log(obj_origin) // { a: 1, b: '2', c: 3 } // 正序列化 var objStr = JSON.stringify(obj_origin) console.log(objStr) // {"a":1,"b":"2","c":3} // 反序列化 var obj = JSON.parse

In node.js, How do you check whether a given string of code is syntactically correct in the most lightweight way?

喜欢而已 提交于 2019-12-08 17:47:25
Imagine that I accept a piece of code from a user and want to just check whether the given string is a valid JS or not? Just from the syntax perspective. function checkCode(x){ // Logic } // returns a boolean, whether 'x' is syntactically right or wrong. I don't want solutions with eval , since the whole nodejs process gets in to a syntax error when the given code, 'x' is syntactically wrong. Don't use eval that is literally the same as handing over the control of your server to the public internet. Anyone can do anything with your server - delete files, leak files, send spam email and so on.

PHP keep me from eval ;) Variables inside string

落花浮王杯 提交于 2019-12-08 14:22:32
For reasons I'd rather not get into right now, I have a string like so: <div>$title</div> that gets stored in a database using mysql_real_escape_string . During normal script execution, that string gets parsed and stored in a variable $string and then gets sent to a function($string) . In this function, I am trying to: function test($string){ $title = 'please print'; echo $string; } //I want the outcome to be <div>please print</div> This seems like the silliest thing, but for the life of me, I cannot get it to "interpret" the variables. I've also tried, echo html_entity_decode($string); echo

force-evaluate an object being passed as an argument to a function

筅森魡賤 提交于 2019-12-08 11:29:42
问题 I would like to pass the value of an object as an argument to a function. # This is my object anObject <- "an_unkown_string" # I would like to do the equivalent of: someFunc("an_unkown_string") # .. by somehow calling on the object containing the string someFunc( ??? (anObject) ) For example, with the sample function below (based on save() ): someFunc <- function(...) { names <- as.character(substitute(list(...)))[-1L] return(names) } # Ideally, the output would be: someFunc( ??? (anObject) )

How to parse a variable number of command line arguments with quotes into eval?

血红的双手。 提交于 2019-12-08 10:14:01
问题 I got this simple script: #!/usr/bin/env bash eval "${@:2}" while [ true ] do FocusApp=`xdotool getwindowfocus getwindowname` if [[ "$FocusApp" == *"$1"* ]]; then wmctrl -ir $(xdotool getactivewindow) -b add,maximized_vert,maximized_horz break fi done I run it like this: $ ./maximize.sh "Sublime Text" /usr/bin/subl -n "./My File With Spaces in the Name" But when I run it, Sublime Text tries to open a file named My , another named File , and etc. If I replace the eval "${@:2}" with: eval "\"$2

Defining a bunch of functions with eval (PHP)

喜你入骨 提交于 2019-12-08 09:21:21
问题 Is this anywhere near something acceptable? I need a function for each HTML tag, and they need to be defined for later use in a Heredoc string. This is my code so far. <?php $tags = array (h1, h2, h3, b, i); foreach ($tags as $key => $value) { eval ('function '.$value.' ($str) { return "<'.$value.'>$str</'.$value.'>"; }'); } This basically takes care of the Heredoc problem concerning functions within heredoc. A quick example: <<<example <h1>This is ordinary HTML</h1> {$h1('This is HTML via.