Completed
Push — master ( 64314c...9b9cff )
by Kyle
58s
created

_cdata()   B

Complexity

Conditions 5

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
cc 5
c 3
b 0
f 1
dl 0
loc 11
rs 8.5454

1 Method

Rating   Name   Duplication   Size   Complexity  
A _bytes() 0 4 2
1
"""This module focuses on improvements to the Python libtcod API.
2
"""
3
from __future__ import absolute_import as _
4
5
import os as _os
0 ignored issues
show
Unused Code introduced by
Unused os imported as _os
Loading history...
6
import sys as _sys
7
8
import platform as _platform
0 ignored issues
show
Unused Code introduced by
Unused platform imported as _platform
Loading history...
9
import weakref as _weakref
0 ignored issues
show
Unused Code introduced by
Unused weakref imported as _weakref
Loading history...
10
import functools as _functools
0 ignored issues
show
Unused Code introduced by
Unused functools imported as _functools
Loading history...
11
12
import numpy as _np
0 ignored issues
show
Configuration introduced by
The import numpy could not be resolved.

This can be caused by one of the following:

1. Missing Dependencies

This error could indicate a configuration issue of Pylint. Make sure that your libraries are available by adding the necessary commands.

# .scrutinizer.yml
before_commands:
    - sudo pip install abc # Python2
    - sudo pip3 install abc # Python3
Tip: We are currently not using virtualenv to run pylint, when installing your modules make sure to use the command for the correct version.

2. Missing __init__.py files

This error could also result from missing __init__.py files in your module folders. Make sure that you place one file in each sub-folder.

Loading history...
Unused Code introduced by
Unused numpy imported as _np
Loading history...
13
14
from tcod.libtcod import lib, ffi, BKGND_DEFAULT, BKGND_SET
0 ignored issues
show
Bug introduced by
The name BKGND_DEFAULT does not seem to exist in module tcod.libtcod.
Loading history...
Bug introduced by
The name BKGND_SET does not seem to exist in module tcod.libtcod.
Loading history...
Unused Code introduced by
Unused BKGND_SET imported from tcod.libtcod
Loading history...
Unused Code introduced by
Unused BKGND_DEFAULT imported from tcod.libtcod
Loading history...
Unused Code introduced by
Unused lib imported from tcod.libtcod
Loading history...
15
16
17
def _unpack_char_p(char_p):
0 ignored issues
show
Coding Style introduced by
This function should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
18
    if char_p == ffi.NULL:
19
        return ''
20
    return ffi.string(char_p).decode()
21
22
23
def _int(int_or_str):
24
    'return an integer where a single character string may be expected'
25
    if isinstance(int_or_str, str):
26
        return ord(int_or_str)
27
    if isinstance(int_or_str, bytes):
28
        return int_or_str[0]
29
    return int(int_or_str) # check for __count__
30
31
32
if _sys.version_info[0] == 2: # Python 2
33
    def _bytes(string):
0 ignored issues
show
Coding Style introduced by
This function should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
34
        if isinstance(string, unicode):
0 ignored issues
show
Comprehensibility Best Practice introduced by
Undefined variable 'unicode'
Loading history...
35
            return string.encode()
36
        return string
37
38
    def _unicode(string):
0 ignored issues
show
Coding Style introduced by
This function should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
39
        if not isinstance(string, unicode):
0 ignored issues
show
Comprehensibility Best Practice introduced by
Undefined variable 'unicode'
Loading history...
40
            return string.decode('latin-1')
41
        return string
42
43
else: # Python 3
44
    def _bytes(string):
0 ignored issues
show
Coding Style introduced by
This function should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
45
        if isinstance(string, str):
46
            return string.encode()
47
        return string
48
49
    def _unicode(string):
0 ignored issues
show
Coding Style introduced by
This function should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
50
        if isinstance(string, bytes):
51
            return string.decode('latin-1')
52
        return string
