trying to put python .pyc file to .py, "Unknown type 64 #" error occured - uncompyle6

I'm trying to decompile a python .pyc file into a source code .py file with uncompyle6, but I'm getting the error "Unknown type 64 #".
Here is the error message:
Unknown type 64 #
Unknown type 0
Unknown type 0
Unknown type 0
Unknown type 64 #
Unknown type 0
Unknown type 0
Unknown type 0
Unknown type 15
Unknown type 24
Unknown type 125 }
Unknown type 2
Unknown type 124 |
Unknown type 2
Unknown type 32
Unknown type 1
Unknown type 87 W
Traceback (most recent call last):
File "c:\users\angela\appdata\local\programs\python\python39\lib\site-packages\xdis\load.py", line 304, in load_module_from_file_object
co = xdis.unmarshal.load_code(fp, magic_int, code_objects)
File "c:\users\angela\appdata\local\programs\python\python39\lib\site-packages\xdis\unmarshal.py", line 565, in load_code
return um_gen.load()
File "c:\users\angela\appdata\local\programs\python\python39\lib\site-packages\xdis\unmarshal.py", line 179, in load
return self.r_object()
File "c:\users\angela\appdata\local\programs\python\python39\lib\site-packages\xdis\unmarshal.py", line 222, in r_object
return unmarshal_func(save_ref, bytes_for_s)
File "c:\users\angela\appdata\local\programs\python\python39\lib\site-packages\xdis\unmarshal.py", line 500, in t_code
co_cellvars = self.r_object(bytes_for_s=bytes_for_s)
File "c:\users\angela\appdata\local\programs\python\python39\lib\site-packages\xdis\unmarshal.py", line 222, in r_object
return unmarshal_func(save_ref, bytes_for_s)
File "c:\users\angela\appdata\local\programs\python\python39\lib\site-packages\xdis\unmarshal.py", line 381, in t_small_tuple
ret += (self.r_object(bytes_for_s=bytes_for_s),)
File "c:\users\angela\appdata\local\programs\python\python39\lib\site-packages\xdis\unmarshal.py", line 222, in r_object
return unmarshal_func(save_ref, bytes_for_s)
File "c:\users\angela\appdata\local\programs\python\python39\lib\site-packages\xdis\unmarshal.py", line 500, in t_code
co_cellvars = self.r_object(bytes_for_s=bytes_for_s)
File "c:\users\angela\appdata\local\programs\python\python39\lib\site-packages\xdis\unmarshal.py", line 222, in r_object
return unmarshal_func(save_ref, bytes_for_s)
File "c:\users\angela\appdata\local\programs\python\python39\lib\site-packages\xdis\unmarshal.py", line 381, in t_small_tuple
ret += (self.r_object(bytes_for_s=bytes_for_s),)
File "c:\users\angela\appdata\local\programs\python\python39\lib\site-packages\xdis\unmarshal.py", line 222, in r_object
return unmarshal_func(save_ref, bytes_for_s)
File "c:\users\angela\appdata\local\programs\python\python39\lib\site-packages\xdis\unmarshal.py", line 486, in t_code
co_code = self.r_object(bytes_for_s=True)
File "c:\users\angela\appdata\local\programs\python\python39\lib\site-packages\xdis\unmarshal.py", line 222, in r_object
return unmarshal_func(save_ref, bytes_for_s)
File "c:\users\angela\appdata\local\programs\python\python39\lib\site-packages\xdis\unmarshal.py", line 520, in t_code
code = to_portable(
File "c:\users\angela\appdata\local\programs\python\python39\lib\site-packages\xdis\codetype\__init__.py", line 200, in to_portable
return codeType2Portable(code, version_triple)
File "c:\users\angela\appdata\local\programs\python\python39\lib\site-packages\xdis\codetype\__init__.py", line 43, in codeType2Portable
return Code3(
File "c:\users\angela\appdata\local\programs\python\python39\lib\site-packages\xdis\codetype\code30.py", line 80, in __init__
self.check()
File "c:\users\angela\appdata\local\programs\python\python39\lib\site-packages\xdis\codetype\code13.py", line 87, in check
assert type(val) in fieldtype, "%s should be one of the types %s; is type %s" % (field, fieldtype, type(val))
AssertionError: co_code should be one of the types (<class 'str'>, <class 'bytes'>, <class 'list'>, <class 'tuple'>); is type <class 'NoneType'>
Ill-formed bytecode file fbs13.pyc
<class 'AssertionError'>; co_code should be one of the types (<class 'str'>, <class 'bytes'>, <class 'list'>, <class 'tuple'>); is type <class 'NoneType'>
I tried to change the magic number in the header of the .pyc file but no change.
After searching on the internet some people said it was because my operating system was 64bit.
Does anyone have any idea. thanks in advance.

Related

Python3 library pyxb throwing NamespaceError that category has no elementBiding

I've started getting pyxb error while parsing a response. It worked before. The classes that parse the XML are made with pyxb from a the schema.
The XML is:
<result xmlns="http://payment.website.com/Schema/V2/Res">
<success>true</success>
<referenceId>reference_id_hash</referenceId>
<purchaseId>purchase_id_hash</purchaseId>
<returnType>REDIRECT</returnType>
<redirectUrl>https://payment.website.com/redirect/8f9359e15709e87bd0e4/MTAzYWQ5YWE4YjRjOGRjMjJ=</redirectUrl>
</result>
When I execute the CreateFromDocument I get an error that I cannot figure it out.
transaction_result = result.CreateFromDocument(response_content)
File "/usr/lib/python3.8/xml/sax/expatreader.py", line 111, in parse
xmlreader.IncrementalParser.parse(self, source)
File "/usr/lib/python3.8/xml/sax/xmlreader.py", line 125, in parse
self.feed(buffer)
File "/usr/lib/python3.8/xml/sax/expatreader.py", line 217, in feed
self._parser.Parse(data, isFinal)
File "../Modules/pyexpat.c", line 407, in StartElement
File "/usr/lib/python3.8/xml/sax/expatreader.py", line 369, in start_element_ns
self._cont_handler.startElementNS(pair, None,
File "/opt/venv/lib/python3.8/site-packages/pyxb/binding/saxer.py", line 324, in startElementNS
element_binding = name_en.elementBinding()
File "/opt/venv//lib/python3.8/site-packages/pyxb/namespace/__init__.py", line 104, in __getattr__
category_value = ns.categoryMap(name).get(self.localName())
File "/opt/venv/lib/python3.8/site-packages/pyxb/namespace/__init__.py", line 309, in categoryMap
raise pyxb.NamespaceError(self, '%s has no category %s' % (self, category))
Exception
pyxb.exceptions_.NamespaceError: http://payment.website.com/Schema/V2/Res has no category elementBinding 6 0.006 0.364
How can I fix this?

Invalid argument: TypeError: an integer is required (got type NoneType) when closing Tensorflow

I'm implementing the IMPALA framework https://github.com/deepmind/scalable_agent which uses tensorflows multiprocessing. Everything seems to work fine as the experiment ran for the specified number of steps... however I start to get a type error when tensorflow is closing processes.
Does someone know what could've contributed to this error and how to solve it? This seems to be something with tensorflow rather than the original code itself.
INFO:tensorflow:Closing all processes.
[750. 450. 25.125]
2020-01-13 17:01:00.624889: W tensorflow/core/framework/op_kernel.cc:1389] Invalid argument: TypeError: an integer is required (got type NoneType)
Traceback (most recent call last):
File "/home/haianh/anaconda3/envs/lab/lib/python3.6/site-packages/tensorflow/python/ops/script_ops.py", line 207, in __call__
ret = func(*args)
File "/home/haianh/anaconda3/envs/lab/scalable_agent/py_process.py", line 86, in py_call
result = self._out.recv()
File "/home/haianh/anaconda3/envs/lab/lib/python3.6/multiprocessing/connection.py", line 250, in recv
buf = self._recv_bytes()
File "/home/haianh/anaconda3/envs/lab/lib/python3.6/multiprocessing/connection.py", line 411, in _recv_bytes
return self._recv(size)
File "/home/haianh/anaconda3/envs/lab/lib/python3.6/multiprocessing/connection.py", line 379, in _recv
chunk = read(handle, remaining)
TypeError: an integer is required (got type NoneType)
INFO:tensorflow:Error reported to Coordinator: <class 'tensorflow.python.framework.errors_impl.InvalidArgumentError'>, TypeError: an integer is required (got type NoneType)
Traceback (most recent call last):
File "/home/haianh/anaconda3/envs/lab/lib/python3.6/site-packages/tensorflow/python/ops/script_ops.py", line 207, in __call__
ret = func(*args)
File "/home/haianh/anaconda3/envs/lab/scalable_agent/py_process.py", line 86, in py_call
result = self._out.recv()
File "/home/haianh/anaconda3/envs/lab/lib/python3.6/multiprocessing/connection.py", line 250, in recv
buf = self._recv_bytes()
File "/home/haianh/anaconda3/envs/lab/lib/python3.6/multiprocessing/connection.py", line 411, in _recv_bytes
return self._recv(size)
File "/home/haianh/anaconda3/envs/lab/lib/python3.6/multiprocessing/connection.py", line 379, in _recv
chunk = read(handle, remaining)
TypeError: an integer is required (got type NoneType)
[[{{node scan/while/flow_environment_step/step}}]]
INFO:tensorflow:All processes closed.
Traceback (most recent call last):
File "experiment.py", line 689, in <module>
tf.app.run()
File "/home/haianh/anaconda3/envs/lab/lib/python3.6/site-packages/tensorflow/python/platform/app.py", line 125, in run
_sys.exit(main(argv))
File "experiment.py", line 683, in main
train(action_set, level_names)
File "experiment.py", line 630, in train
session.run(enqueue_ops)
File "/home/haianh/anaconda3/envs/lab/lib/python3.6/site-packages/tensorflow/python/training/monitored_session.py", line 788, in __exit__
self._close_internal(exception_type)
File "/home/haianh/anaconda3/envs/lab/lib/python3.6/site-packages/tensorflow/python/training/monitored_session.py", line 826, in _close_internal
self._sess.close()
File "/home/haianh/anaconda3/envs/lab/lib/python3.6/site-packages/tensorflow/python/training/monitored_session.py", line 1082, in close
self._sess.close()
File "/home/haianh/anaconda3/envs/lab/lib/python3.6/site-packages/tensorflow/python/training/monitored_session.py", line 1244, in close
ignore_live_threads=True)
File "/home/haianh/anaconda3/envs/lab/lib/python3.6/site-packages/tensorflow/python/training/coordinator.py", line 389, in join
six.reraise(*self._exc_info_to_raise)
File "/home/haianh/anaconda3/envs/lab/lib/python3.6/site-packages/six.py", line 696, in reraise
raise value
File "/home/haianh/anaconda3/envs/lab/lib/python3.6/site-packages/tensorflow/python/training/queue_runner_impl.py", line 257, in _run
enqueue_callable()
File "/home/haianh/anaconda3/envs/lab/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1257, in _single_operation_run
self._call_tf_sessionrun(None, {}, [], target_list, None)
File "/home/haianh/anaconda3/envs/lab/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1407, in _call_tf_sessionrun
run_metadata)
tensorflow.python.framework.errors_impl.InvalidArgumentError: TypeError: an integer is required (got type NoneType)
Traceback (most recent call last):
File "/home/haianh/anaconda3/envs/lab/lib/python3.6/site-packages/tensorflow/python/ops/script_ops.py", line 207, in __call__
ret = func(*args)
File "/home/haianh/anaconda3/envs/lab/scalable_agent/py_process.py", line 86, in py_call
result = self._out.recv()
File "/home/haianh/anaconda3/envs/lab/lib/python3.6/multiprocessing/connection.py", line 250, in recv
buf = self._recv_bytes()
File "/home/haianh/anaconda3/envs/lab/lib/python3.6/multiprocessing/connection.py", line 411, in _recv_bytes
return self._recv(size)
File "/home/haianh/anaconda3/envs/lab/lib/python3.6/multiprocessing/connection.py", line 379, in _recv
chunk = read(handle, remaining)
TypeError: an integer is required (got type NoneType)
[[{{node scan/while/flow_environment_step/step}}]]

