Skip to content

Commit b0de31d

Browse files
authored
Merge pull request #1648 from ranaroussi/hotfix/tkr-tz-already-in-cache
Fix multithread error 'tz already in cache'
2 parents ddf0cf1 + cc87608 commit b0de31d

File tree

2 files changed

+58
-3
lines changed

2 files changed

+58
-3
lines changed

tests/utils.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
"""
2+
Tests for utils
3+
4+
To run all tests in suite from commandline:
5+
python -m unittest tests.utils
6+
7+
Specific test class:
8+
python -m unittest tests.utils.TestTicker
9+
10+
"""
11+
# import pandas as pd
12+
# import numpy as np
13+
14+
from .context import yfinance as yf
15+
from .context import session_gbl
16+
17+
import unittest
18+
# import requests_cache
19+
import tempfile
20+
21+
22+
class TestUtils(unittest.TestCase):
23+
session = None
24+
25+
@classmethod
26+
def setUpClass(cls):
27+
cls.tempCacheDir = tempfile.TemporaryDirectory()
28+
yf.set_tz_cache_location(cls.tempCacheDir.name)
29+
30+
@classmethod
31+
def tearDownClass(cls):
32+
cls.tempCacheDir.cleanup()
33+
34+
def test_storeTzNoRaise(self):
35+
# storing TZ to cache should never raise exception
36+
tkr = 'AMZN'
37+
tz1 = "America/New_York"
38+
tz2 = "London/Europe"
39+
cache = yf.utils.get_tz_cache()
40+
cache.store(tkr, tz1)
41+
cache.store(tkr, tz2)
42+
43+
44+
def suite():
45+
suite = unittest.TestSuite()
46+
suite.addTest(TestUtils('Test utils'))
47+
return suite
48+
49+
50+
if __name__ == '__main__':
51+
unittest.main()

yfinance/utils.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -980,10 +980,14 @@ def lookup(self, tkr):
980980
def store(self, tkr, tz):
981981
if tz is None:
982982
self.tz_db.delete(tkr)
983-
elif self.tz_db.get(tkr) is not None:
984-
raise Exception("Tkr {} tz already in cache".format(tkr))
985983
else:
986-
self.tz_db.set(tkr, tz)
984+
tz_db = self.tz_db.get(tkr)
985+
if tz_db is not None:
986+
if tz != tz_db:
987+
get_yf_logger().debug(f'{tkr}: Overwriting cached TZ "{tz_db}" with different TZ "{tz}"')
988+
self.tz_db.set(tkr, tz)
989+
else:
990+
self.tz_db.set(tkr, tz)
987991

988992
@property
989993
def _db_dir(self):

0 commit comments

Comments
 (0)