How to use py3to2 method in Robotframework

Best Python code snippet using robotframework

recipe-574437.py

Source:recipe-574437.py Github

copy

Full Screen

...187 # compile, expand, recompile, exec188 if PYVERSION == 'py2x': print s189 if 0: print s190 else:191 t = t0.py3to2() if hasattr(t0, 'py3to2') else t0; xxx = t; s = repr(t)192if 0: TESTCODE = 'a, *b = 1, 2, 3; print(a, b)' # pep3132 - Extended Iterable Unpacking193if 0: TESTCODE = """194?z = 3195?def foo(x, *y): return x, y, z196?print foo(1)197"""198if 1: TESTCODE = """199?def foo(x):200? def bar(): nonlocal x; x += 1201? bar(); return x202?print(foo(1))""" # nonlocal statement203TESTCODE = TESTCODE.replace('?', '')204# end common205# beg py2x init206from __future__ import nested_scopes, generators, division, absolute_import, with_statement; import py3to2; from py3to2 import *207import __builtin__; sys.modules['builtins'] = builtins = __builtin__; builtins.bytes = str208import itertools; filter, map, range, zip = itertools.ifilter, itertools.imap, xrange, itertools.izip # iterators209execsrc('common')210import mmap, signal, subprocess, time211class _server(object): # python3k server which byte compiles 3k code212 bufsize = 1<<20213 @staticmethod214 def restart(restart = True):215 self = _server216 # def restart(self, restart = True):217 global SERVERIO, SERVER218 if restart:219 if 'SERVERIO' in globals():220 for x in SERVERIO: os.close(x)221 SERVERIO = os.pipe()222 print 'py3to2 server restarting with io: %s'%(SERVERIO, )223 if 'SERVER' in globals(): self.kill()224 SERVER = subprocess.Popen('python3k -i', stdin = subprocess.PIPE, stdout = SERVERIO[1], stderr = subprocess.STDOUT, shell = True)225 SERVER.stdin.write('import py3to2; reload(py3to2); from py3to2 import *\n')226 SERVER.stdin.write('CLIENTPID = %i\n'%PID)227 @staticmethod228 def kill():229 # def kill(self):230 global SERVER231 try: os.kill(SERVER.pid, signal.SIGTERM)232 except Exception, exc: print exc233 del SERVER234 @staticmethod235 def input(s, stdout = True, timeout = 2,236 prompt = rgx2nd(('(.+)', 'py3to2 server: \\1'), ),237 subprompt = rgx2nd(('>>> ', '', None), ),238 fndexc = re.compile('\WTraceback '),239 ):240 self = _server241 if not s: return242 SERVER.stdin.write(s)243 try:244 buf = ''245 SERVER.stdin.write("\n\nimport os, signal; os.kill(CLIENTPID, signal.SIGINT)\n")246 time.sleep(timeout)247 raise IOError('py3to2 server not responding to input: %s'%repr(s))248 except KeyboardInterrupt:249 buf = os.read(SERVERIO[0], self.bufsize)250 buf = subprompt.sub(buf)251 if prompt: buf = prompt.sub(buf)252 if fndexc.search(buf): raise IOError('py3to2 server input: %s\n%s'%(s, buf))253 if stdout: sys.stdout.write(buf)254 else: return buf255 @staticmethod256 def get(varname, **kwds):257 self = _server258 return self.input("print(%s)"%varname, stdout = None, prompt = None, **kwds)259 def __init__(self, restart = None):260 global SERVER261 self.restart(restart = 'SERVER' not in globals())262 self.input('\n')263 @staticmethod264 def test():265 self = _server266 global xxx267 if 1: # server init268 self.restart()269 self.input("1, 2;3")270 if 0: # py3k codetree271 server.input("codetree.test()")272 if 1: # 3to2273 s = server.get('xxx')274 t = eval(s)275 c = t.compile()276 print dis.dis(c)277 exec(c, globals())278server = _server()279if 1:280 def builtinsadd(f): setattr(__builtin__, f.__name__, f); return f281 @builtinsadd282 def print_py3k(*args, **kwds):283 sep = kwds['sep'] if 'sep' in kwds else ' '284 end = kwds['end'] if 'end' in kwds else '\n'285 file = kwds['file'] if 'file' in kwds else sys.stdout286 s = sep.join(str(x) for x in args)287 file.write(s)288 file.write(end)289 @builtinsadd290 def compile_py3k(source, filename, mode, *args, **kwds):291 """292py3k example:293import py3to2; from py3to2 import print_py3k294src = "a, b, *c = 1, 2, 3, 4, 5; print('a =', a); print('b =', b); print('c =', c)"295code_object = py3to2.compile_py3k(src, '', 'exec')296exec(code_object, globals())297"""298 s = "x = compile(%s, %s, %s, *%s, **%s); x = codetree(x).py3to2()"%tuple(repr(x) for x in (source, filename, mode, args, kwds))299 server.input(s)300 x = server.get('x')301 x = eval(x)302 x = x.compile()303 if 1: dis.dis(x)304 return x305 compile_py3k.__doc__ += compile.__doc__306 if 0: # test307 x = compile_py3k(TESTCODE, '', 'exec')308 print dis.dis(x)309 exec(x)310# execsrc('py3k parse', compile_py3k)311# end py2x init312# beg py3k init313import py3to2; from py3to2 import *314import builtins, imp; builtins.basestring = str, bytes; builtins.reload = imp.reload315execsrc('common')316class codetree(codetree):317 builtins = {'print':'print_py3k'}318 # builtins = {}319 def py3to2(self):320 assert not self.co_kwonlyargcount, (self.co_kwonlyargcount)321 s = bytearray(self.co_code)322 op3to2 = self.op3to2323 HAVE_ARGUMENT = opcode.HAVE_ARGUMENT324 skip = 0325 for i, x in enumerate(s):326 if skip: skip -= 1; continue327 if x >= HAVE_ARGUMENT: skip = 2328 if x in op3to2: s[i] = op3to2[x]329 self.co_code = s.str()330 for i, x in enumerate(self.co_names):331 # if x == 'print': self.co_names[i] = 'py3to2.compiler.print_py3k'332 if x in self.builtins: self.co_names[i] = self.builtins[x]333 for x in self.co_args:334 y = getattr(self, x)335 if isinstance(y, bytes): setattr(self, x, y.str())336 for i, x in enumerate(self.co_consts):337 if isinstance(x, codetree): self.co_consts[i] = x.py3to2()338 return self339# end py3k init340# beg py3k parse341# end py3k parse342################################343copy this to ceval.c344################################345/* Execute compiled code */346/* XXX TO DO:347 XXX speed up searching for keywords by using a dictionary348 XXX document it!349 */350/* enable more aggressive intra-module optimizations, where available */351#define PY_LOCAL_AGGRESSIVE...