Using TIA to pull FX rates from BBG?

I'm trying to use the TIA module to pull EURUSD forward rates from the BBG API? How does one go about doing this?
I tried using the BbgDataManager() to specify a specific forward rate but don't seem to be having any success. The code I tried is below.
df = mgr['EURUSD Curncy','FWD_CURVE']
df
MultiSidAccessor(EURUSD Curncy,FWD_CURVE)
df.FWD_CURVE
Produces the following error message:
File "", line 1
df.EURUSD Curncy
^
SyntaxError: invalid syntax
df.FWD_CURVE
Traceback (most recent call last):
File "", line 1, in
File "C:\Users\anthony.yeh\PycharmProjects\blpapi\venv\lib\site-packages\tia\bbg\datamgr.py", line 85, in getattribute
return self.get_attributes(item, **self.overrides)
File "C:\Users\anthony.yeh\PycharmProjects\blpapi\venv\lib\site-packages\tia\bbg\datamgr.py", line 90, in get_attributes
frame = self.mgr.get_attributes(self.sids, flds, **overrides)
File "C:\Users\anthony.yeh\PycharmProjects\blpapi\venv\lib\site-packages\tia\bbg\datamgr.py", line 148, in get_attributes
return self.terminal.get_reference_data(sids, flds, **overrides).as_frame()
File "C:\Users\anthony.yeh\PycharmProjects\blpapi\venv\lib\site-packages\tia\bbg\v3api.py", line 745, in get_reference_data
return self.execute(req)
File "C:\Users\anthony.yeh\PycharmProjects\blpapi\venv\lib\site-packages\tia\bbg\v3api.py", line 729, in execute
request.has_exception and request.raise_exception()
File "C:\Users\anthony.yeh\PycharmProjects\blpapi\venv\lib\site-packages\tia\bbg\v3api.py", line 215, in raise_exception
raise Exception('SecurityError: %s' % ','.join(msgs))
Exception: SecurityError: (FWD_CURVE, BAD_SEC, Unknown/Invalid Security [nid:2972] )
Similarly, using the mgr with a query similar to the way you would pull this in Excel using BFxForward produces errors..
eurusd_sids = mgr["eurusd curncy","9/12/2019","midoutright"]\
eurusd_sids.PX_LAST
produces this error message:
Traceback (most recent call last):
File "", line 1, in
File "C:\Users\anthony.yeh\PycharmProjects\blpapi\venv\lib\site-packages\tia\bbg\datamgr.py", line 85, in getattribute
return self.get_attributes(item, **self.overrides)
File "C:\Users\anthony.yeh\PycharmProjects\blpapi\venv\lib\site-packages\tia\bbg\datamgr.py", line 90, in get_attributes
frame = self.mgr.get_attributes(self.sids, flds, **overrides)
File "C:\Users\anthony.yeh\PycharmProjects\blpapi\venv\lib\site-packages\tia\bbg\datamgr.py", line 148, in get_attributes
return self.terminal.get_reference_data(sids, flds, **overrides).as_frame()
File "C:\Users\anthony.yeh\PycharmProjects\blpapi\venv\lib\site-packages\tia\bbg\v3api.py", line 745, in get_reference_data
return self.execute(req)
File "C:\Users\anthony.yeh\PycharmProjects\blpapi\venv\lib\site-packages\tia\bbg\v3api.py", line 729, in execute
request.has_exception and request.raise_exception()
File "C:\Users\anthony.yeh\PycharmProjects\blpapi\venv\lib\site-packages\tia\bbg\v3api.py", line 215, in raise_exception
raise Exception('SecurityError: %s' % ','.join(msgs))
Exception: SecurityError: (9/12/2019, BAD_SEC, Unknown/Invalid Security [nid:2972] ),(midoutright, BAD_SEC, Unknown/Invalid Security [nid:2972] )
You may try with "EURUSD BGN Curncy"

