How to use patch_builtins method in Testify

Best Python code snippet using Testify_python

patch.py

Source:patch.py Github

copy

Full Screen

1import jlist as jl2def patch_builtins(*, include_type=False):3 """Replace ``builtins.all`` with ``jlist.all`` and ``builtins.any`` with4 ``jlist.any``.5 Parameters6 ----------7 include_type : bool, optional8 Also replace ``builtins.list`` with ``jlist.jlist``.9 Examples10 --------11 >>> list12 list13 >>> from jlist.patch import patch_builtins14 >>> patch_builtins()15 >>> list16 <class 'jlist.jlist'>17 """18 import builtins19 if include_type:20 builtins.list = jl.jlist21 builtins.all = jl.all22 builtins.any = jl.any23try:24 import codetransformer25except ImportError as e:26 HAVE_CODETRANSFORMER = False27 def _f(name, e=e):28 def f():29 raise ImportError(f'{name} requires codetransformer') from e30 return f31 overloaded_literals = _f('overloaded_literals')32 patch_literals = _f('patch_literals')33 patch_all = _f('patch_all')34 del _f35else:36 import ast37 from functools import wraps38 import operator39 import sys40 from codetransformer import pattern, instructions as instrs41 HAVE_CODETRANSFORMER = True42 class JListLiterals(codetransformer.CodeTransformer):43 IN_COMPREHENSION = 'in_comprehension'44 @pattern(45 instrs.BUILD_LIST,46 codetransformer.matchany[codetransformer.var],47 instrs.LIST_APPEND48 )49 def _list_append(self, *instrs):50 self.begin(self.IN_COMPREHENSION)51 yield from self.patterndispatcher(instrs)52 @pattern(53 instrs.BUILD_LIST,54 instrs.LOAD_FAST,55 startcodes=(IN_COMPREHENSION,),56 )57 def _build_list_in_comprehension(self, build_instr, load_instr):58 yield instrs.LOAD_CONST(jl.jlist).steal(build_instr)59 yield instrs.CALL_FUNCTION(0)60 yield instrs.DUP_TOP()61 yield instrs.DUP_TOP()62 # TOS = <jlist>63 # TOS1 = <jlist>64 # TOS2 = <jlist>65 yield instrs.LOAD_ATTR('append')66 yield instrs.STORE_FAST('.append')67 # TOS = <jlist>68 # TOS1 = <jlist>69 yield load_instr70 # TOS = .071 # TOS1 = <jlist>72 # TOS2 = <jlist>73 yield instrs.DUP_TOP()74 # TOS = .075 # TOS1 = .076 # TOS2 = <jlist>77 # TOS3 = <jlist>78 yield instrs.ROT_THREE()79 # TOS = .080 # TOS1 = <jlist>81 # TOS2 = .082 # TOS3 = <jlist>83 yield instrs.LOAD_CONST(operator.length_hint)84 yield instrs.ROT_TWO()85 yield instrs.CALL_FUNCTION(1)86 # TOS = <length_hint>87 # TOS1 = <jlist>88 # TOS2 = .089 # TOS3 = <jlist>90 yield instrs.ROT_TWO()91 # TOS = <jlist>92 # TOS1 = <length_hint>93 # TOS2 = .094 # TOS3 = <jlist>95 if sys.version_info >= (3, 7):96 yield instrs.LOAD_METHOD('_reserve')97 # TOS = <jlist._reserve>98 # TOS1 = <length_hint>99 # TOS2 = .0100 # TOS3 = <jlist>101 yield instrs.ROT_TWO()102 # TOS = <length_hint>103 # TOS1 = <jlist._reserve>104 # TOS2 = .0105 # TOS3 = <jlist>106 yield instrs.CALL_METHOD(1)107 # TOS = None108 # TOS1 = .0109 # TOS3 = <jlist>110 else:111 yield instrs.LOAD_ATTR('_reserve')112 # TOS = <jlist._reserve>113 # TOS1 = <length_hint>114 # TOS2 = .0115 # TOS3 = <jlist>116 yield instrs.ROT_TWO()117 # TOS = <length_hint>118 # TOS1 = <jlist._reserve>119 # TOS2 = .0120 # TOS3 = <jlist>121 yield instrs.CALL_FUNCTION(1)122 # TOS = None123 # TOS1 = .0124 # TOS3 = <jlist>125 yield instrs.POP_TOP()126 # TOS = .0127 # TOS1 = <jlist>128 @pattern(instrs.LIST_APPEND, startcodes=(IN_COMPREHENSION,))129 def _list_append_in_comprehension(self, instr):130 yield instrs.LOAD_FAST('.append').steal(instr)131 yield instrs.ROT_TWO()132 yield instrs.CALL_FUNCTION(1)133 yield instrs.POP_TOP()134 @pattern(instrs.BUILD_LIST)135 def _build_list(self, instr):136 if instr.arg == 0:137 yield instrs.LOAD_CONST(jl.jlist).steal(instr)138 yield instrs.CALL_FUNCTION(0)139 elif instr.arg == 1:140 yield instrs.LOAD_CONST(jl.jlist._from_starargs).steal(instr)141 yield instrs.ROT_TWO()142 yield instrs.CALL_FUNCTION(1)143 elif instr.arg == 2:144 yield instrs.LOAD_CONST(jl.jlist._from_starargs).steal(instr)145 yield instrs.ROT_THREE()146 yield instrs.CALL_FUNCTION(2)147 else:148 yield instr149 yield instrs.LOAD_CONST(jl.jlist)150 yield instrs.ROT_TWO()151 yield instrs.CALL_FUNCTION(1)152 overloaded_literals = JListLiterals()153 _original_compile = compile154 @wraps(_original_compile)155 def _patched_compile(source,156 filename,157 mode,158 flags=0,159 dont_inherit=False,160 optimize=-1):161 if flags & ast.PyCF_ONLY_AST:162 return _original_compile(163 source,164 filename,165 mode,166 flags,167 dont_inherit,168 optimize,169 )170 code = codetransformer.Code.from_pycode(_original_compile(171 source,172 filename,173 mode,174 flags,175 dont_inherit,176 optimize,177 ))178 return overloaded_literals.transform(code).to_pycode()179 def patch_literals():180 """Convert list literals into ``jlist.jlist`` objects.181 Examples182 --------183 >>> [1, 2, 3]184 [1, 2, 3]185 >>> [x for x in range(3)]186 [1, 2, 3]187 >>> from jlist.patch import patch_literals188 >>> patch_literals()189 >>> [1, 2, 3]190 jlist([1, 2, 3])191 >>> [x for x in range(3)]192 jlist([1, 2, 3])193 """194 import builtins195 builtins.compile = _patched_compile196 def patch_all():197 """Patch ``builtins.list`` and list literal objects.198 See Also199 --------200 jlist.patch.patch_builtins201 jlist.patch.patch_literals202 """203 patch_builtins(include_type=True)...