Full Screen

Full Screen

recipe-574436.py

Source:recipe-574436.py Github

copy

Full Screen

...187 # compile, expand, recompile, exec188 if PYVERSION == 'py2x': print s189 if 0: print s190 else:191 t = t0.py3to2() if hasattr(t0, 'py3to2') else t0; xxx = t; s = repr(t)192if 0: TESTCODE = 'a, *b = 1, 2, 3; print(a, b)' # pep3132 - Extended Iterable Unpacking193if 0: TESTCODE = """194?z = 3195?def foo(x, *y): return x, y, z196?print foo(1)197"""198if 1: TESTCODE = """199?def foo(x):200? def bar(): nonlocal x; x += 1201? bar(); return x202?print(foo(1))""" # nonlocal statement203TESTCODE = TESTCODE.replace('?', '')204# end common205# beg py2x init206from __future__ import nested_scopes, generators, division, absolute_import, with_statement; import py3to2; from py3to2 import *207import __builtin__; sys.modules['builtins'] = builtins = __builtin__; builtins.bytes = str208import itertools; filter, map, range, zip = itertools.ifilter, itertools.imap, xrange, itertools.izip # iterators209execsrc('common')210import mmap, signal, subprocess, time211class _server(object): # python3k server which byte compiles 3k code212 bufsize = 1<<20213 @staticmethod214 def restart(restart = True):215 self = _server216 # def restart(self, restart = True):217 global SERVERIO, SERVER218 if restart:219 if 'SERVERIO' in globals():220 for x in SERVERIO: os.close(x)221 SERVERIO = os.pipe()222 print 'py3to2 server restarting with io: %s'%(SERVERIO, )223 if 'SERVER' in globals(): self.kill()224 SERVER = subprocess.Popen('python3k -i', stdin = subprocess.PIPE, stdout = SERVERIO[1], stderr = subprocess.STDOUT, shell = True)225 SERVER.stdin.write('import py3to2; reload(py3to2); from py3to2 import *\n')226 SERVER.stdin.write('CLIENTPID = %i\n'%PID)227 @staticmethod228 def kill():229 # def kill(self):230 global SERVER231 try: os.kill(SERVER.pid, signal.SIGTERM)232 except Exception, exc: print exc233 del SERVER234 @staticmethod235 def input(s, stdout = True, timeout = 2,236 prompt = rgx2nd(('(.+)', 'py3to2 server: \\1'), ),237 subprompt = rgx2nd(('>>> ', '', None), ),238 fndexc = re.compile('\WTraceback '),239 ):240 self = _server241 if not s: return242 SERVER.stdin.write(s)243 try:244 buf = ''245 SERVER.stdin.write("\n\nimport os, signal; os.kill(CLIENTPID, signal.SIGINT)\n")246 time.sleep(timeout)247 raise IOError('py3to2 server not responding to input: %s'%repr(s))248 except KeyboardInterrupt:249 buf = os.read(SERVERIO[0], self.bufsize)250 buf = subprompt.sub(buf)251 if prompt: buf = prompt.sub(buf)252 if fndexc.search(buf): raise IOError('py3to2 server input: %s\n%s'%(s, buf))253 if stdout: sys.stdout.write(buf)254 else: return buf255 @staticmethod256 def get(varname, **kwds):257 self = _server258 return self.input("print(%s)"%varname, stdout = None, prompt = None, **kwds)259 def __init__(self, restart = None):260 global SERVER261 self.restart(restart = 'SERVER' not in globals())262 self.input('\n')263 @staticmethod264 def test():265 self = _server266 global xxx267 if 1: # server init268 self.restart()269 self.input("1, 2;3")270 if 0: # py3k codetree271 server.input("codetree.test()")272 if 1: # 3to2273 s = server.get('xxx')274 t = eval(s)275 c = t.compile()276 print dis.dis(c)277 exec(c, globals())278server = _server()279if 1:280 def builtinsadd(f): setattr(__builtin__, f.__name__, f); return f281 @builtinsadd282 def print_py3k(*args, **kwds):283 sep = kwds['sep'] if 'sep' in kwds else ' '284 end = kwds['end'] if 'end' in kwds else '\n'285 file = kwds['file'] if 'file' in kwds else sys.stdout286 s = sep.join(str(x) for x in args)287 file.write(s)288 file.write(end)289 @builtinsadd290 def compile_py3k(source, filename, mode, *args, **kwds):291 """292py3k example:293import py3to2; from py3to2 import print_py3k294src = "a, b, *c = 1, 2, 3, 4, 5; print('a =', a); print('b =', b); print('c =', c)"295code_object = py3to2.compile_py3k(src, '', 'exec')296exec(code_object, globals())297"""298 s = "x = compile(%s, %s, %s, *%s, **%s); x = codetree(x).py3to2()"%tuple(repr(x) for x in (source, filename, mode, args, kwds))299 server.input(s)300 x = server.get('x')301 x = eval(x)302 x = x.compile()303 if 1: dis.dis(x)304 return x305 compile_py3k.__doc__ += compile.__doc__306 if 0: # test307 x = compile_py3k(TESTCODE, '', 'exec')308 print dis.dis(x)309 exec(x)310# execsrc('py3k parse', compile_py3k)311# end py2x init312# beg py3k init313import py3to2; from py3to2 import *314import builtins, imp; builtins.basestring = str, bytes; builtins.reload = imp.reload315execsrc('common')316class codetree(codetree):317 builtins = {'print':'print_py3k'}318 # builtins = {}319 def py3to2(self):320 assert not self.co_kwonlyargcount, (self.co_kwonlyargcount)321 s = bytearray(self.co_code)322 op3to2 = self.op3to2323 HAVE_ARGUMENT = opcode.HAVE_ARGUMENT324 skip = 0325 for i, x in enumerate(s):326 if skip: skip -= 1; continue327 if x >= HAVE_ARGUMENT: skip = 2328 if x in op3to2: s[i] = op3to2[x]329 self.co_code = s.str()330 for i, x in enumerate(self.co_names):331 # if x == 'print': self.co_names[i] = 'py3to2.compiler.print_py3k'332 if x in self.builtins: self.co_names[i] = self.builtins[x]333 for x in self.co_args:334 y = getattr(self, x)335 if isinstance(y, bytes): setattr(self, x, y.str())336 for i, x in enumerate(self.co_consts):337 if isinstance(x, codetree): self.co_consts[i] = x.py3to2()338 return self339# end py3k init340# beg py3k parse341# end py3k parse342################################343copy this to ceval.c344################################345/* Execute compiled code */346/* XXX TO DO:347 XXX speed up searching for keywords by using a dictionary348 XXX document it!349 */350/* enable more aggressive intra-module optimizations, where available */351#define PY_LOCAL_AGGRESSIVE...

