Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

" initializer for ctype 'char *' must be a bytes or list or tuple, not str" error #52

Open
vadimkatsman opened this issue Feb 28, 2023 · 0 comments

Comments

@vadimkatsman
Copy link

vadimkatsman commented Feb 28, 2023

When I was trying to setup configuration file for the py script I am running to call my .Net assembly, I've got this error:

  Traceback (most recent call last):
   File "C:\Program Files\Python310\lib\site-packages\pythonnet\__init__.py", line 73, in _create_runtime_from_spec
     return clr_loader.get_netfx(**params)
   File "C:\Program Files\Python310\lib\site-packages\clr_loader\__init__.py", line 168, in get_netfx
     impl = NetFx(domain=domain, config_file=_maybe_path(config_file))
   File "C:\Program Files\Python310\lib\site-packages\clr_loader\netfx.py", line 27, in __init__
     self._domain = _FW.pyclr_create_appdomain(domain_s, config_file_s)
 TypeError: initializer for ctype 'char *' must be a bytes or list or tuple, not str

As it appears the pyclr_create_appdomain end point expects LPUTF8Str and not LPStr.

That raises number of issues on its own - such as what if my config file is located inside the Unicode named path - but immediate problem was in the code:

        initialize()
        if config_file is not None:
            config_file_s = str(config_file) #.encode()
        else:
            config_file_s = ffi.NULL

        self._domain_name = domain
        self._config_file = config_file
        if domain is not None:
            domain_s = domain #.encode()
        else:
            domain_s = ffi.NULL
        self._domain = _FW.pyclr_create_appdomain(domain_s, config_file_s)

As you could see I made a local fix adding .encode() call before passing parameters to the end point.

Questions:

  1. Am I doing something wrong? Here is my code below:
import os
from pathlib import Path
config_file = r"D:\_WorkRoot\Tests\Python_ASR\Diarization\_Examples\Enrichment\sctlib.py.config"
#os.environ["PYTHONNET_NETFX_CONFIG_FILE"] = config_file
config_path = Path(config_file)
from pythonnet import set_runtime
#set_runtime("netfx", domain="sctlib_py", config_file=config_path)
set_runtime("netfx", domain="sctlib_py")
  1. It took me awhile to recognize I don't need to construct Path inside my script and passing string from my end works too. But in your code, you take my string, convert to Path only to convert it back to string. Maybe to validate the path exists? Just a comment.

  2. I made a local change adding .encode() call to by-pass the error. What you want me to do? Abandon that - since you already addresses that, keep my local changes (and remember maintaining that on different machines) or to apply the fix to the repo (I have not done such contributions before, so I simply don't know the customary procedures).

Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant