├── requirements-test.txt ├── .gitignore ├── pyproject.toml ├── upolygon ├── __init__.py ├── run_length_encoding.pyx ├── simplify_polygon.pyx ├── find_contours.pyx ├── draw_polygon.pyx └── __init__.c ├── tests ├── test_simplify_polygon.py ├── test_find_contours.py └── test_draw_polygon.py ├── LICENSE ├── .github └── workflows │ ├── pull_request.yml │ ├── build.yml │ └── deploy.yml ├── setup.py └── README.md /requirements-test.txt: -------------------------------------------------------------------------------- 1 | numpy -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | **/__pycache__/ 2 | .pytest_cache/ 3 | build/ 4 | 5 | *.egg-info 6 | *.so 7 | 8 | .venv -------------------------------------------------------------------------------- /pyproject.toml: -------------------------------------------------------------------------------- 1 | [build-system] 2 | requires = ["setuptools>=42"] 3 | build-backend = "setuptools.build_meta" -------------------------------------------------------------------------------- /upolygon/__init__.py: -------------------------------------------------------------------------------- 1 | from draw_polygon import draw_polygon # noqa 2 | from find_contours import find_contours # noqa 3 | from simplify_polygon import simplify_polygon # noqa 4 | from run_length_encoding import rle_decode, rle_encode # noqa 5 | -------------------------------------------------------------------------------- /tests/test_simplify_polygon.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | from upolygon import simplify_polygon 4 | 5 | 6 | def test_empty_path(): 7 | assert len(simplify_polygon([], 1)) == 0 8 | 9 | 10 | def test_empty_sub_path(): 11 | assert len(simplify_polygon([[]], 1)) == 1 12 | 13 | 14 | def test_removes_linear_points(): 15 | path = [0, 0, 0, 5, 0, 10, 0, 15] 16 | assert simplify_polygon([path], 0) == [[0, 0, 0, 15]] 17 | 18 | 19 | def test_keeps_non_linear_points(): 20 | path = [0, 0, 0, 5, 0, 7, 10, 10, 0, 15] 21 | assert simplify_polygon([path], 1) == [[0, 0, 0, 7, 10, 10, 0, 15]] 22 | 23 | 24 | def test_respects_epsilon(): 25 | path = [0, 0, 1, 1, 0, 10] 26 | assert simplify_polygon([path], 1) == [[0, 0, 0, 10]] 27 | assert simplify_polygon([path], 0.1) == [[0, 0, 1, 1, 0, 10]] 28 | 29 | 30 | def test_handles_repeats(): 31 | path = [0, 0, 0, 0, 0, 0, 1, 1] 32 | assert simplify_polygon([path], 1) == [[0, 0, 1, 1]] 33 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Simon Edwardsson 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /.github/workflows/pull_request.yml: -------------------------------------------------------------------------------- 1 | name: Python package 2 | 3 | on: [pull_request, workflow_dispatch] 4 | 5 | jobs: 6 | build: 7 | runs-on: ubuntu-latest 8 | strategy: 9 | matrix: 10 | python-version: [3.7, 3.8, 3.9, '3.10', '3.11'] 11 | 12 | steps: 13 | - uses: actions/checkout@v2 14 | - name: Set up Python ${{ matrix.python-version }} 15 | uses: actions/setup-python@v2 16 | with: 17 | python-version: ${{ matrix.python-version }} 18 | - name: Install dependencies 19 | run: | 20 | python -m pip install --upgrade pip 21 | pip install flake8 pytest 22 | if [ -f requirements.txt ]; then pip install -r requirements.txt; fi 23 | if [ -f requirements-test.txt ]; then pip install -r requirements-test.txt; fi 24 | pip install --editable . 25 | - name: Lint with flake8 26 | run: | 27 | # stop the build if there are Python syntax errors or undefined names 28 | flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics 29 | # exit-zero treats all errors as warnings 30 | flake8 . --count --exit-zero --max-complexity=10 --max-line-length=100 --statistics 31 | - name: Test with pytest 32 | run: | 33 | pytest 34 | -------------------------------------------------------------------------------- /tests/test_find_contours.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | from upolygon import draw_polygon, find_contours, simplify_polygon 4 | 5 | 6 | def test_single_pixel(): 7 | mask = np.array([[0, 1, 0, 0, 0], [0, 0, 0, 0, 0], [0, 1, 0, 0, 0], [0, 0, 0, 0, 0]], dtype=np.uint8) 8 | _labels, external_paths, internal_paths = find_contours(mask) 9 | assert len(external_paths) == 1 10 | assert len(internal_paths) == 0 11 | 12 | 13 | def test_finds_singular_outer_path(): 14 | mask = np.array([[0, 0, 0, 0, 0], [0, 1, 1, 1, 1], [0, 1, 1, 1, 0], [0, 0, 1, 0, 0]], dtype=np.uint8) 15 | _labels, external_paths, internal_paths = find_contours(mask) 16 | assert len(external_paths) == 1 17 | assert len(internal_paths) == 0 18 | print(mask) 19 | print(np.array(draw_polygon(mask.copy() * 0, external_paths, 1))) 20 | print(external_paths) 21 | assert np.all(mask == draw_polygon(mask.copy() * 0, external_paths, 1)) 22 | 23 | 24 | def test_finds_two_outer_path(): 25 | mask = np.array([[0, 0, 0, 0, 0, 0], [0, 1, 1, 0, 1, 0], [0, 1, 1, 0, 1, 0], [0, 0, 1, 0, 1, 0]], dtype=np.uint8,) 26 | _labels, external_paths, internal_paths = find_contours(mask) 27 | assert len(external_paths) == 2 28 | assert len(internal_paths) == 0 29 | assert np.all(mask == draw_polygon(mask.copy() * 0, external_paths, 1)) 30 | -------------------------------------------------------------------------------- /upolygon/run_length_encoding.pyx: -------------------------------------------------------------------------------- 1 | #cython: language_level=3 2 | 3 | cimport cython 4 | import numpy as np 5 | 6 | @cython.boundscheck(False) 7 | @cython.wraparound(False) 8 | @cython.nonecheck(False) 9 | def rle_encode(binary_mask): 10 | # at most there can be len(binary_mask) different values, therefor we prealloace an array of that size 11 | # unused counts will be stripped at the end 12 | cdef long[:] counts = np.zeros(binary_mask.shape[0] * binary_mask.shape[1], dtype=np.int_) 13 | cdef char[:] mask_view = binary_mask.ravel(order="F").astype(np.int8) 14 | 15 | cdef char last_elem = 0 16 | cdef char elem 17 | cdef long running_length = 0 18 | cdef int i = 0 19 | cdef int j = 0 20 | 21 | for j in range(mask_view.shape[0]): 22 | if mask_view[j] != last_elem: 23 | counts[i] = running_length 24 | i += 1 25 | running_length = 0 26 | last_elem = mask_view[j] 27 | running_length += 1 28 | counts[i] = running_length 29 | i += 1 30 | return counts.base[0:i].tolist() 31 | 32 | def rle_decode(counts, shape): 33 | img = np.zeros(shape[0] * shape[1], dtype=np.uint8) 34 | cdef int val = 1 35 | cdef int n = 0 36 | cdef int pos 37 | for pos in range(len(counts)): 38 | val = not val 39 | img[n : n + counts[pos]] = val 40 | n += counts[pos] 41 | return img.reshape(shape).T 42 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | import setuptools 2 | import os 3 | 4 | with open("README.md", "r") as f: 5 | long_description = f.read() 6 | 7 | USE_CYTHON = os.getenv("USE_CYTHON") in ["TRUE", "true"] 8 | ext = ".pyx" if USE_CYTHON else ".c" 9 | 10 | extensions = [ 11 | setuptools.Extension( 12 | "draw_polygon", 13 | ["upolygon/draw_polygon" + ext], 14 | extra_compile_args=["-O3", "-Wall"], 15 | ), 16 | setuptools.Extension( 17 | "find_contours", 18 | ["upolygon/find_contours" + ext], 19 | extra_compile_args=["-O3", "-Wall"], 20 | ), 21 | setuptools.Extension( 22 | "simplify_polygon", 23 | ["upolygon/simplify_polygon" + ext], 24 | extra_compile_args=["-O3", "-Wall"], 25 | ), 26 | setuptools.Extension( 27 | "run_length_encoding", 28 | ["upolygon/run_length_encoding" + ext], 29 | extra_compile_args=["-O3", "-Wall"], 30 | ), 31 | ] 32 | 33 | if USE_CYTHON: 34 | from Cython.Build import cythonize # noqa 35 | 36 | extensions = cythonize(extensions) 37 | 38 | setuptools.setup( 39 | name="upolygon", 40 | version="0.1.11", 41 | author="V7", 42 | author_email="simon@v7labs.com", 43 | description="Collection of fast polygon operations for DL", 44 | long_description=long_description, 45 | long_description_content_type="text/markdown", 46 | url="https://github.com/v7labs/upolygon", 47 | ext_modules=extensions, 48 | install_requires=["numpy"], 49 | packages=setuptools.find_packages(), 50 | classifiers=[ 51 | "Programming Language :: Python :: 3", 52 | "License :: OSI Approved :: MIT License", 53 | ], 54 | python_requires=">=3.6", 55 | ) 56 | -------------------------------------------------------------------------------- /.github/workflows/build.yml: -------------------------------------------------------------------------------- 1 | name: Build 2 | 3 | on: 4 | push: 5 | tags: 6 | - "v*.*.*" 7 | - "test-*" 8 | workflow_dispatch: 9 | 10 | concurrency: 11 | group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }} 12 | cancel-in-progress: true 13 | 14 | jobs: 15 | build_wheels: 16 | name: Build wheels on ${{ matrix.os }} 17 | runs-on: ${{ matrix.os }} 18 | strategy: 19 | matrix: 20 | os: [ubuntu-latest, windows-latest, macos-latest] 21 | python-version: [3.9] 22 | 23 | steps: 24 | - uses: actions/checkout@v3 25 | - name: Set env 26 | run: echo "RELEASE_VERSION=${GITHUB_REF#refs/*/}" >> $GITHUB_ENV 27 | 28 | - name: Build wheels 29 | env: 30 | CIBW_ARCHS_MACOS: "x86_64 universal2 arm64" 31 | uses: pypa/cibuildwheel@v2.14.1 32 | with: 33 | package-dir: . 34 | output-dir: dist 35 | config-file: "{package}/pyproject.toml" 36 | 37 | - uses: actions/upload-artifact@v4 38 | if: ${{ env.RELEASE_VERSION != ''}} 39 | with: 40 | name: ${{ env.RELEASE_VERSION }}-wheels 41 | retention-days: 15 42 | path: dist/*.whl 43 | 44 | build_sdist: 45 | runs-on: ubuntu-latest 46 | steps: 47 | - uses: actions/checkout@v2 48 | - name: Set env 49 | run: echo "RELEASE_VERSION=${GITHUB_REF#refs/*/}" >> $GITHUB_ENV 50 | - name: Set up Python 51 | uses: actions/setup-python@v2 52 | with: 53 | python-version: "3.x" 54 | - name: Install dependencies 55 | run: | 56 | python -m pip install --upgrade pip 57 | pip install setuptools wheel twine 58 | python setup.py sdist 59 | - uses: actions/upload-artifact@v4 60 | if: ${{ env.RELEASE_VERSION != ''}} 61 | with: 62 | name: ${{ env.RELEASE_VERSION }}-sdist 63 | retention-days: 15 64 | path: dist/*.tar.gz 65 | -------------------------------------------------------------------------------- /.github/workflows/deploy.yml: -------------------------------------------------------------------------------- 1 | name: Deploy 2 | 3 | on: 4 | release: 5 | types: [created] 6 | workflow_dispatch: 7 | 8 | jobs: 9 | # Current issues: 10 | # - The release name recovered seems to be wrong sometimes 11 | # - The structure of a release is wrong to automate releasing - consider Twine instead 12 | 13 | retrieve_artifacts: 14 | runs-on: ubuntu-latest 15 | strategy: 16 | max-parallel: 1 17 | steps: 18 | 19 | - name: Display tag name 20 | run: echo "Tag name is ${{ github.event.release.name }}" 21 | 22 | - name: Is TEST RELEASE 23 | if: ${{ startsWith('test-',github.event.release.name) }} 24 | run: echo "Is test release ${{ startsWith('test-',github.event.release.name) }}" 25 | 26 | - name: Checkout 27 | uses: actions/checkout@v2 28 | 29 | - name: Retrieve wheel artifacts for release 30 | uses: dawidd6/action-download-artifact@v6 31 | with: 32 | name: ${{ github.event.release.name }}-wheels 33 | repo: ${{ github.repository }} 34 | workflow: build.yml 35 | path: ./dist 36 | github_token: ${{ secrets.GITHUB_TOKEN }} 37 | 38 | - name: Retrieve SDIST artifacts for release 39 | uses: dawidd6/action-download-artifact@v6 40 | with: 41 | name: ${{ github.event.release.name }}-sdist 42 | repo: ${{ github.repository }} 43 | workflow: build.yml 44 | path: ./dist 45 | github_token: ${{ secrets.GITHUB_TOKEN }} 46 | 47 | # Test step for debugging 48 | - name: Display structure of downloaded files 49 | run: ls -laR 50 | working-directory: ./dist 51 | 52 | - name: Release code as new release on Pypi 53 | if: ${{ startsWith(github.event.release.name, 'v') }} 54 | uses: pypa/gh-action-pypi-publish@release/v1 55 | with: 56 | user: __token__ 57 | password: ${{ secrets.PYPI_PASSWORD }} 58 | repository-url: https://upload.pypi.org/legacy/ 59 | verbose: true 60 | packages-dir: ./dist 61 | 62 | - name: "TEST: Release code as new release on Test Pypi" 63 | if: ${{ startsWith('test-',github.event.release.name) }} 64 | uses: pypa/gh-action-pypi-publish@release/v1 65 | with: 66 | user: __token__ 67 | password: ${{ secrets.PYPI_TEST_PASSWORD }} 68 | repository-url: https://test.pypi.org/legacy/ 69 | verbose: true 70 | packages-dir: ./dist 71 | 72 | # TODO: Slack announcement 73 | 74 | # TODO: Update linear labels? 75 | -------------------------------------------------------------------------------- /upolygon/simplify_polygon.pyx: -------------------------------------------------------------------------------- 1 | #cython: language_level=3 2 | 3 | cimport cython 4 | from libc.math cimport abs, sqrt 5 | 6 | 7 | cdef perpendicular_distance(float px, float py, float ax, float ay, float bx, float by): 8 | cdef float dist = sqrt((by - ay) * (by - ay) + (bx - ax) * (bx - ax)) 9 | if dist < 0.0001: 10 | return sqrt((py - ay) * (py - ay) + (px - ax) * (px - ax)) 11 | return abs((by - ay) * px - (bx - ax) * py + bx * ay - by * ax) / dist 12 | 13 | @cython.boundscheck(False) 14 | @cython.wraparound(False) 15 | @cython.nonecheck(False) 16 | def simplify_single_polygon(list path, float epsilon): 17 | # Note that we are using an iterative version of this algorithm 18 | # instead of the classical recursive to prevent reaching python's 19 | # max recursion. 20 | # Uses a stack to avoid recursion and iterates over the path where 21 | # path takes the form of [x1,y1,x2,y2,...,xn,yn], therefore the x,y 22 | # tuple is at index 2*i and 2*i+1 respectively and the length is half of the array 23 | # Iterative algorithm comparison found here: https://namekdev.net/2014/06/iterative-version-of-ramer-douglas-peucker-line-simplification-algorithm/ 24 | cdef int length = len(path) // 2 25 | cdef int startIndex = 0 26 | cdef int endIndex = length 27 | cdef float max_distance = 0 28 | cdef int index = 0 29 | cdef int i 30 | deleted = [False] * length 31 | stack = [(startIndex,endIndex)] 32 | while stack: 33 | startIndex, endIndex = stack.pop() 34 | if startIndex == endIndex: 35 | continue 36 | max_distance = 0 37 | for i in range(startIndex+1,endIndex): 38 | if deleted[i]: 39 | continue 40 | distance = perpendicular_distance(path[2*i], path[2*i+1], path[startIndex*2], path[startIndex*2+1], path[2*(endIndex-1)], path[2*(endIndex-1)+1]) 41 | if distance > max_distance: 42 | max_distance = distance 43 | index = i 44 | if max_distance > epsilon: 45 | stack.append((startIndex,index)) 46 | stack.append((index, endIndex)) 47 | else: 48 | for i in range(startIndex+1,endIndex-1): 49 | deleted[i] = True 50 | result = [] 51 | for i in range(0, length): 52 | if not deleted[i]: 53 | result.append(path[2*i]) 54 | result.append(path[2*i+1]) 55 | 56 | return result 57 | 58 | # Basic Ramer–Douglas–Peucker algorithm 59 | @cython.boundscheck(False) 60 | @cython.wraparound(False) 61 | @cython.nonecheck(False) 62 | def simplify_polygon(list paths, float epsilon): 63 | return [simplify_single_polygon(path, epsilon) for path in paths] 64 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # uPolygon 2 | Library of handy polygon related functions to speed up machine learning projects. 3 | 4 | It was born as a replacement for `cv2.fillPoly` when generating masks for instance segmentation, without having to bring in all of opencv. 5 | 6 | ## TODO 7 | - [x] draw_polygon 8 | - [x] find_contours 9 | - [ ] polygon_area 10 | - [ ] point_in_polygon 11 | 12 | 13 | ## Usage 14 | This library expects all polygons to be model as a list of paths, each path is a list of alternating x and y coordinates (`[x1,y1,x2,y2,...]`). 15 | 16 | A simple triangle would be declared as: 17 | ```python 18 | triangle = [[50,50, 100,0, 0,0]] 19 | ``` 20 | 21 | Complex polygons (holes and/or disjoints) follow the even-odd rule. 22 | 23 | 24 | ## draw_polygon 25 | 26 | `draw_polygon(mask: array[:, :], paths: path[]) -> array[:, :]` 27 | 28 | ```python 29 | from upolygon import draw_polygon 30 | import numpy as np 31 | 32 | mask = np.zeros((100,100), dtype=np.int32) 33 | draw_polygon(mask, [[50,50, 100,0, 0,0]], 1) 34 | ``` 35 | 36 | Equivalent of calling `cv2.fillPoly(mask, [np.array([[50,50], [100,0], [0,0]])], 1)` or `cv2.drawContours(mask, [np.array([[50,50], [100,0], [0,0]])], -1, 1, cv2.FILLED)` when using opencv. 37 | 38 | uPolygon is ~ 6 times faster than opencv for large random polygons with many intersecting lines. 39 | For smaller polygons or few intersections, uPolygon is half as fast as opencv. 40 | 41 | ## find_contours 42 | `find_contours(mask: array[:, :]) -> (array[:, :], path[:], path[:])` 43 | 44 | 0 is treated as background, 1 is treated as foreground. 45 | ```python 46 | from upolygon import find_contours 47 | import numpy as np 48 | 49 | mask = np.array([ 50 | [0, 0, 0, 0, 0], 51 | [0, 1, 1, 1, 0], 52 | [0, 1, 1, 1, 0], 53 | [0, 1, 1, 1, 0] 54 | ], dtype=np.uint8) 55 | 56 | _labels, external_paths, internal_paths = find_contours(mask) 57 | ``` 58 | 59 | Similar to OpenCV's `cv2.findContours` but lacking hierarchies. Also similar to BoofCV's `LinearContourLabelChang2004` which is based on the same [algorithm](https://www.iis.sinica.edu.tw/papers/fchang/1362-F.pdf). 60 | 61 | 62 | Note that currently the input mask to find_contour needs to be uint8. 63 | 64 | ## rle_encode 65 | `rle_encode(mask: array[:,:]) -> list` 66 | Takes a 2-dim binary mask and generates a run length encoding according to the coco specs 67 | 68 | ~ 15 times faster than written in plain python 69 | 70 | 71 | 72 | ## Development 73 | This is a Cython project and thus has some additional development dependencies to compile code into binaries, as well as extra steps to build/use the project 74 | ### Dependencies 75 | - gcc: 76 | - Ubuntu/debian: `sudo apt install build-essential` 77 | - Arch: `yay -Sy base-devel` 78 | - Mac/OS: `brew install gcc` 79 | - Cython `pip install Cython` 80 | 81 | 82 | ### Local Testing 83 | To ensure building correctly, set the Cython environment variable 84 | `export USE_CYTHON=true` 85 | To install and test locally, build with the following command 86 | `python setup.py install` 87 | which will locate the virtual environment activated, build and then install the local version to that python environment. 88 | Alternatively, 89 | `python setup.py build_ext --inplace` 90 | will build and install to the working directory for importing from local. 91 | 92 | Each change to the code needs to be rebuilt before it can be used. 93 | 94 | ### Interactive debugging 95 | TODO -------------------------------------------------------------------------------- /upolygon/find_contours.pyx: -------------------------------------------------------------------------------- 1 | #cython: language_level=3 2 | 3 | cimport cython 4 | import numpy as np 5 | from simplify_polygon import simplify_polygon 6 | 7 | # This implementation is based on https://www.iis.sinica.edu.tw/papers/fchang/1362-F.pdf 8 | 9 | # When tracing there is a clockwise index around the current point P 10 | # 5 6 7 11 | # 4 P 0 12 | # 3 2 1 13 | cdef int* directions_x = [1, 1, 0, -1, -1, -1, 0, 1] 14 | cdef int* directions_y = [0, 1, 1, 1, 0, -1, -1, -1] 15 | 16 | 17 | @cython.boundscheck(False) 18 | @cython.wraparound(False) 19 | @cython.nonecheck(False) 20 | cdef int tracer(int px, int py, int old_index, int *nx, int *ny, unsigned char[:, :] image, char[:, :] labels) nogil: 21 | # move two steps clockwise from the previous entry in the trace 22 | cdef int start_index = (old_index + 2) % 8 23 | cdef int i 24 | cdef int tmpx, tmpy 25 | nx[0] = px 26 | ny[0] = py 27 | for i in range(start_index, start_index + 8): 28 | i = i % 8 29 | tmpx = directions_x[i] + px 30 | tmpy = directions_y[i] + py 31 | if image[tmpy][tmpx] == 1: 32 | nx[0] = tmpx 33 | ny[0] = tmpy 34 | # adding four to the index gives us the relative position of px,py to nx,ny in the next call 35 | return i + 4 36 | else: 37 | labels[tmpy][tmpx] = -1 38 | 39 | @cython.boundscheck(False) 40 | @cython.wraparound(False) 41 | @cython.nonecheck(False) 42 | cdef contour_trace(int px, int py, int c, unsigned char[:, :]image, char[:,:] labels, int inner): 43 | cdef int sx = px 44 | cdef int sy = py 45 | cdef int nx, ny, tx, ty 46 | cdef int index = 1 if inner else 5 47 | cdef int last_point_was_s = False 48 | path = [px-1, py-1] 49 | index = tracer(px, py, index, &nx, &ny, image, labels) 50 | tx = nx 51 | ty = ny 52 | 53 | # S was a single point 54 | if tx == sx and ty == sy: 55 | return path 56 | 57 | path.append(tx-1) 58 | path.append(ty-1) 59 | 60 | labels[ny][nx] = c 61 | while True: 62 | index = tracer(nx, ny, index, &nx, &ny, image, labels) 63 | if last_point_was_s and nx == tx and ny == ty: 64 | return path 65 | path.append(nx-1) 66 | path.append(ny-1) 67 | labels[ny][nx] = c 68 | last_point_was_s = nx == sx and ny == sy 69 | 70 | @cython.boundscheck(False) 71 | @cython.wraparound(False) 72 | @cython.nonecheck(False) 73 | def find_contours(unsigned char[:,:] image): 74 | cdef int px = 1 75 | cdef int py = 1 76 | cdef int c = 1 77 | cdef int width = image.shape[1] - 1 78 | cdef int height = image.shape[0] - 1 79 | image = np.pad(image, pad_width=1, mode='constant', constant_values=0) 80 | cdef char[:,:] labels = np.zeros((image.shape[0], image.shape[1]), dtype=np.int8) 81 | inner_paths = [] 82 | outer_paths = [] 83 | 84 | while py < height: 85 | while image[py][px] == 0 and px < width: 86 | px += 1 87 | if image[py][px] == 1: 88 | handled = False 89 | # STEP 1 90 | if labels[py][px] == 0 and image[py-1][px] == 0: 91 | labels[py][px] = c 92 | path = contour_trace(px, py, c, image, labels, 0) 93 | outer_paths.append(path) 94 | c += 1 95 | handled = True 96 | # STEP 2 97 | if labels[py+1][px] != -1 and image[py+1][px] == 0: 98 | handled = True 99 | # unlabeled 100 | if labels[py][px] == 0: 101 | path = contour_trace(px, py, labels[py][px-1], image, labels, 1) 102 | else: 103 | path = contour_trace(px, py, labels[py][px], image, labels, 1) 104 | inner_paths.append(path) 105 | # STEP 3 106 | if not handled and labels[py][px] == 0: 107 | labels[py][px] = labels[py][px-1] 108 | px += 1 109 | if px > width-1: 110 | px = 1 111 | py = py + 1 112 | 113 | # return labels, outer_paths, inner_paths 114 | return labels, simplify_polygon(outer_paths, 0), simplify_polygon(inner_paths, 0) -------------------------------------------------------------------------------- /tests/test_draw_polygon.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | from upolygon import draw_polygon 4 | 5 | triangle = [5, 5, 8, 1, 0, 0] 6 | triangle_sum = 270 7 | triangle_result = np.array( 8 | [ 9 | [1, 1, 1, 1, 1, 0, 0, 0, 0, 0], 10 | [0, 1, 1, 1, 1, 1, 1, 1, 1, 0], 11 | [0, 0, 1, 1, 1, 1, 1, 1, 0, 0], 12 | [0, 0, 0, 1, 1, 1, 1, 0, 0, 0], 13 | [0, 0, 0, 0, 1, 1, 1, 0, 0, 0], 14 | [0, 0, 0, 0, 0, 1, 0, 0, 0, 0], 15 | [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 16 | [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 17 | [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 18 | [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 19 | ], 20 | dtype=np.int32, 21 | ) 22 | triangle_mask_size = triangle_result.shape 23 | 24 | 25 | def test_does_nothing_for_empty_an_empty_polygon(): 26 | mask = np.zeros((10, 10), dtype=np.int32) 27 | draw_polygon(mask, [], 1) 28 | assert np.all(mask == 0) 29 | 30 | 31 | def test_writes_the_given_value(): 32 | mask_1 = np.zeros((100, 100), dtype=np.int32) 33 | mask_2 = np.zeros((100, 100), dtype=np.int32) 34 | draw_polygon(mask_1, [triangle], 1) 35 | draw_polygon(mask_2, [triangle], 2) 36 | assert np.sum(mask_1) * 2 == np.sum(mask_2) 37 | 38 | 39 | def test_crops_negative_coordinates(): 40 | mask = np.zeros((100, 100), dtype=np.int32) 41 | draw_polygon(mask, [[-50, 0, 50, 50, 200, 200]], 1) 42 | 43 | 44 | def test_crop_out_of_bound_horizontal_line(): 45 | mask = np.zeros((100, 100), dtype=np.int32) 46 | draw_polygon(mask, [[-50, 0, 200, 0]], 1) 47 | 48 | 49 | def test_crop_out_of_bound_vertical_line(): 50 | mask = np.zeros((100, 100), dtype=np.int32) 51 | draw_polygon(mask, [[0, -50, 0, -200]], 1) 52 | 53 | 54 | def test_rectangle_large_segments(): 55 | square = [1, 1, 5, 1, 5, 5, 1, 5] 56 | expected = np.array( 57 | [ 58 | [0, 0, 0, 0, 0, 0, 0], 59 | [0, 1, 1, 1, 1, 1, 0], 60 | [0, 1, 1, 1, 1, 1, 0], 61 | [0, 1, 1, 1, 1, 1, 0], 62 | [0, 1, 1, 1, 1, 1, 0], 63 | [0, 1, 1, 1, 1, 1, 0], 64 | [0, 0, 0, 0, 0, 0, 0], 65 | ], 66 | dtype=np.uint8, 67 | ) 68 | 69 | mask = np.zeros((7, 7), dtype=np.int32) 70 | draw_polygon(mask, [square], 1) 71 | assert np.all(mask == expected) 72 | 73 | 74 | def test_rectangle_tiny_segments(): 75 | square = [ 76 | 1, 77 | 1, 78 | 2, 79 | 1, 80 | 3, 81 | 1, 82 | 4, 83 | 1, 84 | 5, 85 | 1, 86 | 5, 87 | 2, 88 | 5, 89 | 3, 90 | 5, 91 | 4, 92 | 5, 93 | 5, 94 | 4, 95 | 5, 96 | 3, 97 | 5, 98 | 2, 99 | 5, 100 | 1, 101 | 5, 102 | 1, 103 | 4, 104 | 1, 105 | 3, 106 | 1, 107 | 2, 108 | ] 109 | 110 | expected = np.array( 111 | [ 112 | [0, 0, 0, 0, 0, 0, 0], 113 | [0, 1, 1, 1, 1, 1, 0], 114 | [0, 1, 1, 1, 1, 1, 0], 115 | [0, 1, 1, 1, 1, 1, 0], 116 | [0, 1, 1, 1, 1, 1, 0], 117 | [0, 1, 1, 1, 1, 1, 0], 118 | [0, 0, 0, 0, 0, 0, 0], 119 | ], 120 | dtype=np.uint8, 121 | ) 122 | 123 | mask = np.zeros((7, 7), dtype=np.int32) 124 | draw_polygon(mask, [square], 1) 125 | assert np.all(mask == expected) 126 | 127 | 128 | def test_decimals_in_path(): 129 | square = [0.5, 0.5, 0.5, 10.5, 10.5, 10.5, 10.5, 0.5] 130 | mask = np.zeros((100, 100), dtype=np.int32) 131 | draw_polygon(mask, [square], 1) 132 | assert np.sum(mask) == 11 * 11 133 | 134 | 135 | def test_out_of_bound(): 136 | square = [0, 0, 0, 10, 10, 10, 10, 0] 137 | mask = np.zeros((1, 1), dtype=np.int32) 138 | draw_polygon(mask, [square], 1) 139 | assert np.sum(mask) == 1 140 | 141 | 142 | def test_supports_uint8(): 143 | mask = np.zeros(triangle_mask_size, dtype=np.uint8) 144 | draw_polygon(mask, [triangle], 1) 145 | assert np.all(mask == triangle_result) 146 | 147 | 148 | def test_supports_int8(): 149 | mask = np.zeros(triangle_mask_size, dtype=np.int8) 150 | draw_polygon(mask, [triangle], 1) 151 | assert np.all(mask == triangle_result) 152 | 153 | 154 | def test_supports_int32(): 155 | mask = np.zeros(triangle_mask_size, dtype=np.int32) 156 | draw_polygon(mask, [triangle], 1) 157 | assert np.all(mask == triangle_result) 158 | 159 | 160 | def test_supports_float(): 161 | mask = np.zeros(triangle_mask_size, dtype=np.floating) 162 | draw_polygon(mask, [triangle], 1) 163 | assert np.all(mask == triangle_result) 164 | -------------------------------------------------------------------------------- /upolygon/draw_polygon.pyx: -------------------------------------------------------------------------------- 1 | #cython: language_level=3, warn.undeclared=True, warn.unused=True, 2 | cimport cython 3 | from libc.stdlib cimport malloc, free, qsort 4 | from libc.math cimport ceil, floor, round 5 | from array import array 6 | 7 | # An edge beteween two adjacent points. 8 | # x_val is the x position at y_min. 9 | cdef struct s_edge: 10 | float y_min 11 | float y_max 12 | float x_val 13 | float m_inv 14 | 15 | # An active edge intersects the scanline 16 | # x_val is updated at every iteration 17 | cdef struct s_active_edge: 18 | float y_max 19 | float x_val 20 | float m_inv 21 | 22 | ctypedef fused data_type: 23 | char 24 | unsigned char 25 | int 26 | unsigned int 27 | double 28 | long long 29 | 30 | cdef int clip(int value, int min_value, int max_value) nogil: 31 | if value < min_value: 32 | return min_value 33 | elif value > max_value: 34 | return max_value 35 | else: 36 | return value 37 | 38 | # Clip the lines inside a rectangle (0,0) (w,h) 39 | # For details see https://arxiv.org/pdf/1908.01350.pdf 40 | @cython.boundscheck(False) 41 | @cython.wraparound(False) 42 | @cython.nonecheck(False) 43 | @cython.cdivision(True) 44 | cdef inline int clip_line(int w, int h, int* x1, int* y1, int* x2, int* y2) nogil: 45 | cdef double _x1 = x1[0] 46 | cdef double _x2 = x2[0] 47 | cdef double _y1 = y1[0] 48 | cdef double _y2 = y2[0] 49 | cdef double __x1 = x1[0] 50 | cdef double __x2 = x2[0] 51 | cdef double __y1 = y1[0] 52 | cdef double __y2 = y2[0] 53 | # first check if both point are outside the viewpoint on the same side 54 | # if so skip them 55 | if _x1 < 0 and _x2 < 0: 56 | return 0 57 | if _x1 >= w and _x2 >= w: 58 | return 0 59 | if _y1 < 0 and _y2 < 0: 60 | return 0 61 | if _y1 >= h and _y2 >= h: 62 | return 0 63 | 64 | if __x1 < 0: 65 | __y1 = (_y2-_y1) / (_x2 - _x1) * (0-_x1) + _y1 66 | __x1 = 0 67 | elif __x1 >= w: 68 | __y1 = (_y2-_y1) / (_x2 - _x1) * (w-_x1) + _y1 69 | __x1 = w - 1 70 | 71 | if __y1 < 0: 72 | __x1 = (_x2-_x1) / (_y2 - _y1) * (0-_y1) + _x1 73 | __y1 = 0 74 | elif __y1 >= h: 75 | __x1 = (_x2-_x1) / (_y2 - _y1) * (h-_y1) + _x1 76 | __y1 = h - 1 77 | 78 | if __x2 < 0: 79 | __y2 = (_y2-_y1) / (_x2 - _x1) * (0-_x1) + _y1 80 | __x2 = 0 81 | elif __x2 >= w: 82 | __y2 = (_y2-_y1) / (_x2 - _x1) * (w-_x1) + _y1 83 | __x2 = w - 1 84 | 85 | if __y2 < 0: 86 | __x2 = (_x2-_x1) / (_y2 - _y1) * (0-_y1) + _x1 87 | __y2 = 0 88 | elif __y2 >= h: 89 | __x2 = (_x2-_x1) / (_y2 - _y1) * (h-_y1) + _x1 90 | __y2 = h - 1 91 | 92 | if (__x1 < 0 and __x2 < 0) or (__x1 >= w and __x2 >= w): 93 | return 0 94 | 95 | x1[0] = __x1 96 | x2[0] = __x2 97 | y1[0] = __y1 98 | y2[0] = __y2 99 | return 1 100 | 101 | @cython.boundscheck(False) 102 | @cython.wraparound(False) 103 | @cython.nonecheck(False) 104 | cdef inline void draw_straight_line(float x1, float x2, int y, data_type[:, :] mask, data_type value) nogil: 105 | cdef int x = max(ceil(x1),0) 106 | cdef int max_x = min(floor(x2), mask.shape[1]-1) + 1 107 | cdef int i 108 | for i in range(x, max_x): 109 | mask[y][i] = value 110 | 111 | # Sort edges first by y_min and then by x_val 112 | cdef int cmp_edges(const void* a, const void* b) nogil: 113 | cdef s_edge a_v = (a)[0] 114 | cdef s_edge b_v = (b)[0] 115 | if a_v.y_min < b_v.y_min: 116 | return -1 117 | elif a_v.y_min == b_v.y_min: 118 | if a_v.x_val < b_v.x_val: 119 | return -1 120 | elif a_v.x_val == b_v.x_val: 121 | return 0 122 | else: 123 | return 1 124 | else: 125 | return 1 126 | 127 | # draw Bresenham 8-connected line 128 | @cython.boundscheck(False) 129 | @cython.wraparound(False) 130 | @cython.nonecheck(False) 131 | @cython.cdivision(True) 132 | cdef void draw_edge_line(data_type [:,:] mask, int x1, int y1, int x2, int y2, data_type value): 133 | cdef int dx = x2 - x1 134 | cdef int dy = y2 - y1 135 | cdef int x, y 136 | 137 | # special case for vertical lines 138 | if dx == 0: 139 | if x1 < 0 or x1 >= mask.shape[1]: 140 | return 141 | y1, y2 = clip(y1, 0, mask.shape[0]-1), clip(y2, 0, mask.shape[0]-1) 142 | y1, y2 = min(y1, y2), max(y1, y2) 143 | for y in range(y1, y2+1): 144 | mask[y][x1] = value 145 | return 146 | 147 | # special case for horizontal lines 148 | if dy == 0: 149 | if y1 < 0 or y1 >= mask.shape[0]: 150 | return 151 | x1, x2 = clip(x1, 0, mask.shape[1]-1), clip(x2, 0, mask.shape[1]-1) 152 | x1, x2 = min(x1, x2), max(x1, x2) 153 | for x in range(x1, x2+1): 154 | mask[y1][x] = value 155 | return 156 | 157 | if clip_line(mask.shape[1], mask.shape[0], &x1, &y1, &x2, &y2) == 0: 158 | return 159 | 160 | dx = x2 - x1 161 | dy = y2 - y1 162 | 163 | cdef int delta_x = 1 164 | cdef int delta_y = 1 165 | 166 | if dx < 0: 167 | dx = -dx 168 | dy = -dy 169 | x1, y1, x2, y2 = x2, y2, x1, y1 170 | 171 | if dy < 0: 172 | delta_y = -1 173 | dy = -dy 174 | 175 | 176 | cdef int flip = dy > dx 177 | cdef int count = abs(x1 - x2) 178 | if flip: 179 | dx, dy = dy, dx 180 | count = abs(y1 - y2) 181 | 182 | cdef int minus_err = 2 * dy 183 | cdef int plus_err = 2 * (dy - dx) 184 | 185 | cdef int err = (dy + dy) - dx 186 | 187 | if flip: 188 | y = max(0, y1) 189 | x = max(0, x1) 190 | for _i in range(count): 191 | mask[y][x] = value 192 | if err <= 0: 193 | err = err + minus_err 194 | else: 195 | x = x + delta_x 196 | err = err + plus_err 197 | y = y + delta_y 198 | else: 199 | y = max(0, y1) 200 | x = max(0, x1) 201 | for _i in range(count): 202 | mask[y][x] = value 203 | if err <= 0: 204 | err = err + minus_err 205 | else: 206 | y = y + delta_y 207 | err = err + plus_err 208 | x = x + delta_x 209 | 210 | 211 | 212 | @cython.boundscheck(False) 213 | @cython.wraparound(False) 214 | @cython.nonecheck(False) 215 | @cython.cdivision(True) 216 | cdef int find_edges(s_edge *edges, list path, data_type [:,:] mask, data_type value): 217 | cdef int length = len(path) 218 | cdef float[:] path_mv = memoryview(array('f', path)) 219 | cdef float x1 220 | cdef float y1 221 | cdef float x2 222 | cdef float y2 223 | cdef int i 224 | cdef int idx = 0 225 | x1, y1 = path_mv[length-2], path_mv[length-1] 226 | y1 = round(y1) 227 | for i in range(0, length, 2): 228 | x2, y2 = path_mv[i], path_mv[i+1] 229 | y2 = round(y2) 230 | draw_edge_line(mask, x1, y1, x2, y2, value) 231 | 232 | if y1 == y2: 233 | x1, y1 = x2, y2 234 | continue 235 | elif y1 < y2: 236 | edges[idx].y_min = y1 237 | edges[idx].y_max = y2 238 | edges[idx].x_val = x1 239 | else: 240 | edges[idx].y_min = y2 241 | edges[idx].y_max = y1 242 | edges[idx].x_val = x2 243 | 244 | if edges[idx].y_max < 0: 245 | x1, y1 = x2, y2 246 | continue 247 | 248 | if edges[idx].y_min < 0: 249 | edges[idx].x_val = (x2-x1) / (y2 - y1) * (0-y1) + x1 250 | edges[idx].y_min = 0 251 | 252 | if edges[idx].y_max >= mask.shape[0]: 253 | edges[idx].y_max = mask.shape[0] 254 | 255 | edges[idx].m_inv = (x1 - x2) / (y1 -y2) 256 | idx += 1 257 | x1, y1 = x2, y2 258 | return idx 259 | 260 | @cython.boundscheck(False) 261 | @cython.wraparound(False) 262 | @cython.nonecheck(False) 263 | cdef void move_active_down(s_active_edge* edges, int i, int length): 264 | cdef int j 265 | for j in range(i, length-1): 266 | edges[j] = edges[j+1] 267 | 268 | @cython.boundscheck(False) 269 | @cython.wraparound(False) 270 | @cython.nonecheck(False) 271 | def draw_polygon(data_type[:, :] mask, list paths, data_type value): 272 | """Draws a polygon with value 273 | Args: 274 | mask: 2d mask on which the polygon will be drawn, note that the mask will be modified 275 | paths: a list of paths ([[x1,y1,x2,y2,...],[x1,y1,x2,y2,...]]) 276 | value: value used to draw the polygon on the mask 277 | 278 | Returns: 279 | The input mask, this is purely for convenience since the input mask is modified. 280 | 281 | Example: 282 | triangle_mask = draw_polygon(np.zeroes(100,100), [[0,50, 50, 0, 50, 50]], 255) 283 | """ 284 | cdef int edges_length = sum(len(path) // 2 for path in paths) 285 | cdef s_edge* edges = malloc(sizeof(s_edge) * edges_length) 286 | cdef int edges_so_far = 0 287 | for path in paths: 288 | edges_so_far += find_edges(edges + edges_so_far, path, mask, value) 289 | # no point in continuing if there are no edges 290 | if edges_so_far == 0: 291 | free(edges) 292 | return mask.base 293 | # edges_so_far can be smaller than edges_length if there are straight lines 294 | edges_length = edges_so_far 295 | qsort(edges, edges_so_far, sizeof(s_edge), &cmp_edges) 296 | 297 | cdef int active_edge_length = 0 298 | # keep an offset of which edges we have already processed, this way we can skip them. 299 | cdef int edge_dead_offset = 0 300 | cdef s_active_edge* active_edges = malloc(sizeof(s_active_edge) * edges_length) 301 | cdef s_active_edge edge 302 | cdef int scanline_y = round(edges[0].y_min) 303 | cdef int max_scanline_y = mask.shape[0] 304 | cdef int i, j 305 | 306 | while (edge_dead_offset < edges_length or active_edge_length > 0) and scanline_y < max_scanline_y: 307 | for i in range(edge_dead_offset, edges_length): 308 | if edges[i].y_min == scanline_y: 309 | active_edges[active_edge_length].y_max = edges[i].y_max 310 | active_edges[active_edge_length].x_val = edges[i].x_val 311 | active_edges[active_edge_length].m_inv = edges[i].m_inv 312 | active_edge_length += 1 313 | edge_dead_offset += 1 314 | elif edges[i].y_min > scanline_y: 315 | break 316 | 317 | # When an active edge is outside the scanline it can be retired 318 | # TODO: this could probably fused with the insert sort. 319 | for i in reversed(range(active_edge_length)): 320 | if active_edges[i].y_max == scanline_y: 321 | move_active_down(active_edges, i, active_edge_length) 322 | active_edge_length -= 1 323 | 324 | # Sort the active edges by x_val. 325 | # This is implemented as insertion sort since the list is mostly sorted 326 | # only edges that cross at this specific scanline will swap places. 327 | for i in range(1, active_edge_length): 328 | edge = active_edges[i] 329 | j = i - 1 330 | while j >= 0 and edge.x_val < active_edges[j].x_val: 331 | active_edges[j+1] = active_edges[j] 332 | j -= 1 333 | active_edges[j+1] = edge 334 | 335 | 336 | for i in range(0, active_edge_length, 2): 337 | draw_straight_line(active_edges[i].x_val, active_edges[i+1].x_val, scanline_y, mask, value) 338 | 339 | for i in range(0,active_edge_length): 340 | active_edges[i].x_val += active_edges[i].m_inv 341 | 342 | scanline_y += 1 343 | 344 | free(edges) 345 | free(active_edges) 346 | return mask.base 347 | -------------------------------------------------------------------------------- /upolygon/__init__.c: -------------------------------------------------------------------------------- 1 | /* Generated by Cython 0.29.30 */ 2 | 3 | /* BEGIN: Cython Metadata 4 | { 5 | "distutils": { 6 | "name": "upolygon.__init__", 7 | "sources": [ 8 | "/Users/simedw/projects/v7/upolygon/upolygon/__init__.py" 9 | ] 10 | }, 11 | "module_name": "upolygon.__init__" 12 | } 13 | END: Cython Metadata */ 14 | 15 | #ifndef PY_SSIZE_T_CLEAN 16 | #define PY_SSIZE_T_CLEAN 17 | #endif /* PY_SSIZE_T_CLEAN */ 18 | #include "Python.h" 19 | #ifndef Py_PYTHON_H 20 | #error Python headers needed to compile C extensions, please install development version of Python. 21 | #elif PY_VERSION_HEX < 0x02060000 || (0x03000000 <= PY_VERSION_HEX && PY_VERSION_HEX < 0x03030000) 22 | #error Cython requires Python 2.6+ or Python 3.3+. 23 | #else 24 | #define CYTHON_ABI "0_29_30" 25 | #define CYTHON_HEX_VERSION 0x001D1EF0 26 | #define CYTHON_FUTURE_DIVISION 0 27 | #include 28 | #ifndef offsetof 29 | #define offsetof(type, member) ( (size_t) & ((type*)0) -> member ) 30 | #endif 31 | #if !defined(WIN32) && !defined(MS_WINDOWS) 32 | #ifndef __stdcall 33 | #define __stdcall 34 | #endif 35 | #ifndef __cdecl 36 | #define __cdecl 37 | #endif 38 | #ifndef __fastcall 39 | #define __fastcall 40 | #endif 41 | #endif 42 | #ifndef DL_IMPORT 43 | #define DL_IMPORT(t) t 44 | #endif 45 | #ifndef DL_EXPORT 46 | #define DL_EXPORT(t) t 47 | #endif 48 | #define __PYX_COMMA , 49 | #ifndef HAVE_LONG_LONG 50 | #if PY_VERSION_HEX >= 0x02070000 51 | #define HAVE_LONG_LONG 52 | #endif 53 | #endif 54 | #ifndef PY_LONG_LONG 55 | #define PY_LONG_LONG LONG_LONG 56 | #endif 57 | #ifndef Py_HUGE_VAL 58 | #define Py_HUGE_VAL HUGE_VAL 59 | #endif 60 | #ifdef PYPY_VERSION 61 | #define CYTHON_COMPILING_IN_PYPY 1 62 | #define CYTHON_COMPILING_IN_PYSTON 0 63 | #define CYTHON_COMPILING_IN_CPYTHON 0 64 | #undef CYTHON_USE_TYPE_SLOTS 65 | #define CYTHON_USE_TYPE_SLOTS 0 66 | #undef CYTHON_USE_PYTYPE_LOOKUP 67 | #define CYTHON_USE_PYTYPE_LOOKUP 0 68 | #if PY_VERSION_HEX < 0x03050000 69 | #undef CYTHON_USE_ASYNC_SLOTS 70 | #define CYTHON_USE_ASYNC_SLOTS 0 71 | #elif !defined(CYTHON_USE_ASYNC_SLOTS) 72 | #define CYTHON_USE_ASYNC_SLOTS 1 73 | #endif 74 | #undef CYTHON_USE_PYLIST_INTERNALS 75 | #define CYTHON_USE_PYLIST_INTERNALS 0 76 | #undef CYTHON_USE_UNICODE_INTERNALS 77 | #define CYTHON_USE_UNICODE_INTERNALS 0 78 | #undef CYTHON_USE_UNICODE_WRITER 79 | #define CYTHON_USE_UNICODE_WRITER 0 80 | #undef CYTHON_USE_PYLONG_INTERNALS 81 | #define CYTHON_USE_PYLONG_INTERNALS 0 82 | #undef CYTHON_AVOID_BORROWED_REFS 83 | #define CYTHON_AVOID_BORROWED_REFS 1 84 | #undef CYTHON_ASSUME_SAFE_MACROS 85 | #define CYTHON_ASSUME_SAFE_MACROS 0 86 | #undef CYTHON_UNPACK_METHODS 87 | #define CYTHON_UNPACK_METHODS 0 88 | #undef CYTHON_FAST_THREAD_STATE 89 | #define CYTHON_FAST_THREAD_STATE 0 90 | #undef CYTHON_FAST_PYCALL 91 | #define CYTHON_FAST_PYCALL 0 92 | #undef CYTHON_PEP489_MULTI_PHASE_INIT 93 | #define CYTHON_PEP489_MULTI_PHASE_INIT 0 94 | #undef CYTHON_USE_TP_FINALIZE 95 | #define CYTHON_USE_TP_FINALIZE 0 96 | #undef CYTHON_USE_DICT_VERSIONS 97 | #define CYTHON_USE_DICT_VERSIONS 0 98 | #undef CYTHON_USE_EXC_INFO_STACK 99 | #define CYTHON_USE_EXC_INFO_STACK 0 100 | #ifndef CYTHON_UPDATE_DESCRIPTOR_DOC 101 | #define CYTHON_UPDATE_DESCRIPTOR_DOC (PYPY_VERSION_HEX >= 0x07030900) 102 | #endif 103 | #elif defined(PYSTON_VERSION) 104 | #define CYTHON_COMPILING_IN_PYPY 0 105 | #define CYTHON_COMPILING_IN_PYSTON 1 106 | #define CYTHON_COMPILING_IN_CPYTHON 0 107 | #ifndef CYTHON_USE_TYPE_SLOTS 108 | #define CYTHON_USE_TYPE_SLOTS 1 109 | #endif 110 | #undef CYTHON_USE_PYTYPE_LOOKUP 111 | #define CYTHON_USE_PYTYPE_LOOKUP 0 112 | #undef CYTHON_USE_ASYNC_SLOTS 113 | #define CYTHON_USE_ASYNC_SLOTS 0 114 | #undef CYTHON_USE_PYLIST_INTERNALS 115 | #define CYTHON_USE_PYLIST_INTERNALS 0 116 | #ifndef CYTHON_USE_UNICODE_INTERNALS 117 | #define CYTHON_USE_UNICODE_INTERNALS 1 118 | #endif 119 | #undef CYTHON_USE_UNICODE_WRITER 120 | #define CYTHON_USE_UNICODE_WRITER 0 121 | #undef CYTHON_USE_PYLONG_INTERNALS 122 | #define CYTHON_USE_PYLONG_INTERNALS 0 123 | #ifndef CYTHON_AVOID_BORROWED_REFS 124 | #define CYTHON_AVOID_BORROWED_REFS 0 125 | #endif 126 | #ifndef CYTHON_ASSUME_SAFE_MACROS 127 | #define CYTHON_ASSUME_SAFE_MACROS 1 128 | #endif 129 | #ifndef CYTHON_UNPACK_METHODS 130 | #define CYTHON_UNPACK_METHODS 1 131 | #endif 132 | #undef CYTHON_FAST_THREAD_STATE 133 | #define CYTHON_FAST_THREAD_STATE 0 134 | #undef CYTHON_FAST_PYCALL 135 | #define CYTHON_FAST_PYCALL 0 136 | #undef CYTHON_PEP489_MULTI_PHASE_INIT 137 | #define CYTHON_PEP489_MULTI_PHASE_INIT 0 138 | #undef CYTHON_USE_TP_FINALIZE 139 | #define CYTHON_USE_TP_FINALIZE 0 140 | #undef CYTHON_USE_DICT_VERSIONS 141 | #define CYTHON_USE_DICT_VERSIONS 0 142 | #undef CYTHON_USE_EXC_INFO_STACK 143 | #define CYTHON_USE_EXC_INFO_STACK 0 144 | #ifndef CYTHON_UPDATE_DESCRIPTOR_DOC 145 | #define CYTHON_UPDATE_DESCRIPTOR_DOC 0 146 | #endif 147 | #else 148 | #define CYTHON_COMPILING_IN_PYPY 0 149 | #define CYTHON_COMPILING_IN_PYSTON 0 150 | #define CYTHON_COMPILING_IN_CPYTHON 1 151 | #ifndef CYTHON_USE_TYPE_SLOTS 152 | #define CYTHON_USE_TYPE_SLOTS 1 153 | #endif 154 | #if PY_VERSION_HEX < 0x02070000 155 | #undef CYTHON_USE_PYTYPE_LOOKUP 156 | #define CYTHON_USE_PYTYPE_LOOKUP 0 157 | #elif !defined(CYTHON_USE_PYTYPE_LOOKUP) 158 | #define CYTHON_USE_PYTYPE_LOOKUP 1 159 | #endif 160 | #if PY_MAJOR_VERSION < 3 161 | #undef CYTHON_USE_ASYNC_SLOTS 162 | #define CYTHON_USE_ASYNC_SLOTS 0 163 | #elif !defined(CYTHON_USE_ASYNC_SLOTS) 164 | #define CYTHON_USE_ASYNC_SLOTS 1 165 | #endif 166 | #if PY_VERSION_HEX < 0x02070000 167 | #undef CYTHON_USE_PYLONG_INTERNALS 168 | #define CYTHON_USE_PYLONG_INTERNALS 0 169 | #elif !defined(CYTHON_USE_PYLONG_INTERNALS) 170 | #define CYTHON_USE_PYLONG_INTERNALS 1 171 | #endif 172 | #ifndef CYTHON_USE_PYLIST_INTERNALS 173 | #define CYTHON_USE_PYLIST_INTERNALS 1 174 | #endif 175 | #ifndef CYTHON_USE_UNICODE_INTERNALS 176 | #define CYTHON_USE_UNICODE_INTERNALS 1 177 | #endif 178 | #if PY_VERSION_HEX < 0x030300F0 || PY_VERSION_HEX >= 0x030B00A2 179 | #undef CYTHON_USE_UNICODE_WRITER 180 | #define CYTHON_USE_UNICODE_WRITER 0 181 | #elif !defined(CYTHON_USE_UNICODE_WRITER) 182 | #define CYTHON_USE_UNICODE_WRITER 1 183 | #endif 184 | #ifndef CYTHON_AVOID_BORROWED_REFS 185 | #define CYTHON_AVOID_BORROWED_REFS 0 186 | #endif 187 | #ifndef CYTHON_ASSUME_SAFE_MACROS 188 | #define CYTHON_ASSUME_SAFE_MACROS 1 189 | #endif 190 | #ifndef CYTHON_UNPACK_METHODS 191 | #define CYTHON_UNPACK_METHODS 1 192 | #endif 193 | #if PY_VERSION_HEX >= 0x030B00A4 194 | #undef CYTHON_FAST_THREAD_STATE 195 | #define CYTHON_FAST_THREAD_STATE 0 196 | #elif !defined(CYTHON_FAST_THREAD_STATE) 197 | #define CYTHON_FAST_THREAD_STATE 1 198 | #endif 199 | #ifndef CYTHON_FAST_PYCALL 200 | #define CYTHON_FAST_PYCALL (PY_VERSION_HEX < 0x030A0000) 201 | #endif 202 | #ifndef CYTHON_PEP489_MULTI_PHASE_INIT 203 | #define CYTHON_PEP489_MULTI_PHASE_INIT (PY_VERSION_HEX >= 0x03050000) 204 | #endif 205 | #ifndef CYTHON_USE_TP_FINALIZE 206 | #define CYTHON_USE_TP_FINALIZE (PY_VERSION_HEX >= 0x030400a1) 207 | #endif 208 | #ifndef CYTHON_USE_DICT_VERSIONS 209 | #define CYTHON_USE_DICT_VERSIONS (PY_VERSION_HEX >= 0x030600B1) 210 | #endif 211 | #if PY_VERSION_HEX >= 0x030B00A4 212 | #undef CYTHON_USE_EXC_INFO_STACK 213 | #define CYTHON_USE_EXC_INFO_STACK 0 214 | #elif !defined(CYTHON_USE_EXC_INFO_STACK) 215 | #define CYTHON_USE_EXC_INFO_STACK (PY_VERSION_HEX >= 0x030700A3) 216 | #endif 217 | #ifndef CYTHON_UPDATE_DESCRIPTOR_DOC 218 | #define CYTHON_UPDATE_DESCRIPTOR_DOC 1 219 | #endif 220 | #endif 221 | #if !defined(CYTHON_FAST_PYCCALL) 222 | #define CYTHON_FAST_PYCCALL (CYTHON_FAST_PYCALL && PY_VERSION_HEX >= 0x030600B1) 223 | #endif 224 | #if CYTHON_USE_PYLONG_INTERNALS 225 | #if PY_MAJOR_VERSION < 3 226 | #include "longintrepr.h" 227 | #endif 228 | #undef SHIFT 229 | #undef BASE 230 | #undef MASK 231 | #ifdef SIZEOF_VOID_P 232 | enum { __pyx_check_sizeof_voidp = 1 / (int)(SIZEOF_VOID_P == sizeof(void*)) }; 233 | #endif 234 | #endif 235 | #ifndef __has_attribute 236 | #define __has_attribute(x) 0 237 | #endif 238 | #ifndef __has_cpp_attribute 239 | #define __has_cpp_attribute(x) 0 240 | #endif 241 | #ifndef CYTHON_RESTRICT 242 | #if defined(__GNUC__) 243 | #define CYTHON_RESTRICT __restrict__ 244 | #elif defined(_MSC_VER) && _MSC_VER >= 1400 245 | #define CYTHON_RESTRICT __restrict 246 | #elif defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L 247 | #define CYTHON_RESTRICT restrict 248 | #else 249 | #define CYTHON_RESTRICT 250 | #endif 251 | #endif 252 | #ifndef CYTHON_UNUSED 253 | # if defined(__GNUC__) 254 | # if !(defined(__cplusplus)) || (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)) 255 | # define CYTHON_UNUSED __attribute__ ((__unused__)) 256 | # else 257 | # define CYTHON_UNUSED 258 | # endif 259 | # elif defined(__ICC) || (defined(__INTEL_COMPILER) && !defined(_MSC_VER)) 260 | # define CYTHON_UNUSED __attribute__ ((__unused__)) 261 | # else 262 | # define CYTHON_UNUSED 263 | # endif 264 | #endif 265 | #ifndef CYTHON_MAYBE_UNUSED_VAR 266 | # if defined(__cplusplus) 267 | template void CYTHON_MAYBE_UNUSED_VAR( const T& ) { } 268 | # else 269 | # define CYTHON_MAYBE_UNUSED_VAR(x) (void)(x) 270 | # endif 271 | #endif 272 | #ifndef CYTHON_NCP_UNUSED 273 | # if CYTHON_COMPILING_IN_CPYTHON 274 | # define CYTHON_NCP_UNUSED 275 | # else 276 | # define CYTHON_NCP_UNUSED CYTHON_UNUSED 277 | # endif 278 | #endif 279 | #define __Pyx_void_to_None(void_result) ((void)(void_result), Py_INCREF(Py_None), Py_None) 280 | #ifdef _MSC_VER 281 | #ifndef _MSC_STDINT_H_ 282 | #if _MSC_VER < 1300 283 | typedef unsigned char uint8_t; 284 | typedef unsigned int uint32_t; 285 | #else 286 | typedef unsigned __int8 uint8_t; 287 | typedef unsigned __int32 uint32_t; 288 | #endif 289 | #endif 290 | #else 291 | #include 292 | #endif 293 | #ifndef CYTHON_FALLTHROUGH 294 | #if defined(__cplusplus) && __cplusplus >= 201103L 295 | #if __has_cpp_attribute(fallthrough) 296 | #define CYTHON_FALLTHROUGH [[fallthrough]] 297 | #elif __has_cpp_attribute(clang::fallthrough) 298 | #define CYTHON_FALLTHROUGH [[clang::fallthrough]] 299 | #elif __has_cpp_attribute(gnu::fallthrough) 300 | #define CYTHON_FALLTHROUGH [[gnu::fallthrough]] 301 | #endif 302 | #endif 303 | #ifndef CYTHON_FALLTHROUGH 304 | #if __has_attribute(fallthrough) 305 | #define CYTHON_FALLTHROUGH __attribute__((fallthrough)) 306 | #else 307 | #define CYTHON_FALLTHROUGH 308 | #endif 309 | #endif 310 | #if defined(__clang__ ) && defined(__apple_build_version__) 311 | #if __apple_build_version__ < 7000000 312 | #undef CYTHON_FALLTHROUGH 313 | #define CYTHON_FALLTHROUGH 314 | #endif 315 | #endif 316 | #endif 317 | 318 | #ifndef CYTHON_INLINE 319 | #if defined(__clang__) 320 | #define CYTHON_INLINE __inline__ __attribute__ ((__unused__)) 321 | #elif defined(__GNUC__) 322 | #define CYTHON_INLINE __inline__ 323 | #elif defined(_MSC_VER) 324 | #define CYTHON_INLINE __inline 325 | #elif defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L 326 | #define CYTHON_INLINE inline 327 | #else 328 | #define CYTHON_INLINE 329 | #endif 330 | #endif 331 | 332 | #if CYTHON_COMPILING_IN_PYPY && PY_VERSION_HEX < 0x02070600 && !defined(Py_OptimizeFlag) 333 | #define Py_OptimizeFlag 0 334 | #endif 335 | #define __PYX_BUILD_PY_SSIZE_T "n" 336 | #define CYTHON_FORMAT_SSIZE_T "z" 337 | #if PY_MAJOR_VERSION < 3 338 | #define __Pyx_BUILTIN_MODULE_NAME "__builtin__" 339 | #define __Pyx_PyCode_New(a, k, l, s, f, code, c, n, v, fv, cell, fn, name, fline, lnos)\ 340 | PyCode_New(a+k, l, s, f, code, c, n, v, fv, cell, fn, name, fline, lnos) 341 | #define __Pyx_DefaultClassType PyClass_Type 342 | #else 343 | #define __Pyx_BUILTIN_MODULE_NAME "builtins" 344 | #define __Pyx_DefaultClassType PyType_Type 345 | #if PY_VERSION_HEX >= 0x030B00A1 346 | static CYTHON_INLINE PyCodeObject* __Pyx_PyCode_New(int a, int k, int l, int s, int f, 347 | PyObject *code, PyObject *c, PyObject* n, PyObject *v, 348 | PyObject *fv, PyObject *cell, PyObject* fn, 349 | PyObject *name, int fline, PyObject *lnos) { 350 | PyObject *kwds=NULL, *argcount=NULL, *posonlyargcount=NULL, *kwonlyargcount=NULL; 351 | PyObject *nlocals=NULL, *stacksize=NULL, *flags=NULL, *replace=NULL, *call_result=NULL, *empty=NULL; 352 | const char *fn_cstr=NULL; 353 | const char *name_cstr=NULL; 354 | PyCodeObject* co=NULL; 355 | PyObject *type, *value, *traceback; 356 | PyErr_Fetch(&type, &value, &traceback); 357 | if (!(kwds=PyDict_New())) goto end; 358 | if (!(argcount=PyLong_FromLong(a))) goto end; 359 | if (PyDict_SetItemString(kwds, "co_argcount", argcount) != 0) goto end; 360 | if (!(posonlyargcount=PyLong_FromLong(0))) goto end; 361 | if (PyDict_SetItemString(kwds, "co_posonlyargcount", posonlyargcount) != 0) goto end; 362 | if (!(kwonlyargcount=PyLong_FromLong(k))) goto end; 363 | if (PyDict_SetItemString(kwds, "co_kwonlyargcount", kwonlyargcount) != 0) goto end; 364 | if (!(nlocals=PyLong_FromLong(l))) goto end; 365 | if (PyDict_SetItemString(kwds, "co_nlocals", nlocals) != 0) goto end; 366 | if (!(stacksize=PyLong_FromLong(s))) goto end; 367 | if (PyDict_SetItemString(kwds, "co_stacksize", stacksize) != 0) goto end; 368 | if (!(flags=PyLong_FromLong(f))) goto end; 369 | if (PyDict_SetItemString(kwds, "co_flags", flags) != 0) goto end; 370 | if (PyDict_SetItemString(kwds, "co_code", code) != 0) goto end; 371 | if (PyDict_SetItemString(kwds, "co_consts", c) != 0) goto end; 372 | if (PyDict_SetItemString(kwds, "co_names", n) != 0) goto end; 373 | if (PyDict_SetItemString(kwds, "co_varnames", v) != 0) goto end; 374 | if (PyDict_SetItemString(kwds, "co_freevars", fv) != 0) goto end; 375 | if (PyDict_SetItemString(kwds, "co_cellvars", cell) != 0) goto end; 376 | if (PyDict_SetItemString(kwds, "co_linetable", lnos) != 0) goto end; 377 | if (!(fn_cstr=PyUnicode_AsUTF8AndSize(fn, NULL))) goto end; 378 | if (!(name_cstr=PyUnicode_AsUTF8AndSize(name, NULL))) goto end; 379 | if (!(co = PyCode_NewEmpty(fn_cstr, name_cstr, fline))) goto end; 380 | if (!(replace = PyObject_GetAttrString((PyObject*)co, "replace"))) goto cleanup_code_too; 381 | if (!(empty = PyTuple_New(0))) goto cleanup_code_too; // unfortunately __pyx_empty_tuple isn't available here 382 | if (!(call_result = PyObject_Call(replace, empty, kwds))) goto cleanup_code_too; 383 | Py_XDECREF((PyObject*)co); 384 | co = (PyCodeObject*)call_result; 385 | call_result = NULL; 386 | if (0) { 387 | cleanup_code_too: 388 | Py_XDECREF((PyObject*)co); 389 | co = NULL; 390 | } 391 | end: 392 | Py_XDECREF(kwds); 393 | Py_XDECREF(argcount); 394 | Py_XDECREF(posonlyargcount); 395 | Py_XDECREF(kwonlyargcount); 396 | Py_XDECREF(nlocals); 397 | Py_XDECREF(stacksize); 398 | Py_XDECREF(replace); 399 | Py_XDECREF(call_result); 400 | Py_XDECREF(empty); 401 | if (type) { 402 | PyErr_Restore(type, value, traceback); 403 | } 404 | return co; 405 | } 406 | #else 407 | #define __Pyx_PyCode_New(a, k, l, s, f, code, c, n, v, fv, cell, fn, name, fline, lnos)\ 408 | PyCode_New(a, k, l, s, f, code, c, n, v, fv, cell, fn, name, fline, lnos) 409 | #endif 410 | #define __Pyx_DefaultClassType PyType_Type 411 | #endif 412 | #ifndef Py_TPFLAGS_CHECKTYPES 413 | #define Py_TPFLAGS_CHECKTYPES 0 414 | #endif 415 | #ifndef Py_TPFLAGS_HAVE_INDEX 416 | #define Py_TPFLAGS_HAVE_INDEX 0 417 | #endif 418 | #ifndef Py_TPFLAGS_HAVE_NEWBUFFER 419 | #define Py_TPFLAGS_HAVE_NEWBUFFER 0 420 | #endif 421 | #ifndef Py_TPFLAGS_HAVE_FINALIZE 422 | #define Py_TPFLAGS_HAVE_FINALIZE 0 423 | #endif 424 | #ifndef METH_STACKLESS 425 | #define METH_STACKLESS 0 426 | #endif 427 | #if PY_VERSION_HEX <= 0x030700A3 || !defined(METH_FASTCALL) 428 | #ifndef METH_FASTCALL 429 | #define METH_FASTCALL 0x80 430 | #endif 431 | typedef PyObject *(*__Pyx_PyCFunctionFast) (PyObject *self, PyObject *const *args, Py_ssize_t nargs); 432 | typedef PyObject *(*__Pyx_PyCFunctionFastWithKeywords) (PyObject *self, PyObject *const *args, 433 | Py_ssize_t nargs, PyObject *kwnames); 434 | #else 435 | #define __Pyx_PyCFunctionFast _PyCFunctionFast 436 | #define __Pyx_PyCFunctionFastWithKeywords _PyCFunctionFastWithKeywords 437 | #endif 438 | #if CYTHON_FAST_PYCCALL 439 | #define __Pyx_PyFastCFunction_Check(func)\ 440 | ((PyCFunction_Check(func) && (METH_FASTCALL == (PyCFunction_GET_FLAGS(func) & ~(METH_CLASS | METH_STATIC | METH_COEXIST | METH_KEYWORDS | METH_STACKLESS))))) 441 | #else 442 | #define __Pyx_PyFastCFunction_Check(func) 0 443 | #endif 444 | #if CYTHON_COMPILING_IN_PYPY && !defined(PyObject_Malloc) 445 | #define PyObject_Malloc(s) PyMem_Malloc(s) 446 | #define PyObject_Free(p) PyMem_Free(p) 447 | #define PyObject_Realloc(p) PyMem_Realloc(p) 448 | #endif 449 | #if CYTHON_COMPILING_IN_CPYTHON && PY_VERSION_HEX < 0x030400A1 450 | #define PyMem_RawMalloc(n) PyMem_Malloc(n) 451 | #define PyMem_RawRealloc(p, n) PyMem_Realloc(p, n) 452 | #define PyMem_RawFree(p) PyMem_Free(p) 453 | #endif 454 | #if CYTHON_COMPILING_IN_PYSTON 455 | #define __Pyx_PyCode_HasFreeVars(co) PyCode_HasFreeVars(co) 456 | #define __Pyx_PyFrame_SetLineNumber(frame, lineno) PyFrame_SetLineNumber(frame, lineno) 457 | #else 458 | #define __Pyx_PyCode_HasFreeVars(co) (PyCode_GetNumFree(co) > 0) 459 | #define __Pyx_PyFrame_SetLineNumber(frame, lineno) (frame)->f_lineno = (lineno) 460 | #endif 461 | #if !CYTHON_FAST_THREAD_STATE || PY_VERSION_HEX < 0x02070000 462 | #define __Pyx_PyThreadState_Current PyThreadState_GET() 463 | #elif PY_VERSION_HEX >= 0x03060000 464 | #define __Pyx_PyThreadState_Current _PyThreadState_UncheckedGet() 465 | #elif PY_VERSION_HEX >= 0x03000000 466 | #define __Pyx_PyThreadState_Current PyThreadState_GET() 467 | #else 468 | #define __Pyx_PyThreadState_Current _PyThreadState_Current 469 | #endif 470 | #if PY_VERSION_HEX < 0x030700A2 && !defined(PyThread_tss_create) && !defined(Py_tss_NEEDS_INIT) 471 | #include "pythread.h" 472 | #define Py_tss_NEEDS_INIT 0 473 | typedef int Py_tss_t; 474 | static CYTHON_INLINE int PyThread_tss_create(Py_tss_t *key) { 475 | *key = PyThread_create_key(); 476 | return 0; 477 | } 478 | static CYTHON_INLINE Py_tss_t * PyThread_tss_alloc(void) { 479 | Py_tss_t *key = (Py_tss_t *)PyObject_Malloc(sizeof(Py_tss_t)); 480 | *key = Py_tss_NEEDS_INIT; 481 | return key; 482 | } 483 | static CYTHON_INLINE void PyThread_tss_free(Py_tss_t *key) { 484 | PyObject_Free(key); 485 | } 486 | static CYTHON_INLINE int PyThread_tss_is_created(Py_tss_t *key) { 487 | return *key != Py_tss_NEEDS_INIT; 488 | } 489 | static CYTHON_INLINE void PyThread_tss_delete(Py_tss_t *key) { 490 | PyThread_delete_key(*key); 491 | *key = Py_tss_NEEDS_INIT; 492 | } 493 | static CYTHON_INLINE int PyThread_tss_set(Py_tss_t *key, void *value) { 494 | return PyThread_set_key_value(*key, value); 495 | } 496 | static CYTHON_INLINE void * PyThread_tss_get(Py_tss_t *key) { 497 | return PyThread_get_key_value(*key); 498 | } 499 | #endif 500 | #if CYTHON_COMPILING_IN_CPYTHON || defined(_PyDict_NewPresized) 501 | #define __Pyx_PyDict_NewPresized(n) ((n <= 8) ? PyDict_New() : _PyDict_NewPresized(n)) 502 | #else 503 | #define __Pyx_PyDict_NewPresized(n) PyDict_New() 504 | #endif 505 | #if PY_MAJOR_VERSION >= 3 || CYTHON_FUTURE_DIVISION 506 | #define __Pyx_PyNumber_Divide(x,y) PyNumber_TrueDivide(x,y) 507 | #define __Pyx_PyNumber_InPlaceDivide(x,y) PyNumber_InPlaceTrueDivide(x,y) 508 | #else 509 | #define __Pyx_PyNumber_Divide(x,y) PyNumber_Divide(x,y) 510 | #define __Pyx_PyNumber_InPlaceDivide(x,y) PyNumber_InPlaceDivide(x,y) 511 | #endif 512 | #if CYTHON_COMPILING_IN_CPYTHON && PY_VERSION_HEX >= 0x030500A1 && CYTHON_USE_UNICODE_INTERNALS 513 | #define __Pyx_PyDict_GetItemStr(dict, name) _PyDict_GetItem_KnownHash(dict, name, ((PyASCIIObject *) name)->hash) 514 | #else 515 | #define __Pyx_PyDict_GetItemStr(dict, name) PyDict_GetItem(dict, name) 516 | #endif 517 | #if PY_VERSION_HEX > 0x03030000 && defined(PyUnicode_KIND) 518 | #define CYTHON_PEP393_ENABLED 1 519 | #if defined(PyUnicode_IS_READY) 520 | #define __Pyx_PyUnicode_READY(op) (likely(PyUnicode_IS_READY(op)) ?\ 521 | 0 : _PyUnicode_Ready((PyObject *)(op))) 522 | #else 523 | #define __Pyx_PyUnicode_READY(op) (0) 524 | #endif 525 | #define __Pyx_PyUnicode_GET_LENGTH(u) PyUnicode_GET_LENGTH(u) 526 | #define __Pyx_PyUnicode_READ_CHAR(u, i) PyUnicode_READ_CHAR(u, i) 527 | #define __Pyx_PyUnicode_MAX_CHAR_VALUE(u) PyUnicode_MAX_CHAR_VALUE(u) 528 | #define __Pyx_PyUnicode_KIND(u) PyUnicode_KIND(u) 529 | #define __Pyx_PyUnicode_DATA(u) PyUnicode_DATA(u) 530 | #define __Pyx_PyUnicode_READ(k, d, i) PyUnicode_READ(k, d, i) 531 | #define __Pyx_PyUnicode_WRITE(k, d, i, ch) PyUnicode_WRITE(k, d, i, ch) 532 | #if defined(PyUnicode_IS_READY) && defined(PyUnicode_GET_SIZE) 533 | #if CYTHON_COMPILING_IN_CPYTHON && PY_VERSION_HEX >= 0x03090000 534 | #define __Pyx_PyUnicode_IS_TRUE(u) (0 != (likely(PyUnicode_IS_READY(u)) ? PyUnicode_GET_LENGTH(u) : ((PyCompactUnicodeObject *)(u))->wstr_length)) 535 | #else 536 | #define __Pyx_PyUnicode_IS_TRUE(u) (0 != (likely(PyUnicode_IS_READY(u)) ? PyUnicode_GET_LENGTH(u) : PyUnicode_GET_SIZE(u))) 537 | #endif 538 | #else 539 | #define __Pyx_PyUnicode_IS_TRUE(u) (0 != PyUnicode_GET_LENGTH(u)) 540 | #endif 541 | #else 542 | #define CYTHON_PEP393_ENABLED 0 543 | #define PyUnicode_1BYTE_KIND 1 544 | #define PyUnicode_2BYTE_KIND 2 545 | #define PyUnicode_4BYTE_KIND 4 546 | #define __Pyx_PyUnicode_READY(op) (0) 547 | #define __Pyx_PyUnicode_GET_LENGTH(u) PyUnicode_GET_SIZE(u) 548 | #define __Pyx_PyUnicode_READ_CHAR(u, i) ((Py_UCS4)(PyUnicode_AS_UNICODE(u)[i])) 549 | #define __Pyx_PyUnicode_MAX_CHAR_VALUE(u) ((sizeof(Py_UNICODE) == 2) ? 65535 : 1114111) 550 | #define __Pyx_PyUnicode_KIND(u) (sizeof(Py_UNICODE)) 551 | #define __Pyx_PyUnicode_DATA(u) ((void*)PyUnicode_AS_UNICODE(u)) 552 | #define __Pyx_PyUnicode_READ(k, d, i) ((void)(k), (Py_UCS4)(((Py_UNICODE*)d)[i])) 553 | #define __Pyx_PyUnicode_WRITE(k, d, i, ch) (((void)(k)), ((Py_UNICODE*)d)[i] = ch) 554 | #define __Pyx_PyUnicode_IS_TRUE(u) (0 != PyUnicode_GET_SIZE(u)) 555 | #endif 556 | #if CYTHON_COMPILING_IN_PYPY 557 | #define __Pyx_PyUnicode_Concat(a, b) PyNumber_Add(a, b) 558 | #define __Pyx_PyUnicode_ConcatSafe(a, b) PyNumber_Add(a, b) 559 | #else 560 | #define __Pyx_PyUnicode_Concat(a, b) PyUnicode_Concat(a, b) 561 | #define __Pyx_PyUnicode_ConcatSafe(a, b) ((unlikely((a) == Py_None) || unlikely((b) == Py_None)) ?\ 562 | PyNumber_Add(a, b) : __Pyx_PyUnicode_Concat(a, b)) 563 | #endif 564 | #if CYTHON_COMPILING_IN_PYPY && !defined(PyUnicode_Contains) 565 | #define PyUnicode_Contains(u, s) PySequence_Contains(u, s) 566 | #endif 567 | #if CYTHON_COMPILING_IN_PYPY && !defined(PyByteArray_Check) 568 | #define PyByteArray_Check(obj) PyObject_TypeCheck(obj, &PyByteArray_Type) 569 | #endif 570 | #if CYTHON_COMPILING_IN_PYPY && !defined(PyObject_Format) 571 | #define PyObject_Format(obj, fmt) PyObject_CallMethod(obj, "__format__", "O", fmt) 572 | #endif 573 | #define __Pyx_PyString_FormatSafe(a, b) ((unlikely((a) == Py_None || (PyString_Check(b) && !PyString_CheckExact(b)))) ? PyNumber_Remainder(a, b) : __Pyx_PyString_Format(a, b)) 574 | #define __Pyx_PyUnicode_FormatSafe(a, b) ((unlikely((a) == Py_None || (PyUnicode_Check(b) && !PyUnicode_CheckExact(b)))) ? PyNumber_Remainder(a, b) : PyUnicode_Format(a, b)) 575 | #if PY_MAJOR_VERSION >= 3 576 | #define __Pyx_PyString_Format(a, b) PyUnicode_Format(a, b) 577 | #else 578 | #define __Pyx_PyString_Format(a, b) PyString_Format(a, b) 579 | #endif 580 | #if PY_MAJOR_VERSION < 3 && !defined(PyObject_ASCII) 581 | #define PyObject_ASCII(o) PyObject_Repr(o) 582 | #endif 583 | #if PY_MAJOR_VERSION >= 3 584 | #define PyBaseString_Type PyUnicode_Type 585 | #define PyStringObject PyUnicodeObject 586 | #define PyString_Type PyUnicode_Type 587 | #define PyString_Check PyUnicode_Check 588 | #define PyString_CheckExact PyUnicode_CheckExact 589 | #ifndef PyObject_Unicode 590 | #define PyObject_Unicode PyObject_Str 591 | #endif 592 | #endif 593 | #if PY_MAJOR_VERSION >= 3 594 | #define __Pyx_PyBaseString_Check(obj) PyUnicode_Check(obj) 595 | #define __Pyx_PyBaseString_CheckExact(obj) PyUnicode_CheckExact(obj) 596 | #else 597 | #define __Pyx_PyBaseString_Check(obj) (PyString_Check(obj) || PyUnicode_Check(obj)) 598 | #define __Pyx_PyBaseString_CheckExact(obj) (PyString_CheckExact(obj) || PyUnicode_CheckExact(obj)) 599 | #endif 600 | #ifndef PySet_CheckExact 601 | #define PySet_CheckExact(obj) (Py_TYPE(obj) == &PySet_Type) 602 | #endif 603 | #if PY_VERSION_HEX >= 0x030900A4 604 | #define __Pyx_SET_REFCNT(obj, refcnt) Py_SET_REFCNT(obj, refcnt) 605 | #define __Pyx_SET_SIZE(obj, size) Py_SET_SIZE(obj, size) 606 | #else 607 | #define __Pyx_SET_REFCNT(obj, refcnt) Py_REFCNT(obj) = (refcnt) 608 | #define __Pyx_SET_SIZE(obj, size) Py_SIZE(obj) = (size) 609 | #endif 610 | #if CYTHON_ASSUME_SAFE_MACROS 611 | #define __Pyx_PySequence_SIZE(seq) Py_SIZE(seq) 612 | #else 613 | #define __Pyx_PySequence_SIZE(seq) PySequence_Size(seq) 614 | #endif 615 | #if PY_MAJOR_VERSION >= 3 616 | #define PyIntObject PyLongObject 617 | #define PyInt_Type PyLong_Type 618 | #define PyInt_Check(op) PyLong_Check(op) 619 | #define PyInt_CheckExact(op) PyLong_CheckExact(op) 620 | #define PyInt_FromString PyLong_FromString 621 | #define PyInt_FromUnicode PyLong_FromUnicode 622 | #define PyInt_FromLong PyLong_FromLong 623 | #define PyInt_FromSize_t PyLong_FromSize_t 624 | #define PyInt_FromSsize_t PyLong_FromSsize_t 625 | #define PyInt_AsLong PyLong_AsLong 626 | #define PyInt_AS_LONG PyLong_AS_LONG 627 | #define PyInt_AsSsize_t PyLong_AsSsize_t 628 | #define PyInt_AsUnsignedLongMask PyLong_AsUnsignedLongMask 629 | #define PyInt_AsUnsignedLongLongMask PyLong_AsUnsignedLongLongMask 630 | #define PyNumber_Int PyNumber_Long 631 | #endif 632 | #if PY_MAJOR_VERSION >= 3 633 | #define PyBoolObject PyLongObject 634 | #endif 635 | #if PY_MAJOR_VERSION >= 3 && CYTHON_COMPILING_IN_PYPY 636 | #ifndef PyUnicode_InternFromString 637 | #define PyUnicode_InternFromString(s) PyUnicode_FromString(s) 638 | #endif 639 | #endif 640 | #if PY_VERSION_HEX < 0x030200A4 641 | typedef long Py_hash_t; 642 | #define __Pyx_PyInt_FromHash_t PyInt_FromLong 643 | #define __Pyx_PyInt_AsHash_t __Pyx_PyIndex_AsHash_t 644 | #else 645 | #define __Pyx_PyInt_FromHash_t PyInt_FromSsize_t 646 | #define __Pyx_PyInt_AsHash_t __Pyx_PyIndex_AsSsize_t 647 | #endif 648 | #if PY_MAJOR_VERSION >= 3 649 | #define __Pyx_PyMethod_New(func, self, klass) ((self) ? ((void)(klass), PyMethod_New(func, self)) : __Pyx_NewRef(func)) 650 | #else 651 | #define __Pyx_PyMethod_New(func, self, klass) PyMethod_New(func, self, klass) 652 | #endif 653 | #if CYTHON_USE_ASYNC_SLOTS 654 | #if PY_VERSION_HEX >= 0x030500B1 655 | #define __Pyx_PyAsyncMethodsStruct PyAsyncMethods 656 | #define __Pyx_PyType_AsAsync(obj) (Py_TYPE(obj)->tp_as_async) 657 | #else 658 | #define __Pyx_PyType_AsAsync(obj) ((__Pyx_PyAsyncMethodsStruct*) (Py_TYPE(obj)->tp_reserved)) 659 | #endif 660 | #else 661 | #define __Pyx_PyType_AsAsync(obj) NULL 662 | #endif 663 | #ifndef __Pyx_PyAsyncMethodsStruct 664 | typedef struct { 665 | unaryfunc am_await; 666 | unaryfunc am_aiter; 667 | unaryfunc am_anext; 668 | } __Pyx_PyAsyncMethodsStruct; 669 | #endif 670 | 671 | #if defined(_WIN32) || defined(WIN32) || defined(MS_WINDOWS) 672 | #if !defined(_USE_MATH_DEFINES) 673 | #define _USE_MATH_DEFINES 674 | #endif 675 | #endif 676 | #include 677 | #ifdef NAN 678 | #define __PYX_NAN() ((float) NAN) 679 | #else 680 | static CYTHON_INLINE float __PYX_NAN() { 681 | float value; 682 | memset(&value, 0xFF, sizeof(value)); 683 | return value; 684 | } 685 | #endif 686 | #if defined(__CYGWIN__) && defined(_LDBL_EQ_DBL) 687 | #define __Pyx_truncl trunc 688 | #else 689 | #define __Pyx_truncl truncl 690 | #endif 691 | 692 | #define __PYX_MARK_ERR_POS(f_index, lineno) \ 693 | { __pyx_filename = __pyx_f[f_index]; (void)__pyx_filename; __pyx_lineno = lineno; (void)__pyx_lineno; __pyx_clineno = __LINE__; (void)__pyx_clineno; } 694 | #define __PYX_ERR(f_index, lineno, Ln_error) \ 695 | { __PYX_MARK_ERR_POS(f_index, lineno) goto Ln_error; } 696 | 697 | #ifndef __PYX_EXTERN_C 698 | #ifdef __cplusplus 699 | #define __PYX_EXTERN_C extern "C" 700 | #else 701 | #define __PYX_EXTERN_C extern 702 | #endif 703 | #endif 704 | 705 | #define __PYX_HAVE__upolygon____init__ 706 | #define __PYX_HAVE_API__upolygon____init__ 707 | /* Early includes */ 708 | #ifdef _OPENMP 709 | #include 710 | #endif /* _OPENMP */ 711 | 712 | #if defined(PYREX_WITHOUT_ASSERTIONS) && !defined(CYTHON_WITHOUT_ASSERTIONS) 713 | #define CYTHON_WITHOUT_ASSERTIONS 714 | #endif 715 | 716 | typedef struct {PyObject **p; const char *s; const Py_ssize_t n; const char* encoding; 717 | const char is_unicode; const char is_str; const char intern; } __Pyx_StringTabEntry; 718 | 719 | #define __PYX_DEFAULT_STRING_ENCODING_IS_ASCII 0 720 | #define __PYX_DEFAULT_STRING_ENCODING_IS_UTF8 0 721 | #define __PYX_DEFAULT_STRING_ENCODING_IS_DEFAULT (PY_MAJOR_VERSION >= 3 && __PYX_DEFAULT_STRING_ENCODING_IS_UTF8) 722 | #define __PYX_DEFAULT_STRING_ENCODING "" 723 | #define __Pyx_PyObject_FromString __Pyx_PyBytes_FromString 724 | #define __Pyx_PyObject_FromStringAndSize __Pyx_PyBytes_FromStringAndSize 725 | #define __Pyx_uchar_cast(c) ((unsigned char)c) 726 | #define __Pyx_long_cast(x) ((long)x) 727 | #define __Pyx_fits_Py_ssize_t(v, type, is_signed) (\ 728 | (sizeof(type) < sizeof(Py_ssize_t)) ||\ 729 | (sizeof(type) > sizeof(Py_ssize_t) &&\ 730 | likely(v < (type)PY_SSIZE_T_MAX ||\ 731 | v == (type)PY_SSIZE_T_MAX) &&\ 732 | (!is_signed || likely(v > (type)PY_SSIZE_T_MIN ||\ 733 | v == (type)PY_SSIZE_T_MIN))) ||\ 734 | (sizeof(type) == sizeof(Py_ssize_t) &&\ 735 | (is_signed || likely(v < (type)PY_SSIZE_T_MAX ||\ 736 | v == (type)PY_SSIZE_T_MAX))) ) 737 | static CYTHON_INLINE int __Pyx_is_valid_index(Py_ssize_t i, Py_ssize_t limit) { 738 | return (size_t) i < (size_t) limit; 739 | } 740 | #if defined (__cplusplus) && __cplusplus >= 201103L 741 | #include 742 | #define __Pyx_sst_abs(value) std::abs(value) 743 | #elif SIZEOF_INT >= SIZEOF_SIZE_T 744 | #define __Pyx_sst_abs(value) abs(value) 745 | #elif SIZEOF_LONG >= SIZEOF_SIZE_T 746 | #define __Pyx_sst_abs(value) labs(value) 747 | #elif defined (_MSC_VER) 748 | #define __Pyx_sst_abs(value) ((Py_ssize_t)_abs64(value)) 749 | #elif defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L 750 | #define __Pyx_sst_abs(value) llabs(value) 751 | #elif defined (__GNUC__) 752 | #define __Pyx_sst_abs(value) __builtin_llabs(value) 753 | #else 754 | #define __Pyx_sst_abs(value) ((value<0) ? -value : value) 755 | #endif 756 | static CYTHON_INLINE const char* __Pyx_PyObject_AsString(PyObject*); 757 | static CYTHON_INLINE const char* __Pyx_PyObject_AsStringAndSize(PyObject*, Py_ssize_t* length); 758 | #define __Pyx_PyByteArray_FromString(s) PyByteArray_FromStringAndSize((const char*)s, strlen((const char*)s)) 759 | #define __Pyx_PyByteArray_FromStringAndSize(s, l) PyByteArray_FromStringAndSize((const char*)s, l) 760 | #define __Pyx_PyBytes_FromString PyBytes_FromString 761 | #define __Pyx_PyBytes_FromStringAndSize PyBytes_FromStringAndSize 762 | static CYTHON_INLINE PyObject* __Pyx_PyUnicode_FromString(const char*); 763 | #if PY_MAJOR_VERSION < 3 764 | #define __Pyx_PyStr_FromString __Pyx_PyBytes_FromString 765 | #define __Pyx_PyStr_FromStringAndSize __Pyx_PyBytes_FromStringAndSize 766 | #else 767 | #define __Pyx_PyStr_FromString __Pyx_PyUnicode_FromString 768 | #define __Pyx_PyStr_FromStringAndSize __Pyx_PyUnicode_FromStringAndSize 769 | #endif 770 | #define __Pyx_PyBytes_AsWritableString(s) ((char*) PyBytes_AS_STRING(s)) 771 | #define __Pyx_PyBytes_AsWritableSString(s) ((signed char*) PyBytes_AS_STRING(s)) 772 | #define __Pyx_PyBytes_AsWritableUString(s) ((unsigned char*) PyBytes_AS_STRING(s)) 773 | #define __Pyx_PyBytes_AsString(s) ((const char*) PyBytes_AS_STRING(s)) 774 | #define __Pyx_PyBytes_AsSString(s) ((const signed char*) PyBytes_AS_STRING(s)) 775 | #define __Pyx_PyBytes_AsUString(s) ((const unsigned char*) PyBytes_AS_STRING(s)) 776 | #define __Pyx_PyObject_AsWritableString(s) ((char*) __Pyx_PyObject_AsString(s)) 777 | #define __Pyx_PyObject_AsWritableSString(s) ((signed char*) __Pyx_PyObject_AsString(s)) 778 | #define __Pyx_PyObject_AsWritableUString(s) ((unsigned char*) __Pyx_PyObject_AsString(s)) 779 | #define __Pyx_PyObject_AsSString(s) ((const signed char*) __Pyx_PyObject_AsString(s)) 780 | #define __Pyx_PyObject_AsUString(s) ((const unsigned char*) __Pyx_PyObject_AsString(s)) 781 | #define __Pyx_PyObject_FromCString(s) __Pyx_PyObject_FromString((const char*)s) 782 | #define __Pyx_PyBytes_FromCString(s) __Pyx_PyBytes_FromString((const char*)s) 783 | #define __Pyx_PyByteArray_FromCString(s) __Pyx_PyByteArray_FromString((const char*)s) 784 | #define __Pyx_PyStr_FromCString(s) __Pyx_PyStr_FromString((const char*)s) 785 | #define __Pyx_PyUnicode_FromCString(s) __Pyx_PyUnicode_FromString((const char*)s) 786 | static CYTHON_INLINE size_t __Pyx_Py_UNICODE_strlen(const Py_UNICODE *u) { 787 | const Py_UNICODE *u_end = u; 788 | while (*u_end++) ; 789 | return (size_t)(u_end - u - 1); 790 | } 791 | #define __Pyx_PyUnicode_FromUnicode(u) PyUnicode_FromUnicode(u, __Pyx_Py_UNICODE_strlen(u)) 792 | #define __Pyx_PyUnicode_FromUnicodeAndLength PyUnicode_FromUnicode 793 | #define __Pyx_PyUnicode_AsUnicode PyUnicode_AsUnicode 794 | #define __Pyx_NewRef(obj) (Py_INCREF(obj), obj) 795 | #define __Pyx_Owned_Py_None(b) __Pyx_NewRef(Py_None) 796 | static CYTHON_INLINE PyObject * __Pyx_PyBool_FromLong(long b); 797 | static CYTHON_INLINE int __Pyx_PyObject_IsTrue(PyObject*); 798 | static CYTHON_INLINE int __Pyx_PyObject_IsTrueAndDecref(PyObject*); 799 | static CYTHON_INLINE PyObject* __Pyx_PyNumber_IntOrLong(PyObject* x); 800 | #define __Pyx_PySequence_Tuple(obj)\ 801 | (likely(PyTuple_CheckExact(obj)) ? __Pyx_NewRef(obj) : PySequence_Tuple(obj)) 802 | static CYTHON_INLINE Py_ssize_t __Pyx_PyIndex_AsSsize_t(PyObject*); 803 | static CYTHON_INLINE PyObject * __Pyx_PyInt_FromSize_t(size_t); 804 | static CYTHON_INLINE Py_hash_t __Pyx_PyIndex_AsHash_t(PyObject*); 805 | #if CYTHON_ASSUME_SAFE_MACROS 806 | #define __pyx_PyFloat_AsDouble(x) (PyFloat_CheckExact(x) ? PyFloat_AS_DOUBLE(x) : PyFloat_AsDouble(x)) 807 | #else 808 | #define __pyx_PyFloat_AsDouble(x) PyFloat_AsDouble(x) 809 | #endif 810 | #define __pyx_PyFloat_AsFloat(x) ((float) __pyx_PyFloat_AsDouble(x)) 811 | #if PY_MAJOR_VERSION >= 3 812 | #define __Pyx_PyNumber_Int(x) (PyLong_CheckExact(x) ? __Pyx_NewRef(x) : PyNumber_Long(x)) 813 | #else 814 | #define __Pyx_PyNumber_Int(x) (PyInt_CheckExact(x) ? __Pyx_NewRef(x) : PyNumber_Int(x)) 815 | #endif 816 | #define __Pyx_PyNumber_Float(x) (PyFloat_CheckExact(x) ? __Pyx_NewRef(x) : PyNumber_Float(x)) 817 | #if PY_MAJOR_VERSION < 3 && __PYX_DEFAULT_STRING_ENCODING_IS_ASCII 818 | static int __Pyx_sys_getdefaultencoding_not_ascii; 819 | static int __Pyx_init_sys_getdefaultencoding_params(void) { 820 | PyObject* sys; 821 | PyObject* default_encoding = NULL; 822 | PyObject* ascii_chars_u = NULL; 823 | PyObject* ascii_chars_b = NULL; 824 | const char* default_encoding_c; 825 | sys = PyImport_ImportModule("sys"); 826 | if (!sys) goto bad; 827 | default_encoding = PyObject_CallMethod(sys, (char*) "getdefaultencoding", NULL); 828 | Py_DECREF(sys); 829 | if (!default_encoding) goto bad; 830 | default_encoding_c = PyBytes_AsString(default_encoding); 831 | if (!default_encoding_c) goto bad; 832 | if (strcmp(default_encoding_c, "ascii") == 0) { 833 | __Pyx_sys_getdefaultencoding_not_ascii = 0; 834 | } else { 835 | char ascii_chars[128]; 836 | int c; 837 | for (c = 0; c < 128; c++) { 838 | ascii_chars[c] = c; 839 | } 840 | __Pyx_sys_getdefaultencoding_not_ascii = 1; 841 | ascii_chars_u = PyUnicode_DecodeASCII(ascii_chars, 128, NULL); 842 | if (!ascii_chars_u) goto bad; 843 | ascii_chars_b = PyUnicode_AsEncodedString(ascii_chars_u, default_encoding_c, NULL); 844 | if (!ascii_chars_b || !PyBytes_Check(ascii_chars_b) || memcmp(ascii_chars, PyBytes_AS_STRING(ascii_chars_b), 128) != 0) { 845 | PyErr_Format( 846 | PyExc_ValueError, 847 | "This module compiled with c_string_encoding=ascii, but default encoding '%.200s' is not a superset of ascii.", 848 | default_encoding_c); 849 | goto bad; 850 | } 851 | Py_DECREF(ascii_chars_u); 852 | Py_DECREF(ascii_chars_b); 853 | } 854 | Py_DECREF(default_encoding); 855 | return 0; 856 | bad: 857 | Py_XDECREF(default_encoding); 858 | Py_XDECREF(ascii_chars_u); 859 | Py_XDECREF(ascii_chars_b); 860 | return -1; 861 | } 862 | #endif 863 | #if __PYX_DEFAULT_STRING_ENCODING_IS_DEFAULT && PY_MAJOR_VERSION >= 3 864 | #define __Pyx_PyUnicode_FromStringAndSize(c_str, size) PyUnicode_DecodeUTF8(c_str, size, NULL) 865 | #else 866 | #define __Pyx_PyUnicode_FromStringAndSize(c_str, size) PyUnicode_Decode(c_str, size, __PYX_DEFAULT_STRING_ENCODING, NULL) 867 | #if __PYX_DEFAULT_STRING_ENCODING_IS_DEFAULT 868 | static char* __PYX_DEFAULT_STRING_ENCODING; 869 | static int __Pyx_init_sys_getdefaultencoding_params(void) { 870 | PyObject* sys; 871 | PyObject* default_encoding = NULL; 872 | char* default_encoding_c; 873 | sys = PyImport_ImportModule("sys"); 874 | if (!sys) goto bad; 875 | default_encoding = PyObject_CallMethod(sys, (char*) (const char*) "getdefaultencoding", NULL); 876 | Py_DECREF(sys); 877 | if (!default_encoding) goto bad; 878 | default_encoding_c = PyBytes_AsString(default_encoding); 879 | if (!default_encoding_c) goto bad; 880 | __PYX_DEFAULT_STRING_ENCODING = (char*) malloc(strlen(default_encoding_c) + 1); 881 | if (!__PYX_DEFAULT_STRING_ENCODING) goto bad; 882 | strcpy(__PYX_DEFAULT_STRING_ENCODING, default_encoding_c); 883 | Py_DECREF(default_encoding); 884 | return 0; 885 | bad: 886 | Py_XDECREF(default_encoding); 887 | return -1; 888 | } 889 | #endif 890 | #endif 891 | 892 | 893 | /* Test for GCC > 2.95 */ 894 | #if defined(__GNUC__) && (__GNUC__ > 2 || (__GNUC__ == 2 && (__GNUC_MINOR__ > 95))) 895 | #define likely(x) __builtin_expect(!!(x), 1) 896 | #define unlikely(x) __builtin_expect(!!(x), 0) 897 | #else /* !__GNUC__ or GCC < 2.95 */ 898 | #define likely(x) (x) 899 | #define unlikely(x) (x) 900 | #endif /* __GNUC__ */ 901 | static CYTHON_INLINE void __Pyx_pretend_to_initialize(void* ptr) { (void)ptr; } 902 | 903 | static PyObject *__pyx_m = NULL; 904 | static PyObject *__pyx_d; 905 | static PyObject *__pyx_b; 906 | static PyObject *__pyx_cython_runtime = NULL; 907 | static PyObject *__pyx_empty_tuple; 908 | static PyObject *__pyx_empty_bytes; 909 | static PyObject *__pyx_empty_unicode; 910 | static int __pyx_lineno; 911 | static int __pyx_clineno = 0; 912 | static const char * __pyx_cfilenm= __FILE__; 913 | static const char *__pyx_filename; 914 | 915 | 916 | static const char *__pyx_f[] = { 917 | "upolygon/__init__.py", 918 | }; 919 | 920 | /*--- Type declarations ---*/ 921 | 922 | /* --- Runtime support code (head) --- */ 923 | /* Refnanny.proto */ 924 | #ifndef CYTHON_REFNANNY 925 | #define CYTHON_REFNANNY 0 926 | #endif 927 | #if CYTHON_REFNANNY 928 | typedef struct { 929 | void (*INCREF)(void*, PyObject*, int); 930 | void (*DECREF)(void*, PyObject*, int); 931 | void (*GOTREF)(void*, PyObject*, int); 932 | void (*GIVEREF)(void*, PyObject*, int); 933 | void* (*SetupContext)(const char*, int, const char*); 934 | void (*FinishContext)(void**); 935 | } __Pyx_RefNannyAPIStruct; 936 | static __Pyx_RefNannyAPIStruct *__Pyx_RefNanny = NULL; 937 | static __Pyx_RefNannyAPIStruct *__Pyx_RefNannyImportAPI(const char *modname); 938 | #define __Pyx_RefNannyDeclarations void *__pyx_refnanny = NULL; 939 | #ifdef WITH_THREAD 940 | #define __Pyx_RefNannySetupContext(name, acquire_gil)\ 941 | if (acquire_gil) {\ 942 | PyGILState_STATE __pyx_gilstate_save = PyGILState_Ensure();\ 943 | __pyx_refnanny = __Pyx_RefNanny->SetupContext((name), __LINE__, __FILE__);\ 944 | PyGILState_Release(__pyx_gilstate_save);\ 945 | } else {\ 946 | __pyx_refnanny = __Pyx_RefNanny->SetupContext((name), __LINE__, __FILE__);\ 947 | } 948 | #else 949 | #define __Pyx_RefNannySetupContext(name, acquire_gil)\ 950 | __pyx_refnanny = __Pyx_RefNanny->SetupContext((name), __LINE__, __FILE__) 951 | #endif 952 | #define __Pyx_RefNannyFinishContext()\ 953 | __Pyx_RefNanny->FinishContext(&__pyx_refnanny) 954 | #define __Pyx_INCREF(r) __Pyx_RefNanny->INCREF(__pyx_refnanny, (PyObject *)(r), __LINE__) 955 | #define __Pyx_DECREF(r) __Pyx_RefNanny->DECREF(__pyx_refnanny, (PyObject *)(r), __LINE__) 956 | #define __Pyx_GOTREF(r) __Pyx_RefNanny->GOTREF(__pyx_refnanny, (PyObject *)(r), __LINE__) 957 | #define __Pyx_GIVEREF(r) __Pyx_RefNanny->GIVEREF(__pyx_refnanny, (PyObject *)(r), __LINE__) 958 | #define __Pyx_XINCREF(r) do { if((r) != NULL) {__Pyx_INCREF(r); }} while(0) 959 | #define __Pyx_XDECREF(r) do { if((r) != NULL) {__Pyx_DECREF(r); }} while(0) 960 | #define __Pyx_XGOTREF(r) do { if((r) != NULL) {__Pyx_GOTREF(r); }} while(0) 961 | #define __Pyx_XGIVEREF(r) do { if((r) != NULL) {__Pyx_GIVEREF(r);}} while(0) 962 | #else 963 | #define __Pyx_RefNannyDeclarations 964 | #define __Pyx_RefNannySetupContext(name, acquire_gil) 965 | #define __Pyx_RefNannyFinishContext() 966 | #define __Pyx_INCREF(r) Py_INCREF(r) 967 | #define __Pyx_DECREF(r) Py_DECREF(r) 968 | #define __Pyx_GOTREF(r) 969 | #define __Pyx_GIVEREF(r) 970 | #define __Pyx_XINCREF(r) Py_XINCREF(r) 971 | #define __Pyx_XDECREF(r) Py_XDECREF(r) 972 | #define __Pyx_XGOTREF(r) 973 | #define __Pyx_XGIVEREF(r) 974 | #endif 975 | #define __Pyx_XDECREF_SET(r, v) do {\ 976 | PyObject *tmp = (PyObject *) r;\ 977 | r = v; __Pyx_XDECREF(tmp);\ 978 | } while (0) 979 | #define __Pyx_DECREF_SET(r, v) do {\ 980 | PyObject *tmp = (PyObject *) r;\ 981 | r = v; __Pyx_DECREF(tmp);\ 982 | } while (0) 983 | #define __Pyx_CLEAR(r) do { PyObject* tmp = ((PyObject*)(r)); r = NULL; __Pyx_DECREF(tmp);} while(0) 984 | #define __Pyx_XCLEAR(r) do { if((r) != NULL) {PyObject* tmp = ((PyObject*)(r)); r = NULL; __Pyx_DECREF(tmp);}} while(0) 985 | 986 | /* PyObjectGetAttrStr.proto */ 987 | #if CYTHON_USE_TYPE_SLOTS 988 | static CYTHON_INLINE PyObject* __Pyx_PyObject_GetAttrStr(PyObject* obj, PyObject* attr_name); 989 | #else 990 | #define __Pyx_PyObject_GetAttrStr(o,n) PyObject_GetAttr(o,n) 991 | #endif 992 | 993 | /* SetPackagePathFromImportLib.proto */ 994 | #if PY_MAJOR_VERSION >= 3 && !CYTHON_PEP489_MULTI_PHASE_INIT 995 | static int __Pyx_SetPackagePathFromImportLib(const char* parent_package_name, PyObject *module_name); 996 | #else 997 | #define __Pyx_SetPackagePathFromImportLib(a, b) 0 998 | #endif 999 | 1000 | /* Import.proto */ 1001 | static PyObject *__Pyx_Import(PyObject *name, PyObject *from_list, int level); 1002 | 1003 | /* ImportFrom.proto */ 1004 | static PyObject* __Pyx_ImportFrom(PyObject* module, PyObject* name); 1005 | 1006 | /* PyDictVersioning.proto */ 1007 | #if CYTHON_USE_DICT_VERSIONS && CYTHON_USE_TYPE_SLOTS 1008 | #define __PYX_DICT_VERSION_INIT ((PY_UINT64_T) -1) 1009 | #define __PYX_GET_DICT_VERSION(dict) (((PyDictObject*)(dict))->ma_version_tag) 1010 | #define __PYX_UPDATE_DICT_CACHE(dict, value, cache_var, version_var)\ 1011 | (version_var) = __PYX_GET_DICT_VERSION(dict);\ 1012 | (cache_var) = (value); 1013 | #define __PYX_PY_DICT_LOOKUP_IF_MODIFIED(VAR, DICT, LOOKUP) {\ 1014 | static PY_UINT64_T __pyx_dict_version = 0;\ 1015 | static PyObject *__pyx_dict_cached_value = NULL;\ 1016 | if (likely(__PYX_GET_DICT_VERSION(DICT) == __pyx_dict_version)) {\ 1017 | (VAR) = __pyx_dict_cached_value;\ 1018 | } else {\ 1019 | (VAR) = __pyx_dict_cached_value = (LOOKUP);\ 1020 | __pyx_dict_version = __PYX_GET_DICT_VERSION(DICT);\ 1021 | }\ 1022 | } 1023 | static CYTHON_INLINE PY_UINT64_T __Pyx_get_tp_dict_version(PyObject *obj); 1024 | static CYTHON_INLINE PY_UINT64_T __Pyx_get_object_dict_version(PyObject *obj); 1025 | static CYTHON_INLINE int __Pyx_object_dict_version_matches(PyObject* obj, PY_UINT64_T tp_dict_version, PY_UINT64_T obj_dict_version); 1026 | #else 1027 | #define __PYX_GET_DICT_VERSION(dict) (0) 1028 | #define __PYX_UPDATE_DICT_CACHE(dict, value, cache_var, version_var) 1029 | #define __PYX_PY_DICT_LOOKUP_IF_MODIFIED(VAR, DICT, LOOKUP) (VAR) = (LOOKUP); 1030 | #endif 1031 | 1032 | /* PyThreadStateGet.proto */ 1033 | #if CYTHON_FAST_THREAD_STATE 1034 | #define __Pyx_PyThreadState_declare PyThreadState *__pyx_tstate; 1035 | #define __Pyx_PyThreadState_assign __pyx_tstate = __Pyx_PyThreadState_Current; 1036 | #define __Pyx_PyErr_Occurred() __pyx_tstate->curexc_type 1037 | #else 1038 | #define __Pyx_PyThreadState_declare 1039 | #define __Pyx_PyThreadState_assign 1040 | #define __Pyx_PyErr_Occurred() PyErr_Occurred() 1041 | #endif 1042 | 1043 | /* PyErrFetchRestore.proto */ 1044 | #if CYTHON_FAST_THREAD_STATE 1045 | #define __Pyx_PyErr_Clear() __Pyx_ErrRestore(NULL, NULL, NULL) 1046 | #define __Pyx_ErrRestoreWithState(type, value, tb) __Pyx_ErrRestoreInState(PyThreadState_GET(), type, value, tb) 1047 | #define __Pyx_ErrFetchWithState(type, value, tb) __Pyx_ErrFetchInState(PyThreadState_GET(), type, value, tb) 1048 | #define __Pyx_ErrRestore(type, value, tb) __Pyx_ErrRestoreInState(__pyx_tstate, type, value, tb) 1049 | #define __Pyx_ErrFetch(type, value, tb) __Pyx_ErrFetchInState(__pyx_tstate, type, value, tb) 1050 | static CYTHON_INLINE void __Pyx_ErrRestoreInState(PyThreadState *tstate, PyObject *type, PyObject *value, PyObject *tb); 1051 | static CYTHON_INLINE void __Pyx_ErrFetchInState(PyThreadState *tstate, PyObject **type, PyObject **value, PyObject **tb); 1052 | #if CYTHON_COMPILING_IN_CPYTHON 1053 | #define __Pyx_PyErr_SetNone(exc) (Py_INCREF(exc), __Pyx_ErrRestore((exc), NULL, NULL)) 1054 | #else 1055 | #define __Pyx_PyErr_SetNone(exc) PyErr_SetNone(exc) 1056 | #endif 1057 | #else 1058 | #define __Pyx_PyErr_Clear() PyErr_Clear() 1059 | #define __Pyx_PyErr_SetNone(exc) PyErr_SetNone(exc) 1060 | #define __Pyx_ErrRestoreWithState(type, value, tb) PyErr_Restore(type, value, tb) 1061 | #define __Pyx_ErrFetchWithState(type, value, tb) PyErr_Fetch(type, value, tb) 1062 | #define __Pyx_ErrRestoreInState(tstate, type, value, tb) PyErr_Restore(type, value, tb) 1063 | #define __Pyx_ErrFetchInState(tstate, type, value, tb) PyErr_Fetch(type, value, tb) 1064 | #define __Pyx_ErrRestore(type, value, tb) PyErr_Restore(type, value, tb) 1065 | #define __Pyx_ErrFetch(type, value, tb) PyErr_Fetch(type, value, tb) 1066 | #endif 1067 | 1068 | /* CLineInTraceback.proto */ 1069 | #ifdef CYTHON_CLINE_IN_TRACEBACK 1070 | #define __Pyx_CLineForTraceback(tstate, c_line) (((CYTHON_CLINE_IN_TRACEBACK)) ? c_line : 0) 1071 | #else 1072 | static int __Pyx_CLineForTraceback(PyThreadState *tstate, int c_line); 1073 | #endif 1074 | 1075 | /* CodeObjectCache.proto */ 1076 | typedef struct { 1077 | PyCodeObject* code_object; 1078 | int code_line; 1079 | } __Pyx_CodeObjectCacheEntry; 1080 | struct __Pyx_CodeObjectCache { 1081 | int count; 1082 | int max_count; 1083 | __Pyx_CodeObjectCacheEntry* entries; 1084 | }; 1085 | static struct __Pyx_CodeObjectCache __pyx_code_cache = {0,0,NULL}; 1086 | static int __pyx_bisect_code_objects(__Pyx_CodeObjectCacheEntry* entries, int count, int code_line); 1087 | static PyCodeObject *__pyx_find_code_object(int code_line); 1088 | static void __pyx_insert_code_object(int code_line, PyCodeObject* code_object); 1089 | 1090 | /* AddTraceback.proto */ 1091 | static void __Pyx_AddTraceback(const char *funcname, int c_line, 1092 | int py_line, const char *filename); 1093 | 1094 | /* GCCDiagnostics.proto */ 1095 | #if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) 1096 | #define __Pyx_HAS_GCC_DIAGNOSTIC 1097 | #endif 1098 | 1099 | /* CIntToPy.proto */ 1100 | static CYTHON_INLINE PyObject* __Pyx_PyInt_From_long(long value); 1101 | 1102 | /* CIntFromPy.proto */ 1103 | static CYTHON_INLINE long __Pyx_PyInt_As_long(PyObject *); 1104 | 1105 | /* CIntFromPy.proto */ 1106 | static CYTHON_INLINE int __Pyx_PyInt_As_int(PyObject *); 1107 | 1108 | /* FastTypeChecks.proto */ 1109 | #if CYTHON_COMPILING_IN_CPYTHON 1110 | #define __Pyx_TypeCheck(obj, type) __Pyx_IsSubtype(Py_TYPE(obj), (PyTypeObject *)type) 1111 | static CYTHON_INLINE int __Pyx_IsSubtype(PyTypeObject *a, PyTypeObject *b); 1112 | static CYTHON_INLINE int __Pyx_PyErr_GivenExceptionMatches(PyObject *err, PyObject *type); 1113 | static CYTHON_INLINE int __Pyx_PyErr_GivenExceptionMatches2(PyObject *err, PyObject *type1, PyObject *type2); 1114 | #else 1115 | #define __Pyx_TypeCheck(obj, type) PyObject_TypeCheck(obj, (PyTypeObject *)type) 1116 | #define __Pyx_PyErr_GivenExceptionMatches(err, type) PyErr_GivenExceptionMatches(err, type) 1117 | #define __Pyx_PyErr_GivenExceptionMatches2(err, type1, type2) (PyErr_GivenExceptionMatches(err, type1) || PyErr_GivenExceptionMatches(err, type2)) 1118 | #endif 1119 | #define __Pyx_PyException_Check(obj) __Pyx_TypeCheck(obj, PyExc_Exception) 1120 | 1121 | /* CheckBinaryVersion.proto */ 1122 | static int __Pyx_check_binary_version(void); 1123 | 1124 | /* InitStrings.proto */ 1125 | static int __Pyx_InitStrings(__Pyx_StringTabEntry *t); 1126 | 1127 | 1128 | /* Module declarations from 'upolygon.__init__' */ 1129 | #define __Pyx_MODULE_NAME "upolygon.__init__" 1130 | extern int __pyx_module_is_main_upolygon____init__; 1131 | int __pyx_module_is_main_upolygon____init__ = 0; 1132 | 1133 | /* Implementation of 'upolygon.__init__' */ 1134 | static const char __pyx_k_main[] = "__main__"; 1135 | static const char __pyx_k_name[] = "__name__"; 1136 | static const char __pyx_k_test[] = "__test__"; 1137 | static const char __pyx_k_import[] = "__import__"; 1138 | static const char __pyx_k_upolygon[] = "upolygon"; 1139 | static const char __pyx_k_rle_decode[] = "rle_decode"; 1140 | static const char __pyx_k_rle_encode[] = "rle_encode"; 1141 | static const char __pyx_k_draw_polygon[] = "draw_polygon"; 1142 | static const char __pyx_k_find_contours[] = "find_contours"; 1143 | static const char __pyx_k_simplify_polygon[] = "simplify_polygon"; 1144 | static const char __pyx_k_cline_in_traceback[] = "cline_in_traceback"; 1145 | static const char __pyx_k_run_length_encoding[] = "run_length_encoding"; 1146 | static PyObject *__pyx_n_s_cline_in_traceback; 1147 | static PyObject *__pyx_n_s_draw_polygon; 1148 | static PyObject *__pyx_n_s_find_contours; 1149 | static PyObject *__pyx_n_s_import; 1150 | static PyObject *__pyx_n_s_main; 1151 | static PyObject *__pyx_n_s_name; 1152 | static PyObject *__pyx_n_s_rle_decode; 1153 | static PyObject *__pyx_n_s_rle_encode; 1154 | static PyObject *__pyx_n_s_run_length_encoding; 1155 | static PyObject *__pyx_n_s_simplify_polygon; 1156 | static PyObject *__pyx_n_s_test; 1157 | static PyObject *__pyx_n_u_upolygon; 1158 | /* Late includes */ 1159 | 1160 | static PyMethodDef __pyx_methods[] = { 1161 | {0, 0, 0, 0} 1162 | }; 1163 | 1164 | #if PY_MAJOR_VERSION >= 3 1165 | #if CYTHON_PEP489_MULTI_PHASE_INIT 1166 | static PyObject* __pyx_pymod_create(PyObject *spec, PyModuleDef *def); /*proto*/ 1167 | static int __pyx_pymod_exec_upolygon(PyObject* module); /*proto*/ 1168 | static PyModuleDef_Slot __pyx_moduledef_slots[] = { 1169 | {Py_mod_create, (void*)__pyx_pymod_create}, 1170 | {Py_mod_exec, (void*)__pyx_pymod_exec_upolygon}, 1171 | {0, NULL} 1172 | }; 1173 | #endif 1174 | 1175 | static struct PyModuleDef __pyx_moduledef = { 1176 | PyModuleDef_HEAD_INIT, 1177 | "upolygon", 1178 | 0, /* m_doc */ 1179 | #if CYTHON_PEP489_MULTI_PHASE_INIT 1180 | 0, /* m_size */ 1181 | #else 1182 | -1, /* m_size */ 1183 | #endif 1184 | __pyx_methods /* m_methods */, 1185 | #if CYTHON_PEP489_MULTI_PHASE_INIT 1186 | __pyx_moduledef_slots, /* m_slots */ 1187 | #else 1188 | NULL, /* m_reload */ 1189 | #endif 1190 | NULL, /* m_traverse */ 1191 | NULL, /* m_clear */ 1192 | NULL /* m_free */ 1193 | }; 1194 | #endif 1195 | #ifndef CYTHON_SMALL_CODE 1196 | #if defined(__clang__) 1197 | #define CYTHON_SMALL_CODE 1198 | #elif defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3)) 1199 | #define CYTHON_SMALL_CODE __attribute__((cold)) 1200 | #else 1201 | #define CYTHON_SMALL_CODE 1202 | #endif 1203 | #endif 1204 | 1205 | static __Pyx_StringTabEntry __pyx_string_tab[] = { 1206 | {&__pyx_n_s_cline_in_traceback, __pyx_k_cline_in_traceback, sizeof(__pyx_k_cline_in_traceback), 0, 0, 1, 1}, 1207 | {&__pyx_n_s_draw_polygon, __pyx_k_draw_polygon, sizeof(__pyx_k_draw_polygon), 0, 0, 1, 1}, 1208 | {&__pyx_n_s_find_contours, __pyx_k_find_contours, sizeof(__pyx_k_find_contours), 0, 0, 1, 1}, 1209 | {&__pyx_n_s_import, __pyx_k_import, sizeof(__pyx_k_import), 0, 0, 1, 1}, 1210 | {&__pyx_n_s_main, __pyx_k_main, sizeof(__pyx_k_main), 0, 0, 1, 1}, 1211 | {&__pyx_n_s_name, __pyx_k_name, sizeof(__pyx_k_name), 0, 0, 1, 1}, 1212 | {&__pyx_n_s_rle_decode, __pyx_k_rle_decode, sizeof(__pyx_k_rle_decode), 0, 0, 1, 1}, 1213 | {&__pyx_n_s_rle_encode, __pyx_k_rle_encode, sizeof(__pyx_k_rle_encode), 0, 0, 1, 1}, 1214 | {&__pyx_n_s_run_length_encoding, __pyx_k_run_length_encoding, sizeof(__pyx_k_run_length_encoding), 0, 0, 1, 1}, 1215 | {&__pyx_n_s_simplify_polygon, __pyx_k_simplify_polygon, sizeof(__pyx_k_simplify_polygon), 0, 0, 1, 1}, 1216 | {&__pyx_n_s_test, __pyx_k_test, sizeof(__pyx_k_test), 0, 0, 1, 1}, 1217 | {&__pyx_n_u_upolygon, __pyx_k_upolygon, sizeof(__pyx_k_upolygon), 0, 1, 0, 1}, 1218 | {0, 0, 0, 0, 0, 0, 0} 1219 | }; 1220 | static CYTHON_SMALL_CODE int __Pyx_InitCachedBuiltins(void) { 1221 | return 0; 1222 | } 1223 | 1224 | static CYTHON_SMALL_CODE int __Pyx_InitCachedConstants(void) { 1225 | __Pyx_RefNannyDeclarations 1226 | __Pyx_RefNannySetupContext("__Pyx_InitCachedConstants", 0); 1227 | __Pyx_RefNannyFinishContext(); 1228 | return 0; 1229 | } 1230 | 1231 | static CYTHON_SMALL_CODE int __Pyx_InitGlobals(void) { 1232 | if (__Pyx_InitStrings(__pyx_string_tab) < 0) __PYX_ERR(0, 1, __pyx_L1_error); 1233 | return 0; 1234 | __pyx_L1_error:; 1235 | return -1; 1236 | } 1237 | 1238 | static CYTHON_SMALL_CODE int __Pyx_modinit_global_init_code(void); /*proto*/ 1239 | static CYTHON_SMALL_CODE int __Pyx_modinit_variable_export_code(void); /*proto*/ 1240 | static CYTHON_SMALL_CODE int __Pyx_modinit_function_export_code(void); /*proto*/ 1241 | static CYTHON_SMALL_CODE int __Pyx_modinit_type_init_code(void); /*proto*/ 1242 | static CYTHON_SMALL_CODE int __Pyx_modinit_type_import_code(void); /*proto*/ 1243 | static CYTHON_SMALL_CODE int __Pyx_modinit_variable_import_code(void); /*proto*/ 1244 | static CYTHON_SMALL_CODE int __Pyx_modinit_function_import_code(void); /*proto*/ 1245 | 1246 | static int __Pyx_modinit_global_init_code(void) { 1247 | __Pyx_RefNannyDeclarations 1248 | __Pyx_RefNannySetupContext("__Pyx_modinit_global_init_code", 0); 1249 | /*--- Global init code ---*/ 1250 | __Pyx_RefNannyFinishContext(); 1251 | return 0; 1252 | } 1253 | 1254 | static int __Pyx_modinit_variable_export_code(void) { 1255 | __Pyx_RefNannyDeclarations 1256 | __Pyx_RefNannySetupContext("__Pyx_modinit_variable_export_code", 0); 1257 | /*--- Variable export code ---*/ 1258 | __Pyx_RefNannyFinishContext(); 1259 | return 0; 1260 | } 1261 | 1262 | static int __Pyx_modinit_function_export_code(void) { 1263 | __Pyx_RefNannyDeclarations 1264 | __Pyx_RefNannySetupContext("__Pyx_modinit_function_export_code", 0); 1265 | /*--- Function export code ---*/ 1266 | __Pyx_RefNannyFinishContext(); 1267 | return 0; 1268 | } 1269 | 1270 | static int __Pyx_modinit_type_init_code(void) { 1271 | __Pyx_RefNannyDeclarations 1272 | __Pyx_RefNannySetupContext("__Pyx_modinit_type_init_code", 0); 1273 | /*--- Type init code ---*/ 1274 | __Pyx_RefNannyFinishContext(); 1275 | return 0; 1276 | } 1277 | 1278 | static int __Pyx_modinit_type_import_code(void) { 1279 | __Pyx_RefNannyDeclarations 1280 | __Pyx_RefNannySetupContext("__Pyx_modinit_type_import_code", 0); 1281 | /*--- Type import code ---*/ 1282 | __Pyx_RefNannyFinishContext(); 1283 | return 0; 1284 | } 1285 | 1286 | static int __Pyx_modinit_variable_import_code(void) { 1287 | __Pyx_RefNannyDeclarations 1288 | __Pyx_RefNannySetupContext("__Pyx_modinit_variable_import_code", 0); 1289 | /*--- Variable import code ---*/ 1290 | __Pyx_RefNannyFinishContext(); 1291 | return 0; 1292 | } 1293 | 1294 | static int __Pyx_modinit_function_import_code(void) { 1295 | __Pyx_RefNannyDeclarations 1296 | __Pyx_RefNannySetupContext("__Pyx_modinit_function_import_code", 0); 1297 | /*--- Function import code ---*/ 1298 | __Pyx_RefNannyFinishContext(); 1299 | return 0; 1300 | } 1301 | 1302 | 1303 | #ifndef CYTHON_NO_PYINIT_EXPORT 1304 | #define __Pyx_PyMODINIT_FUNC PyMODINIT_FUNC 1305 | #elif PY_MAJOR_VERSION < 3 1306 | #ifdef __cplusplus 1307 | #define __Pyx_PyMODINIT_FUNC extern "C" void 1308 | #else 1309 | #define __Pyx_PyMODINIT_FUNC void 1310 | #endif 1311 | #else 1312 | #ifdef __cplusplus 1313 | #define __Pyx_PyMODINIT_FUNC extern "C" PyObject * 1314 | #else 1315 | #define __Pyx_PyMODINIT_FUNC PyObject * 1316 | #endif 1317 | #endif 1318 | 1319 | 1320 | #if PY_MAJOR_VERSION < 3 1321 | __Pyx_PyMODINIT_FUNC initupolygon(void) CYTHON_SMALL_CODE; /*proto*/ 1322 | __Pyx_PyMODINIT_FUNC initupolygon(void) 1323 | #else 1324 | __Pyx_PyMODINIT_FUNC PyInit_upolygon(void) CYTHON_SMALL_CODE; /*proto*/ 1325 | __Pyx_PyMODINIT_FUNC PyInit_upolygon(void) 1326 | #if CYTHON_PEP489_MULTI_PHASE_INIT 1327 | { 1328 | return PyModuleDef_Init(&__pyx_moduledef); 1329 | } 1330 | static CYTHON_SMALL_CODE int __Pyx_check_single_interpreter(void) { 1331 | #if PY_VERSION_HEX >= 0x030700A1 1332 | static PY_INT64_T main_interpreter_id = -1; 1333 | PY_INT64_T current_id = PyInterpreterState_GetID(PyThreadState_Get()->interp); 1334 | if (main_interpreter_id == -1) { 1335 | main_interpreter_id = current_id; 1336 | return (unlikely(current_id == -1)) ? -1 : 0; 1337 | } else if (unlikely(main_interpreter_id != current_id)) 1338 | #else 1339 | static PyInterpreterState *main_interpreter = NULL; 1340 | PyInterpreterState *current_interpreter = PyThreadState_Get()->interp; 1341 | if (!main_interpreter) { 1342 | main_interpreter = current_interpreter; 1343 | } else if (unlikely(main_interpreter != current_interpreter)) 1344 | #endif 1345 | { 1346 | PyErr_SetString( 1347 | PyExc_ImportError, 1348 | "Interpreter change detected - this module can only be loaded into one interpreter per process."); 1349 | return -1; 1350 | } 1351 | return 0; 1352 | } 1353 | static CYTHON_SMALL_CODE int __Pyx_copy_spec_to_module(PyObject *spec, PyObject *moddict, const char* from_name, const char* to_name, int allow_none) { 1354 | PyObject *value = PyObject_GetAttrString(spec, from_name); 1355 | int result = 0; 1356 | if (likely(value)) { 1357 | if (allow_none || value != Py_None) { 1358 | result = PyDict_SetItemString(moddict, to_name, value); 1359 | } 1360 | Py_DECREF(value); 1361 | } else if (PyErr_ExceptionMatches(PyExc_AttributeError)) { 1362 | PyErr_Clear(); 1363 | } else { 1364 | result = -1; 1365 | } 1366 | return result; 1367 | } 1368 | static CYTHON_SMALL_CODE PyObject* __pyx_pymod_create(PyObject *spec, CYTHON_UNUSED PyModuleDef *def) { 1369 | PyObject *module = NULL, *moddict, *modname; 1370 | if (__Pyx_check_single_interpreter()) 1371 | return NULL; 1372 | if (__pyx_m) 1373 | return __Pyx_NewRef(__pyx_m); 1374 | modname = PyObject_GetAttrString(spec, "name"); 1375 | if (unlikely(!modname)) goto bad; 1376 | module = PyModule_NewObject(modname); 1377 | Py_DECREF(modname); 1378 | if (unlikely(!module)) goto bad; 1379 | moddict = PyModule_GetDict(module); 1380 | if (unlikely(!moddict)) goto bad; 1381 | if (unlikely(__Pyx_copy_spec_to_module(spec, moddict, "loader", "__loader__", 1) < 0)) goto bad; 1382 | if (unlikely(__Pyx_copy_spec_to_module(spec, moddict, "origin", "__file__", 1) < 0)) goto bad; 1383 | if (unlikely(__Pyx_copy_spec_to_module(spec, moddict, "parent", "__package__", 1) < 0)) goto bad; 1384 | if (unlikely(__Pyx_copy_spec_to_module(spec, moddict, "submodule_search_locations", "__path__", 0) < 0)) goto bad; 1385 | return module; 1386 | bad: 1387 | Py_XDECREF(module); 1388 | return NULL; 1389 | } 1390 | 1391 | 1392 | static CYTHON_SMALL_CODE int __pyx_pymod_exec_upolygon(PyObject *__pyx_pyinit_module) 1393 | #endif 1394 | #endif 1395 | { 1396 | PyObject *__pyx_t_1 = NULL; 1397 | PyObject *__pyx_t_2 = NULL; 1398 | int __pyx_lineno = 0; 1399 | const char *__pyx_filename = NULL; 1400 | int __pyx_clineno = 0; 1401 | __Pyx_RefNannyDeclarations 1402 | #if CYTHON_PEP489_MULTI_PHASE_INIT 1403 | if (__pyx_m) { 1404 | if (__pyx_m == __pyx_pyinit_module) return 0; 1405 | PyErr_SetString(PyExc_RuntimeError, "Module 'upolygon' has already been imported. Re-initialisation is not supported."); 1406 | return -1; 1407 | } 1408 | #elif PY_MAJOR_VERSION >= 3 1409 | if (__pyx_m) return __Pyx_NewRef(__pyx_m); 1410 | #endif 1411 | #if CYTHON_REFNANNY 1412 | __Pyx_RefNanny = __Pyx_RefNannyImportAPI("refnanny"); 1413 | if (!__Pyx_RefNanny) { 1414 | PyErr_Clear(); 1415 | __Pyx_RefNanny = __Pyx_RefNannyImportAPI("Cython.Runtime.refnanny"); 1416 | if (!__Pyx_RefNanny) 1417 | Py_FatalError("failed to import 'refnanny' module"); 1418 | } 1419 | #endif 1420 | __Pyx_RefNannySetupContext("__Pyx_PyMODINIT_FUNC PyInit_upolygon(void)", 0); 1421 | if (__Pyx_check_binary_version() < 0) __PYX_ERR(0, 1, __pyx_L1_error) 1422 | #ifdef __Pxy_PyFrame_Initialize_Offsets 1423 | __Pxy_PyFrame_Initialize_Offsets(); 1424 | #endif 1425 | __pyx_empty_tuple = PyTuple_New(0); if (unlikely(!__pyx_empty_tuple)) __PYX_ERR(0, 1, __pyx_L1_error) 1426 | __pyx_empty_bytes = PyBytes_FromStringAndSize("", 0); if (unlikely(!__pyx_empty_bytes)) __PYX_ERR(0, 1, __pyx_L1_error) 1427 | __pyx_empty_unicode = PyUnicode_FromStringAndSize("", 0); if (unlikely(!__pyx_empty_unicode)) __PYX_ERR(0, 1, __pyx_L1_error) 1428 | #ifdef __Pyx_CyFunction_USED 1429 | if (__pyx_CyFunction_init() < 0) __PYX_ERR(0, 1, __pyx_L1_error) 1430 | #endif 1431 | #ifdef __Pyx_FusedFunction_USED 1432 | if (__pyx_FusedFunction_init() < 0) __PYX_ERR(0, 1, __pyx_L1_error) 1433 | #endif 1434 | #ifdef __Pyx_Coroutine_USED 1435 | if (__pyx_Coroutine_init() < 0) __PYX_ERR(0, 1, __pyx_L1_error) 1436 | #endif 1437 | #ifdef __Pyx_Generator_USED 1438 | if (__pyx_Generator_init() < 0) __PYX_ERR(0, 1, __pyx_L1_error) 1439 | #endif 1440 | #ifdef __Pyx_AsyncGen_USED 1441 | if (__pyx_AsyncGen_init() < 0) __PYX_ERR(0, 1, __pyx_L1_error) 1442 | #endif 1443 | #ifdef __Pyx_StopAsyncIteration_USED 1444 | if (__pyx_StopAsyncIteration_init() < 0) __PYX_ERR(0, 1, __pyx_L1_error) 1445 | #endif 1446 | /*--- Library function declarations ---*/ 1447 | /*--- Threads initialization code ---*/ 1448 | #if defined(WITH_THREAD) && PY_VERSION_HEX < 0x030700F0 && defined(__PYX_FORCE_INIT_THREADS) && __PYX_FORCE_INIT_THREADS 1449 | PyEval_InitThreads(); 1450 | #endif 1451 | /*--- Module creation code ---*/ 1452 | #if CYTHON_PEP489_MULTI_PHASE_INIT 1453 | __pyx_m = __pyx_pyinit_module; 1454 | Py_INCREF(__pyx_m); 1455 | #else 1456 | #if PY_MAJOR_VERSION < 3 1457 | __pyx_m = Py_InitModule4("upolygon", __pyx_methods, 0, 0, PYTHON_API_VERSION); Py_XINCREF(__pyx_m); 1458 | #else 1459 | __pyx_m = PyModule_Create(&__pyx_moduledef); 1460 | #endif 1461 | if (unlikely(!__pyx_m)) __PYX_ERR(0, 1, __pyx_L1_error) 1462 | #endif 1463 | __pyx_d = PyModule_GetDict(__pyx_m); if (unlikely(!__pyx_d)) __PYX_ERR(0, 1, __pyx_L1_error) 1464 | Py_INCREF(__pyx_d); 1465 | __pyx_b = PyImport_AddModule(__Pyx_BUILTIN_MODULE_NAME); if (unlikely(!__pyx_b)) __PYX_ERR(0, 1, __pyx_L1_error) 1466 | Py_INCREF(__pyx_b); 1467 | __pyx_cython_runtime = PyImport_AddModule((char *) "cython_runtime"); if (unlikely(!__pyx_cython_runtime)) __PYX_ERR(0, 1, __pyx_L1_error) 1468 | Py_INCREF(__pyx_cython_runtime); 1469 | if (PyObject_SetAttrString(__pyx_m, "__builtins__", __pyx_b) < 0) __PYX_ERR(0, 1, __pyx_L1_error); 1470 | /*--- Initialize various global constants etc. ---*/ 1471 | if (__Pyx_InitGlobals() < 0) __PYX_ERR(0, 1, __pyx_L1_error) 1472 | #if PY_MAJOR_VERSION < 3 && (__PYX_DEFAULT_STRING_ENCODING_IS_ASCII || __PYX_DEFAULT_STRING_ENCODING_IS_DEFAULT) 1473 | if (__Pyx_init_sys_getdefaultencoding_params() < 0) __PYX_ERR(0, 1, __pyx_L1_error) 1474 | #endif 1475 | if (__pyx_module_is_main_upolygon____init__) { 1476 | if (PyObject_SetAttr(__pyx_m, __pyx_n_s_name, __pyx_n_s_main) < 0) __PYX_ERR(0, 1, __pyx_L1_error) 1477 | } 1478 | if (!CYTHON_PEP489_MULTI_PHASE_INIT) { 1479 | if (unlikely(__Pyx_SetPackagePathFromImportLib(NULL, __pyx_n_u_upolygon) < 0)) __PYX_ERR(0, 1, __pyx_L1_error) 1480 | } 1481 | #if PY_MAJOR_VERSION >= 3 1482 | { 1483 | PyObject *modules = PyImport_GetModuleDict(); if (unlikely(!modules)) __PYX_ERR(0, 1, __pyx_L1_error) 1484 | if (!PyDict_GetItemString(modules, "upolygon")) { 1485 | if (unlikely(PyDict_SetItemString(modules, "upolygon", __pyx_m) < 0)) __PYX_ERR(0, 1, __pyx_L1_error) 1486 | } 1487 | } 1488 | #endif 1489 | /*--- Builtin init code ---*/ 1490 | if (__Pyx_InitCachedBuiltins() < 0) __PYX_ERR(0, 1, __pyx_L1_error) 1491 | /*--- Constants init code ---*/ 1492 | if (__Pyx_InitCachedConstants() < 0) __PYX_ERR(0, 1, __pyx_L1_error) 1493 | /*--- Global type/function init code ---*/ 1494 | (void)__Pyx_modinit_global_init_code(); 1495 | (void)__Pyx_modinit_variable_export_code(); 1496 | (void)__Pyx_modinit_function_export_code(); 1497 | (void)__Pyx_modinit_type_init_code(); 1498 | (void)__Pyx_modinit_type_import_code(); 1499 | (void)__Pyx_modinit_variable_import_code(); 1500 | (void)__Pyx_modinit_function_import_code(); 1501 | /*--- Execution code ---*/ 1502 | #if defined(__Pyx_Generator_USED) || defined(__Pyx_Coroutine_USED) 1503 | if (__Pyx_patch_abc() < 0) __PYX_ERR(0, 1, __pyx_L1_error) 1504 | #endif 1505 | 1506 | /* "upolygon/__init__.py":1 1507 | * from draw_polygon import draw_polygon # noqa # <<<<<<<<<<<<<< 1508 | * from find_contours import find_contours # noqa 1509 | * from simplify_polygon import simplify_polygon # noqa 1510 | */ 1511 | __pyx_t_1 = PyList_New(1); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 1, __pyx_L1_error) 1512 | __Pyx_GOTREF(__pyx_t_1); 1513 | __Pyx_INCREF(__pyx_n_s_draw_polygon); 1514 | __Pyx_GIVEREF(__pyx_n_s_draw_polygon); 1515 | PyList_SET_ITEM(__pyx_t_1, 0, __pyx_n_s_draw_polygon); 1516 | __pyx_t_2 = __Pyx_Import(__pyx_n_s_draw_polygon, __pyx_t_1, -1); if (unlikely(!__pyx_t_2)) __PYX_ERR(0, 1, __pyx_L1_error) 1517 | __Pyx_GOTREF(__pyx_t_2); 1518 | __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; 1519 | __pyx_t_1 = __Pyx_ImportFrom(__pyx_t_2, __pyx_n_s_draw_polygon); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 1, __pyx_L1_error) 1520 | __Pyx_GOTREF(__pyx_t_1); 1521 | if (PyDict_SetItem(__pyx_d, __pyx_n_s_draw_polygon, __pyx_t_1) < 0) __PYX_ERR(0, 1, __pyx_L1_error) 1522 | __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; 1523 | __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; 1524 | 1525 | /* "upolygon/__init__.py":2 1526 | * from draw_polygon import draw_polygon # noqa 1527 | * from find_contours import find_contours # noqa # <<<<<<<<<<<<<< 1528 | * from simplify_polygon import simplify_polygon # noqa 1529 | * from run_length_encoding import rle_decode, rle_encode # noqa 1530 | */ 1531 | __pyx_t_2 = PyList_New(1); if (unlikely(!__pyx_t_2)) __PYX_ERR(0, 2, __pyx_L1_error) 1532 | __Pyx_GOTREF(__pyx_t_2); 1533 | __Pyx_INCREF(__pyx_n_s_find_contours); 1534 | __Pyx_GIVEREF(__pyx_n_s_find_contours); 1535 | PyList_SET_ITEM(__pyx_t_2, 0, __pyx_n_s_find_contours); 1536 | __pyx_t_1 = __Pyx_Import(__pyx_n_s_find_contours, __pyx_t_2, -1); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 2, __pyx_L1_error) 1537 | __Pyx_GOTREF(__pyx_t_1); 1538 | __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; 1539 | __pyx_t_2 = __Pyx_ImportFrom(__pyx_t_1, __pyx_n_s_find_contours); if (unlikely(!__pyx_t_2)) __PYX_ERR(0, 2, __pyx_L1_error) 1540 | __Pyx_GOTREF(__pyx_t_2); 1541 | if (PyDict_SetItem(__pyx_d, __pyx_n_s_find_contours, __pyx_t_2) < 0) __PYX_ERR(0, 2, __pyx_L1_error) 1542 | __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; 1543 | __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; 1544 | 1545 | /* "upolygon/__init__.py":3 1546 | * from draw_polygon import draw_polygon # noqa 1547 | * from find_contours import find_contours # noqa 1548 | * from simplify_polygon import simplify_polygon # noqa # <<<<<<<<<<<<<< 1549 | * from run_length_encoding import rle_decode, rle_encode # noqa 1550 | */ 1551 | __pyx_t_1 = PyList_New(1); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 3, __pyx_L1_error) 1552 | __Pyx_GOTREF(__pyx_t_1); 1553 | __Pyx_INCREF(__pyx_n_s_simplify_polygon); 1554 | __Pyx_GIVEREF(__pyx_n_s_simplify_polygon); 1555 | PyList_SET_ITEM(__pyx_t_1, 0, __pyx_n_s_simplify_polygon); 1556 | __pyx_t_2 = __Pyx_Import(__pyx_n_s_simplify_polygon, __pyx_t_1, -1); if (unlikely(!__pyx_t_2)) __PYX_ERR(0, 3, __pyx_L1_error) 1557 | __Pyx_GOTREF(__pyx_t_2); 1558 | __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; 1559 | __pyx_t_1 = __Pyx_ImportFrom(__pyx_t_2, __pyx_n_s_simplify_polygon); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 3, __pyx_L1_error) 1560 | __Pyx_GOTREF(__pyx_t_1); 1561 | if (PyDict_SetItem(__pyx_d, __pyx_n_s_simplify_polygon, __pyx_t_1) < 0) __PYX_ERR(0, 3, __pyx_L1_error) 1562 | __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; 1563 | __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; 1564 | 1565 | /* "upolygon/__init__.py":4 1566 | * from find_contours import find_contours # noqa 1567 | * from simplify_polygon import simplify_polygon # noqa 1568 | * from run_length_encoding import rle_decode, rle_encode # noqa # <<<<<<<<<<<<<< 1569 | */ 1570 | __pyx_t_2 = PyList_New(2); if (unlikely(!__pyx_t_2)) __PYX_ERR(0, 4, __pyx_L1_error) 1571 | __Pyx_GOTREF(__pyx_t_2); 1572 | __Pyx_INCREF(__pyx_n_s_rle_decode); 1573 | __Pyx_GIVEREF(__pyx_n_s_rle_decode); 1574 | PyList_SET_ITEM(__pyx_t_2, 0, __pyx_n_s_rle_decode); 1575 | __Pyx_INCREF(__pyx_n_s_rle_encode); 1576 | __Pyx_GIVEREF(__pyx_n_s_rle_encode); 1577 | PyList_SET_ITEM(__pyx_t_2, 1, __pyx_n_s_rle_encode); 1578 | __pyx_t_1 = __Pyx_Import(__pyx_n_s_run_length_encoding, __pyx_t_2, -1); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 4, __pyx_L1_error) 1579 | __Pyx_GOTREF(__pyx_t_1); 1580 | __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; 1581 | __pyx_t_2 = __Pyx_ImportFrom(__pyx_t_1, __pyx_n_s_rle_decode); if (unlikely(!__pyx_t_2)) __PYX_ERR(0, 4, __pyx_L1_error) 1582 | __Pyx_GOTREF(__pyx_t_2); 1583 | if (PyDict_SetItem(__pyx_d, __pyx_n_s_rle_decode, __pyx_t_2) < 0) __PYX_ERR(0, 4, __pyx_L1_error) 1584 | __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; 1585 | __pyx_t_2 = __Pyx_ImportFrom(__pyx_t_1, __pyx_n_s_rle_encode); if (unlikely(!__pyx_t_2)) __PYX_ERR(0, 4, __pyx_L1_error) 1586 | __Pyx_GOTREF(__pyx_t_2); 1587 | if (PyDict_SetItem(__pyx_d, __pyx_n_s_rle_encode, __pyx_t_2) < 0) __PYX_ERR(0, 4, __pyx_L1_error) 1588 | __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; 1589 | __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; 1590 | 1591 | /* "upolygon/__init__.py":1 1592 | * from draw_polygon import draw_polygon # noqa # <<<<<<<<<<<<<< 1593 | * from find_contours import find_contours # noqa 1594 | * from simplify_polygon import simplify_polygon # noqa 1595 | */ 1596 | __pyx_t_1 = __Pyx_PyDict_NewPresized(0); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 1, __pyx_L1_error) 1597 | __Pyx_GOTREF(__pyx_t_1); 1598 | if (PyDict_SetItem(__pyx_d, __pyx_n_s_test, __pyx_t_1) < 0) __PYX_ERR(0, 1, __pyx_L1_error) 1599 | __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; 1600 | 1601 | /*--- Wrapped vars code ---*/ 1602 | 1603 | goto __pyx_L0; 1604 | __pyx_L1_error:; 1605 | __Pyx_XDECREF(__pyx_t_1); 1606 | __Pyx_XDECREF(__pyx_t_2); 1607 | if (__pyx_m) { 1608 | if (__pyx_d) { 1609 | __Pyx_AddTraceback("init upolygon.__init__", __pyx_clineno, __pyx_lineno, __pyx_filename); 1610 | } 1611 | Py_CLEAR(__pyx_m); 1612 | } else if (!PyErr_Occurred()) { 1613 | PyErr_SetString(PyExc_ImportError, "init upolygon.__init__"); 1614 | } 1615 | __pyx_L0:; 1616 | __Pyx_RefNannyFinishContext(); 1617 | #if CYTHON_PEP489_MULTI_PHASE_INIT 1618 | return (__pyx_m != NULL) ? 0 : -1; 1619 | #elif PY_MAJOR_VERSION >= 3 1620 | return __pyx_m; 1621 | #else 1622 | return; 1623 | #endif 1624 | } 1625 | 1626 | /* --- Runtime support code --- */ 1627 | /* Refnanny */ 1628 | #if CYTHON_REFNANNY 1629 | static __Pyx_RefNannyAPIStruct *__Pyx_RefNannyImportAPI(const char *modname) { 1630 | PyObject *m = NULL, *p = NULL; 1631 | void *r = NULL; 1632 | m = PyImport_ImportModule(modname); 1633 | if (!m) goto end; 1634 | p = PyObject_GetAttrString(m, "RefNannyAPI"); 1635 | if (!p) goto end; 1636 | r = PyLong_AsVoidPtr(p); 1637 | end: 1638 | Py_XDECREF(p); 1639 | Py_XDECREF(m); 1640 | return (__Pyx_RefNannyAPIStruct *)r; 1641 | } 1642 | #endif 1643 | 1644 | /* PyObjectGetAttrStr */ 1645 | #if CYTHON_USE_TYPE_SLOTS 1646 | static CYTHON_INLINE PyObject* __Pyx_PyObject_GetAttrStr(PyObject* obj, PyObject* attr_name) { 1647 | PyTypeObject* tp = Py_TYPE(obj); 1648 | if (likely(tp->tp_getattro)) 1649 | return tp->tp_getattro(obj, attr_name); 1650 | #if PY_MAJOR_VERSION < 3 1651 | if (likely(tp->tp_getattr)) 1652 | return tp->tp_getattr(obj, PyString_AS_STRING(attr_name)); 1653 | #endif 1654 | return PyObject_GetAttr(obj, attr_name); 1655 | } 1656 | #endif 1657 | 1658 | /* SetPackagePathFromImportLib */ 1659 | #if PY_MAJOR_VERSION >= 3 && !CYTHON_PEP489_MULTI_PHASE_INIT 1660 | static int __Pyx_SetPackagePathFromImportLib(const char* parent_package_name, PyObject *module_name) { 1661 | PyObject *importlib, *loader, *osmod, *ossep, *parts, *package_path; 1662 | PyObject *path = NULL, *file_path = NULL; 1663 | int result; 1664 | if (parent_package_name) { 1665 | PyObject *package = PyImport_ImportModule(parent_package_name); 1666 | if (unlikely(!package)) 1667 | goto bad; 1668 | path = PyObject_GetAttrString(package, "__path__"); 1669 | Py_DECREF(package); 1670 | if (unlikely(!path) || unlikely(path == Py_None)) 1671 | goto bad; 1672 | } else { 1673 | path = Py_None; Py_INCREF(Py_None); 1674 | } 1675 | importlib = PyImport_ImportModule("importlib"); 1676 | if (unlikely(!importlib)) 1677 | goto bad; 1678 | loader = PyObject_CallMethod(importlib, "find_loader", "(OO)", module_name, path); 1679 | Py_DECREF(importlib); 1680 | Py_DECREF(path); path = NULL; 1681 | if (unlikely(!loader)) 1682 | goto bad; 1683 | file_path = PyObject_GetAttrString(loader, "path"); 1684 | Py_DECREF(loader); 1685 | if (unlikely(!file_path)) 1686 | goto bad; 1687 | if (unlikely(PyObject_SetAttrString(__pyx_m, "__file__", file_path) < 0)) 1688 | goto bad; 1689 | osmod = PyImport_ImportModule("os"); 1690 | if (unlikely(!osmod)) 1691 | goto bad; 1692 | ossep = PyObject_GetAttrString(osmod, "sep"); 1693 | Py_DECREF(osmod); 1694 | if (unlikely(!ossep)) 1695 | goto bad; 1696 | parts = PyObject_CallMethod(file_path, "rsplit", "(Oi)", ossep, 1); 1697 | Py_DECREF(file_path); file_path = NULL; 1698 | Py_DECREF(ossep); 1699 | if (unlikely(!parts)) 1700 | goto bad; 1701 | package_path = Py_BuildValue("[O]", PyList_GET_ITEM(parts, 0)); 1702 | Py_DECREF(parts); 1703 | if (unlikely(!package_path)) 1704 | goto bad; 1705 | goto set_path; 1706 | bad: 1707 | PyErr_WriteUnraisable(module_name); 1708 | Py_XDECREF(path); 1709 | Py_XDECREF(file_path); 1710 | PyErr_Clear(); 1711 | package_path = PyList_New(0); 1712 | if (unlikely(!package_path)) 1713 | return -1; 1714 | set_path: 1715 | result = PyObject_SetAttrString(__pyx_m, "__path__", package_path); 1716 | Py_DECREF(package_path); 1717 | return result; 1718 | } 1719 | #endif 1720 | 1721 | /* Import */ 1722 | static PyObject *__Pyx_Import(PyObject *name, PyObject *from_list, int level) { 1723 | PyObject *empty_list = 0; 1724 | PyObject *module = 0; 1725 | PyObject *global_dict = 0; 1726 | PyObject *empty_dict = 0; 1727 | PyObject *list; 1728 | #if PY_MAJOR_VERSION < 3 1729 | PyObject *py_import; 1730 | py_import = __Pyx_PyObject_GetAttrStr(__pyx_b, __pyx_n_s_import); 1731 | if (!py_import) 1732 | goto bad; 1733 | #endif 1734 | if (from_list) 1735 | list = from_list; 1736 | else { 1737 | empty_list = PyList_New(0); 1738 | if (!empty_list) 1739 | goto bad; 1740 | list = empty_list; 1741 | } 1742 | global_dict = PyModule_GetDict(__pyx_m); 1743 | if (!global_dict) 1744 | goto bad; 1745 | empty_dict = PyDict_New(); 1746 | if (!empty_dict) 1747 | goto bad; 1748 | { 1749 | #if PY_MAJOR_VERSION >= 3 1750 | if (level == -1) { 1751 | if ((1) && (strchr(__Pyx_MODULE_NAME, '.'))) { 1752 | module = PyImport_ImportModuleLevelObject( 1753 | name, global_dict, empty_dict, list, 1); 1754 | if (!module) { 1755 | if (!PyErr_ExceptionMatches(PyExc_ImportError)) 1756 | goto bad; 1757 | PyErr_Clear(); 1758 | } 1759 | } 1760 | level = 0; 1761 | } 1762 | #endif 1763 | if (!module) { 1764 | #if PY_MAJOR_VERSION < 3 1765 | PyObject *py_level = PyInt_FromLong(level); 1766 | if (!py_level) 1767 | goto bad; 1768 | module = PyObject_CallFunctionObjArgs(py_import, 1769 | name, global_dict, empty_dict, list, py_level, (PyObject *)NULL); 1770 | Py_DECREF(py_level); 1771 | #else 1772 | module = PyImport_ImportModuleLevelObject( 1773 | name, global_dict, empty_dict, list, level); 1774 | #endif 1775 | } 1776 | } 1777 | bad: 1778 | #if PY_MAJOR_VERSION < 3 1779 | Py_XDECREF(py_import); 1780 | #endif 1781 | Py_XDECREF(empty_list); 1782 | Py_XDECREF(empty_dict); 1783 | return module; 1784 | } 1785 | 1786 | /* ImportFrom */ 1787 | static PyObject* __Pyx_ImportFrom(PyObject* module, PyObject* name) { 1788 | PyObject* value = __Pyx_PyObject_GetAttrStr(module, name); 1789 | if (unlikely(!value) && PyErr_ExceptionMatches(PyExc_AttributeError)) { 1790 | PyErr_Format(PyExc_ImportError, 1791 | #if PY_MAJOR_VERSION < 3 1792 | "cannot import name %.230s", PyString_AS_STRING(name)); 1793 | #else 1794 | "cannot import name %S", name); 1795 | #endif 1796 | } 1797 | return value; 1798 | } 1799 | 1800 | /* PyDictVersioning */ 1801 | #if CYTHON_USE_DICT_VERSIONS && CYTHON_USE_TYPE_SLOTS 1802 | static CYTHON_INLINE PY_UINT64_T __Pyx_get_tp_dict_version(PyObject *obj) { 1803 | PyObject *dict = Py_TYPE(obj)->tp_dict; 1804 | return likely(dict) ? __PYX_GET_DICT_VERSION(dict) : 0; 1805 | } 1806 | static CYTHON_INLINE PY_UINT64_T __Pyx_get_object_dict_version(PyObject *obj) { 1807 | PyObject **dictptr = NULL; 1808 | Py_ssize_t offset = Py_TYPE(obj)->tp_dictoffset; 1809 | if (offset) { 1810 | #if CYTHON_COMPILING_IN_CPYTHON 1811 | dictptr = (likely(offset > 0)) ? (PyObject **) ((char *)obj + offset) : _PyObject_GetDictPtr(obj); 1812 | #else 1813 | dictptr = _PyObject_GetDictPtr(obj); 1814 | #endif 1815 | } 1816 | return (dictptr && *dictptr) ? __PYX_GET_DICT_VERSION(*dictptr) : 0; 1817 | } 1818 | static CYTHON_INLINE int __Pyx_object_dict_version_matches(PyObject* obj, PY_UINT64_T tp_dict_version, PY_UINT64_T obj_dict_version) { 1819 | PyObject *dict = Py_TYPE(obj)->tp_dict; 1820 | if (unlikely(!dict) || unlikely(tp_dict_version != __PYX_GET_DICT_VERSION(dict))) 1821 | return 0; 1822 | return obj_dict_version == __Pyx_get_object_dict_version(obj); 1823 | } 1824 | #endif 1825 | 1826 | /* PyErrFetchRestore */ 1827 | #if CYTHON_FAST_THREAD_STATE 1828 | static CYTHON_INLINE void __Pyx_ErrRestoreInState(PyThreadState *tstate, PyObject *type, PyObject *value, PyObject *tb) { 1829 | PyObject *tmp_type, *tmp_value, *tmp_tb; 1830 | tmp_type = tstate->curexc_type; 1831 | tmp_value = tstate->curexc_value; 1832 | tmp_tb = tstate->curexc_traceback; 1833 | tstate->curexc_type = type; 1834 | tstate->curexc_value = value; 1835 | tstate->curexc_traceback = tb; 1836 | Py_XDECREF(tmp_type); 1837 | Py_XDECREF(tmp_value); 1838 | Py_XDECREF(tmp_tb); 1839 | } 1840 | static CYTHON_INLINE void __Pyx_ErrFetchInState(PyThreadState *tstate, PyObject **type, PyObject **value, PyObject **tb) { 1841 | *type = tstate->curexc_type; 1842 | *value = tstate->curexc_value; 1843 | *tb = tstate->curexc_traceback; 1844 | tstate->curexc_type = 0; 1845 | tstate->curexc_value = 0; 1846 | tstate->curexc_traceback = 0; 1847 | } 1848 | #endif 1849 | 1850 | /* CLineInTraceback */ 1851 | #ifndef CYTHON_CLINE_IN_TRACEBACK 1852 | static int __Pyx_CLineForTraceback(CYTHON_NCP_UNUSED PyThreadState *tstate, int c_line) { 1853 | PyObject *use_cline; 1854 | PyObject *ptype, *pvalue, *ptraceback; 1855 | #if CYTHON_COMPILING_IN_CPYTHON 1856 | PyObject **cython_runtime_dict; 1857 | #endif 1858 | if (unlikely(!__pyx_cython_runtime)) { 1859 | return c_line; 1860 | } 1861 | __Pyx_ErrFetchInState(tstate, &ptype, &pvalue, &ptraceback); 1862 | #if CYTHON_COMPILING_IN_CPYTHON 1863 | cython_runtime_dict = _PyObject_GetDictPtr(__pyx_cython_runtime); 1864 | if (likely(cython_runtime_dict)) { 1865 | __PYX_PY_DICT_LOOKUP_IF_MODIFIED( 1866 | use_cline, *cython_runtime_dict, 1867 | __Pyx_PyDict_GetItemStr(*cython_runtime_dict, __pyx_n_s_cline_in_traceback)) 1868 | } else 1869 | #endif 1870 | { 1871 | PyObject *use_cline_obj = __Pyx_PyObject_GetAttrStr(__pyx_cython_runtime, __pyx_n_s_cline_in_traceback); 1872 | if (use_cline_obj) { 1873 | use_cline = PyObject_Not(use_cline_obj) ? Py_False : Py_True; 1874 | Py_DECREF(use_cline_obj); 1875 | } else { 1876 | PyErr_Clear(); 1877 | use_cline = NULL; 1878 | } 1879 | } 1880 | if (!use_cline) { 1881 | c_line = 0; 1882 | (void) PyObject_SetAttr(__pyx_cython_runtime, __pyx_n_s_cline_in_traceback, Py_False); 1883 | } 1884 | else if (use_cline == Py_False || (use_cline != Py_True && PyObject_Not(use_cline) != 0)) { 1885 | c_line = 0; 1886 | } 1887 | __Pyx_ErrRestoreInState(tstate, ptype, pvalue, ptraceback); 1888 | return c_line; 1889 | } 1890 | #endif 1891 | 1892 | /* CodeObjectCache */ 1893 | static int __pyx_bisect_code_objects(__Pyx_CodeObjectCacheEntry* entries, int count, int code_line) { 1894 | int start = 0, mid = 0, end = count - 1; 1895 | if (end >= 0 && code_line > entries[end].code_line) { 1896 | return count; 1897 | } 1898 | while (start < end) { 1899 | mid = start + (end - start) / 2; 1900 | if (code_line < entries[mid].code_line) { 1901 | end = mid; 1902 | } else if (code_line > entries[mid].code_line) { 1903 | start = mid + 1; 1904 | } else { 1905 | return mid; 1906 | } 1907 | } 1908 | if (code_line <= entries[mid].code_line) { 1909 | return mid; 1910 | } else { 1911 | return mid + 1; 1912 | } 1913 | } 1914 | static PyCodeObject *__pyx_find_code_object(int code_line) { 1915 | PyCodeObject* code_object; 1916 | int pos; 1917 | if (unlikely(!code_line) || unlikely(!__pyx_code_cache.entries)) { 1918 | return NULL; 1919 | } 1920 | pos = __pyx_bisect_code_objects(__pyx_code_cache.entries, __pyx_code_cache.count, code_line); 1921 | if (unlikely(pos >= __pyx_code_cache.count) || unlikely(__pyx_code_cache.entries[pos].code_line != code_line)) { 1922 | return NULL; 1923 | } 1924 | code_object = __pyx_code_cache.entries[pos].code_object; 1925 | Py_INCREF(code_object); 1926 | return code_object; 1927 | } 1928 | static void __pyx_insert_code_object(int code_line, PyCodeObject* code_object) { 1929 | int pos, i; 1930 | __Pyx_CodeObjectCacheEntry* entries = __pyx_code_cache.entries; 1931 | if (unlikely(!code_line)) { 1932 | return; 1933 | } 1934 | if (unlikely(!entries)) { 1935 | entries = (__Pyx_CodeObjectCacheEntry*)PyMem_Malloc(64*sizeof(__Pyx_CodeObjectCacheEntry)); 1936 | if (likely(entries)) { 1937 | __pyx_code_cache.entries = entries; 1938 | __pyx_code_cache.max_count = 64; 1939 | __pyx_code_cache.count = 1; 1940 | entries[0].code_line = code_line; 1941 | entries[0].code_object = code_object; 1942 | Py_INCREF(code_object); 1943 | } 1944 | return; 1945 | } 1946 | pos = __pyx_bisect_code_objects(__pyx_code_cache.entries, __pyx_code_cache.count, code_line); 1947 | if ((pos < __pyx_code_cache.count) && unlikely(__pyx_code_cache.entries[pos].code_line == code_line)) { 1948 | PyCodeObject* tmp = entries[pos].code_object; 1949 | entries[pos].code_object = code_object; 1950 | Py_DECREF(tmp); 1951 | return; 1952 | } 1953 | if (__pyx_code_cache.count == __pyx_code_cache.max_count) { 1954 | int new_max = __pyx_code_cache.max_count + 64; 1955 | entries = (__Pyx_CodeObjectCacheEntry*)PyMem_Realloc( 1956 | __pyx_code_cache.entries, ((size_t)new_max) * sizeof(__Pyx_CodeObjectCacheEntry)); 1957 | if (unlikely(!entries)) { 1958 | return; 1959 | } 1960 | __pyx_code_cache.entries = entries; 1961 | __pyx_code_cache.max_count = new_max; 1962 | } 1963 | for (i=__pyx_code_cache.count; i>pos; i--) { 1964 | entries[i] = entries[i-1]; 1965 | } 1966 | entries[pos].code_line = code_line; 1967 | entries[pos].code_object = code_object; 1968 | __pyx_code_cache.count++; 1969 | Py_INCREF(code_object); 1970 | } 1971 | 1972 | /* AddTraceback */ 1973 | #include "compile.h" 1974 | #include "frameobject.h" 1975 | #include "traceback.h" 1976 | #if PY_VERSION_HEX >= 0x030b00a6 1977 | #ifndef Py_BUILD_CORE 1978 | #define Py_BUILD_CORE 1 1979 | #endif 1980 | #include "internal/pycore_frame.h" 1981 | #endif 1982 | static PyCodeObject* __Pyx_CreateCodeObjectForTraceback( 1983 | const char *funcname, int c_line, 1984 | int py_line, const char *filename) { 1985 | PyCodeObject *py_code = NULL; 1986 | PyObject *py_funcname = NULL; 1987 | #if PY_MAJOR_VERSION < 3 1988 | PyObject *py_srcfile = NULL; 1989 | py_srcfile = PyString_FromString(filename); 1990 | if (!py_srcfile) goto bad; 1991 | #endif 1992 | if (c_line) { 1993 | #if PY_MAJOR_VERSION < 3 1994 | py_funcname = PyString_FromFormat( "%s (%s:%d)", funcname, __pyx_cfilenm, c_line); 1995 | if (!py_funcname) goto bad; 1996 | #else 1997 | py_funcname = PyUnicode_FromFormat( "%s (%s:%d)", funcname, __pyx_cfilenm, c_line); 1998 | if (!py_funcname) goto bad; 1999 | funcname = PyUnicode_AsUTF8(py_funcname); 2000 | if (!funcname) goto bad; 2001 | #endif 2002 | } 2003 | else { 2004 | #if PY_MAJOR_VERSION < 3 2005 | py_funcname = PyString_FromString(funcname); 2006 | if (!py_funcname) goto bad; 2007 | #endif 2008 | } 2009 | #if PY_MAJOR_VERSION < 3 2010 | py_code = __Pyx_PyCode_New( 2011 | 0, 2012 | 0, 2013 | 0, 2014 | 0, 2015 | 0, 2016 | __pyx_empty_bytes, /*PyObject *code,*/ 2017 | __pyx_empty_tuple, /*PyObject *consts,*/ 2018 | __pyx_empty_tuple, /*PyObject *names,*/ 2019 | __pyx_empty_tuple, /*PyObject *varnames,*/ 2020 | __pyx_empty_tuple, /*PyObject *freevars,*/ 2021 | __pyx_empty_tuple, /*PyObject *cellvars,*/ 2022 | py_srcfile, /*PyObject *filename,*/ 2023 | py_funcname, /*PyObject *name,*/ 2024 | py_line, 2025 | __pyx_empty_bytes /*PyObject *lnotab*/ 2026 | ); 2027 | Py_DECREF(py_srcfile); 2028 | #else 2029 | py_code = PyCode_NewEmpty(filename, funcname, py_line); 2030 | #endif 2031 | Py_XDECREF(py_funcname); // XDECREF since it's only set on Py3 if cline 2032 | return py_code; 2033 | bad: 2034 | Py_XDECREF(py_funcname); 2035 | #if PY_MAJOR_VERSION < 3 2036 | Py_XDECREF(py_srcfile); 2037 | #endif 2038 | return NULL; 2039 | } 2040 | static void __Pyx_AddTraceback(const char *funcname, int c_line, 2041 | int py_line, const char *filename) { 2042 | PyCodeObject *py_code = 0; 2043 | PyFrameObject *py_frame = 0; 2044 | PyThreadState *tstate = __Pyx_PyThreadState_Current; 2045 | PyObject *ptype, *pvalue, *ptraceback; 2046 | if (c_line) { 2047 | c_line = __Pyx_CLineForTraceback(tstate, c_line); 2048 | } 2049 | py_code = __pyx_find_code_object(c_line ? -c_line : py_line); 2050 | if (!py_code) { 2051 | __Pyx_ErrFetchInState(tstate, &ptype, &pvalue, &ptraceback); 2052 | py_code = __Pyx_CreateCodeObjectForTraceback( 2053 | funcname, c_line, py_line, filename); 2054 | if (!py_code) { 2055 | /* If the code object creation fails, then we should clear the 2056 | fetched exception references and propagate the new exception */ 2057 | Py_XDECREF(ptype); 2058 | Py_XDECREF(pvalue); 2059 | Py_XDECREF(ptraceback); 2060 | goto bad; 2061 | } 2062 | __Pyx_ErrRestoreInState(tstate, ptype, pvalue, ptraceback); 2063 | __pyx_insert_code_object(c_line ? -c_line : py_line, py_code); 2064 | } 2065 | py_frame = PyFrame_New( 2066 | tstate, /*PyThreadState *tstate,*/ 2067 | py_code, /*PyCodeObject *code,*/ 2068 | __pyx_d, /*PyObject *globals,*/ 2069 | 0 /*PyObject *locals*/ 2070 | ); 2071 | if (!py_frame) goto bad; 2072 | __Pyx_PyFrame_SetLineNumber(py_frame, py_line); 2073 | PyTraceBack_Here(py_frame); 2074 | bad: 2075 | Py_XDECREF(py_code); 2076 | Py_XDECREF(py_frame); 2077 | } 2078 | 2079 | /* CIntToPy */ 2080 | static CYTHON_INLINE PyObject* __Pyx_PyInt_From_long(long value) { 2081 | #ifdef __Pyx_HAS_GCC_DIAGNOSTIC 2082 | #pragma GCC diagnostic push 2083 | #pragma GCC diagnostic ignored "-Wconversion" 2084 | #endif 2085 | const long neg_one = (long) -1, const_zero = (long) 0; 2086 | #ifdef __Pyx_HAS_GCC_DIAGNOSTIC 2087 | #pragma GCC diagnostic pop 2088 | #endif 2089 | const int is_unsigned = neg_one > const_zero; 2090 | if (is_unsigned) { 2091 | if (sizeof(long) < sizeof(long)) { 2092 | return PyInt_FromLong((long) value); 2093 | } else if (sizeof(long) <= sizeof(unsigned long)) { 2094 | return PyLong_FromUnsignedLong((unsigned long) value); 2095 | #ifdef HAVE_LONG_LONG 2096 | } else if (sizeof(long) <= sizeof(unsigned PY_LONG_LONG)) { 2097 | return PyLong_FromUnsignedLongLong((unsigned PY_LONG_LONG) value); 2098 | #endif 2099 | } 2100 | } else { 2101 | if (sizeof(long) <= sizeof(long)) { 2102 | return PyInt_FromLong((long) value); 2103 | #ifdef HAVE_LONG_LONG 2104 | } else if (sizeof(long) <= sizeof(PY_LONG_LONG)) { 2105 | return PyLong_FromLongLong((PY_LONG_LONG) value); 2106 | #endif 2107 | } 2108 | } 2109 | { 2110 | int one = 1; int little = (int)*(unsigned char *)&one; 2111 | unsigned char *bytes = (unsigned char *)&value; 2112 | return _PyLong_FromByteArray(bytes, sizeof(long), 2113 | little, !is_unsigned); 2114 | } 2115 | } 2116 | 2117 | /* CIntFromPyVerify */ 2118 | #define __PYX_VERIFY_RETURN_INT(target_type, func_type, func_value)\ 2119 | __PYX__VERIFY_RETURN_INT(target_type, func_type, func_value, 0) 2120 | #define __PYX_VERIFY_RETURN_INT_EXC(target_type, func_type, func_value)\ 2121 | __PYX__VERIFY_RETURN_INT(target_type, func_type, func_value, 1) 2122 | #define __PYX__VERIFY_RETURN_INT(target_type, func_type, func_value, exc)\ 2123 | {\ 2124 | func_type value = func_value;\ 2125 | if (sizeof(target_type) < sizeof(func_type)) {\ 2126 | if (unlikely(value != (func_type) (target_type) value)) {\ 2127 | func_type zero = 0;\ 2128 | if (exc && unlikely(value == (func_type)-1 && PyErr_Occurred()))\ 2129 | return (target_type) -1;\ 2130 | if (is_unsigned && unlikely(value < zero))\ 2131 | goto raise_neg_overflow;\ 2132 | else\ 2133 | goto raise_overflow;\ 2134 | }\ 2135 | }\ 2136 | return (target_type) value;\ 2137 | } 2138 | 2139 | /* CIntFromPy */ 2140 | static CYTHON_INLINE long __Pyx_PyInt_As_long(PyObject *x) { 2141 | #ifdef __Pyx_HAS_GCC_DIAGNOSTIC 2142 | #pragma GCC diagnostic push 2143 | #pragma GCC diagnostic ignored "-Wconversion" 2144 | #endif 2145 | const long neg_one = (long) -1, const_zero = (long) 0; 2146 | #ifdef __Pyx_HAS_GCC_DIAGNOSTIC 2147 | #pragma GCC diagnostic pop 2148 | #endif 2149 | const int is_unsigned = neg_one > const_zero; 2150 | #if PY_MAJOR_VERSION < 3 2151 | if (likely(PyInt_Check(x))) { 2152 | if (sizeof(long) < sizeof(long)) { 2153 | __PYX_VERIFY_RETURN_INT(long, long, PyInt_AS_LONG(x)) 2154 | } else { 2155 | long val = PyInt_AS_LONG(x); 2156 | if (is_unsigned && unlikely(val < 0)) { 2157 | goto raise_neg_overflow; 2158 | } 2159 | return (long) val; 2160 | } 2161 | } else 2162 | #endif 2163 | if (likely(PyLong_Check(x))) { 2164 | if (is_unsigned) { 2165 | #if CYTHON_USE_PYLONG_INTERNALS 2166 | const digit* digits = ((PyLongObject*)x)->ob_digit; 2167 | switch (Py_SIZE(x)) { 2168 | case 0: return (long) 0; 2169 | case 1: __PYX_VERIFY_RETURN_INT(long, digit, digits[0]) 2170 | case 2: 2171 | if (8 * sizeof(long) > 1 * PyLong_SHIFT) { 2172 | if (8 * sizeof(unsigned long) > 2 * PyLong_SHIFT) { 2173 | __PYX_VERIFY_RETURN_INT(long, unsigned long, (((((unsigned long)digits[1]) << PyLong_SHIFT) | (unsigned long)digits[0]))) 2174 | } else if (8 * sizeof(long) >= 2 * PyLong_SHIFT) { 2175 | return (long) (((((long)digits[1]) << PyLong_SHIFT) | (long)digits[0])); 2176 | } 2177 | } 2178 | break; 2179 | case 3: 2180 | if (8 * sizeof(long) > 2 * PyLong_SHIFT) { 2181 | if (8 * sizeof(unsigned long) > 3 * PyLong_SHIFT) { 2182 | __PYX_VERIFY_RETURN_INT(long, unsigned long, (((((((unsigned long)digits[2]) << PyLong_SHIFT) | (unsigned long)digits[1]) << PyLong_SHIFT) | (unsigned long)digits[0]))) 2183 | } else if (8 * sizeof(long) >= 3 * PyLong_SHIFT) { 2184 | return (long) (((((((long)digits[2]) << PyLong_SHIFT) | (long)digits[1]) << PyLong_SHIFT) | (long)digits[0])); 2185 | } 2186 | } 2187 | break; 2188 | case 4: 2189 | if (8 * sizeof(long) > 3 * PyLong_SHIFT) { 2190 | if (8 * sizeof(unsigned long) > 4 * PyLong_SHIFT) { 2191 | __PYX_VERIFY_RETURN_INT(long, unsigned long, (((((((((unsigned long)digits[3]) << PyLong_SHIFT) | (unsigned long)digits[2]) << PyLong_SHIFT) | (unsigned long)digits[1]) << PyLong_SHIFT) | (unsigned long)digits[0]))) 2192 | } else if (8 * sizeof(long) >= 4 * PyLong_SHIFT) { 2193 | return (long) (((((((((long)digits[3]) << PyLong_SHIFT) | (long)digits[2]) << PyLong_SHIFT) | (long)digits[1]) << PyLong_SHIFT) | (long)digits[0])); 2194 | } 2195 | } 2196 | break; 2197 | } 2198 | #endif 2199 | #if CYTHON_COMPILING_IN_CPYTHON 2200 | if (unlikely(Py_SIZE(x) < 0)) { 2201 | goto raise_neg_overflow; 2202 | } 2203 | #else 2204 | { 2205 | int result = PyObject_RichCompareBool(x, Py_False, Py_LT); 2206 | if (unlikely(result < 0)) 2207 | return (long) -1; 2208 | if (unlikely(result == 1)) 2209 | goto raise_neg_overflow; 2210 | } 2211 | #endif 2212 | if (sizeof(long) <= sizeof(unsigned long)) { 2213 | __PYX_VERIFY_RETURN_INT_EXC(long, unsigned long, PyLong_AsUnsignedLong(x)) 2214 | #ifdef HAVE_LONG_LONG 2215 | } else if (sizeof(long) <= sizeof(unsigned PY_LONG_LONG)) { 2216 | __PYX_VERIFY_RETURN_INT_EXC(long, unsigned PY_LONG_LONG, PyLong_AsUnsignedLongLong(x)) 2217 | #endif 2218 | } 2219 | } else { 2220 | #if CYTHON_USE_PYLONG_INTERNALS 2221 | const digit* digits = ((PyLongObject*)x)->ob_digit; 2222 | switch (Py_SIZE(x)) { 2223 | case 0: return (long) 0; 2224 | case -1: __PYX_VERIFY_RETURN_INT(long, sdigit, (sdigit) (-(sdigit)digits[0])) 2225 | case 1: __PYX_VERIFY_RETURN_INT(long, digit, +digits[0]) 2226 | case -2: 2227 | if (8 * sizeof(long) - 1 > 1 * PyLong_SHIFT) { 2228 | if (8 * sizeof(unsigned long) > 2 * PyLong_SHIFT) { 2229 | __PYX_VERIFY_RETURN_INT(long, long, -(long) (((((unsigned long)digits[1]) << PyLong_SHIFT) | (unsigned long)digits[0]))) 2230 | } else if (8 * sizeof(long) - 1 > 2 * PyLong_SHIFT) { 2231 | return (long) (((long)-1)*(((((long)digits[1]) << PyLong_SHIFT) | (long)digits[0]))); 2232 | } 2233 | } 2234 | break; 2235 | case 2: 2236 | if (8 * sizeof(long) > 1 * PyLong_SHIFT) { 2237 | if (8 * sizeof(unsigned long) > 2 * PyLong_SHIFT) { 2238 | __PYX_VERIFY_RETURN_INT(long, unsigned long, (((((unsigned long)digits[1]) << PyLong_SHIFT) | (unsigned long)digits[0]))) 2239 | } else if (8 * sizeof(long) - 1 > 2 * PyLong_SHIFT) { 2240 | return (long) ((((((long)digits[1]) << PyLong_SHIFT) | (long)digits[0]))); 2241 | } 2242 | } 2243 | break; 2244 | case -3: 2245 | if (8 * sizeof(long) - 1 > 2 * PyLong_SHIFT) { 2246 | if (8 * sizeof(unsigned long) > 3 * PyLong_SHIFT) { 2247 | __PYX_VERIFY_RETURN_INT(long, long, -(long) (((((((unsigned long)digits[2]) << PyLong_SHIFT) | (unsigned long)digits[1]) << PyLong_SHIFT) | (unsigned long)digits[0]))) 2248 | } else if (8 * sizeof(long) - 1 > 3 * PyLong_SHIFT) { 2249 | return (long) (((long)-1)*(((((((long)digits[2]) << PyLong_SHIFT) | (long)digits[1]) << PyLong_SHIFT) | (long)digits[0]))); 2250 | } 2251 | } 2252 | break; 2253 | case 3: 2254 | if (8 * sizeof(long) > 2 * PyLong_SHIFT) { 2255 | if (8 * sizeof(unsigned long) > 3 * PyLong_SHIFT) { 2256 | __PYX_VERIFY_RETURN_INT(long, unsigned long, (((((((unsigned long)digits[2]) << PyLong_SHIFT) | (unsigned long)digits[1]) << PyLong_SHIFT) | (unsigned long)digits[0]))) 2257 | } else if (8 * sizeof(long) - 1 > 3 * PyLong_SHIFT) { 2258 | return (long) ((((((((long)digits[2]) << PyLong_SHIFT) | (long)digits[1]) << PyLong_SHIFT) | (long)digits[0]))); 2259 | } 2260 | } 2261 | break; 2262 | case -4: 2263 | if (8 * sizeof(long) - 1 > 3 * PyLong_SHIFT) { 2264 | if (8 * sizeof(unsigned long) > 4 * PyLong_SHIFT) { 2265 | __PYX_VERIFY_RETURN_INT(long, long, -(long) (((((((((unsigned long)digits[3]) << PyLong_SHIFT) | (unsigned long)digits[2]) << PyLong_SHIFT) | (unsigned long)digits[1]) << PyLong_SHIFT) | (unsigned long)digits[0]))) 2266 | } else if (8 * sizeof(long) - 1 > 4 * PyLong_SHIFT) { 2267 | return (long) (((long)-1)*(((((((((long)digits[3]) << PyLong_SHIFT) | (long)digits[2]) << PyLong_SHIFT) | (long)digits[1]) << PyLong_SHIFT) | (long)digits[0]))); 2268 | } 2269 | } 2270 | break; 2271 | case 4: 2272 | if (8 * sizeof(long) > 3 * PyLong_SHIFT) { 2273 | if (8 * sizeof(unsigned long) > 4 * PyLong_SHIFT) { 2274 | __PYX_VERIFY_RETURN_INT(long, unsigned long, (((((((((unsigned long)digits[3]) << PyLong_SHIFT) | (unsigned long)digits[2]) << PyLong_SHIFT) | (unsigned long)digits[1]) << PyLong_SHIFT) | (unsigned long)digits[0]))) 2275 | } else if (8 * sizeof(long) - 1 > 4 * PyLong_SHIFT) { 2276 | return (long) ((((((((((long)digits[3]) << PyLong_SHIFT) | (long)digits[2]) << PyLong_SHIFT) | (long)digits[1]) << PyLong_SHIFT) | (long)digits[0]))); 2277 | } 2278 | } 2279 | break; 2280 | } 2281 | #endif 2282 | if (sizeof(long) <= sizeof(long)) { 2283 | __PYX_VERIFY_RETURN_INT_EXC(long, long, PyLong_AsLong(x)) 2284 | #ifdef HAVE_LONG_LONG 2285 | } else if (sizeof(long) <= sizeof(PY_LONG_LONG)) { 2286 | __PYX_VERIFY_RETURN_INT_EXC(long, PY_LONG_LONG, PyLong_AsLongLong(x)) 2287 | #endif 2288 | } 2289 | } 2290 | { 2291 | #if CYTHON_COMPILING_IN_PYPY && !defined(_PyLong_AsByteArray) 2292 | PyErr_SetString(PyExc_RuntimeError, 2293 | "_PyLong_AsByteArray() not available in PyPy, cannot convert large numbers"); 2294 | #else 2295 | long val; 2296 | PyObject *v = __Pyx_PyNumber_IntOrLong(x); 2297 | #if PY_MAJOR_VERSION < 3 2298 | if (likely(v) && !PyLong_Check(v)) { 2299 | PyObject *tmp = v; 2300 | v = PyNumber_Long(tmp); 2301 | Py_DECREF(tmp); 2302 | } 2303 | #endif 2304 | if (likely(v)) { 2305 | int one = 1; int is_little = (int)*(unsigned char *)&one; 2306 | unsigned char *bytes = (unsigned char *)&val; 2307 | int ret = _PyLong_AsByteArray((PyLongObject *)v, 2308 | bytes, sizeof(val), 2309 | is_little, !is_unsigned); 2310 | Py_DECREF(v); 2311 | if (likely(!ret)) 2312 | return val; 2313 | } 2314 | #endif 2315 | return (long) -1; 2316 | } 2317 | } else { 2318 | long val; 2319 | PyObject *tmp = __Pyx_PyNumber_IntOrLong(x); 2320 | if (!tmp) return (long) -1; 2321 | val = __Pyx_PyInt_As_long(tmp); 2322 | Py_DECREF(tmp); 2323 | return val; 2324 | } 2325 | raise_overflow: 2326 | PyErr_SetString(PyExc_OverflowError, 2327 | "value too large to convert to long"); 2328 | return (long) -1; 2329 | raise_neg_overflow: 2330 | PyErr_SetString(PyExc_OverflowError, 2331 | "can't convert negative value to long"); 2332 | return (long) -1; 2333 | } 2334 | 2335 | /* CIntFromPy */ 2336 | static CYTHON_INLINE int __Pyx_PyInt_As_int(PyObject *x) { 2337 | #ifdef __Pyx_HAS_GCC_DIAGNOSTIC 2338 | #pragma GCC diagnostic push 2339 | #pragma GCC diagnostic ignored "-Wconversion" 2340 | #endif 2341 | const int neg_one = (int) -1, const_zero = (int) 0; 2342 | #ifdef __Pyx_HAS_GCC_DIAGNOSTIC 2343 | #pragma GCC diagnostic pop 2344 | #endif 2345 | const int is_unsigned = neg_one > const_zero; 2346 | #if PY_MAJOR_VERSION < 3 2347 | if (likely(PyInt_Check(x))) { 2348 | if (sizeof(int) < sizeof(long)) { 2349 | __PYX_VERIFY_RETURN_INT(int, long, PyInt_AS_LONG(x)) 2350 | } else { 2351 | long val = PyInt_AS_LONG(x); 2352 | if (is_unsigned && unlikely(val < 0)) { 2353 | goto raise_neg_overflow; 2354 | } 2355 | return (int) val; 2356 | } 2357 | } else 2358 | #endif 2359 | if (likely(PyLong_Check(x))) { 2360 | if (is_unsigned) { 2361 | #if CYTHON_USE_PYLONG_INTERNALS 2362 | const digit* digits = ((PyLongObject*)x)->ob_digit; 2363 | switch (Py_SIZE(x)) { 2364 | case 0: return (int) 0; 2365 | case 1: __PYX_VERIFY_RETURN_INT(int, digit, digits[0]) 2366 | case 2: 2367 | if (8 * sizeof(int) > 1 * PyLong_SHIFT) { 2368 | if (8 * sizeof(unsigned long) > 2 * PyLong_SHIFT) { 2369 | __PYX_VERIFY_RETURN_INT(int, unsigned long, (((((unsigned long)digits[1]) << PyLong_SHIFT) | (unsigned long)digits[0]))) 2370 | } else if (8 * sizeof(int) >= 2 * PyLong_SHIFT) { 2371 | return (int) (((((int)digits[1]) << PyLong_SHIFT) | (int)digits[0])); 2372 | } 2373 | } 2374 | break; 2375 | case 3: 2376 | if (8 * sizeof(int) > 2 * PyLong_SHIFT) { 2377 | if (8 * sizeof(unsigned long) > 3 * PyLong_SHIFT) { 2378 | __PYX_VERIFY_RETURN_INT(int, unsigned long, (((((((unsigned long)digits[2]) << PyLong_SHIFT) | (unsigned long)digits[1]) << PyLong_SHIFT) | (unsigned long)digits[0]))) 2379 | } else if (8 * sizeof(int) >= 3 * PyLong_SHIFT) { 2380 | return (int) (((((((int)digits[2]) << PyLong_SHIFT) | (int)digits[1]) << PyLong_SHIFT) | (int)digits[0])); 2381 | } 2382 | } 2383 | break; 2384 | case 4: 2385 | if (8 * sizeof(int) > 3 * PyLong_SHIFT) { 2386 | if (8 * sizeof(unsigned long) > 4 * PyLong_SHIFT) { 2387 | __PYX_VERIFY_RETURN_INT(int, unsigned long, (((((((((unsigned long)digits[3]) << PyLong_SHIFT) | (unsigned long)digits[2]) << PyLong_SHIFT) | (unsigned long)digits[1]) << PyLong_SHIFT) | (unsigned long)digits[0]))) 2388 | } else if (8 * sizeof(int) >= 4 * PyLong_SHIFT) { 2389 | return (int) (((((((((int)digits[3]) << PyLong_SHIFT) | (int)digits[2]) << PyLong_SHIFT) | (int)digits[1]) << PyLong_SHIFT) | (int)digits[0])); 2390 | } 2391 | } 2392 | break; 2393 | } 2394 | #endif 2395 | #if CYTHON_COMPILING_IN_CPYTHON 2396 | if (unlikely(Py_SIZE(x) < 0)) { 2397 | goto raise_neg_overflow; 2398 | } 2399 | #else 2400 | { 2401 | int result = PyObject_RichCompareBool(x, Py_False, Py_LT); 2402 | if (unlikely(result < 0)) 2403 | return (int) -1; 2404 | if (unlikely(result == 1)) 2405 | goto raise_neg_overflow; 2406 | } 2407 | #endif 2408 | if (sizeof(int) <= sizeof(unsigned long)) { 2409 | __PYX_VERIFY_RETURN_INT_EXC(int, unsigned long, PyLong_AsUnsignedLong(x)) 2410 | #ifdef HAVE_LONG_LONG 2411 | } else if (sizeof(int) <= sizeof(unsigned PY_LONG_LONG)) { 2412 | __PYX_VERIFY_RETURN_INT_EXC(int, unsigned PY_LONG_LONG, PyLong_AsUnsignedLongLong(x)) 2413 | #endif 2414 | } 2415 | } else { 2416 | #if CYTHON_USE_PYLONG_INTERNALS 2417 | const digit* digits = ((PyLongObject*)x)->ob_digit; 2418 | switch (Py_SIZE(x)) { 2419 | case 0: return (int) 0; 2420 | case -1: __PYX_VERIFY_RETURN_INT(int, sdigit, (sdigit) (-(sdigit)digits[0])) 2421 | case 1: __PYX_VERIFY_RETURN_INT(int, digit, +digits[0]) 2422 | case -2: 2423 | if (8 * sizeof(int) - 1 > 1 * PyLong_SHIFT) { 2424 | if (8 * sizeof(unsigned long) > 2 * PyLong_SHIFT) { 2425 | __PYX_VERIFY_RETURN_INT(int, long, -(long) (((((unsigned long)digits[1]) << PyLong_SHIFT) | (unsigned long)digits[0]))) 2426 | } else if (8 * sizeof(int) - 1 > 2 * PyLong_SHIFT) { 2427 | return (int) (((int)-1)*(((((int)digits[1]) << PyLong_SHIFT) | (int)digits[0]))); 2428 | } 2429 | } 2430 | break; 2431 | case 2: 2432 | if (8 * sizeof(int) > 1 * PyLong_SHIFT) { 2433 | if (8 * sizeof(unsigned long) > 2 * PyLong_SHIFT) { 2434 | __PYX_VERIFY_RETURN_INT(int, unsigned long, (((((unsigned long)digits[1]) << PyLong_SHIFT) | (unsigned long)digits[0]))) 2435 | } else if (8 * sizeof(int) - 1 > 2 * PyLong_SHIFT) { 2436 | return (int) ((((((int)digits[1]) << PyLong_SHIFT) | (int)digits[0]))); 2437 | } 2438 | } 2439 | break; 2440 | case -3: 2441 | if (8 * sizeof(int) - 1 > 2 * PyLong_SHIFT) { 2442 | if (8 * sizeof(unsigned long) > 3 * PyLong_SHIFT) { 2443 | __PYX_VERIFY_RETURN_INT(int, long, -(long) (((((((unsigned long)digits[2]) << PyLong_SHIFT) | (unsigned long)digits[1]) << PyLong_SHIFT) | (unsigned long)digits[0]))) 2444 | } else if (8 * sizeof(int) - 1 > 3 * PyLong_SHIFT) { 2445 | return (int) (((int)-1)*(((((((int)digits[2]) << PyLong_SHIFT) | (int)digits[1]) << PyLong_SHIFT) | (int)digits[0]))); 2446 | } 2447 | } 2448 | break; 2449 | case 3: 2450 | if (8 * sizeof(int) > 2 * PyLong_SHIFT) { 2451 | if (8 * sizeof(unsigned long) > 3 * PyLong_SHIFT) { 2452 | __PYX_VERIFY_RETURN_INT(int, unsigned long, (((((((unsigned long)digits[2]) << PyLong_SHIFT) | (unsigned long)digits[1]) << PyLong_SHIFT) | (unsigned long)digits[0]))) 2453 | } else if (8 * sizeof(int) - 1 > 3 * PyLong_SHIFT) { 2454 | return (int) ((((((((int)digits[2]) << PyLong_SHIFT) | (int)digits[1]) << PyLong_SHIFT) | (int)digits[0]))); 2455 | } 2456 | } 2457 | break; 2458 | case -4: 2459 | if (8 * sizeof(int) - 1 > 3 * PyLong_SHIFT) { 2460 | if (8 * sizeof(unsigned long) > 4 * PyLong_SHIFT) { 2461 | __PYX_VERIFY_RETURN_INT(int, long, -(long) (((((((((unsigned long)digits[3]) << PyLong_SHIFT) | (unsigned long)digits[2]) << PyLong_SHIFT) | (unsigned long)digits[1]) << PyLong_SHIFT) | (unsigned long)digits[0]))) 2462 | } else if (8 * sizeof(int) - 1 > 4 * PyLong_SHIFT) { 2463 | return (int) (((int)-1)*(((((((((int)digits[3]) << PyLong_SHIFT) | (int)digits[2]) << PyLong_SHIFT) | (int)digits[1]) << PyLong_SHIFT) | (int)digits[0]))); 2464 | } 2465 | } 2466 | break; 2467 | case 4: 2468 | if (8 * sizeof(int) > 3 * PyLong_SHIFT) { 2469 | if (8 * sizeof(unsigned long) > 4 * PyLong_SHIFT) { 2470 | __PYX_VERIFY_RETURN_INT(int, unsigned long, (((((((((unsigned long)digits[3]) << PyLong_SHIFT) | (unsigned long)digits[2]) << PyLong_SHIFT) | (unsigned long)digits[1]) << PyLong_SHIFT) | (unsigned long)digits[0]))) 2471 | } else if (8 * sizeof(int) - 1 > 4 * PyLong_SHIFT) { 2472 | return (int) ((((((((((int)digits[3]) << PyLong_SHIFT) | (int)digits[2]) << PyLong_SHIFT) | (int)digits[1]) << PyLong_SHIFT) | (int)digits[0]))); 2473 | } 2474 | } 2475 | break; 2476 | } 2477 | #endif 2478 | if (sizeof(int) <= sizeof(long)) { 2479 | __PYX_VERIFY_RETURN_INT_EXC(int, long, PyLong_AsLong(x)) 2480 | #ifdef HAVE_LONG_LONG 2481 | } else if (sizeof(int) <= sizeof(PY_LONG_LONG)) { 2482 | __PYX_VERIFY_RETURN_INT_EXC(int, PY_LONG_LONG, PyLong_AsLongLong(x)) 2483 | #endif 2484 | } 2485 | } 2486 | { 2487 | #if CYTHON_COMPILING_IN_PYPY && !defined(_PyLong_AsByteArray) 2488 | PyErr_SetString(PyExc_RuntimeError, 2489 | "_PyLong_AsByteArray() not available in PyPy, cannot convert large numbers"); 2490 | #else 2491 | int val; 2492 | PyObject *v = __Pyx_PyNumber_IntOrLong(x); 2493 | #if PY_MAJOR_VERSION < 3 2494 | if (likely(v) && !PyLong_Check(v)) { 2495 | PyObject *tmp = v; 2496 | v = PyNumber_Long(tmp); 2497 | Py_DECREF(tmp); 2498 | } 2499 | #endif 2500 | if (likely(v)) { 2501 | int one = 1; int is_little = (int)*(unsigned char *)&one; 2502 | unsigned char *bytes = (unsigned char *)&val; 2503 | int ret = _PyLong_AsByteArray((PyLongObject *)v, 2504 | bytes, sizeof(val), 2505 | is_little, !is_unsigned); 2506 | Py_DECREF(v); 2507 | if (likely(!ret)) 2508 | return val; 2509 | } 2510 | #endif 2511 | return (int) -1; 2512 | } 2513 | } else { 2514 | int val; 2515 | PyObject *tmp = __Pyx_PyNumber_IntOrLong(x); 2516 | if (!tmp) return (int) -1; 2517 | val = __Pyx_PyInt_As_int(tmp); 2518 | Py_DECREF(tmp); 2519 | return val; 2520 | } 2521 | raise_overflow: 2522 | PyErr_SetString(PyExc_OverflowError, 2523 | "value too large to convert to int"); 2524 | return (int) -1; 2525 | raise_neg_overflow: 2526 | PyErr_SetString(PyExc_OverflowError, 2527 | "can't convert negative value to int"); 2528 | return (int) -1; 2529 | } 2530 | 2531 | /* FastTypeChecks */ 2532 | #if CYTHON_COMPILING_IN_CPYTHON 2533 | static int __Pyx_InBases(PyTypeObject *a, PyTypeObject *b) { 2534 | while (a) { 2535 | a = a->tp_base; 2536 | if (a == b) 2537 | return 1; 2538 | } 2539 | return b == &PyBaseObject_Type; 2540 | } 2541 | static CYTHON_INLINE int __Pyx_IsSubtype(PyTypeObject *a, PyTypeObject *b) { 2542 | PyObject *mro; 2543 | if (a == b) return 1; 2544 | mro = a->tp_mro; 2545 | if (likely(mro)) { 2546 | Py_ssize_t i, n; 2547 | n = PyTuple_GET_SIZE(mro); 2548 | for (i = 0; i < n; i++) { 2549 | if (PyTuple_GET_ITEM(mro, i) == (PyObject *)b) 2550 | return 1; 2551 | } 2552 | return 0; 2553 | } 2554 | return __Pyx_InBases(a, b); 2555 | } 2556 | #if PY_MAJOR_VERSION == 2 2557 | static int __Pyx_inner_PyErr_GivenExceptionMatches2(PyObject *err, PyObject* exc_type1, PyObject* exc_type2) { 2558 | PyObject *exception, *value, *tb; 2559 | int res; 2560 | __Pyx_PyThreadState_declare 2561 | __Pyx_PyThreadState_assign 2562 | __Pyx_ErrFetch(&exception, &value, &tb); 2563 | res = exc_type1 ? PyObject_IsSubclass(err, exc_type1) : 0; 2564 | if (unlikely(res == -1)) { 2565 | PyErr_WriteUnraisable(err); 2566 | res = 0; 2567 | } 2568 | if (!res) { 2569 | res = PyObject_IsSubclass(err, exc_type2); 2570 | if (unlikely(res == -1)) { 2571 | PyErr_WriteUnraisable(err); 2572 | res = 0; 2573 | } 2574 | } 2575 | __Pyx_ErrRestore(exception, value, tb); 2576 | return res; 2577 | } 2578 | #else 2579 | static CYTHON_INLINE int __Pyx_inner_PyErr_GivenExceptionMatches2(PyObject *err, PyObject* exc_type1, PyObject *exc_type2) { 2580 | int res = exc_type1 ? __Pyx_IsSubtype((PyTypeObject*)err, (PyTypeObject*)exc_type1) : 0; 2581 | if (!res) { 2582 | res = __Pyx_IsSubtype((PyTypeObject*)err, (PyTypeObject*)exc_type2); 2583 | } 2584 | return res; 2585 | } 2586 | #endif 2587 | static int __Pyx_PyErr_GivenExceptionMatchesTuple(PyObject *exc_type, PyObject *tuple) { 2588 | Py_ssize_t i, n; 2589 | assert(PyExceptionClass_Check(exc_type)); 2590 | n = PyTuple_GET_SIZE(tuple); 2591 | #if PY_MAJOR_VERSION >= 3 2592 | for (i=0; i '9'); 2641 | break; 2642 | } 2643 | if (rt_from_call[i] != ctversion[i]) { 2644 | same = 0; 2645 | break; 2646 | } 2647 | } 2648 | if (!same) { 2649 | char rtversion[5] = {'\0'}; 2650 | char message[200]; 2651 | for (i=0; i<4; ++i) { 2652 | if (rt_from_call[i] == '.') { 2653 | if (found_dot) break; 2654 | found_dot = 1; 2655 | } else if (rt_from_call[i] < '0' || rt_from_call[i] > '9') { 2656 | break; 2657 | } 2658 | rtversion[i] = rt_from_call[i]; 2659 | } 2660 | PyOS_snprintf(message, sizeof(message), 2661 | "compiletime version %s of module '%.100s' " 2662 | "does not match runtime version %s", 2663 | ctversion, __Pyx_MODULE_NAME, rtversion); 2664 | return PyErr_WarnEx(NULL, message, 1); 2665 | } 2666 | return 0; 2667 | } 2668 | 2669 | /* InitStrings */ 2670 | static int __Pyx_InitStrings(__Pyx_StringTabEntry *t) { 2671 | while (t->p) { 2672 | #if PY_MAJOR_VERSION < 3 2673 | if (t->is_unicode) { 2674 | *t->p = PyUnicode_DecodeUTF8(t->s, t->n - 1, NULL); 2675 | } else if (t->intern) { 2676 | *t->p = PyString_InternFromString(t->s); 2677 | } else { 2678 | *t->p = PyString_FromStringAndSize(t->s, t->n - 1); 2679 | } 2680 | #else 2681 | if (t->is_unicode | t->is_str) { 2682 | if (t->intern) { 2683 | *t->p = PyUnicode_InternFromString(t->s); 2684 | } else if (t->encoding) { 2685 | *t->p = PyUnicode_Decode(t->s, t->n - 1, t->encoding, NULL); 2686 | } else { 2687 | *t->p = PyUnicode_FromStringAndSize(t->s, t->n - 1); 2688 | } 2689 | } else { 2690 | *t->p = PyBytes_FromStringAndSize(t->s, t->n - 1); 2691 | } 2692 | #endif 2693 | if (!*t->p) 2694 | return -1; 2695 | if (PyObject_Hash(*t->p) == -1) 2696 | return -1; 2697 | ++t; 2698 | } 2699 | return 0; 2700 | } 2701 | 2702 | static CYTHON_INLINE PyObject* __Pyx_PyUnicode_FromString(const char* c_str) { 2703 | return __Pyx_PyUnicode_FromStringAndSize(c_str, (Py_ssize_t)strlen(c_str)); 2704 | } 2705 | static CYTHON_INLINE const char* __Pyx_PyObject_AsString(PyObject* o) { 2706 | Py_ssize_t ignore; 2707 | return __Pyx_PyObject_AsStringAndSize(o, &ignore); 2708 | } 2709 | #if __PYX_DEFAULT_STRING_ENCODING_IS_ASCII || __PYX_DEFAULT_STRING_ENCODING_IS_DEFAULT 2710 | #if !CYTHON_PEP393_ENABLED 2711 | static const char* __Pyx_PyUnicode_AsStringAndSize(PyObject* o, Py_ssize_t *length) { 2712 | char* defenc_c; 2713 | PyObject* defenc = _PyUnicode_AsDefaultEncodedString(o, NULL); 2714 | if (!defenc) return NULL; 2715 | defenc_c = PyBytes_AS_STRING(defenc); 2716 | #if __PYX_DEFAULT_STRING_ENCODING_IS_ASCII 2717 | { 2718 | char* end = defenc_c + PyBytes_GET_SIZE(defenc); 2719 | char* c; 2720 | for (c = defenc_c; c < end; c++) { 2721 | if ((unsigned char) (*c) >= 128) { 2722 | PyUnicode_AsASCIIString(o); 2723 | return NULL; 2724 | } 2725 | } 2726 | } 2727 | #endif 2728 | *length = PyBytes_GET_SIZE(defenc); 2729 | return defenc_c; 2730 | } 2731 | #else 2732 | static CYTHON_INLINE const char* __Pyx_PyUnicode_AsStringAndSize(PyObject* o, Py_ssize_t *length) { 2733 | if (unlikely(__Pyx_PyUnicode_READY(o) == -1)) return NULL; 2734 | #if __PYX_DEFAULT_STRING_ENCODING_IS_ASCII 2735 | if (likely(PyUnicode_IS_ASCII(o))) { 2736 | *length = PyUnicode_GET_LENGTH(o); 2737 | return PyUnicode_AsUTF8(o); 2738 | } else { 2739 | PyUnicode_AsASCIIString(o); 2740 | return NULL; 2741 | } 2742 | #else 2743 | return PyUnicode_AsUTF8AndSize(o, length); 2744 | #endif 2745 | } 2746 | #endif 2747 | #endif 2748 | static CYTHON_INLINE const char* __Pyx_PyObject_AsStringAndSize(PyObject* o, Py_ssize_t *length) { 2749 | #if __PYX_DEFAULT_STRING_ENCODING_IS_ASCII || __PYX_DEFAULT_STRING_ENCODING_IS_DEFAULT 2750 | if ( 2751 | #if PY_MAJOR_VERSION < 3 && __PYX_DEFAULT_STRING_ENCODING_IS_ASCII 2752 | __Pyx_sys_getdefaultencoding_not_ascii && 2753 | #endif 2754 | PyUnicode_Check(o)) { 2755 | return __Pyx_PyUnicode_AsStringAndSize(o, length); 2756 | } else 2757 | #endif 2758 | #if (!CYTHON_COMPILING_IN_PYPY) || (defined(PyByteArray_AS_STRING) && defined(PyByteArray_GET_SIZE)) 2759 | if (PyByteArray_Check(o)) { 2760 | *length = PyByteArray_GET_SIZE(o); 2761 | return PyByteArray_AS_STRING(o); 2762 | } else 2763 | #endif 2764 | { 2765 | char* result; 2766 | int r = PyBytes_AsStringAndSize(o, &result, length); 2767 | if (unlikely(r < 0)) { 2768 | return NULL; 2769 | } else { 2770 | return result; 2771 | } 2772 | } 2773 | } 2774 | static CYTHON_INLINE int __Pyx_PyObject_IsTrue(PyObject* x) { 2775 | int is_true = x == Py_True; 2776 | if (is_true | (x == Py_False) | (x == Py_None)) return is_true; 2777 | else return PyObject_IsTrue(x); 2778 | } 2779 | static CYTHON_INLINE int __Pyx_PyObject_IsTrueAndDecref(PyObject* x) { 2780 | int retval; 2781 | if (unlikely(!x)) return -1; 2782 | retval = __Pyx_PyObject_IsTrue(x); 2783 | Py_DECREF(x); 2784 | return retval; 2785 | } 2786 | static PyObject* __Pyx_PyNumber_IntOrLongWrongResultType(PyObject* result, const char* type_name) { 2787 | #if PY_MAJOR_VERSION >= 3 2788 | if (PyLong_Check(result)) { 2789 | if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1, 2790 | "__int__ returned non-int (type %.200s). " 2791 | "The ability to return an instance of a strict subclass of int " 2792 | "is deprecated, and may be removed in a future version of Python.", 2793 | Py_TYPE(result)->tp_name)) { 2794 | Py_DECREF(result); 2795 | return NULL; 2796 | } 2797 | return result; 2798 | } 2799 | #endif 2800 | PyErr_Format(PyExc_TypeError, 2801 | "__%.4s__ returned non-%.4s (type %.200s)", 2802 | type_name, type_name, Py_TYPE(result)->tp_name); 2803 | Py_DECREF(result); 2804 | return NULL; 2805 | } 2806 | static CYTHON_INLINE PyObject* __Pyx_PyNumber_IntOrLong(PyObject* x) { 2807 | #if CYTHON_USE_TYPE_SLOTS 2808 | PyNumberMethods *m; 2809 | #endif 2810 | const char *name = NULL; 2811 | PyObject *res = NULL; 2812 | #if PY_MAJOR_VERSION < 3 2813 | if (likely(PyInt_Check(x) || PyLong_Check(x))) 2814 | #else 2815 | if (likely(PyLong_Check(x))) 2816 | #endif 2817 | return __Pyx_NewRef(x); 2818 | #if CYTHON_USE_TYPE_SLOTS 2819 | m = Py_TYPE(x)->tp_as_number; 2820 | #if PY_MAJOR_VERSION < 3 2821 | if (m && m->nb_int) { 2822 | name = "int"; 2823 | res = m->nb_int(x); 2824 | } 2825 | else if (m && m->nb_long) { 2826 | name = "long"; 2827 | res = m->nb_long(x); 2828 | } 2829 | #else 2830 | if (likely(m && m->nb_int)) { 2831 | name = "int"; 2832 | res = m->nb_int(x); 2833 | } 2834 | #endif 2835 | #else 2836 | if (!PyBytes_CheckExact(x) && !PyUnicode_CheckExact(x)) { 2837 | res = PyNumber_Int(x); 2838 | } 2839 | #endif 2840 | if (likely(res)) { 2841 | #if PY_MAJOR_VERSION < 3 2842 | if (unlikely(!PyInt_Check(res) && !PyLong_Check(res))) { 2843 | #else 2844 | if (unlikely(!PyLong_CheckExact(res))) { 2845 | #endif 2846 | return __Pyx_PyNumber_IntOrLongWrongResultType(res, name); 2847 | } 2848 | } 2849 | else if (!PyErr_Occurred()) { 2850 | PyErr_SetString(PyExc_TypeError, 2851 | "an integer is required"); 2852 | } 2853 | return res; 2854 | } 2855 | static CYTHON_INLINE Py_ssize_t __Pyx_PyIndex_AsSsize_t(PyObject* b) { 2856 | Py_ssize_t ival; 2857 | PyObject *x; 2858 | #if PY_MAJOR_VERSION < 3 2859 | if (likely(PyInt_CheckExact(b))) { 2860 | if (sizeof(Py_ssize_t) >= sizeof(long)) 2861 | return PyInt_AS_LONG(b); 2862 | else 2863 | return PyInt_AsSsize_t(b); 2864 | } 2865 | #endif 2866 | if (likely(PyLong_CheckExact(b))) { 2867 | #if CYTHON_USE_PYLONG_INTERNALS 2868 | const digit* digits = ((PyLongObject*)b)->ob_digit; 2869 | const Py_ssize_t size = Py_SIZE(b); 2870 | if (likely(__Pyx_sst_abs(size) <= 1)) { 2871 | ival = likely(size) ? digits[0] : 0; 2872 | if (size == -1) ival = -ival; 2873 | return ival; 2874 | } else { 2875 | switch (size) { 2876 | case 2: 2877 | if (8 * sizeof(Py_ssize_t) > 2 * PyLong_SHIFT) { 2878 | return (Py_ssize_t) (((((size_t)digits[1]) << PyLong_SHIFT) | (size_t)digits[0])); 2879 | } 2880 | break; 2881 | case -2: 2882 | if (8 * sizeof(Py_ssize_t) > 2 * PyLong_SHIFT) { 2883 | return -(Py_ssize_t) (((((size_t)digits[1]) << PyLong_SHIFT) | (size_t)digits[0])); 2884 | } 2885 | break; 2886 | case 3: 2887 | if (8 * sizeof(Py_ssize_t) > 3 * PyLong_SHIFT) { 2888 | return (Py_ssize_t) (((((((size_t)digits[2]) << PyLong_SHIFT) | (size_t)digits[1]) << PyLong_SHIFT) | (size_t)digits[0])); 2889 | } 2890 | break; 2891 | case -3: 2892 | if (8 * sizeof(Py_ssize_t) > 3 * PyLong_SHIFT) { 2893 | return -(Py_ssize_t) (((((((size_t)digits[2]) << PyLong_SHIFT) | (size_t)digits[1]) << PyLong_SHIFT) | (size_t)digits[0])); 2894 | } 2895 | break; 2896 | case 4: 2897 | if (8 * sizeof(Py_ssize_t) > 4 * PyLong_SHIFT) { 2898 | return (Py_ssize_t) (((((((((size_t)digits[3]) << PyLong_SHIFT) | (size_t)digits[2]) << PyLong_SHIFT) | (size_t)digits[1]) << PyLong_SHIFT) | (size_t)digits[0])); 2899 | } 2900 | break; 2901 | case -4: 2902 | if (8 * sizeof(Py_ssize_t) > 4 * PyLong_SHIFT) { 2903 | return -(Py_ssize_t) (((((((((size_t)digits[3]) << PyLong_SHIFT) | (size_t)digits[2]) << PyLong_SHIFT) | (size_t)digits[1]) << PyLong_SHIFT) | (size_t)digits[0])); 2904 | } 2905 | break; 2906 | } 2907 | } 2908 | #endif 2909 | return PyLong_AsSsize_t(b); 2910 | } 2911 | x = PyNumber_Index(b); 2912 | if (!x) return -1; 2913 | ival = PyInt_AsSsize_t(x); 2914 | Py_DECREF(x); 2915 | return ival; 2916 | } 2917 | static CYTHON_INLINE Py_hash_t __Pyx_PyIndex_AsHash_t(PyObject* o) { 2918 | if (sizeof(Py_hash_t) == sizeof(Py_ssize_t)) { 2919 | return (Py_hash_t) __Pyx_PyIndex_AsSsize_t(o); 2920 | #if PY_MAJOR_VERSION < 3 2921 | } else if (likely(PyInt_CheckExact(o))) { 2922 | return PyInt_AS_LONG(o); 2923 | #endif 2924 | } else { 2925 | Py_ssize_t ival; 2926 | PyObject *x; 2927 | x = PyNumber_Index(o); 2928 | if (!x) return -1; 2929 | ival = PyInt_AsLong(x); 2930 | Py_DECREF(x); 2931 | return ival; 2932 | } 2933 | } 2934 | static CYTHON_INLINE PyObject * __Pyx_PyBool_FromLong(long b) { 2935 | return b ? __Pyx_NewRef(Py_True) : __Pyx_NewRef(Py_False); 2936 | } 2937 | static CYTHON_INLINE PyObject * __Pyx_PyInt_FromSize_t(size_t ival) { 2938 | return PyInt_FromSize_t(ival); 2939 | } 2940 | 2941 | 2942 | #endif /* Py_PYTHON_H */ 2943 | --------------------------------------------------------------------------------