KingsburyLab / pyEQL

A Python library for solution chemistry
Other
64 stars 17 forks source link

UnicodeDecodeError when importing pyEQL #122

Closed xiaoxiaozhu123 closed 3 months ago

xiaoxiaozhu123 commented 5 months ago

This is what I encountered recently when importing pyEQL on a Windows 10 platform with system language as Chinese:

---------------------------------------------------------------------------
UnicodeDecodeError                        Traceback (most recent call last)
Cell In[1], line 1
----> 1 import pyEQL

File ~\.conda\envs\pyiclab\Lib\site-packages\pyEQL\__init__.py:54
     50 IonDB = JSONStore(str(json_db_file), key="formula")
     51 # By calling connect on init, we get the expensive JSON reading operation out
     52 # of the way. Subsequent calls to connect will bypass this and access the already-
     53 # instantiated Store in memory, which should speed up instantiation of Solution objects.
---> 54 IonDB.connect()
     56 from pyEQL.solution import Solution  # noqa: E402

File ~\.conda\envs\pyiclab\Lib\site-packages\maggma\stores\mongolike.py:694, in JSONStore.connect(self, force_reset)
    691         f.write(bytesdata.decode("utf-8"))
    693 for path in self.paths:
--> 694     objects = self.read_json_file(path)
    695     try:
    696         self.update(objects)

File ~\.conda\envs\pyiclab\Lib\site-packages\maggma\stores\mongolike.py:716, in JSONStore.read_json_file(self, path)
    708 """
    709 Helper method to read the contents of a JSON file and generate
    710 a list of docs.
   (...)
    713     path: Path to the JSON file to be read
    714 """
    715 with zopen(path) as f:
--> 716     data = f.read()
    717     data = data.decode() if isinstance(data, bytes) else data
    718     objects = bson.json_util.loads(data) if "$oid" in data else orjson.loads(data)

UnicodeDecodeError: 'gbk' codec can't decode byte 0xab in position 893: illegal multibyte sequence�

This bug origins from function zopen (which traces back to monty.io.zopen), Line 715, mongolike.py, maggma libray. Maybe the developpers didn't test their packages on non-English platforms... I have "fixed" this bug by mannually modifying source code Line 715 to:

with zopen(path, encoding='utf-8') as f:

However it won't help other users who might face the same issue. I am neither a maggma nor a monty user so I decide to report this bug here. I suggest the developers rewrite moty.io.zopen to force accept kw-argument encoding='utf-8' before importing magma on pyEQL's initalization. Here is a demo code snippet (forgive me for showing the ugly lambda):

import monty.io
zopen = monty.io.zopen
monty.io.zopen = lambda *args, **kwargs: zopen(*args, encoding='utf-8', **{k: v for k, v in kwargs.items() if k != 'encoding'})
import magma
monty.io.zopen = zopen # Don't forget to roll it back to avoid unexpected bugs
<import other libraries..>
rkingsbury commented 5 months ago

Hi @xiaoxiaozhu123 , thank you for the detailed report and proposed solution! I will open an issue in monty about this. (I am also a maggma maintainer, so if it's appropriate to address there, I can make those changes, too).

Before I open the bug report, can you please share the exact versions of maggma and monty and python that you have installed?

xiaoxiaozhu123 commented 5 months ago

Hi @xiaoxiaozhu123 , thank you for the detailed report and proposed solution! I will open an issue in monty about this. (I am also a maggma maintainer, so if it's appropriate to address there, I can make those changes, too).

Before I open the bug report, can you please share the exact versions of maggma and monty and python that you have installed?

python 3.11.8
monty.__version__
>> '2024.2.26'
maggma.__version__
>> '0.64.0'
xiaoxiaozhu123 commented 4 months ago

Hi @xiaoxiaozhu123 , thank you for the detailed report and proposed solution! I will open an issue in monty about this. (I am also a maggma maintainer, so if it's appropriate to address there, I can make those changes, too).

Before I open the bug report, can you please share the exact versions of maggma and monty and python that you have installed?

Hi. I wanted to follow up on an email I sent regarding a request for permission to utilize pyEQL's database within my project.

Would you be able to provide an update or any guidance on this matter?If you didn't receive the email, please let me know.