question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Calling a previous called library throws UnboundNameException

See original GitHub issue

Prerequisites

The issue tracker is used to report bugs and request new features, NOT to ask questions.

Questions should be posted to the users mailing list which can be accessed at https://ironpython.groups.io/g/users.

  • Are you running the latest version?
  • Are you reporting to the correct repository?
  • Did you perform a cursory search?

Description

We get IronPython.Runtime.UnboundNameException when we call a previous called library in a script. LibA -> LibB -> LibC -> LibA -> UnboundNameException Our python host implementation is base on this Stack Overflow response: https://stackoverflow.com/a/4644585 Un-commenting the # Workaround rows and import the lib before it is called fixes the issue, but does not scale very well.

Steps to Reproduce

# Test script
from LibStaticA import *
from LibInstanceA import *
testCalls = 10

print "Static test"
try:
	LibStaticA().Test(testCalls)
except Exception as ex:
	print ex.ToString().Split("\n")[0]

print "Instance test"
try:
	LibInstanceA().Test(testCalls)
except Exception as ex:
	print ex.ToString().Split("\n")[0]
# LibInstanceA
from LibInstanceB import *

class LibInstanceA:
	def Test(self, num):
	#	from LibInstanceA import * # Workaround
		LInstanceB().Test(num)
# LibInstanceB
from LibInstanceC import *

class LInstanceB:
	def Test(self, num):
	#	from LibInstanceC import * # Workaround
		LibInstanceC().Test(num)
# LibInstanceC
from LibInstanceA import *

class LibInstanceC:
	def Test(self, num):
		if 0 < num:
			print "LibInstanceC laps remaining: "+unicode(num)
		#	from LibInstanceA import * # Workaround
			LibInstanceA().Test(num-1)
		else:
			print "Done!"
# LibStaticA
from LibStaticB import *

class LibStaticA:
	@staticmethod
	def Test(num):
	#	from LibStaticB import * # Workaround
		LibStaticB.Test(num)
# LibStaticB
from LibStaticC import *

class LibStaticB:
	@staticmethod
	def Test(num):
	#	from LibStaticC import * # Workaround
		LibStaticC.Test(num)
# LibStaticC
from LibStaticA import *

class LibStaticC:
	@staticmethod
	def Test(num):
		if 0 < num:
			print "LibStaticC laps remaining: "+unicode(num)
		#	from LibStaticA import * # Workaround
			LibStaticA.Test(num-1)
		else:
			print "Done!"

Expected behavior: [What you expected to happen]

Static test
LibStaticC laps remaining: 10
LibStaticC laps remaining: 9
LibStaticC laps remaining: 8
LibStaticC laps remaining: 7
LibStaticC laps remaining: 6
LibStaticC laps remaining: 5
LibStaticC laps remaining: 4
LibStaticC laps remaining: 3
LibStaticC laps remaining: 2
LibStaticC laps remaining: 1
Done!
Instance test
LibInstanceC laps remaining: 10
LibInstanceC laps remaining: 9
LibInstanceC laps remaining: 8
LibInstanceC laps remaining: 7
LibInstanceC laps remaining: 6
LibInstanceC laps remaining: 5
LibInstanceC laps remaining: 4
LibInstanceC laps remaining: 3
LibInstanceC laps remaining: 2
LibInstanceC laps remaining: 1
Done!

Actual behavior: [What actually happened]

Static test
LibStaticC laps remaining: 10
IronPython.Runtime.UnboundNameException: global name 'LibStaticA' is not defined
Instance test
LibInstanceC laps remaining: 10
IronPython.Runtime.UnboundNameException: global name 'LibInstanceA' is not defined

Any idea on what could be the source of our problem? Is there any other information that I could try to provide that would help?

Versions

You can get this information from executing ipy -V.

Unable to execute, but has been tested on 2.7.12.

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
sloziercommented, Sep 7, 2022

Should be more like (difference is in the import):

# LibDirectStaticCFile
import LibDirectStaticCFile

class LibDirectStaticC:
	@staticmethod
	def Test(num):
		if 0 < num:
			print "LibDirectStaticC laps remaining: "+unicode(num)
			LibDirectStaticCFile.LibDirectStaticA.Test(num-1)
		else:
			print "Done!"
1reaction
sloziercommented, Sep 6, 2022

I believe the NameError (which shows up as UnboundNameException in C#) is the expected behavior. CPython results in exactly the same error.

Calling from LibStaticA import * will import what’s in LibStaticA at the time of the call and not what would eventually be loaded. For example, when you run your main file, as soon as it hits from LibStaticA import * it executes LibStaticA which in turn loads LibStaticB and then LibStaticC, but when you do the from LibStaticA import * at the top of LibStaticC, there’s nothing loaded in the LibStaticA module.

You can easily check this if you edit the top of LibStaticA:

# LibStaticA
A = 1
from LibStaticB import *
B = 2

You can use A in LibStaticC, but you will get an exception if you try to use B.

This type of circular dependency A->B->C->A is probably not recommended, but if it’s really what you need then the only thing that comes to mind would be to not use from LibStaticA import * and go with something like import LibStaticA and later call it as LibStaticA.LibStaticA.Test(num-1).

Read more comments on GitHub >

github_iconTop Results From Across the Web

IronPython.Runtime.UnboundNameException: name 'str' is not ...
The line: exec builtins_code in builtins.__dict__. just adds _ to the interactive environment so it repeats the last evaluated variable.
Read more >
My Newest Script -- Metric Cap Screws (Mxx) | Alibre Forum
Hi folks. Today I am offering something that works, not a 'howdoyafixit' post. Something new for me.:D This script will generate Metric ...
Read more >
Presentations/CHANGELOG.txt at master - GitHub
Silverlight.dll to not have the tutorial throw and error. ... $/Dev10/feature/vs_langs01_s/Merlin/Main/Languages/Ruby/Ruby/Runtime/Calls/RubyAccessorInfo.cs.
Read more >
Important - Setup PyCharm for Python plugins - Fougerite
Your call. I'm gonna go ahead and create a directory. I have created a folder called KillLog, now lets create a py file...
Read more >
[IronPython] IronPython 2.6 CodePlex Source Update
Dynamic/Actions/Calls/ApplicableCandidate.cs ... $/IronPython/IronPython_Main/Languages/IronPython/IronPython/Lib/iptest/assert_util.py ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found