53
54
55
def _fmt_bytes(string):
0 ignored issues
show
Coding Style introduced by
This function should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
56
    return _bytes(string).replace(b'%', b'%%')
57
58
def _fmt_unicode(string):
0 ignored issues
show
Coding Style introduced by
This function should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
59
    return _unicode(string).replace(u'%', u'%%')
60
61
62
class _PropagateException():
63
    """ context manager designed to propagate exceptions outside of a cffi
64
    callback context.  normally cffi suppresses the exception
65
66
    when propagate is called this class will hold onto the error until the
67
    control flow leaves the context, then the error will be raised
68
69
    with _PropagateException as propagate:
70
    # give propagate as onerror parameter for ffi.def_extern
71
    """
72
73
    def __init__(self):
74
        self.exc_info = None # (exception, exc_value, traceback)
75
76
    def propagate(self, *exc_info):
77
        """ set an exception to be raised once this context exits
78
79
        if multiple errors are caught, only keep the first exception raised
80
        """
81
        if not self.exc_info:
82
            self.exc_info = exc_info
83
84
    def __enter__(self):
85
        """ once in context, only the propagate call is needed to use this
86
        class effectively
87
        """
88
        return self.propagate
89
90
    def __exit__(self, type, value, traceback):
0 ignored issues
show
Bug Best Practice introduced by
This seems to re-define the built-in type.

It is generally discouraged to redefine built-ins as this makes code very hard to read.

Loading history...
91
        """ if we're holding on to an exception, raise it now
92
93
        prefers our held exception over any current raising error
94
95
        self.exc_info is reset now in case of nested manager shenanigans
96
        """
97
        if self.exc_info:
98
            type, value, traceback = self.exc_info
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are trying to unpack a non-sequence, which was defined at line 74.
Loading history...
Bug introduced by
The tuple unpacking with sequence seems to be unbalanced; 3 value(s) for 0 label(s)

This happens when the amount of values does not equal the amount of labels:

a, b = ("a", "b", "c")  # only 2 labels for 3 values
Loading history...
Bug Best Practice introduced by
It seems like you are trying to unpack a non-sequence, which was defined at line 99.
Loading history...
99
            self.exc_info = None
100
        if type:
101
            # Python 2/3 compatible throw
102
            exception = type(value)
103
            exception.__traceback__ = traceback
104
            raise exception
105
106
107
class _CDataWrapper(object):
0 ignored issues
show
Coding Style introduced by
This class should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
108
109
    def __init__(self, *args, **kargs):
110
        self.cdata = self._get_cdata_from_args(*args, **kargs)
111
        if self.cdata == None:
112
            self.cdata = ffi.NULL
113
        super(_CDataWrapper, self).__init__()
114
115
    def _get_cdata_from_args(self, *args, **kargs):
0 ignored issues
show
Coding Style introduced by
This method should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
Coding Style introduced by
This method could be written as a function/class method.

If a method does not access any attributes of the class, it could also be implemented as a function or static method. This can help improve readability. For example

class Foo:
    def some_method(self, x, y):
        return x + y;

could be written as

class Foo:
    @classmethod
    def some_method(cls, x, y):
        return x + y;
Loading history...
116
        if len(args) == 1 and isinstance(args[0], ffi.CData) and not kargs:
117
            return args[0]
118
        else:
119
            return None
120
121
122
    def __hash__(self):
123
        return hash(self.cdata)
124
125
    def __eq__(self, other):
126
        try:
127
            return self.cdata == other.cdata
128
        except AttributeError:
129
            return NotImplemented
130
131
    def __getattr__(self, attr):
132
        if 'cdata' in self.__dict__:
133
            return getattr(self.__dict__['cdata'], attr)
134
        raise AttributeError(attr)
135
136
    def __setattr__(self, attr, value):
137
        if hasattr(self, 'cdata') and hasattr(self.cdata, attr):
138
            setattr(self.cdata, attr, value)
139
        else:
140
            super(_CDataWrapper, self).__setattr__(attr, value)
141