How to make Robot Framework functions accept arguments as strings instead of the default unicode type

1,564 views
Skip to first unread message

binithb

unread,
Dec 3, 2013, 12:50:23 AM12/3/13
to robotframe...@googlegroups.com




I am writing a new RF library which is expected to take string arguments because the (pre-existing) Python library that I use is expecting strings, not unicode. Ofcourse I can convert each unicode to string before calling my existing function which supports only strings.

import ConfigParser

class RFConfigParser:

def get (self,section, option):
    print type (section) #prints unicode
    section = str (section) #works but I dont want to do this
    return self._config.get (section, option) #this pre-existing function expect a string input

The problem is I have lots of similar functions and in each of these functions I will have to call this unicode to string conversion circus.

Is there a straight forward way of doing this, so that the RF function will directly accept in string format

Another question is the default unicode support a Robot Framework feature or a RIDE feature ? (I am using RIDE, is that why I am getting this problem)


Thanks

binith

Pekka Klärck

unread,
Dec 3, 2013, 2:30:11 PM12/3/13
to binith a k, robotframework-users
Hello,

Robot Framework parses all normal test data to Unicode strings. There is no way to tell it to convert arguments to bytes (Python str object) when calling keywords. 

It's strange that your Python library handles only bytes and not Unicode even when you have only ASCII data. I would first look is it possible to change that. If that's not possible, you need to handle unicode -> str conversion yourself.

If you have lot of methods, you may want to consider writing a decorator that does the argument conversion for you. If there's a really big amount of those methods and all of them should handle the same conversion, you may consider using metaclasses or class decorators to automatically decorate all your functions. Notice, though, that your decorators need to be signature-preserving or otherwise Robo, RIDE, and Libdoc won't see correct arguments. This is discussed briefly in the User Guide:

Cheers,
    .peke


2013/12/3 binithb <bin...@gmail.com>

--
You received this message because you are subscribed to the Google Groups "robotframework-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to robotframework-u...@googlegroups.com.
To post to this group, send email to robotframe...@googlegroups.com.
Visit this group at http://groups.google.com/group/robotframework-users.
For more options, visit https://groups.google.com/groups/opt_out.



--
Agile Tester/Developer/Consultant :: http://eliga.fi
Lead Developer of Robot Framework :: http://robotframework.org

binithb

unread,
Dec 4, 2013, 7:41:40 AM12/4/13
to robotframe...@googlegroups.com, binith a k
Hi Peke,

Thanks for the time and the clarity.
The python library that I am using is the default ConfigParser module available by default with Python.
Anyway now that I have got official confirmation that there is no work around in RF for this, I will try to do something with ConfigParser itself so that can support unicode, that will be my first try anyhow.

Thanks again,
-binith




On Tuesday, 3 December 2013 11:30:11 UTC-8, Pekka Klärck wrote:
Hello,

Robot Framework parses all normal test data to Unicode strings. There is no way to tell it to convert arguments to bytes (Python str object) when calling keywords. 

It's strange that your Python library handles only bytes and not Unicode even when you have only ASCII data. I would first look is it possible to change that. If that's not possible, you need to handle unicode -> str conversion yourself.

If you have lot of methods, you may want to consider writing a decorator that does the argument conversion for you. If there's a really big amount of those methods and all of them should handle the same conversion, you may consider using metaclasses or class decorators to automatically decorate all your functions. Notice, though, that your decorators need to be signature-preserving or otherwise Robo, RIDE, and Libdoc won't see correct arguments. This is discussed briefly in the User Guide:

Cheers,
    .peke


2013/12/3 binithb <bin...@gmail.com>




I am writing a new RF library which is expected to take string arguments because the (pre-existing) Python library that I use is expecting strings, not unicode. Ofcourse I can convert each unicode to string before calling my existing function which supports only strings.

import ConfigParser

class RFConfigParser:

def get (self,section, option):
    print type (section) #prints unicode
    section = str (section) #works but I dont want to do this
    return self._config.get (section, option) #this pre-existing function expect a string input

The problem is I have lots of similar functions and in each of these functions I will have to call this unicode to string conversion circus.

Is there a straight forward way of doing this, so that the RF function will directly accept in string format

Another question is the default unicode support a Robot Framework feature or a RIDE feature ? (I am using RIDE, is that why I am getting this problem)


Thanks

binith

--
You received this message because you are subscribed to the Google Groups "robotframework-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to robotframework-users+unsub...@googlegroups.com.

To post to this group, send email to robotframe...@googlegroups.com.
Visit this group at http://groups.google.com/group/robotframework-users.
For more options, visit https://groups.google.com/groups/opt_out.

Pekka Klärck

unread,
Dec 4, 2013, 8:01:41 AM12/4/13
to binith a k, robotframework-users
2013/12/4 binithb <bin...@gmail.com>
>
> The python library that I am using is the default ConfigParser module available by default with Python.

I see. Apparently it not supporting Unicode is a know bug that
unfortunately will not be fixed in Python 2 series:
http://bugs.python.org/issue11597

> Anyway now that I have got official confirmation that there is no work around in RF for this, I will try to do something with ConfigParser itself so that can support unicode, that will be my first try anyhow.

Based on the discussion on the above bug report, your odds getting
ConfigParser fixed and fixes actually applied to Python standard
library is pretty slim. If using an external module isn't a problem,
you should try this version instead:
https://pypi.python.org/pypi/configparser

Alternatively you could see do workarounds discussed here work for you:
http://stackoverflow.com/questions/1648517/configparser-with-unicode-items

Cheers,
.peke

binithb

unread,
Dec 4, 2013, 8:50:22 AM12/4/13
to robotframe...@googlegroups.com, binith a k
Oh this again will save me a lot of time.
I dont have a problem using 3rd party libs, I will go ahead with the pypi configparser.
Will post back about the progress.

Thank you very much :)
-binith

binithb

unread,
Aug 13, 2014, 1:13:46 PM8/13/14
to robotframe...@googlegroups.com
And, it has worked well.
Reply all
Reply to author
Forward
0 new messages