Jump to content
_c_

Comparing None (NIL) handles in Python strange error

Recommended Posts

I don't understand this.

if I use in an external editor (Visual Studio Code, should that be relevant) this code I get bucketloads of errors:

h = vs.GetCustomObjectPath( gPio_H )
if h is not None:
....

 

 

While the same, expressed with operators, doesn't:

h = vs.GetCustomObjectPath( gPio_H )
if h != None:
....

But only in the external editor. In a doc script both forms possible without any problem.

What could cause this?

 

Please read the Python Programming recommendations:

www.python.org/dev/peps/pep-0008/#programming-recommendations

 

 

Quote

Comparisons to singletons like None should always be done with is or is not, never the equality operators.

 

 

Share this post


Link to post

Possibly the level of error checking in Visual Studio?
I use PyCharm and have not much experience in VS. Pycharm recommends the h is not None flavor. Using the h != None, displays a warning to adjust my code.

Share this post


Link to post

The errors come up in VW, though.

 

I have a lot of problems but perhaps it depends on what I am doing. 

I was silly enough to begin my belated catching up of Python from.... vs.GetPt.

Share this post


Link to post

No, it is definitely weird. I do have it also in the normal VS editor.

 

This causes errors:

import vs
def LinkIt(obj):
    recH = vs.GetParametricRecord(obj)
    
    if recH is not None: # uses words
        vs.AlrtDialog(vs.GetName(recH))
        
def Sub(pt):
    vs.ForEachObjectAtPoint(LinkIt, 0, 0, pt[0], pt[1], 5)
    
h = vs.FSActLayer
vs.GetPt( Sub )

 

While this does not:

import vs

def LinkIt(obj):
	recH = vs.GetParametricRecord(obj)
	
	if recH != None: # uses operators
		vs.AlrtDialog(vs.GetName(recH))

def Sub(pt):
	vs.ForEachObjectAtPoint(LinkIt, 0, 0, pt[0], pt[1], 5)

h = vs.FSActLayer
vs.GetPt( Sub )

 

But this never gives problems:

 

import vs

def test(h):
	if h is not None: # uses words
		vs.AlrtDialog('not NIL')

h = vs.FSActLayer
test(h)

1115646917_Screenshot2021-01-24at12_48_14.thumb.png.329a2356a6cd131bb0f95ea6b18ef46c.png

Edited by _c_

Share this post


Link to post

You can also directly check "if ! h" the handle class has the boolean method implemented.

 

regards,

Patrick

Edited by PatW

Share this post


Link to post

Ciao @PatW, the vs.py comes from one of the latest SDK.

I don't think that the problem is in the None access, I think that there are underlying problems related to user interaction -or lack thereof.

The whole trouble comes from vs.GetPt in some subtle way.

Share this post


Link to post

I believe the issue is that the handle is actually a class and not a value, so that indeed it’s not None (the variable is assigned to an object), it’s that the object evaluates to nil. So your line 6 is actually succeeding when it shouldn’t and line 7 executes with a null pointer. I suspect that the internal/external editor difference is a red herring — you were just detecting an invalid object. 
 

The test I use is  != vs.Handle(0), that is, a handle that points to nothing, which is different from a non-existent handle. 

  • Like 1

Share this post


Link to post

And to piggy back on @PatW’s comment, it’s possible the class is set to override the = / != operator, so using those compares the value of the handle while is / is not compares the presence of the class. 

Share this post


Link to post

It has been said before and it is perfectly useless to say it again, but indeed variable declaration and handling, while less powerful, is clearly a no-brainer in Vectorscript Pascal, while in Python it is a major source of headaches. I can't begin even speaking of how much I miss Vector variables in Pascal. And yes, I am using vector classes in Python.

 

Rant off. I'll get fit in this fast enough.

 

Edited by _c_

Share this post


Link to post

And I am crashing/hanging non stop as soon as I use the GetPt call in PIO from a button event. But one step at a time.

Share this post


Link to post

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


 

7150 Riverwood Drive, Columbia, Maryland 21046, USA   |   Contact Us:   410-290-5114

 

© 2018 Vectorworks, Inc. All Rights Reserved. Vectorworks, Inc. is part of the Nemetschek Group.

×
×
  • Create New...