Full Screen

Full Screen

py3to2.py

Source:py3to2.py Github

copy

Full Screen

1"""2@file3@brief Helper to convert a script written in Python 3 to Python 24"""5import os6import re7import shutil8from ..filehelper.synchelper import explore_folder_iterfile9from ..loghelper.flog import noLOG10from .default_regular_expression import _setup_pattern_copy11class Convert3to2Exception(Exception):12 """13 exception raised for an exception happening during the conversion14 """15 pass16def py3to2_convert_tree(folder, dest, encoding="utf8", pattern=".*[.]py$",17 pattern_copy=_setup_pattern_copy,18 unittest_modules=None, fLOG=noLOG):19 """20 Converts files in a folder and its subfolders from python 3 to python 2,21 the function only considers python script (verifying *pattern*).22 @param folder folder23 @param dest destination24 @param encoding all files will be saved with this encoding25 @param pattern pattern to find source code26 @param pattern_copy copy these files, do not modify them27 @param fLOG logging function28 @param unittest_modules modules used during unit tests but not installed29 @return list of copied files30 If a folder does not exists, it will create it.31 The function excludes all files in subfolders32 starting by ``dist``, ``_doc``, ``build``, ``extensions``, ``nbextensions``.33 The function also exclude subfolders inside34 subfolders following the pattern ``ut_.*``.35 There are some issues difficult to solve with strings.36 Python 2.7 is not friendly with strings. Some needed pieces of code::37 if sys.version_info[0]==2:38 from codecs import open39 You can also read blog post :ref:`b-migration-py2py3`.40 The variable *unittest_modules* indicates the list of41 modules which are not installed in :epkg:`Python` distribution42 but still used and placed in the same folder as the same which43 has to converted.44 *unittest_modules* can be either a list or a tuple ``(module, alias)``.45 Then the alias appears instead of the module name.46 The function does not convert the exception47 `FileNotFoundError <https://docs.python.org/3/library/exceptions.html>`_48 which only exists in Python 3. The module will fail in version 2.749 if this exception is raised.50 The following page51 `Cheat Sheet: Writing Python 2-3 compatible code52 <http://python-future.org/compatible_idioms.html>`_53 gives the difference between the two versions of Python54 and how to write compatible code.55 """56 exclude = ("temp_", "dist", "_doc", "build", "extensions",57 "nbextensions", "dist_module27", "_virtualenv", "_venv")58 reg = re.compile(".*/ut_.*/.*/.*")59 conv = []60 for file in explore_folder_iterfile(folder, pattern=pattern):61 full = os.path.join(folder, file)62 if "site-packages" in full:63 continue64 file = os.path.relpath(file, folder)65 # undesired sub folders66 ex = False67 for exc in exclude:68 if file.startswith(exc) or "\\temp_" in file or \69 "/temp_" in file or "dist_module27" in file:70 ex = True71 break72 if ex:73 continue74 # subfolders inside unit tests folder75 lfile = file.replace("\\", "/")76 if reg.search(lfile):77 continue78 py2 = py3to2_convert(full, unittest_modules)79 destfile = os.path.join(dest, file)80 dirname = os.path.dirname(destfile)81 if not os.path.exists(dirname):82 os.makedirs(dirname)83 with open(destfile, "w", encoding="utf8") as f:84 f.write(py2)85 conv.append(destfile)86 for file in explore_folder_iterfile(folder, pattern=pattern_copy):87 full = os.path.join(folder, file)88 file = os.path.relpath(file, folder)89 # undesired sub folders90 ex = False91 for exc in exclude:92 if file.startswith(exc) or "\\temp_" in file or \93 "/temp_" in file or "dist_module27" in file:94 ex = True95 break96 if ex:97 continue98 destfile = os.path.join(dest, file)99 dirname = os.path.dirname(destfile)100 if not os.path.exists(dirname):101 os.makedirs(dirname)102 shutil.copy(full, dirname)103 conv.append(destfile)104 fLOG("py3to2_convert_tree, copied", len(conv), "files")105 return conv106def py3to2_convert(script, unittest_modules):107 """108 converts a script into from python 3 to python 2109 @param script script or filename110 @param unittest_modules modules used during unit test but not installed,111 @see fn py3to2_convert_tree112 @return string113 See see @fn py3to2_convert_tree for more information.114 """115 if os.path.exists(script):116 try:117 with open(script, "r", encoding="utf8") as f:118 content = f.read()119 except (UnicodeEncodeError, UnicodeDecodeError): # pragma: no cover120 with open(script, "r") as f:121 content = f.read()122 else:123 content = script # pragma: no cover124 # start processing125 content = py3to2_remove_raise_from(content)126 # unicode127 if ("install_requires=" in content or "package_data" in content) and "setup" in content:128 # we skip the file setup.py as it raises an error129 pass130 else:131 try:132 content = py3to2_future(content)133 except Convert3to2Exception as e: # pragma: no cover134 raise Convert3to2Exception(135 'unable to convert a file due to unicode issue.\n File "{0}", line 1'.format(script)) from e136 # some other modification137 content = content.replace("from queue import", "from Queue import")138 content = content.replace("nonlocal ", "# nonlocal ")139 # long and unicode140 content = content.replace("int #long#", "long")141 content = content.replace("int # long#", "long")142 content = content.replace("str #unicode#", "unicode")143 content = content.replace("str # unicode#", "unicode")144 content = content.replace(145 "Programming Language :: Python :: 3", "Programming Language :: Python :: 2")146 content = content.replace(', sep="\\t")', ', sep="\\t".encode("ascii"))')147 # imported modules148 if unittest_modules is not None:149 content = py3to2_imported_local_modules(content, unittest_modules)150 # end151 return content152def py3to2_future(content):153 """154 checks that import ``from __future__ import unicode_literals``155 is always present, the function assumes it is a python code156 @param content file content157 @return new content158 """159 find = "from __future__ import unicode_literals"160 if find in content and '"{0}"'.format(find) not in content:161 # the second condition avoid to raise this162 # exception when parsing this file163 # this case should only happen for this file164 raise Convert3to2Exception( # pragma: no cover165 "unable to convert a file")166 lines = content.split("\n")167 position = 0168 incomment = None169 while (position < len(lines) and not lines[position].startswith("import ") and170 not lines[position].startswith("from ") and171 not lines[position].startswith("def ") and172 not lines[position].startswith("class ")):173 if incomment is None:174 if lines[position].startswith("'''"):175 incomment = "'''" # pragma: no cover176 elif lines[position].startswith('"""'):177 incomment = '"""'178 else:179 if lines[position].endswith("'''"): # pragma: no cover180 incomment = None181 position += 1182 break183 if lines[position].endswith('"""'):184 incomment = None185 position += 1186 break187 position += 1188 if position < len(lines):189 lines[position] = "{0}\n{1}".format(find, lines[position])190 return "\n".join(lines)191def py3to2_remove_raise_from(content):192 """193 Removes expression such as: ``raise Exception ("...") from e``.194 The function is very basic. It should be done with a grammar.195 @param content file content196 @return script197 """198 lines = content.split("\n")199 r = None200 for i, line in enumerate(lines):201 if " raise " in line:202 r = i203 if " from " in line and r is not None:204 spl = line.split(" from ")205 if len(spl[0].strip(" \n")) > 0:206 lines[i] = line = spl[0] + "# from " + " - ".join(spl[1:])207 if r is not None and i > r + 3:208 r = None209 return "\n".join(lines)210def py3to2_imported_local_modules(content, unittest_modules):211 """212 See function @see fn py3to2_convert_tree213 and documentation about parameter *unittest_modules*.214 @param content script or filename215 @param unittest_modules modules used during unit test but not installed,216 @see fn py3to2_convert_tree217 """218 lines = content.split("\n")219 for modname in unittest_modules:220 if isinstance(modname, tuple):221 modname, alias = modname # pragma: no cover222 else:223 alias = modname224 s1 = '"{0}"'.format(modname)225 s2 = "'{0}'".format(modname)226 s3 = "import {0}".format(modname)227 s4 = '"{0}"'.format(modname.upper())228 s4_rep = '"{0}27"'.format(modname.upper())229 if (s1 in content or s2 in content or s4 in content) and s3 in content:230 for i, line in enumerate(lines):231 if " in " in line or "ModuleInstall" in line:232 continue233 if s1 in line:234 line = line.replace(235 s1, '"..", "{0}", "dist_module27"'.format(alias))236 lines[i] = line237 elif s2 in line:238 line = line.replace( # pragma: no cover239 s2, "'..', '{0}', 'dist_module27'".format(alias))240 lines[i] = line # pragma: no cover241 elif s4 in line:242 line = line.replace(s4, s4_rep)243 lines[i] = line...

Full Screen

Full Screen

compat.py

Source:compat.py Github

copy

Full Screen

...23 """Deprecated since RF 4.0. Use 'py3to2' instead."""24 if hasattr(cls, '__unicode__'):25 cls.__str__ = lambda self: unicode(self).encode('UTF-8')26 return cls27 def py3to2(cls):28 if ismethod(cls.__str__) and cls.__str__.im_func is not unicode_to_str:29 cls.__unicode__ = cls.__str__30 cls.__str__ = unicode_to_str31 if hasattr(cls, '__bool__'):32 cls.__nonzero__ = cls.__bool__33 return cls34 def unicode_to_str(self):35 return unicode(self).encode('UTF-8')36else:37 from inspect import unwrap38 from io import StringIO39 def py2to3(cls):40 """Deprecated since RF 4.0. Use 'py3to2' instead."""41 if hasattr(cls, '__unicode__'):42 cls.__str__ = lambda self: self.__unicode__()43 if hasattr(cls, '__nonzero__'):44 cls.__bool__ = lambda self: self.__nonzero__()45 return cls46 def py3to2(cls):47 return cls48# Copied from Jinja2, released under the BSD license.49# https://github.com/mitsuhiko/jinja2/blob/743598d788528921df825479d64f492ef60bef82/jinja2/_compat.py#L8850def with_metaclass(meta, *bases):51 """Create a base class with a metaclass."""52 # This requires a bit of explanation: the basic idea is to make a53 # dummy metaclass for one level of class instantiation that replaces54 # itself with the actual metaclass.55 class metaclass(type):56 def __new__(cls, name, this_bases, d):57 return meta(name, bases, d)58 return type.__new__(metaclass, 'temporary_class', (), {})59# On IronPython sys.stdxxx.isatty() always returns True60if not IRONPYTHON:...

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 Robotframework 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