[PYTHON]Trouble with Unit Names with Non-Alphabet Characters...

Afforess

The White Wizard
Joined
Jul 31, 2007
Messages
12,239
Location
Austin, Texas
I'm merging the Mercenaries Modcomp with BUG 4.3. I've gotten pretty far, and everything works, as long as units don't have any characters in their names that is not in the English alphabet. Hyphens, parenthesis, and numbers cause issues.

Specifically, the non-standard characters cause the Mercenary Manager screen to not correctly recognize input data. Clicking on buttons has no effect.

Take this button for example.
Code:
	def handleInput (self, inputClass):
...
			if(function == "UnitInfoButton"):
			
				# Get the player ID
				iPlayer = gc.getGame().getActivePlayer()

				# Get the actual player reference
				player = gc.getPlayer(iPlayer)

				# Split up the mercenary name into the actual mercenary name
				# and the unit ID string
				[COLOR="Red"]mercenaryName, id  = mercenaryName.split("-")[/COLOR]

				# Convert the unit ID string back into a number
				id = self.alphaToNumber(id)
				
				# Get the mercenary 
				mercenary = objMercenaryUtils.getMercenary(mercenaryName)

				# If we didn't get a mercenary from the mercenary pool then
				# it is safe to assume that the unit has never been a 
				# mercenary.
				if(mercenary == None):
					
					# Create a blank mercenary
					mercenary = objMercenaryUtils.createBlankMercenary()

					# Populate the mercenary object with the data from 
					# the unit that we want to look at
					mercenary.loadUnitData(player.getUnit(id))
					mercenary.setName(mercenaryName)
					
				# Calculate the screen information
				self.calculateScreenWidgetData(screen)

				# Populate the unit information panel with the mercenary/unit
				# information.
				self.populateUnitInformation(screen,mercenary)	
...

I've put debugging statements in here, and the code stops executing at the red line, and never moves past it. However, if I change the name of the unit to something without numbers, etc... and click again, the code executes past the red line.

Why do numbers and parenthesis cause issues?
 
You may find it helpful to look at the python exception which is produced in this case. It is likely that the exception details will give you something helpful to debug with, and perhaps even enough to solve the problem.

The red line uses some slightly obscure syntax which "assumes" that the name will contain exactly one dash. Then split returns a list of two items, which are assigned to the two variables on the left hand side. My guess, based on the information you gave, is that when the name contains numbers or parentheses, the code elsewhere that adds the unit name does something unexpected. After this unexpected behavior, the name does not match the pattern which the split call expects, and you get a python exception. The modcomp author did not expect this behavior and so did not protect the split call against failure.
 
You may find it helpful to look at the python exception which is produced in this case. It is likely that the exception details will give you something helpful to debug with, and perhaps even enough to solve the problem.

If the problem was this easy to solve, I wouldn't have posted. :rolleyes:

The red line uses some slightly obscure syntax which "assumes" that the name will contain exactly one dash. Then split returns a list of two items, which are assigned to the two variables on the left hand side. My guess, based on the information you gave, is that when the name contains numbers or parentheses, the code elsewhere that adds the unit name does something unexpected. After this unexpected behavior, the name does not match the pattern which the split call expects, and you get a python exception. The modcomp author did not expect this behavior and so did not protect the split call against failure.

Yeah, well I thought the same thing. I looked up the description for the .split() string method. I can't see how goes wrong, it seems basic to me:
Spoiler Documentation :

Python Docs said:
str.split([sep[, maxsplit]])
Return a list of the words in the string, using sep as the delimiter string. If maxsplit is given, at most maxsplit splits are done (thus, the list will have at most maxsplit+1 elements). If maxsplit is not specified, then there is no limit on the number of splits (all possible splits are made).

If sep is given, consecutive delimiters are not grouped together and are deemed to delimit empty strings (for example, '1,,2'.split(',') returns ['1', '', '2']). The sep argument may consist of multiple characters (for example, '1<>2<>3'.split('<>') returns ['1', '2', '3']). Splitting an empty string with a specified separator returns [''].

If sep is not specified or is None, a different splitting algorithm is applied: runs of consecutive whitespace are regarded as a single separator, and the result will contain no empty strings at the start or end if the string has leading or trailing whitespace. Consequently, splitting an empty string or a string consisting of just whitespace with a None separator returns [].

For example, ' 1 2 3 '.split() returns ['1', '2', '3'], and ' 1 2 3 '.split(None, 1) returns ['1', '2 3 '].


Which leaves me just as clueless as before...:sad:
 
You mention the code "stops" at the red line. I have to imagine it is throwing an exception. What is the exception? What is the value of mercenaryName just before it throws?
 
Thanks for the advice. Checking out what the MercenaryName was before and after lead me to the answer to my problem. Apparently when the EXE is passed a string as a function name with a digit or ( ), it truncates it to part of the string without those portions. Not good. I rebuilt the string right before passing it through without those.
 
Top Bottom