python-opcua icon indicating copy to clipboard operation
python-opcua copied to clipboard

Node id from string containing ';' character throws exception

Open szmgabor opened this issue 6 years ago • 7 comments

For a node id string like this: 'ns=2;s=myId;something' which is a valid node id string, but contains a ; in the identifier, the python opc-ua library throws the following exception, when I try to create a NodeId from the string:

Traceback (most recent call last):
  File "C:\Users\smg.SRO\AppData\Local\Programs\Python\Python36-32\lib\site-packages\opcua\ua\uatypes.py", line 339, 
	in from_string return NodeId._from_string(string)`
	
  File "C:\Users\smg.SRO\AppData\Local\Programs\Python\Python36-32\lib\site-packages\opcua\ua\uatypes.py", line 354, 
	in _from_string k, v = el.split("=", 1) ValueError: not enough values to unpack (expected 2, got 1)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "C:\Users\smg.SRO\AppData\Local\Programs\Python\Python36-32\lib\site-packages\opcua\client\client.py", line 509, 
	in get_node return Node(self.uaclient, nodeid)
	
  File "C:\Users\smg.SRO\AppData\Local\Programs\Python\Python36-32\lib\site-packages\opcua\common\node.py", line 46, 
	in __init__ self.nodeid = ua.NodeId.from_string(nodeid)
	
  File "C:\Users\smg.SRO\AppData\Local\Programs\Python\Python36-32\lib\site-packages\opcua\ua\uatypes.py", line 341, 
	in from_string raise UaStringParsingError("Error parsing string {0}".format(string), ex) opcua.ua.uaerrors._base.UaStringParsingError: 
	("Error parsing string ns=2;s=myId;something", ValueError('not enough values to unpack (expected 2, got 1)',))

As far as I have seen the problem is in the _from_string function in the NodeId class (uatypes.py file at line 344). The split on the semicolon from line 345 in my opinion should be done until the first occurrence of the ; and not all ; characters (like in case of the = at line 354).

I'm not an expert in the OPC UA standard, so I might be mistaken. Please let me know if this is correct as it is or not in your opinion.

Regards, Gabor.

szmgabor avatar Sep 18 '18 11:09 szmgabor

Hi szmgabor,

I am facing the same problem. Have you found out any solution for your problem?

Kind regards Thomas

TommyTomson585 avatar Mar 25 '21 10:03 TommyTomson585

it might be that we fixed that in opcua-asyncio

AndreasHeine avatar Mar 25 '21 14:03 AndreasHeine

Hi @TommyTomson585,

This was quite some time ago, but as far as I remember I've written my own implementation of the _from_string function.

The fix to this problem was rather simple, instead of l = string.split(";") you need l = string.split(";", 1) on the first line of that function.

Regards, Gabor

szmgabor avatar Mar 26 '21 04:03 szmgabor

why no PR ?

AndreasHeine avatar Mar 26 '21 06:03 AndreasHeine

Hi @all,

@szmgabor :It works fine with changing the "string.split" function.

@AndreasHeine: I tried it according to the 'client_to_kepware' example. But unfortunately it doesn't work with a string including two times ';'

But after all thank you for the feedback.

Regards

Thomas

TommyTomson585 avatar Mar 26 '21 13:03 TommyTomson585

this is fixed in opcua-asyncio

oroulet avatar Mar 26 '21 15:03 oroulet

@TommyTomson585, I'm glad it worked.

szmgabor avatar Mar 31 '21 04:03 szmgabor