numpy array advanced indexing in numba

I'm converting a function to numba (nopython mode). In this function I need to access some elements of a 2D array, whose row indices are contained in a list and the column is fixed.
In usual python I do:
array[list,column_index]
but when compiling in numba I receive the following error:
TypingError: Invalid use of Function(<built-in function getitem>) with argument(s) of type(s): (array(float64, 2d, C), (reflected list(int64), int64))
* parameterized
In definition 0:
All templates rejected with literals.
In definition 1:
All templates rejected without literals.
In definition 2:
All templates rejected with literals.
In definition 3:
All templates rejected without literals.
In definition 4:
All templates rejected with literals.
In definition 5:
All templates rejected without literals.
In definition 6:
TypeError: unsupported array index type reflected list(int64) in (reflected list(int64), int64)
raised from C:\ProgramData\Anaconda2\envs\Python 3.5\lib\site-packages\numba\typing\arraydecl.py:71
In definition 7:
TypeError: unsupported array index type reflected list(int64) in (reflected list(int64), int64)
raised from C:\ProgramData\Anaconda2\envs\Python 3.5\lib\site-packages\numba\typing\arraydecl.py:71
This error is usually caused by passing an argument of a type that is unsupported by the named function.
I already tried converting the list to an array as it would seem the correct way from https://numba.pydata.org/numba-doc/dev/reference/numpysupported.html
"A subset of advanced indexing is also supported: only one advanced index is allowed, and it has to be a one-dimensional array (it can be combined with an arbitrary number of basic indices as well)"
but coding:
array[np.array(list),column_index]
produces the following error:
Traceback (most recent call last):
File "<ipython-input-123-c5ca441bb6b7>", line 1, in <module>
tmp = L.numba_montecarlo_barab(SEED,w,R,T,mpx,0.5)
File "C:\ProgramData\Anaconda2\envs\Python 3.5\lib\site-packages\numba\dispatcher.py", line 367, in _compile_for_args
raise e
File "C:\ProgramData\Anaconda2\envs\Python 3.5\lib\site-packages\numba\dispatcher.py", line 324, in _compile_for_args
return self.compile(tuple(argtypes))
File "C:\ProgramData\Anaconda2\envs\Python 3.5\lib\site-packages\numba\compiler_lock.py", line 32, in _acquire_compile_lock
return func(*args, **kwargs)
File "C:\ProgramData\Anaconda2\envs\Python 3.5\lib\site-packages\numba\dispatcher.py", line 655, in compile
cres = self._compiler.compile(args, return_type)
File "C:\ProgramData\Anaconda2\envs\Python 3.5\lib\site-packages\numba\dispatcher.py", line 82, in compile
pipeline_class=self.pipeline_class)
File "C:\ProgramData\Anaconda2\envs\Python 3.5\lib\site-packages\numba\compiler.py", line 926, in compile_extra
return pipeline.compile_extra(func)
File "C:\ProgramData\Anaconda2\envs\Python 3.5\lib\site-packages\numba\compiler.py", line 374, in compile_extra
return self._compile_bytecode()
File "C:\ProgramData\Anaconda2\envs\Python 3.5\lib\site-packages\numba\compiler.py", line 857, in _compile_bytecode
return self._compile_core()
File "C:\ProgramData\Anaconda2\envs\Python 3.5\lib\site-packages\numba\compiler.py", line 844, in _compile_core
res = pm.run(self.status)
File "C:\ProgramData\Anaconda2\envs\Python 3.5\lib\site-packages\numba\compiler_lock.py", line 32, in _acquire_compile_lock
return func(*args, **kwargs)
File "C:\ProgramData\Anaconda2\envs\Python 3.5\lib\site-packages\numba\compiler.py", line 255, in run
raise patched_exception
File "C:\ProgramData\Anaconda2\envs\Python 3.5\lib\site-packages\numba\compiler.py", line 246, in run
stage()
File "C:\ProgramData\Anaconda2\envs\Python 3.5\lib\site-packages\numba\compiler.py", line 717, in stage_nopython_backend
self._backend(lowerfn, objectmode=False)
File "C:\ProgramData\Anaconda2\envs\Python 3.5\lib\site-packages\numba\compiler.py", line 666, in _backend
lowered = lowerfn()
File "C:\ProgramData\Anaconda2\envs\Python 3.5\lib\site-packages\numba\compiler.py", line 653, in backend_nopython_mode
self.metadata)
File "C:\ProgramData\Anaconda2\envs\Python 3.5\lib\site-packages\numba\compiler.py", line 1050, in native_lowering_stage
lower.create_cpython_wrapper(flags.release_gil)
File "C:\ProgramData\Anaconda2\envs\Python 3.5\lib\site-packages\numba\lowering.py", line 272, in create_cpython_wrapper
release_gil=release_gil)
File "C:\ProgramData\Anaconda2\envs\Python 3.5\lib\site-packages\numba\targets\cpu.py", line 149, in create_cpython_wrapper
builder.build()
File "C:\ProgramData\Anaconda2\envs\Python 3.5\lib\site-packages\numba\callwrapper.py", line 122, in build
self.build_wrapper(api, builder, closure, args, kws)
File "C:\ProgramData\Anaconda2\envs\Python 3.5\lib\site-packages\numba\callwrapper.py", line 155, in build_wrapper
val = cleanup_manager.add_arg(builder.load(obj), ty)
File "C:\ProgramData\Anaconda2\envs\Python 3.5\lib\site-packages\numba\callwrapper.py", line 58, in add_arg
cleanup_arg()
File "C:\ProgramData\Anaconda2\envs\Python 3.5\lib\site-packages\numba\callwrapper.py", line 41, in cleanup_arg
self.api.reflect_native_value(ty, native.value, self.env_manager)
File "C:\ProgramData\Anaconda2\envs\Python 3.5\lib\site-packages\numba\pythonapi.py", line 1417, in reflect_native_value
impl(typ, val, c)
File "C:\ProgramData\Anaconda2\envs\Python 3.5\lib\site-packages\numba\targets\boxing.py", line 740, in reflect_list
raise TypeError(msg)
TypeError: Failed in nopython mode pipeline (step: nopython mode backend)
cannot reflect element of reflected container: reflected list(reflected list(reflected list(int64)))
also converting to a tuple didn't work, but I understand that tuple are not supported.
What is the correct way to use advanced indexing in numba nopython mode?