Full Screen

Full Screen

__init__.py

Source:__init__.py Github

copy

Full Screen

1"""2pylib: rapid testing and development utils3this module uses apipkg.py for lazy-loading sub modules4and classes. The initpkg-dictionary below specifies5name->value mappings where value can be another namespace6dictionary or an import path.7(c) Holger Krekel and others, 2004-20148"""9from py._error import error10try:11 from py._vendored_packages import apipkg12 lib_not_mangled_by_packagers = True13 vendor_prefix = '._vendored_packages.'14except ImportError:15 import apipkg16 lib_not_mangled_by_packagers = False17 vendor_prefix = ''18try:19 from ._version import version as __version__20except ImportError:21 # broken installation, we don't even try22 __version__ = "unknown"23apipkg.initpkg(__name__, attr={'_apipkg': apipkg, 'error': error}, exportdefs={24 # access to all standard lib modules25 'std': '._std:std',26 '_pydir' : '.__metainfo:pydir',27 'version': 'py:__version__', # backward compatibility28 # pytest-2.0 has a flat namespace, we use alias modules29 # to keep old references compatible30 'test' : 'pytest',31 # hook into the top-level standard library32 'process' : {33 '__doc__' : '._process:__doc__',34 'cmdexec' : '._process.cmdexec:cmdexec',35 'kill' : '._process.killproc:kill',36 'ForkedFunc' : '._process.forkedfunc:ForkedFunc',37 },38 'apipkg' : {39 'initpkg' : vendor_prefix + 'apipkg:initpkg',40 'ApiModule' : vendor_prefix + 'apipkg:ApiModule',41 },42 'iniconfig' : {43 'IniConfig' : vendor_prefix + 'iniconfig:IniConfig',44 'ParseError' : vendor_prefix + 'iniconfig:ParseError',45 },46 'path' : {47 '__doc__' : '._path:__doc__',48 'svnwc' : '._path.svnwc:SvnWCCommandPath',49 'svnurl' : '._path.svnurl:SvnCommandPath',50 'local' : '._path.local:LocalPath',51 'SvnAuth' : '._path.svnwc:SvnAuth',52 },53 # python inspection/code-generation API54 'code' : {55 '__doc__' : '._code:__doc__',56 'compile' : '._code.source:compile_',57 'Source' : '._code.source:Source',58 'Code' : '._code.code:Code',59 'Frame' : '._code.code:Frame',60 'ExceptionInfo' : '._code.code:ExceptionInfo',61 'Traceback' : '._code.code:Traceback',62 'getfslineno' : '._code.source:getfslineno',63 'getrawcode' : '._code.code:getrawcode',64 'patch_builtins' : '._code.code:patch_builtins',65 'unpatch_builtins' : '._code.code:unpatch_builtins',66 '_AssertionError' : '._code.assertion:AssertionError',67 '_reinterpret_old' : '._code.assertion:reinterpret_old',68 '_reinterpret' : '._code.assertion:reinterpret',69 '_reprcompare' : '._code.assertion:_reprcompare',70 '_format_explanation' : '._code.assertion:_format_explanation',71 },72 # backports and additions of builtins73 'builtin' : {74 '__doc__' : '._builtin:__doc__',75 'enumerate' : '._builtin:enumerate',76 'reversed' : '._builtin:reversed',77 'sorted' : '._builtin:sorted',78 'any' : '._builtin:any',79 'all' : '._builtin:all',80 'set' : '._builtin:set',81 'frozenset' : '._builtin:frozenset',82 'BaseException' : '._builtin:BaseException',83 'GeneratorExit' : '._builtin:GeneratorExit',84 '_sysex' : '._builtin:_sysex',85 'print_' : '._builtin:print_',86 '_reraise' : '._builtin:_reraise',87 '_tryimport' : '._builtin:_tryimport',88 'exec_' : '._builtin:exec_',89 '_basestring' : '._builtin:_basestring',90 '_totext' : '._builtin:_totext',91 '_isbytes' : '._builtin:_isbytes',92 '_istext' : '._builtin:_istext',93 '_getimself' : '._builtin:_getimself',94 '_getfuncdict' : '._builtin:_getfuncdict',95 '_getcode' : '._builtin:_getcode',96 'builtins' : '._builtin:builtins',97 'execfile' : '._builtin:execfile',98 'callable' : '._builtin:callable',99 'bytes' : '._builtin:bytes',100 'text' : '._builtin:text',101 },102 # input-output helping103 'io' : {104 '__doc__' : '._io:__doc__',105 'dupfile' : '._io.capture:dupfile',106 'TextIO' : '._io.capture:TextIO',107 'BytesIO' : '._io.capture:BytesIO',108 'FDCapture' : '._io.capture:FDCapture',109 'StdCapture' : '._io.capture:StdCapture',110 'StdCaptureFD' : '._io.capture:StdCaptureFD',111 'TerminalWriter' : '._io.terminalwriter:TerminalWriter',112 'ansi_print' : '._io.terminalwriter:ansi_print',113 'get_terminal_width' : '._io.terminalwriter:get_terminal_width',114 'saferepr' : '._io.saferepr:saferepr',115 },116 # small and mean xml/html generation117 'xml' : {118 '__doc__' : '._xmlgen:__doc__',119 'html' : '._xmlgen:html',120 'Tag' : '._xmlgen:Tag',121 'raw' : '._xmlgen:raw',122 'Namespace' : '._xmlgen:Namespace',123 'escape' : '._xmlgen:escape',124 },125 'log' : {126 # logging API ('producers' and 'consumers' connected via keywords)127 '__doc__' : '._log:__doc__',128 '_apiwarn' : '._log.warning:_apiwarn',129 'Producer' : '._log.log:Producer',130 'setconsumer' : '._log.log:setconsumer',131 '_setstate' : '._log.log:setstate',132 '_getstate' : '._log.log:getstate',133 'Path' : '._log.log:Path',134 'STDOUT' : '._log.log:STDOUT',135 'STDERR' : '._log.log:STDERR',136 'Syslog' : '._log.log:Syslog',137 },...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Testify automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful