def execute(self, target): #print self.scriptcode #print '%s::%s' % (self.cleanname, self.cleanname), target perl.call('%s::%s' % (self.cleanname, self.cleanname), target) #ds = perl.get_ref("@%s::descriptors" % (self.cleanname,)) p = perloutput results = p.read() #results = "\n".join([";".join(d) + ";" for d in ds]) return results
def test_trap_exceptions(self): # try to trap exceptions try: perl.eval("die 'Oops!'") except perl.PerlError as val: self.assertEqual(str(val)[:5],"Oops!") try: perl.call("not_there", 3, 4) except perl.PerlError as val: self.assertEqual(str(val), "Undefined subroutine &main::not_there called.\n")
def test_trap_exceptions(self): # try to trap exceptions try: perl.eval("die 'Oops!'") except perl.PerlError as val: self.assertEqual(str(val)[:5], "Oops!") try: perl.call("not_there", 3, 4) except perl.PerlError as val: self.assertEqual( str(val), "Undefined subroutine &main::not_there called.\n")
def test_pass_hashes_both_ways(self): # can we pass hashes both ways if perl.MULTI_PERL: self.skipTest("not on MULTI_PERL...") else: perl.eval("sub foo_elem { shift->{foo} }") hash = perl.eval("{ foo => 42 }") self.assertEqual(perl.call("foo_elem", hash), 42)
sub compile { my($code) = @_; $code = "package main; sub do { " . $code . "}"; #print "[[$code]]\\n"; eval $code; die if $@; } sub foo { 42; } *Safe1::_compile = \&compile; """) mask = perl.call("Opcode::opset", "bless", "add") #print perl.call_tuple("Opcode::opset_to_ops", mask) perl.safecall("Safe1", mask, ('_compile', 'my $n = shift; print "ok $n\\n";')) perl.safecall("Safe1", mask, ('do', 1)) # try a trapped opcode try: perl.safecall("Safe1", mask, ('_compile', 'return bless {}, "Foo"')) except perl.PerlError, v: #print v if not re.match('^\'bless\' trapped by operation mask', str(v)): print "not ", print "ok 2" # The following call reset the perl parser state enought to
if not perl.eval("3+3") == 6: print "not", print "ok 1" # can we pass strings back if not perl.eval("substr('abcd', 0, 3)") == "abc": print "not", print "ok 2" # can we pass hashes both ways if perl.MULTI_PERL: print "actually skipping test 3..." print "ok 3" else: perl.eval("sub foo_elem { shift->{foo} }") hash = perl.eval("{ foo => 42 }") if not perl.call("foo_elem", hash) == 42: print "not", print "ok 3" # try to trap exceptions try: perl.eval("die 'Oops!'") except perl.PerlError, val: if str(val)[:5] != "Oops!": print "not", print "ok 4" try: perl.call("not_there", 3, 4) except perl.PerlError, val: if str(val) != "Undefined subroutine &main::not_there called.\n": print "not", print "ok 5"
print "not " unless "$list" eq "[1, 2, 3]" && "@list" eq "1 2 3"; print "ok 11\n"; # try access to non-existing attribute eval { $foo->not_there; }; print "not " unless $@ && Python::Err::AttributeError($@->type); print "ok 12\n"; # try calling something which is not callable eval { $foo->plain_list("foo", "bar"); }; print "not " unless $@ && $@ =~ /^Can't call a non-callable object/; print "ok 13\n"; # Strings are a sequences too, but they are not unwrapped. $foo->string("string"); @list = $foo->string; print "not " if "@list" ne "string"; print "ok 14\n"; } """) perl.call("foo", foo)
def search_pkgs(deps): requested = dict() state = perl.get_ref("%") perl.call("urpm::select::search_packages", urpm, requested, deps, use_provides=1) # create a dictionary of URPM::Package objects, indexed by fullname # for us to easier lookup packages in pkgdict = dict() for key in list(requested.keys()): if not key: requested.pop(key) continue pkgids = key.split("|") if not pkgids: continue dep = None for pkgid in pkgids: pkg = urpm['depslist'][int(pkgid)] if excludere.match(pkg.name()): requested.pop(key) print(color("skipping candidate for requested packages: %s" % pkg.fullname(), YELLOW)) break if not dep: dep = pkg elif dep.compare_pkg(pkg) < 0: dep = pkg if dep: if len(pkgids) > 1: # XXX if key in requested: requested.pop(key) requested[str(dep.id())] = 1 pkgdict[pkg.fullname()] = dep urpm.resolve_requested(empty_db, state, requested, no_suggests = not suggests, callback_choices = stop_on_choices, nodeps = 1) allpkgs = [] # As we try resolving all packages we'd like to include in the distribution # release at once, there's a fair chance of there being some requested # packages conflicting with eachother, resulting in requested packages # getting rejected. To workaround this, we'll try resolve these packages # separately to still include them and their dependencies. rejects = [] for key in list(state['rejected'].keys()): reject = state['rejected'][key] #print color("rejected: %s" % key, RED, RESET, DIM) # FIXME: #if 'backtrack' in reject: if reject.has_key('backtrack'): backtrack = reject['backtrack'] #if 'conflicts' in backtrack: if backtrack.has_key('conflicts'): if key in pkgdict: pkg = pkgdict[key] print(color("conflicts: %s with %s" % (key, list(backtrack['conflicts'])), RED, RESET, DIM)) if pkg.name() in deps and pkg.name() not in rejects: conflicts = backtrack['conflicts'] skip = False for c in conflicts: # XXX if c in pkgdict: cpkg = pkgdict[c] # if it's a package rejected due to conflict with a package of same name, # it's most likely some leftover package in repos that haven't been # removed yet and that we can safely ignore if cpkg.name() == pkg.name(): skip = True else: skip = True if not skip: print(color("The requested package %s has been rejected due to conflicts with: %s" % (pkg.fullname(), string.join(conflicts)), RED, RESET, BRIGHT)) rejects.append(pkg.name()) if rejects: print(color("Trying to resolve the following requested packages rejected due to conflicts: %s" % string.join(rejects, " "), BLUE, RESET, BRIGHT)) res = search_pkgs(rejects) for pkg in res: pkgid = str(pkg.id()) if not pkgid in list(state['selected'].keys()): print(color("adding %s" % pkg.fullname(), BLUE)) state['selected'][pkgid] = 1 for pkgid in list(state['selected'].keys()): pkgids = pkgid.split('|') dep = None for pkgid in pkgids: pkgid = int(pkgid) pkg = urpm['depslist'][pkgid] if excludere.match(pkg.name()): print(color("skipping1: %s" % pkg.fullname(), YELLOW, RESET, DIM)) continue #else: # print color("including1: %s" % pkg.fullname(), YELLOW, RESET, BRIGHT) if not dep: dep = pkg else: print(color("hum: %s" % pkg.fullname(), YELLOW, RESET, DIM)) True if dep is None: print(color("dep is none: %s" % pkg.fullname(), YELLOW, RESET, DIM)) continue else: #print color("including: %s" % pkg.fullname(), YELLOW, RESET, BRIGHT) allpkgs.append(dep) return allpkgs
#print "$res\\n"; print "not " unless $res eq "a=101, b=102"; print "ok 3\n"; } $res = $o->foo(101, *b => 102); #print "$res\\n"; print "not " unless $res eq "a=101, b=102"; print "ok 4\n"; # Test KW constructor $kw = KW(b => 102); $kw->{a} = 101; $res = $o->foo($kw); #print "$res\\n"; print "not " unless $res eq "a=101, b=102"; print "ok 5\n"; $res = $o->foo(KW(a => 101)); #print "$res\\n"; print "not " unless $res eq "a=101, b=None"; print "ok 6\n"; } """) perl.call("foo", Foo())
def test_function_scalar_context(self): # scalar context self.assertEqual(perl.call("foo2"), 42)
sub compile { my($code) = @_; $code = "package main; sub do { " . $code . "}"; #print "[[$code]]\\n"; eval $code; die if $@; } sub foo { 42; } *Safe1::_compile = \&compile; """) mask = perl.call("Opcode::opset", "bless", "add") #print perl.call_tuple("Opcode::opset_to_ops", mask) perl.safecall("Safe1", mask, ('_compile', 'my $n = shift; print "ok $n\\n";')) perl.safecall("Safe1", mask, ('do', 1)) # try a trapped opcode try: perl.safecall("Safe1", mask, ('_compile', 'return bless {}, "Foo"')) except perl.PerlError as v: #print v if not re.match('^\'bless\' trapped by operation mask', str(v)): print("not ", end=' ') print("ok 2") # The following call reset the perl parser state enought to # avoid the 'nexttoke' bug.
import perl perl.eval(""" use Term::ReadLine; my $term = Term::ReadLine->new("perlsh"); use Python @Python::EXPORT_OK; sub shell { my $prompt = shift || "perlsh> "; while (defined($_ = $term->readline($prompt))) { chomp; my $res = eval $_; print "$res\n" if defined $res; print $@ if $@; } print "\n"; } """) perl.call("shell")
global testno if res != expect: print("Expected", repr(expect), "got", repr(res)) print("not", end=' ') print("ok", testno) testno = testno + 1 void = None scalar = "scalar" array = ("array", ) expect(foo(), scalar) expect(foo(__wantarray__=1), array) expect(foo(__wantarray__=None), void) foo.__wantarray__ = 1 expect(foo(), array) expect(foo(__wantarray__=0), scalar) expect(foo(__wantarray__=None), void) foo.__wantarray__ = None expect(foo(), void) expect(perl.call("foo"), scalar) expect(perl.call_tuple("foo"), array) expect(perl.call("foo", __wantarray__=1), array) expect(perl.call_tuple("foo", __wantarray__=0), scalar)
def expect(res, expect): global testno if res != expect: print "Expected", repr(expect), "got", repr(res) print "not", print "ok", testno testno = testno + 1 void = None scalar = "scalar" array = ("array",) expect(foo(), scalar) expect(foo(__wantarray__ = 1), array) expect(foo(__wantarray__ = None), void) foo.__wantarray__ = 1; expect(foo(), array) expect(foo(__wantarray__ = 0), scalar) expect(foo(__wantarray__ = None), void) foo.__wantarray__ = None expect(foo(), void) expect(perl.call("foo"), scalar) expect(perl.call_tuple("foo"), array) expect(perl.call("foo", __wantarray__ = 1), array) expect(perl.call_tuple("foo", __wantarray__ = 0), scalar)
package Foo::Bar; $res = $o->foo(*b => 102, *a => 101); #print "$res\\n"; print "not " unless $res eq "a=101, b=102"; print "ok 3\n"; } $res = $o->foo(101, *b => 102); #print "$res\\n"; print "not " unless $res eq "a=101, b=102"; print "ok 4\n"; # Test KW constructor $kw = KW(b => 102); $kw->{a} = 101; $res = $o->foo($kw); #print "$res\\n"; print "not " unless $res eq "a=101, b=102"; print "ok 5\n"; $res = $o->foo(KW(a => 101)); #print "$res\\n"; print "not " unless $res eq "a=101, b=None"; print "ok 6\n"; } """) perl.call("foo", Foo())
def search_pkgs(deps): requested = dict() state = perl.get_ref("%") perl.call("urpm::select::search_packages", urpm, requested, deps, use_provides=1) # create a dictionary of URPM::Package objects, indexed by fullname # for us to easier lookup packages in pkgdict = dict() for key in requested.keys(): if not key: requested.pop(key) continue pkgids = key.split("|") if not pkgids: continue dep = None for pkgid in pkgids: pkg = urpm['depslist'][int(pkgid)] if excludere.match(pkg.name()): requested.pop(key) print color("skipping candidate for requested packages: %s" % pkg.fullname(), YELLOW) break if not dep: dep = pkg elif dep.compare_pkg(pkg) < 0: dep = pkg if dep: if len(pkgids) > 1: # XXX if key in requested: requested.pop(key) requested[str(dep.id())] = 1 pkgdict[pkg.fullname()] = dep urpm.resolve_requested(empty_db, state, requested, no_suggests = not suggests, callback_choices = stop_on_choices, nodeps = 1) allpkgs = [] # As we try resolving all packages we'd like to include in the distribution # release at once, there's a fair chance of there being some requested # packages conflicting with eachother, resulting in requested packages # getting rejected. To workaround this, we'll try resolve these packages # separately to still include them and their dependencies. rejects = [] for key in state['rejected'].keys(): reject = state['rejected'][key] #print color("rejected: %s" % key, RED, RESET, DIM) if reject.has_key('backtrack'): backtrack = reject['backtrack'] if backtrack.has_key('conflicts'): if key in pkgdict: pkg = pkgdict[key] print color("conflicts: %s with %s" % (key, list(backtrack['conflicts'])), RED, RESET, DIM) if pkg.name() in deps and pkg.name() not in rejects: conflicts = backtrack['conflicts'] skip = False for c in conflicts: # XXX if c in pkgdict: cpkg = pkgdict[c] # if it's a package rejected due to conflict with a package of same name, # it's most likely some leftover package in repos that haven't been # removed yet and that we can safely ignore if cpkg.name() == pkg.name(): skip = True else: skip = True if not skip: print color("The requested package %s has been rejected due to conflicts with: %s" % (pkg.fullname(), string.join(conflicts)), RED, RESET, BRIGHT) rejects.append(pkg.name()) if rejects: print color("Trying to resolve the following requested packages rejected due to conflicts: %s" % string.join(rejects, " "), BLUE, RESET, BRIGHT) res = search_pkgs(rejects) for pkg in res: pkgid = str(pkg.id()) if not pkgid in state['selected'].keys(): print color("adding %s" % pkg.fullname(), BLUE) state['selected'][pkgid] = 1 for pkgid in state['selected'].keys(): pkgids = pkgid.split('|') dep = None for pkgid in pkgids: pkgid = int(pkgid) pkg = urpm['depslist'][pkgid] if excludere.match(pkg.name()): print color("skipping1: %s" % pkg.fullname(), YELLOW, RESET, DIM) continue #else: # print color("including1: %s" % pkg.fullname(), YELLOW, RESET, BRIGHT) if not dep: dep = pkg else: print color("hum: %s" % pkg.fullname(), YELLOW, RESET, DIM) True if dep is None: print color("dep is none: %s" % pkg.fullname(), YELLOW, RESET, DIM) continue else: #print color("including: %s" % pkg.fullname(), YELLOW, RESET, BRIGHT) allpkgs.append(dep) return allpkgs
if not perl.eval("3+3") == 6: print("not", end=' ') print("ok 1") # can we pass strings back if not perl.eval("substr('abcd', 0, 3)") == "abc": print("not", end=' ') print("ok 2") # can we pass hashes both ways if perl.MULTI_PERL: print("actually skipping test 3...") print("ok 3") else: perl.eval("sub foo_elem { shift->{foo} }") hash = perl.eval("{ foo => 42 }") if not perl.call("foo_elem", hash) == 42: print("not", end=' ') print("ok 3") # try to trap exceptions try: perl.eval("die 'Oops!'") except perl.PerlError as val: if str(val)[:5] != "Oops!": print("not", end=' ') print("ok 4") try: perl.call("not_there", 3, 4) except perl.PerlError as val: if str(val) != "Undefined subroutine &main::not_there called.\n": print("not", end=' ') print("ok 5")