Select to view content in your preferred language

CalculateField_management() crashing (I)Python

1021
3
09-03-2010 08:22 PM
CedricWannaz
Emerging Contributor
Dear all,

    Is it clear to anybody why the following code crashes? It does crash when I am calling CalculateField_management() with a Python expression AND that my object name is "arcgis". It does NOT happen with a VB expression or when the object is named differently. To me, it is like CalculateField_management() is somehow getting out of its namespace to kill a resource named 'arcgis' in another namespace.. ?

   The following code (a dummy example that isolates my problem) crashes IPython and somehow kills my arcgis object in Eclipse (without crashing Eclispe) the first time I am calling CalculateField_Python() so the second call generates the traceback below:


--- START.

Call #1
Call #2
Traceback (most recent call last):
  File "C:\Users\wannaz\Documents\Eclipse\Workspace\ArcGIS - General\src\testCalculateField.py", line 28, in <module>
    arcgis.CalculateField_Python()
AttributeError: 'module' object has no attribute 'CalculateField_Python'


Does anybody understand or know a post related to similar issues?

Thank you and best regards,

Cedric



myWorkspace = r'C:\Users\wannaz\Documents\Projects\ArcGIS General\CrashCalculateField\scratch.gdb'


import arcgisscripting


class MyClass:
    
    def __init__(self, workspace):
        self.gp = arcgisscripting.create( 9.3 )
        self.gp.SetProduct( 'ArcInfo' )
        self.gp.Workspace = workspace
        self.gp.OverwriteOutput = 1

    def CalculateField_Python(self):
        expression = '!Shape_Length!'
        self.gp.CalculateField_management( 'testFC', 'testField', expression, "PYTHON_9.3" )



print '\n--- START.\n'

# Give the object any other name than 'arcgis' and it works..
arcgis = MyClass( myWorkspace )

print "Call #1"
arcgis.CalculateField_Python()

print "Call #2"
arcgis.CalculateField_Python()

print '\n--- END.'
0 Kudos
3 Replies
RichardFairhurst
MVP Alum
(Edited) I have deleted my original suggestion after rereading your code.  I realized I had misread the code and that my suggestion about the class name capitalization could not be the source of the problem (and your revised code proves that).  I just don't like the idea of using arcgis as a class name or variable, since it could be a word that ESRI may reserve at some time (or may have already reserved based on the fact that you seem to think that your code is generating a conflict with an existing resource).  Try changing the arcgis variable name to something else and see if the same error is generated.  If it is, something other than a conflicting resource is at work.

I also just noticed your comment where your wrote "# Give the object any other name than 'arcgis' and it works..".  This indicates that arcgis is a reserved name in some form.  I have not looked for documentation on that being a reserved word, but based on your code's behavior it does seem like that is somehow at the root of the code failure.  (I also agree that the namespaces should prevent the conflict normally, but if somehow namespace ambiguity arises then I guess odd behaviors are possible if the namespaces are not clarified.  At least that is the case in .NET.  Most likely the import of arcgisscripting introduces an arcgis object from that namespace and your code creates ambiguity with that object name.)
0 Kudos
CedricWannaz
Emerging Contributor
Thank you for your answer. It was not an issue to name the class 'ArcGIS', precisely because Python is case sensitive, but you are right it was not the smartest choice of mine to do so. I changed it for 'MyClass' to clarify the example, which still crashes.

Thanks !

Cedric
0 Kudos
CedricWannaz
Emerging Contributor
Richard, thank you for the second paragraph of your reply, I totally agree. After discovering this behavior I banned 'arcgis' from the list of possible resource names in my code, but I thought that it was worth mentioning the issue, because it is somehow an illegal behavior.

CalculateField_management() is the only method that has this behavior up to my knowledge, and only when then expression is given in Python. An object 'arcgis' is created in a namespace that this method shouldn't access. This object has clearly properties that aren't my object's properties (among which a 'rand' method).

Best regards,

Cedric
0 Kudos