Blank sessionid cookie causes error in request.user

An end user somehow ended up with their sessionid cookie blank (as in "sessionid=;"). This causes the following error call stack (below the function call to request.user) when using Django in conjunction with GAE:
File "/src/django/utils/functional.py", line 204, in inner
self._setup()
File "/src/django/utils/functional.py", line 270, in _setup
self._wrapped = self._setupfunc()
File "/src/django/contrib/auth/middleware.py", line 18, in <lambda>
request.user = SimpleLazyObject(lambda: get_user(request))
File "/src/django/contrib/auth/middleware.py", line 10, in get_user
request._cached_user = auth.get_user(request)
File "/src/django/contrib/auth/__init__.py", line 136, in get_user
user_id = request.session[SESSION_KEY]
File "/src/django/contrib/sessions/backends/base.py", line 44, in __getitem__
return self._session[key]
File "/src/django/contrib/sessions/backends/base.py", line 167, in _get_session
self._session_cache = self.load()
File "/src/django/contrib/sessions/backends/cached_db.py", line 39, in load
expire_date__gt=timezone.now()
File "/src/django/db/models/manager.py", line 143, in get
return self.get_query_set().get(*args, **kwargs)
File "/src/django/db/models/query.py", line 398, in get
num = len(clone)
File "/src/django/db/models/query.py", line 106, in __len__
self._result_cache = list(self.iterator())
File "/src/django/db/models/query.py", line 317, in iterator
for row in compiler.results_iter():
File "/src/djangotoolbox/db/basecompiler.py", line 375, in results_iter
results = self.build_query(fields).fetch(
File "/src/djangotoolbox/db/basecompiler.py", line 481, in build_query
query.add_filters(self.query.where)
File "/src/djangotoolbox/db/basecompiler.py", line 174, in add_filters
self.add_filters(child)
File "/src/djangotoolbox/db/basecompiler.py", line 176, in add_filters
field, lookup_type, value = self._decode_child(child)
File "/src/djangotoolbox/db/basecompiler.py", line 216, in _decode_child
lookup_type, value, field, annotation)
File "/src/djangotoolbox/db/basecompiler.py", line 254, in _normalize_lookup_value
return self.ops.value_for_db(value, field, lookup_type)
File "/src/djangoappengine/db/base.py", line 128, in value_for_db
return super_value_for_db(value, field, lookup)
File "/src/djangotoolbox/db/base.py", line 245, in value_for_db
field_kind, db_type, lookup)
File "/src/djangoappengine/db/base.py", line 160, in _value_for_db
raise DatabaseError("Only strings and positive integers "
DatabaseError: Only strings and positive integers may be used as keys on GAE.
This error does not occur if sessionid is set to some invalid non-empty value (such as "session=garbage;"). I think it is related to follow contrast of behavior in a python shell:
>>> Session.objects.filter(session_key='abc').exists()
0
>>> Session.objects.filter(session_key='').exists()
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/src/django/db/models/query.py", line 610, in exists
return self.query.has_results(using=self.db)
File "/src/django/db/models/sql/query.py", line 445, in has_results
return compiler.has_results()
File "/src/dbindexer/compiler.py", line 32, in has_results
return super(SQLCompiler, self).has_results()
File "/src/djangotoolbox/db/basecompiler.py", line 384, in has_results
return self.get_count(check_exists=True)
File "/src/djangotoolbox/db/basecompiler.py", line 468, in get_count
return self.build_query().count(high_mark)
File "/src/djangotoolbox/db/basecompiler.py", line 481, in build_query
query.add_filters(self.query.where)
File "/src/djangotoolbox/db/basecompiler.py", line 174, in add_filters
self.add_filters(child)
File "/src/djangotoolbox/db/basecompiler.py", line 176, in add_filters
field, lookup_type, value = self._decode_child(child)
File "/src/djangotoolbox/db/basecompiler.py", line 216, in _decode_child
lookup_type, value, field, annotation)
File "/src/djangotoolbox/db/basecompiler.py", line 254, in _normalize_lookup_value
return self.ops.value_for_db(value, field, lookup_type)
File "/src/djangoappengine/db/base.py", line 128, in value_for_db
return super_value_for_db(value, field, lookup)
File "/src/djangotoolbox/db/base.py", line 245, in value_for_db
field_kind, db_type, lookup)
File "/src/djangoappengine/db/base.py", line 160, in _value_for_db
raise DatabaseError("Only strings and positive integers "
DatabaseError: Only strings and positive integers may be used as keys on GAE.
Is this a djangoappengine or djangotoolbox bug, or a Django bug? What's the proper way to prevent this error, and consider user unauthenticated?
Ok, I think I may have to add a middleware class to handle this special case, and place it directly after SessionMiddleware:
class EmptySessionMiddleware(object):
def process_request(self, request):
session = request.session
if session.session_key is not None and len(session.session_key) == 0:
logging.info('[EmptySessionMiddleware] setting empty session key to None')
session._session_key = None
It's a weird special case, but basically the problem is that Django session middleware only checks for None session before looking up in db (not empty string), and an empty string primary key query in djangoappengine raises an exception. I'm not sure there's another way to handle this case.